diff options
Diffstat (limited to 'src/clojure/contrib/monads/test.clj')
-rw-r--r-- | src/clojure/contrib/monads/test.clj | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/clojure/contrib/monads/test.clj b/src/clojure/contrib/monads/test.clj new file mode 100644 index 00000000..b63b8946 --- /dev/null +++ b/src/clojure/contrib/monads/test.clj @@ -0,0 +1,47 @@ +;; Test routines for monads.clj + +;; by Konrad Hinsen +;; last updated December 31, 2008 + +;; Copyright (c) Konrad Hinsen, 2008. All rights reserved. The use +;; and distribution terms for this software are covered by the Eclipse +;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) +;; which can be found in the file epl-v10.html at the root of this +;; distribution. By using this software in any fashion, you are +;; agreeing to be bound by the terms of this license. You must not +;; remove this notice, or any other, from this software. + +(ns clojure.contrib.test-monads + (:use clojure.contrib.test-is clojure.contrib.monads)) + +(deftest sequence-monad + (with-monad sequence + (are (= _1 _2) + (domonad [x (range 3) y (range 2)] (+ x y)) + '(0 1 1 2 2 3) + (domonad [x (range 5) y (range (+ 1 x)) :when (= (+ x y) 2)] (list x y)) + '((1 1) (2 0)) + ((m-lift 2 #(list %1 %2)) (range 3) (range 2)) + '((0 0) (0 1) (1 0) (1 1) (2 0) (2 1)) + (m-seq (replicate 3 (range 2))) + '((0 0 0) (0 0 1) (0 1 0) (0 1 1) (1 0 0) (1 0 1) (1 1 0) (1 1 1)) + ((m-chain (replicate 3 range)) 5) + '(0 0 0 1 0 0 1 0 1 2) + (m-plus (range 3) (range 2)) + '(0 1 2 0 1)))) + +(deftest maybe-monad + (with-monad maybe + (let [m+ (m-lift 2 +) + mdiv (fn [x y] (domonad [a x b y :when (not (zero? b))] (/ a b)))] + (are (= _1 _2) + (m+ (m-result 1) (m-result 3)) + (m-result 4) + (mdiv (m-result 1) (m-result 3)) + (m-result (/ 1 3)) + (m+ 1 (mdiv (m-result 1) (m-result 0))) + m-zero + (m-plus m-zero (m-result 1) m-zero (m-result 2)) + (m-result 1))))) + +(run-tests) |