Skip to content

Instantly share code, notes, and snippets.

@minimal
Last active November 27, 2018 15:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minimal/40085951c277d8ce26ad to your computer and use it in GitHub Desktop.
Save minimal/40085951c277d8ce26ad to your computer and use it in GitHub Desktop.
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