aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrantisek Sodomka <fsodomka@gmail.com>2009-02-12 14:07:55 +0000
committerFrantisek Sodomka <fsodomka@gmail.com>2009-02-12 14:07:55 +0000
commit6d1b17b9f7d7723d71b2fea70d51bf88a9fb0442 (patch)
tree22dc87222021279b4449457f29a9d126bab82e11
parent596972e13f4ef16284de1cf2b6dbb86f6b543362 (diff)
test numbers: mod, rem, quot
-rw-r--r--src/clojure/contrib/test_clojure/numbers.clj165
1 files changed, 162 insertions, 3 deletions
diff --git a/src/clojure/contrib/test_clojure/numbers.clj b/src/clojure/contrib/test_clojure/numbers.clj
index 4b7c19f3..31341789 100644
--- a/src/clojure/contrib/test_clojure/numbers.clj
+++ b/src/clojure/contrib/test_clojure/numbers.clj
@@ -6,14 +6,16 @@
;; terms of this license. You must not remove this notice, or any other,
;; from this software.
;;
-;; clojure.contrib.test-clojure.numbers
-;;
;; scgilardi (gmail)
;; Created 30 October 2008
+;;
(ns clojure.contrib.test-clojure.numbers
(:use clojure.contrib.test-is))
+
+;; *** Types ***
+
(deftest Coerced-Byte
(let [v (byte 3)]
(are _
@@ -77,7 +79,164 @@
(not (float? v)))))
-;; *** Number predicates ***
+;; *** Functions ***
+
+;; mod
+;; http://en.wikipedia.org/wiki/Modulo_operation
+;; http://mathforum.org/library/drmath/view/52343.html
+;;
+;; is mod correct?
+;; http://groups.google.com/group/clojure/browse_frm/thread/2a0ee4d248f3d131#
+;;
+;; Issue 23: mod (modulo) operator
+;; http://code.google.com/p/clojure/issues/detail?id=23
+
+(deftest test-mod
+ ; wrong number of args
+ (is (thrown? IllegalArgumentException (mod)))
+ (is (thrown? IllegalArgumentException (mod 1)))
+ (is (thrown? IllegalArgumentException (mod 3 2 1)))
+
+ ; divide by zero
+ (is (thrown? ArithmeticException (mod 9 0)))
+ (is (thrown? ArithmeticException (mod 0 0)))
+
+ (are (= _1 _2)
+ (mod 4 2) 0
+ (mod 3 2) 1
+ (mod 6 4) 2
+ (mod 0 5) 0
+
+ (mod 2 1/2) 0
+ (mod 2/3 1/2) 1/6
+ (mod 1 2/3) 1/3
+
+ (mod 4.0 2.0) 0.0
+ (mod 4.5 2.0) 0.5
+
+ ; |num| > |div|, num != k * div
+ (mod 42 5) 2 ; (42 / 5) * 5 + (42 mod 5) = 8 * 5 + 2 = 42
+ (mod 42 -5) -3 ; (42 / -5) * (-5) + (42 mod -5) = -9 * (-5) + (-3) = 42
+ (mod -42 5) 3 ; (-42 / 5) * 5 + (-42 mod 5) = -9 * 5 + 3 = -42
+ (mod -42 -5) -2 ; (-42 / -5) * (-5) + (-42 mod -5) = 8 * (-5) + (-2) = -42
+
+ ; |num| > |div|, num = k * div
+ (mod 9 3) 0 ; (9 / 3) * 3 + (9 mod 3) = 3 * 3 + 0 = 9
+ (mod 9 -3) 0
+ (mod -9 3) 0
+ (mod -9 -3) 0
+
+ ; |num| < |div|
+ (mod 2 5) 2 ; (2 / 5) * 5 + (2 mod 5) = 0 * 5 + 2 = 2
+ (mod 2 -5) -3 ; (2 / -5) * (-5) + (2 mod -5) = (-1) * (-5) + (-3) = 2
+ (mod -2 5) 3 ; (-2 / 5) * 5 + (-2 mod 5) = (-1) * 5 + 3 = -2
+ (mod -2 -5) -2 ; (-2 / -5) * (-5) + (-2 mod -5) = 0 * (-5) + (-2) = -2
+
+ ; num = 0, div != 0
+ (mod 0 3) 0 ; (0 / 3) * 3 + (0 mod 3) = 0 * 3 + 0 = 0
+ (mod 0 -3) 0
+ )
+)
+
+;; rem & quot
+;; http://en.wikipedia.org/wiki/Remainder
+
+(deftest test-rem
+ ; wrong number of args
+ (is (thrown? IllegalArgumentException (rem)))
+ (is (thrown? IllegalArgumentException (rem 1)))
+ (is (thrown? IllegalArgumentException (rem 3 2 1)))
+
+ ; divide by zero
+ (is (thrown? ArithmeticException (rem 9 0)))
+ (is (thrown? ArithmeticException (rem 0 0)))
+
+ (are (= _1 _2)
+ (rem 4 2) 0
+ (rem 3 2) 1
+ (rem 6 4) 2
+ (rem 0 5) 0
+
+ (rem 2 1/2) 0
+ (rem 2/3 1/2) 1/6
+ (rem 1 2/3) 1/3
+
+ (rem 4.0 2.0) 0.0
+ (rem 4.5 2.0) 0.5
+
+ ; |num| > |div|, num != k * div
+ (rem 42 5) 2 ; (8 * 5) + 2 == 42
+ (rem 42 -5) 2 ; (-8 * -5) + 2 == 42
+ (rem -42 5) -2 ; (-8 * 5) + -2 == -42
+ (rem -42 -5) -2 ; (8 * -5) + -2 == -42
+
+ ; |num| > |div|, num = k * div
+ (rem 9 3) 0
+ (rem 9 -3) 0
+ (rem -9 3) 0
+ (rem -9 -3) 0
+
+ ; |num| < |div|
+ (rem 2 5) 2
+ (rem 2 -5) 2
+ (rem -2 5) -2
+ (rem -2 -5) -2
+
+ ; num = 0, div != 0
+ (rem 0 3) 0
+ (rem 0 -3) 0
+ )
+)
+
+(deftest test-quot
+ ; wrong number of args
+ (is (thrown? IllegalArgumentException (quot)))
+ (is (thrown? IllegalArgumentException (quot 1)))
+ (is (thrown? IllegalArgumentException (quot 3 2 1)))
+
+ ; divide by zero
+ (is (thrown? ArithmeticException (quot 9 0)))
+ (is (thrown? ArithmeticException (quot 0 0)))
+
+ (are (= _1 _2)
+ (quot 4 2) 2
+ (quot 3 2) 1
+ (quot 6 4) 1
+ (quot 0 5) 0
+
+ (quot 2 1/2) 4
+ (quot 2/3 1/2) 1
+ (quot 1 2/3) 1
+
+ (quot 4.0 2.0) 2.0
+ (quot 4.5 2.0) 2.0
+
+ ; |num| > |div|, num != k * div
+ (quot 42 5) 8 ; (8 * 5) + 2 == 42
+ (quot 42 -5) -8 ; (-8 * -5) + 2 == 42
+ (quot -42 5) -8 ; (-8 * 5) + -2 == -42
+ (quot -42 -5) 8 ; (8 * -5) + -2 == -42
+
+ ; |num| > |div|, num = k * div
+ (quot 9 3) 3
+ (quot 9 -3) -3
+ (quot -9 3) -3
+ (quot -9 -3) 3
+
+ ; |num| < |div|
+ (quot 2 5) 0
+ (quot 2 -5) 0
+ (quot -2 5) 0
+ (quot -2 -5) 0
+
+ ; num = 0, div != 0
+ (quot 0 3) 0
+ (quot 0 -3) 0
+ )
+)
+
+
+;; *** Predicates ***
;; pos? zero? neg?