Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/com/gfredericks/schpec/fns.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(ns com.gfredericks.schpec.fns)

(defn commutative
"Returns a mapping fn suitable for use in clojure.spec.test/check that
specifies the given fn is commutative"
[f]
(fn [mapping]
(let [{:keys [args ret]} mapping]
(if (> (count args) 1)
(= ret (apply f (reverse args)))
true))))

(defn associative
"Returns a mapping fn suitable for use in clojure.spec.test/check that
specifies the given fn is associative"
[f]
(fn [mapping]
(let [{:keys [args ret]} mapping]
(= ret (f (first args) (apply f (rest args)))))))

(defn has-identity
"Returns a mapping fn suitable for use in clojure.spec.test/check that
specifies the given fn has the given identity value"
[f i]
(fn [mapping]
(let [{:keys [args ret]} mapping]
(= ret (f i ret)))))
27 changes: 27 additions & 0 deletions test/com/gfredericks/schpec/fns_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(ns com.gfredericks.schpec.fns-test
(:require [clojure.test :refer :all]
[com.gfredericks.schpec.fns :refer :all]))

(deftest test-commutative
(let [f (commutative +)]
(is (f {:args [1 2]
:ret 3})))
(let [f (commutative -)]
(is (not (f {:args [1 2]
:ret -1})))))

(deftest test-associative
(let [f (associative +)]
(is (f {:args [1 2 3]
:ret 6})))
(let [f (associative -)]
(is (not (f {:args [1 2 3]
:ret -4})))))

(deftest test-has-identity
(let [f (has-identity + 0)]
(is (f {:args [1 2]
:ret 3})))
(let [f (has-identity + 1)]
(is (not (f {:args [1 2]
:ret 3})))))