I'm trying to do property-based testing for this simple function:
(defn distinct-kw-keys
[maps]
(->> (map keys maps)
(flatten)
(filter keyword?)
(distinct)
(vec)))
... using fdef
and check
:
(require '[clojure.spec.alpha :as s]
'[clojure.spec.test.alpha :as test])
(s/fdef distinct-kw-keys
:args (s/cat :maps (s/coll-of map?))
:ret (s/coll-of keyword?
:kind vector?
:distinct true))
(test/check `distinct-kw-keys)
Calling test/check
terminates after a while throwing OutOfMemoryError
:
Exception in thread "Timer-1" Error printing return value (OutOfMemoryError) at clojure.test.check.generators/choose$fn (generators.cljc:260). Java heap space
etc.
I can't figure out what's the problem here. Function and specs seem to work fine, e.g.
(require '[clojure.spec.gen.alpha :as gen])
(s/valid?
(s/coll-of keyword?
:kind vector?
:distinct true)
(distinct-kw-keys
(gen/generate
(s/gen
(s/coll-of map?))))) ;; => true
It works on my machine (Macbook Pro 2015, 16GB of memory), so I can't reproduce your problem.
To reduce the number of generative tests, you can write:
(test/check `distinct-kw-keys {:clojure.spec.test.check/opts {:num-tests 1}})
A variation of your function using transducers, it may be slightly faster:
(defn distinct-kw-keys
[maps]
(into []
(comp (mapcat keys)
(filter keyword?)
(distinct))
maps))
(test/check `distinct-kw-keys)
;;=> ({:spec #object[clojure.spec.alpha$fspec_impl$reify__2524 0x18025ced "clojure.spec.alpha$fspec_impl$reify__2524@18025ced"], :clojure.spec.test.check/ret {:result true, :pass? true, :num-tests 1000, :time-elapsed-ms 26488, :seed 1548072234986}, :sym user/distinct-kw-keys})