Mercurial > lasercutter
view src/clojure/test_clojure/numbers.clj @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line source
1 ; Copyright (c) Rich Hickey. All rights reserved.2 ; The use and distribution terms for this software are covered by the3 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)4 ; which can be found in the file epl-v10.html at the root of this distribution.5 ; By using this software in any fashion, you are agreeing to be bound by6 ; the terms of this license.7 ; You must not remove this notice, or any other, from this software.9 ; Author: Stephen C. Gilardi10 ;; scgilardi (gmail)11 ;; Created 30 October 200812 ;;14 (ns clojure.test-clojure.numbers15 (:use clojure.test))18 ; TODO:19 ; ==20 ; and more...23 ;; *** Types ***25 (deftest Coerced-Byte26 (let [v (byte 3)]27 (are [x] (true? x)28 (instance? Byte v)29 (number? v)30 (integer? v)31 (rational? v))))33 (deftest Coerced-Short34 (let [v (short 3)]35 (are [x] (true? x)36 (instance? Short v)37 (number? v)38 (integer? v)39 (rational? v))))41 (deftest Coerced-Integer42 (let [v (int 3)]43 (are [x] (true? x)44 (instance? Integer v)45 (number? v)46 (integer? v)47 (rational? v))))49 (deftest Coerced-Long50 (let [v (long 3)]51 (are [x] (true? x)52 (instance? Long v)53 (number? v)54 (integer? v)55 (rational? v))))57 (deftest Coerced-BigInteger58 (let [v (bigint 3)]59 (are [x] (true? x)60 (instance? BigInteger v)61 (number? v)62 (integer? v)63 (rational? v))))65 (deftest Coerced-Float66 (let [v (float 3)]67 (are [x] (true? x)68 (instance? Float v)69 (number? v)70 (float? v))))72 (deftest Coerced-Double73 (let [v (double 3)]74 (are [x] (true? x)75 (instance? Double v)76 (number? v)77 (float? v))))79 (deftest Coerced-BigDecimal80 (let [v (bigdec 3)]81 (are [x] (true? x)82 (instance? BigDecimal v)83 (number? v)84 (decimal? v)85 (not (float? v)))))88 ;; *** Functions ***90 (defonce DELTA 1e-12)92 (deftest test-add93 (are [x y] (= x y)94 (+) 095 (+ 1) 196 (+ 1 2) 397 (+ 1 2 3) 699 (+ -1) -1100 (+ -1 -2) -3101 (+ -1 +2 -3) -2103 (+ 1 -1) 0104 (+ -1 1) 0106 (+ 2/3) 2/3107 (+ 2/3 1) 5/3108 (+ 2/3 1/3) 1 )110 (are [x y] (< (- x y) DELTA)111 (+ 1.2) 1.2112 (+ 1.1 2.4) 3.5113 (+ 1.1 2.2 3.3) 6.6 )115 (is (> (+ Integer/MAX_VALUE 10) Integer/MAX_VALUE)) ; no overflow116 (is (thrown? ClassCastException (+ "ab" "cd"))) ) ; no string concatenation119 (deftest test-subtract120 (is (thrown? IllegalArgumentException (-)))121 (are [x y] (= x y)122 (- 1) -1123 (- 1 2) -1124 (- 1 2 3) -4126 (- -2) 2127 (- 1 -2) 3128 (- 1 -2 -3) 6130 (- 1 1) 0131 (- -1 -1) 0133 (- 2/3) -2/3134 (- 2/3 1) -1/3135 (- 2/3 1/3) 1/3 )137 (are [x y] (< (- x y) DELTA)138 (- 1.2) -1.2139 (- 2.2 1.1) 1.1140 (- 6.6 2.2 1.1) 3.3 )142 (is (< (- Integer/MIN_VALUE 10) Integer/MIN_VALUE)) ) ; no underflow145 (deftest test-multiply146 (are [x y] (= x y)147 (*) 1148 (* 2) 2149 (* 2 3) 6150 (* 2 3 4) 24152 (* -2) -2153 (* 2 -3) -6154 (* 2 -3 -1) 6156 (* 1/2) 1/2157 (* 1/2 1/3) 1/6158 (* 1/2 1/3 -1/4) -1/24 )160 (are [x y] (< (- x y) DELTA)161 (* 1.2) 1.2162 (* 2.0 1.2) 2.4163 (* 3.5 2.0 1.2) 8.4 )165 (is (> (* 3 (int (/ Integer/MAX_VALUE 2.0))) Integer/MAX_VALUE)) ) ; no overflow167 (deftest test-ratios-simplify-to-ints-where-appropriate168 (testing "negative denominator (assembla #275)"169 (is (integer? (/ 1 -1/2)))170 (is (integer? (/ 0 -1/2)))))172 (deftest test-divide173 (are [x y] (= x y)174 (/ 1) 1175 (/ 2) 1/2176 (/ 3 2) 3/2177 (/ 4 2) 2178 (/ 24 3 2) 4179 (/ 24 3 2 -1) -4181 (/ -1) -1182 (/ -2) -1/2183 (/ -3 -2) 3/2184 (/ -4 -2) 2185 (/ -4 2) -2 )187 (are [x y] (< (- x y) DELTA)188 (/ 4.5 3) 1.5189 (/ 4.5 3.0 3.0) 0.5 )191 (is (thrown? ArithmeticException (/ 0)))192 (is (thrown? ArithmeticException (/ 2 0)))193 (is (thrown? IllegalArgumentException (/))) )196 ;; mod197 ;; http://en.wikipedia.org/wiki/Modulo_operation198 ;; http://mathforum.org/library/drmath/view/52343.html199 ;;200 ;; is mod correct?201 ;; http://groups.google.com/group/clojure/browse_frm/thread/2a0ee4d248f3d131#202 ;;203 ;; Issue 23: mod (modulo) operator204 ;; http://code.google.com/p/clojure/issues/detail?id=23206 (deftest test-mod207 ; wrong number of args208 (is (thrown? IllegalArgumentException (mod)))209 (is (thrown? IllegalArgumentException (mod 1)))210 (is (thrown? IllegalArgumentException (mod 3 2 1)))212 ; divide by zero213 (is (thrown? ArithmeticException (mod 9 0)))214 (is (thrown? ArithmeticException (mod 0 0)))216 (are [x y] (= x y)217 (mod 4 2) 0218 (mod 3 2) 1219 (mod 6 4) 2220 (mod 0 5) 0222 (mod 2 1/2) 0223 (mod 2/3 1/2) 1/6224 (mod 1 2/3) 1/3226 (mod 4.0 2.0) 0.0227 (mod 4.5 2.0) 0.5229 ; |num| > |div|, num != k * div230 (mod 42 5) 2 ; (42 / 5) * 5 + (42 mod 5) = 8 * 5 + 2 = 42231 (mod 42 -5) -3 ; (42 / -5) * (-5) + (42 mod -5) = -9 * (-5) + (-3) = 42232 (mod -42 5) 3 ; (-42 / 5) * 5 + (-42 mod 5) = -9 * 5 + 3 = -42233 (mod -42 -5) -2 ; (-42 / -5) * (-5) + (-42 mod -5) = 8 * (-5) + (-2) = -42235 ; |num| > |div|, num = k * div236 (mod 9 3) 0 ; (9 / 3) * 3 + (9 mod 3) = 3 * 3 + 0 = 9237 (mod 9 -3) 0238 (mod -9 3) 0239 (mod -9 -3) 0241 ; |num| < |div|242 (mod 2 5) 2 ; (2 / 5) * 5 + (2 mod 5) = 0 * 5 + 2 = 2243 (mod 2 -5) -3 ; (2 / -5) * (-5) + (2 mod -5) = (-1) * (-5) + (-3) = 2244 (mod -2 5) 3 ; (-2 / 5) * 5 + (-2 mod 5) = (-1) * 5 + 3 = -2245 (mod -2 -5) -2 ; (-2 / -5) * (-5) + (-2 mod -5) = 0 * (-5) + (-2) = -2247 ; num = 0, div != 0248 (mod 0 3) 0 ; (0 / 3) * 3 + (0 mod 3) = 0 * 3 + 0 = 0249 (mod 0 -3) 0250 )251 )253 ;; rem & quot254 ;; http://en.wikipedia.org/wiki/Remainder256 (deftest test-rem257 ; wrong number of args258 (is (thrown? IllegalArgumentException (rem)))259 (is (thrown? IllegalArgumentException (rem 1)))260 (is (thrown? IllegalArgumentException (rem 3 2 1)))262 ; divide by zero263 (is (thrown? ArithmeticException (rem 9 0)))264 (is (thrown? ArithmeticException (rem 0 0)))266 (are [x y] (= x y)267 (rem 4 2) 0268 (rem 3 2) 1269 (rem 6 4) 2270 (rem 0 5) 0272 (rem 2 1/2) 0273 (rem 2/3 1/2) 1/6274 (rem 1 2/3) 1/3276 (rem 4.0 2.0) 0.0277 (rem 4.5 2.0) 0.5279 ; |num| > |div|, num != k * div280 (rem 42 5) 2 ; (8 * 5) + 2 == 42281 (rem 42 -5) 2 ; (-8 * -5) + 2 == 42282 (rem -42 5) -2 ; (-8 * 5) + -2 == -42283 (rem -42 -5) -2 ; (8 * -5) + -2 == -42285 ; |num| > |div|, num = k * div286 (rem 9 3) 0287 (rem 9 -3) 0288 (rem -9 3) 0289 (rem -9 -3) 0291 ; |num| < |div|292 (rem 2 5) 2293 (rem 2 -5) 2294 (rem -2 5) -2295 (rem -2 -5) -2297 ; num = 0, div != 0298 (rem 0 3) 0299 (rem 0 -3) 0300 )301 )303 (deftest test-quot304 ; wrong number of args305 (is (thrown? IllegalArgumentException (quot)))306 (is (thrown? IllegalArgumentException (quot 1)))307 (is (thrown? IllegalArgumentException (quot 3 2 1)))309 ; divide by zero310 (is (thrown? ArithmeticException (quot 9 0)))311 (is (thrown? ArithmeticException (quot 0 0)))313 (are [x y] (= x y)314 (quot 4 2) 2315 (quot 3 2) 1316 (quot 6 4) 1317 (quot 0 5) 0319 (quot 2 1/2) 4320 (quot 2/3 1/2) 1321 (quot 1 2/3) 1323 (quot 4.0 2.0) 2.0324 (quot 4.5 2.0) 2.0326 ; |num| > |div|, num != k * div327 (quot 42 5) 8 ; (8 * 5) + 2 == 42328 (quot 42 -5) -8 ; (-8 * -5) + 2 == 42329 (quot -42 5) -8 ; (-8 * 5) + -2 == -42330 (quot -42 -5) 8 ; (8 * -5) + -2 == -42332 ; |num| > |div|, num = k * div333 (quot 9 3) 3334 (quot 9 -3) -3335 (quot -9 3) -3336 (quot -9 -3) 3338 ; |num| < |div|339 (quot 2 5) 0340 (quot 2 -5) 0341 (quot -2 5) 0342 (quot -2 -5) 0344 ; num = 0, div != 0345 (quot 0 3) 0346 (quot 0 -3) 0347 )348 )351 ;; *** Predicates ***353 ;; pos? zero? neg?355 (deftest test-pos?-zero?-neg?356 (let [nums [[(byte 2) (byte 0) (byte -2)]357 [(short 3) (short 0) (short -3)]358 [(int 4) (int 0) (int -4)]359 [(long 5) (long 0) (long -5)]360 [(bigint 6) (bigint 0) (bigint -6)]361 [(float 7) (float 0) (float -7)]362 [(double 8) (double 0) (double -8)]363 [(bigdec 9) (bigdec 0) (bigdec -9)]364 [2/3 0 -2/3]]365 pred-result [[pos? [true false false]]366 [zero? [false true false]]367 [neg? [false false true]]] ]368 (doseq [pr pred-result]369 (doseq [n nums]370 (is (= (map (first pr) n) (second pr))371 (pr-str (first pr) n))))))374 ;; even? odd?376 (deftest test-even?377 (are [x] (true? x)378 (even? -4)379 (not (even? -3))380 (even? 0)381 (not (even? 5))382 (even? 8))383 (is (thrown? ArithmeticException (even? 1/2)))384 (is (thrown? ArithmeticException (even? (double 10)))))386 (deftest test-odd?387 (are [x] (true? x)388 (not (odd? -4))389 (odd? -3)390 (not (odd? 0))391 (odd? 5)392 (not (odd? 8)))393 (is (thrown? ArithmeticException (odd? 1/2)))394 (is (thrown? ArithmeticException (odd? (double 10)))))396 (defn- expt397 "clojure.contrib.math/expt is a better and much faster impl, but this works.398 Math/pow overflows to Infinity."399 [x n] (apply * (replicate n x)))401 (deftest test-bit-shift-left402 (are [x y] (= x y)403 2r10 (bit-shift-left 2r1 1)404 2r100 (bit-shift-left 2r1 2)405 2r1000 (bit-shift-left 2r1 3)406 2r00101110 (bit-shift-left 2r00010111 1)407 2r00101110 (apply bit-shift-left [2r00010111 1])408 2r01 (bit-shift-left 2r10 -1)409 (expt 2 32) (bit-shift-left 1 32)410 (expt 2 10000) (bit-shift-left 1 10000)411 ))413 (deftest test-bit-shift-right414 (are [x y] (= x y)415 2r0 (bit-shift-right 2r1 1)416 2r010 (bit-shift-right 2r100 1)417 2r001 (bit-shift-right 2r100 2)418 2r000 (bit-shift-right 2r100 3)419 2r0001011 (bit-shift-right 2r00010111 1)420 2r0001011 (apply bit-shift-right [2r00010111 1])421 2r100 (bit-shift-right 2r10 -1)422 1 (bit-shift-right (expt 2 32) 32)423 1 (bit-shift-right (expt 2 10000) 10000)424 ))427 ;; arrays428 (deftest test-array-types429 (are [x y z] (= (Class/forName x) (class y) (class z))430 "[Z" (boolean-array 1) (booleans (boolean-array 1 true))431 "[B" (byte-array 1) (bytes (byte-array 1 (byte 1)))432 "[C" (char-array 1) (chars (char-array 1 \a))433 "[S" (short-array 1) (shorts (short-array 1 (short 1)))434 "[F" (float-array 1) (floats (float-array 1 1))435 "[D" (double-array 1) (doubles (double-array 1 1))436 "[I" (int-array 1) (ints (int-array 1 1))437 "[J" (long-array 1) (longs (long-array 1 1))))440 (deftest test-ratios441 (is (= (denominator 1/2) 2))442 (is (= (numerator 1/2) 1))443 (is (= (bigint (/ 100000000000000000000 3)) 33333333333333333333))444 (is (= (long 10000000000000000000/3) 3333333333333333333)))