Skip to content

Instantly share code, notes, and snippets.

@minimal

minimal/core_test.clj

Last active Nov 27, 2018
Embed
What would you like to do?
testing monoids with test.check
(defn passes-monoid-props
[f id a b c]
(and (= (f (f a b) c) (f a (f b c))) ;; associativity
(= (f a id) a) ;; identity element
(= (f id a) a)))
(defspec plus-zero-are-monoid 100
(prop/for-all [[a b c] (gen/vector gen/int 3)]
(passes-monoid-props + 0 a b c)))
(defspec *-1-are-monoid 100
(prop/for-all [[a b c] (gen/vector gen/int 3)]
(passes-monoid-props * 1 a b c)))
(defspec concat-emptyvec-are-monoid 50
(prop/for-all [[a b c] (gen/vector (gen/vector gen/any 5) 3)]
(passes-monoid-props concat [] a b c)))
(defspec intersection-set-are-monoid 50
(prop/for-all [[a b c] (gen/vector (gen/fmap set (gen/vector gen/any-printable 5))
3)]
(passes-monoid-props clojure.set/intersection a a b c)))
(defspec union-empty-set-are-monoid 50
(prop/for-all [[a b c] (gen/vector (gen/fmap set (gen/vector gen/any-printable 5))
3)]
(passes-monoid-props clojure.set/union #{} a b c)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment