annotate src/clojure/contrib/test_contrib/test_math.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
rev   line source
rlm@10 1 (ns clojure.contrib.test-math
rlm@10 2 (:use clojure.test
rlm@10 3 clojure.contrib.math))
rlm@10 4
rlm@10 5 (deftest test-expt
rlm@10 6 (are [x y] (= x y)
rlm@10 7 (expt 2 3) 8
rlm@10 8 (expt (expt 2 16) 2) (expt 2 32)
rlm@10 9 (expt 4/3 2) 16/9
rlm@10 10 (expt 2 -10) 1/1024
rlm@10 11 (expt 0.5M 2) 0.25M
rlm@10 12 (expt 5 4.2) (Math/pow 5 4.2)
rlm@10 13 (expt 5.3 4) (Math/pow 5.3 4)))
rlm@10 14
rlm@10 15 (deftest test-abs
rlm@10 16 (are [x y] (= x y)
rlm@10 17 (abs -2) 2
rlm@10 18 (abs 0) 0
rlm@10 19 (abs 5) 5
rlm@10 20 (abs 123456789123456789) 123456789123456789
rlm@10 21 (abs -123456789123456789) 123456789123456789
rlm@10 22 (abs 5/3) 5/3
rlm@10 23 (abs -4/3) 4/3
rlm@10 24 (abs 4.3M) 4.3M
rlm@10 25 (abs -4.3M) 4.3M
rlm@10 26 (abs 2.8) 2.8
rlm@10 27 (abs -2.8) 2.8))
rlm@10 28
rlm@10 29 (deftest test-gcd
rlm@10 30 (are [x y] (= x y)
rlm@10 31 (gcd 4 3) 1
rlm@10 32 (gcd 24 12) 12
rlm@10 33 (gcd 24 27) 3
rlm@10 34 (gcd 1 0) 1
rlm@10 35 (gcd 0 1) 1
rlm@10 36 (gcd 0 0) 0)
rlm@10 37 (is (thrown? IllegalArgumentException (gcd nil 0)))
rlm@10 38 (is (thrown? IllegalArgumentException (gcd 0 nil)))
rlm@10 39 (is (thrown? IllegalArgumentException (gcd 7.0 0))))
rlm@10 40
rlm@10 41 (deftest test-lcm
rlm@10 42 (are [x y] (= x y)
rlm@10 43 (lcm 2 3) 6
rlm@10 44 (lcm 3 2) 6
rlm@10 45 (lcm -2 3) 6
rlm@10 46 (lcm 2 -3) 6
rlm@10 47 (lcm -2 -3) 6
rlm@10 48 (lcm 4 10) 20
rlm@10 49 (lcm 1 0) 0
rlm@10 50 (lcm 0 1) 0
rlm@10 51 (lcm 0 0))
rlm@10 52 (is (thrown? IllegalArgumentException (lcm nil 0)))
rlm@10 53 (is (thrown? IllegalArgumentException (lcm 0 nil)))
rlm@10 54 (is (thrown? IllegalArgumentException (lcm 7.0 0))))
rlm@10 55
rlm@10 56 (deftest test-floor
rlm@10 57 (are [x y] (== x y)
rlm@10 58 (floor 6) 6
rlm@10 59 (floor -6) -6
rlm@10 60 (floor 123456789123456789) 123456789123456789
rlm@10 61 (floor -123456789123456789) -123456789123456789
rlm@10 62 (floor 4/3) 1
rlm@10 63 (floor -4/3) -2
rlm@10 64 (floor 4.3M) 4
rlm@10 65 (floor -4.3M) -5
rlm@10 66 (floor 4.3) 4.0
rlm@10 67 (floor -4.3) -5.0))
rlm@10 68
rlm@10 69 (deftest test-ceil
rlm@10 70 (are [x y] (== x y)
rlm@10 71 (ceil 6) 6
rlm@10 72 (ceil -6) -6
rlm@10 73 (ceil 123456789123456789) 123456789123456789
rlm@10 74 (ceil -123456789123456789) -123456789123456789
rlm@10 75 (ceil 4/3) 2
rlm@10 76 (ceil -4/3) -1
rlm@10 77 (ceil 4.3M) 5
rlm@10 78 (ceil -4.3M) -4
rlm@10 79 (ceil 4.3) 5.0
rlm@10 80 (ceil -4.3) -4.0))
rlm@10 81
rlm@10 82 (deftest test-round
rlm@10 83 (are [x y] (== x y)
rlm@10 84 (round 6) 6
rlm@10 85 (round -6) -6
rlm@10 86 (round 123456789123456789) 123456789123456789
rlm@10 87 (round -123456789123456789) -123456789123456789
rlm@10 88 (round 4/3) 1
rlm@10 89 (round 5/3) 2
rlm@10 90 (round 5/2) 3
rlm@10 91 (round -4/3) -1
rlm@10 92 (round -5/3) -2
rlm@10 93 (round -5/2) -2
rlm@10 94 (round 4.3M) 4
rlm@10 95 (round 4.7M) 5
rlm@10 96 (round -4.3M) -4
rlm@10 97 (round -4.7M) -5
rlm@10 98 (round 4.5M) 5
rlm@10 99 (round -4.5M) -4
rlm@10 100 (round 4.3) 4
rlm@10 101 (round 4.7) 5
rlm@10 102 (round -4.3) -4
rlm@10 103 (round -4.7) -5
rlm@10 104 (round 4.5) 5
rlm@10 105 (round -4.5) -4))
rlm@10 106
rlm@10 107 (deftest test-sqrt
rlm@10 108 (are [x y] (= x y)
rlm@10 109 (sqrt 9) 3
rlm@10 110 (sqrt 16/9) 4/3
rlm@10 111 (sqrt 0.25M) 0.5M
rlm@10 112 (sqrt 2) (Math/sqrt 2)))
rlm@10 113
rlm@10 114 (deftest test-exact-integer-sqrt
rlm@10 115 (are [x y] (= x y)
rlm@10 116 (exact-integer-sqrt 15) [3 6]
rlm@10 117 (exact-integer-sqrt (inc (expt 2 32))) [(expt 2 16) 1]
rlm@10 118 (exact-integer-sqrt 1000000000000) [1000000 0]))