2

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
2
  • 1
    what version of test.check are you using? 0.9.0 has a tendency to OOM, that was improved in subsequent releases. Commented Jan 21, 2019 at 13:25
  • @gfredericks yes, I was using 0.9.0. Upgrade to 0.10.0-alpha3 fixed the problem... Thanks! Commented Jan 21, 2019 at 15:13

1 Answer 1

1

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})

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.