我发现了一些奇怪的行为,但是无法找到一个小的失败案例。
(defn quicksort [v]
(if (<= (count v) 1)
v
(let [[x & xs] v]
(rrb/catvec (quicksort (filterv #(<= % x) xs))
[x]
(quicksort (filterv #(> % x) xs))))))
我们可以使用 test.check 来检查这个实现
(defn ascending? [coll]
(every? (fn [[a b]] (<= a b))
(partition 2 1 coll)))
(def property
(prop/for-all [v (gen/vector gen/int)]
(let [s (quicksort v)]
(and (= (count v) (count s))
(ascending? s)))))
(tc/quick-check 10000 property)
;; => {:result true, :num-tests 10000, :seed 1440948212354}
(我已将检查次数增加至 1e6,但 test.check 无法找到失败案例)
尽管如此,我还是在包含 1193 个元素的向量中遇到了 "ClassCastException: clojure.lang.PersistentVector$Node cannot be cast to [I" 问题。
{}
(quicksort (read-string (slurp "failing-1193.edn")))
{}