Clojure规范-测试检查OutOfMemoryError

时间:2019-01-21 00:35:20

标签: clojure clojure.spec test.check

我正在尝试为此简单功能进行基于属性的测试:

(defn distinct-kw-keys
  [maps]
  (->> (map keys maps)
       (flatten)
       (filter keyword?)
       (distinct)
       (vec)))

...使用fdefcheck

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

调用test/check一会儿后,呼叫OutOfMemoryError终止:

Exception in thread "Timer-1" Error printing return value (OutOfMemoryError) at clojure.test.check.generators/choose$fn (generators.cljc:260). Java heap space

我不知道这里出了什么问题。功能和规格似乎可以正常工作,例如

(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

1 个答案:

答案 0 :(得分:1)

它可以在我的机器上运行(Macbook Pro 2015,16GB内存),所以我无法重现您的问题。

要减少生成测试的数量,您可以编写:

outputTopic

使用换能器的功能变化,可能会更快一些:

(test/check `distinct-kw-keys {:clojure.spec.test.check/opts {:num-tests 1}})