diff options
author | Frantisek Sodomka <fsodomka@gmail.com> | 2009-03-18 17:19:27 +0000 |
---|---|---|
committer | Frantisek Sodomka <fsodomka@gmail.com> | 2009-03-18 17:19:27 +0000 |
commit | 3ea2f7c795b79f0f9d59938ff16770c0d2627a37 (patch) | |
tree | a6709adeadb68b960bea8d7b59850fcc91caf761 /src/clojure/contrib/test_clojure | |
parent | 7a688087c8afebeaa743fe5c064da54a2e8e360f (diff) |
test-numbers: + - * /
test-sequences: empty, not-empty, repeat
Diffstat (limited to 'src/clojure/contrib/test_clojure')
-rw-r--r-- | src/clojure/contrib/test_clojure/numbers.clj | 102 | ||||
-rw-r--r-- | src/clojure/contrib/test_clojure/sequences.clj | 101 |
2 files changed, 203 insertions, 0 deletions
diff --git a/src/clojure/contrib/test_clojure/numbers.clj b/src/clojure/contrib/test_clojure/numbers.clj index 31341789..0f65ed29 100644 --- a/src/clojure/contrib/test_clojure/numbers.clj +++ b/src/clojure/contrib/test_clojure/numbers.clj @@ -81,6 +81,108 @@ ;; *** Functions *** +(defonce DELTA 1e-12) + +(deftest test-add + (are (= _1 _2) + (+) 0 + (+ 1) 1 + (+ 1 2) 3 + (+ 1 2 3) 6 + + (+ -1) -1 + (+ -1 -2) -3 + (+ -1 +2 -3) -2 + + (+ 1 -1) 0 + (+ -1 1) 0 + + (+ 2/3) 2/3 + (+ 2/3 1) 5/3 + (+ 2/3 1/3) 1 ) + + (are (< (- _1 _2) DELTA) + (+ 1.2) 1.2 + (+ 1.1 2.4) 3.5 + (+ 1.1 2.2 3.3) 6.6 ) + + (is (> (+ Integer/MAX_VALUE 10) Integer/MAX_VALUE)) ; no overflow + (is (thrown? ClassCastException (+ "ab" "cd"))) ) ; no string concatenation + + +(deftest test-subtract + (is (thrown? IllegalArgumentException (-))) + (are (= _1 _2) + (- 1) -1 + (- 1 2) -1 + (- 1 2 3) -4 + + (- -2) 2 + (- 1 -2) 3 + (- 1 -2 -3) 6 + + (- 1 1) 0 + (- -1 -1) 0 + + (- 2/3) -2/3 + (- 2/3 1) -1/3 + (- 2/3 1/3) 1/3 ) + + (are (< (- _1 _2) DELTA) + (- 1.2) -1.2 + (- 2.2 1.1) 1.1 + (- 6.6 2.2 1.1) 3.3 ) + + (is (< (- Integer/MIN_VALUE 10) Integer/MIN_VALUE)) ) ; no underflow + + +(deftest test-multiply + (are (= _1 _2) + (*) 1 + (* 2) 2 + (* 2 3) 6 + (* 2 3 4) 24 + + (* -2) -2 + (* 2 -3) -6 + (* 2 -3 -1) 6 + + (* 1/2) 1/2 + (* 1/2 1/3) 1/6 + (* 1/2 1/3 -1/4) -1/24 ) + + (are (< (- _1 _2) DELTA) + (* 1.2) 1.2 + (* 2.0 1.2) 2.4 + (* 3.5 2.0 1.2) 8.4 ) + + (is (> (* 3 (int (/ Integer/MAX_VALUE 2.0))) Integer/MAX_VALUE)) ) ; no overflow + + +(deftest test-divide + (are (= _1 _2) + (/ 1) 1 + (/ 2) 1/2 + (/ 3 2) 3/2 + (/ 4 2) 2 + (/ 24 3 2) 4 + (/ 24 3 2 -1) -4 + + (/ -1) -1 + (/ -2) -1/2 + (/ -3 -2) 3/2 + (/ -4 -2) 2 + (/ -4 2) -2 ) + + (are (< (- _1 _2) DELTA) + (/ 4.5 3) 1.5 + (/ 4.5 3.0 3.0) 0.5 ) + + (is (thrown? ArithmeticException (/ 0))) + (is (thrown? ArithmeticException (/ 2 0))) + (is (thrown? IllegalArgumentException (/))) ) + + ;; mod ;; http://en.wikipedia.org/wiki/Modulo_operation ;; http://mathforum.org/library/drmath/view/52343.html diff --git a/src/clojure/contrib/test_clojure/sequences.clj b/src/clojure/contrib/test_clojure/sequences.clj index e990f5c8..995a1bc9 100644 --- a/src/clojure/contrib/test_clojure/sequences.clj +++ b/src/clojure/contrib/test_clojure/sequences.clj @@ -101,6 +101,69 @@ (cons 1 (into-array [2 3])) '(1 2 3) )) +(deftest test-empty + (are (and (= (empty _1) _2) + (= (class (empty _1)) (class _2))) + nil nil + + () () + '(1 2) () + + [] [] + [1 2] [] + + {} {} + {:a 1 :b 2} {} + + #{} #{} + #{1 2} #{} + + (seq ()) nil ; (seq ()) => nil + (seq '(1 2)) () + + (seq []) nil ; (seq []) => nil + (seq [1 2]) () + + (seq "") nil ; (seq "") => nil + (seq "ab") () + + (lazy-seq ()) () + (lazy-seq '(1 2)) () + + (lazy-seq []) () + (lazy-seq [1 2]) () + + ; non-coll, non-seq => nil + 42 nil + 1.2 nil + "abc" nil )) + + +(deftest test-not-empty + ; empty coll/seq => nil + (are (= (not-empty _) nil) + () + [] + {} + #{} + (seq ()) + (seq []) + (lazy-seq ()) + (lazy-seq []) ) + + ; non-empty coll/seq => identity + (are (and (= (not-empty _) _) + (= (class (not-empty _)) (class _))) + '(1 2) + [1 2] + {:a 1} + #{1 2} + (seq '(1 2)) + (seq [1 2]) + (lazy-seq '(1 2)) + (lazy-seq [1 2]) )) + + (deftest test-first (is (thrown? IllegalArgumentException (first))) (is (thrown? IllegalArgumentException (first true))) @@ -735,6 +798,44 @@ (split-with number? [1 -2 "abc" \x]) [(list 1 -2) (list "abc" \x)] )) +(deftest test-repeat + (is (thrown? IllegalArgumentException (repeat))) + + ; infinite sequence => use take + (are (= _1 _2) + (take 0 (repeat 7)) () + (take 1 (repeat 7)) '(7) + (take 2 (repeat 7)) '(7 7) + (take 5 (repeat 7)) '(7 7 7 7 7) ) + + ; limited sequence + (are (= _1 _2) + (repeat 0 7) () + (repeat 1 7) '(7) + (repeat 2 7) '(7 7) + (repeat 5 7) '(7 7 7 7 7) + + (repeat -1 7) () + (repeat -3 7) () ) + + ; test different data types + (are (= (repeat 3 _) (list _ _ _)) + nil + false true + 0 42 + 0.0 3.14 + 2/3 + 0M 1M + \c + "" "abc" + 'sym + :kw + () '(1 2) + [] [1 2] + {} {:a 1 :b 2} + #{} #{1 2} )) + + (deftest test-range (are (= _1 _2) (range 0) () ; exclusive end! |