test-check

defndaines 2019-04-15T14:37:38.001100Z

Recently learned that prn-str and edn/read-string are not symmetric, thanks to playing around with test.check.

(defspec prop-symmetric
  ;; NOTE This actually fails. prn-str and read-string are not symmetric.
  100
  (prop/for-all [s gen/any]
    (let [encoded (prn-str s)]
      (and (string? encoded)
           (= s (edn/read-string encoded))))))

2019-04-15T14:38:30.001800Z

this sounds like a good trivia question

1
defndaines 2019-04-15T14:38:34.002Z

(Note, there isn’t any official documentation that they are supposed to be, if you exclude the comment here https://clojuredocs.org/clojure.edn/read-string#example-542dc112e4b05f4d257a2993 from phreed)

2019-04-15T14:39:19.002400Z

the first thing I can think of is things like (keyword " ")

2019-04-15T14:39:50.002800Z

I can't remember if gen/any tries to generate those though; I think it doesn't

2019-04-15T14:39:59.003100Z

I'm 99.7% sure it doesn't after thinking about it for two more seconds

2019-04-15T14:40:37.003900Z

(symbol "true") would be problematic; I don't think it avoids that, but it's also probably highly unlikely to generate it by chance

defndaines 2019-04-15T14:40:44.004200Z

I know that a Double/NAN is one thing that isn’t symmetric.

2019-04-15T14:40:44.004300Z

I'm sure there's something much more basic

2019-04-15T14:41:03.004700Z

oh, well that probably reads back in as the correct object but = won't be true

2019-04-15T14:41:19.005200Z

that says more about = than it does about prn-str and edn/read-string

defndaines 2019-04-15T14:41:49.005900Z

Ah, true. Would have to use a specialized = to see what else pops up.

2019-04-15T14:41:59.006200Z

(symbol "nil") is easier to generate than (symbol "true") I guess

2019-04-15T14:43:24.006600Z

there's a lot more stuff that would come up if gen/any was more ambitious

2019-04-15T14:43:56.007Z

e.g., queues, sorted sets, sorted maps, ...