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 the
3 ; 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 by
6 ; the terms of this license.
7 ; You must not remove this notice, or any other, from this software.
9 ; Author: Stephen C. Gilardi
10 ;; scgilardi (gmail)
11 ;; Created 30 October 2008
12 ;;
14 (ns clojure.test-clojure.numbers
15 (:use clojure.test))
18 ; TODO:
19 ; ==
20 ; and more...
23 ;; *** Types ***
25 (deftest Coerced-Byte
26 (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-Short
34 (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-Integer
42 (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-Long
50 (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-BigInteger
58 (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-Float
66 (let [v (float 3)]
67 (are [x] (true? x)
68 (instance? Float v)
69 (number? v)
70 (float? v))))
72 (deftest Coerced-Double
73 (let [v (double 3)]
74 (are [x] (true? x)
75 (instance? Double v)
76 (number? v)
77 (float? v))))
79 (deftest Coerced-BigDecimal
80 (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-add
93 (are [x y] (= x y)
94 (+) 0
95 (+ 1) 1
96 (+ 1 2) 3
97 (+ 1 2 3) 6
99 (+ -1) -1
100 (+ -1 -2) -3
101 (+ -1 +2 -3) -2
103 (+ 1 -1) 0
104 (+ -1 1) 0
106 (+ 2/3) 2/3
107 (+ 2/3 1) 5/3
108 (+ 2/3 1/3) 1 )
110 (are [x y] (< (- x y) DELTA)
111 (+ 1.2) 1.2
112 (+ 1.1 2.4) 3.5
113 (+ 1.1 2.2 3.3) 6.6 )
115 (is (> (+ Integer/MAX_VALUE 10) Integer/MAX_VALUE)) ; no overflow
116 (is (thrown? ClassCastException (+ "ab" "cd"))) ) ; no string concatenation
119 (deftest test-subtract
120 (is (thrown? IllegalArgumentException (-)))
121 (are [x y] (= x y)
122 (- 1) -1
123 (- 1 2) -1
124 (- 1 2 3) -4
126 (- -2) 2
127 (- 1 -2) 3
128 (- 1 -2 -3) 6
130 (- 1 1) 0
131 (- -1 -1) 0
133 (- 2/3) -2/3
134 (- 2/3 1) -1/3
135 (- 2/3 1/3) 1/3 )
137 (are [x y] (< (- x y) DELTA)
138 (- 1.2) -1.2
139 (- 2.2 1.1) 1.1
140 (- 6.6 2.2 1.1) 3.3 )
142 (is (< (- Integer/MIN_VALUE 10) Integer/MIN_VALUE)) ) ; no underflow
145 (deftest test-multiply
146 (are [x y] (= x y)
147 (*) 1
148 (* 2) 2
149 (* 2 3) 6
150 (* 2 3 4) 24
152 (* -2) -2
153 (* 2 -3) -6
154 (* 2 -3 -1) 6
156 (* 1/2) 1/2
157 (* 1/2 1/3) 1/6
158 (* 1/2 1/3 -1/4) -1/24 )
160 (are [x y] (< (- x y) DELTA)
161 (* 1.2) 1.2
162 (* 2.0 1.2) 2.4
163 (* 3.5 2.0 1.2) 8.4 )
165 (is (> (* 3 (int (/ Integer/MAX_VALUE 2.0))) Integer/MAX_VALUE)) ) ; no overflow
167 (deftest test-ratios-simplify-to-ints-where-appropriate
168 (testing "negative denominator (assembla #275)"
169 (is (integer? (/ 1 -1/2)))
170 (is (integer? (/ 0 -1/2)))))
172 (deftest test-divide
173 (are [x y] (= x y)
174 (/ 1) 1
175 (/ 2) 1/2
176 (/ 3 2) 3/2
177 (/ 4 2) 2
178 (/ 24 3 2) 4
179 (/ 24 3 2 -1) -4
181 (/ -1) -1
182 (/ -2) -1/2
183 (/ -3 -2) 3/2
184 (/ -4 -2) 2
185 (/ -4 2) -2 )
187 (are [x y] (< (- x y) DELTA)
188 (/ 4.5 3) 1.5
189 (/ 4.5 3.0 3.0) 0.5 )
191 (is (thrown? ArithmeticException (/ 0)))
192 (is (thrown? ArithmeticException (/ 2 0)))
193 (is (thrown? IllegalArgumentException (/))) )
196 ;; mod
197 ;; http://en.wikipedia.org/wiki/Modulo_operation
198 ;; http://mathforum.org/library/drmath/view/52343.html
199 ;;
200 ;; is mod correct?
201 ;; http://groups.google.com/group/clojure/browse_frm/thread/2a0ee4d248f3d131#
202 ;;
203 ;; Issue 23: mod (modulo) operator
204 ;; http://code.google.com/p/clojure/issues/detail?id=23
206 (deftest test-mod
207 ; wrong number of args
208 (is (thrown? IllegalArgumentException (mod)))
209 (is (thrown? IllegalArgumentException (mod 1)))
210 (is (thrown? IllegalArgumentException (mod 3 2 1)))
212 ; divide by zero
213 (is (thrown? ArithmeticException (mod 9 0)))
214 (is (thrown? ArithmeticException (mod 0 0)))
216 (are [x y] (= x y)
217 (mod 4 2) 0
218 (mod 3 2) 1
219 (mod 6 4) 2
220 (mod 0 5) 0
222 (mod 2 1/2) 0
223 (mod 2/3 1/2) 1/6
224 (mod 1 2/3) 1/3
226 (mod 4.0 2.0) 0.0
227 (mod 4.5 2.0) 0.5
229 ; |num| > |div|, num != k * div
230 (mod 42 5) 2 ; (42 / 5) * 5 + (42 mod 5) = 8 * 5 + 2 = 42
231 (mod 42 -5) -3 ; (42 / -5) * (-5) + (42 mod -5) = -9 * (-5) + (-3) = 42
232 (mod -42 5) 3 ; (-42 / 5) * 5 + (-42 mod 5) = -9 * 5 + 3 = -42
233 (mod -42 -5) -2 ; (-42 / -5) * (-5) + (-42 mod -5) = 8 * (-5) + (-2) = -42
235 ; |num| > |div|, num = k * div
236 (mod 9 3) 0 ; (9 / 3) * 3 + (9 mod 3) = 3 * 3 + 0 = 9
237 (mod 9 -3) 0
238 (mod -9 3) 0
239 (mod -9 -3) 0
241 ; |num| < |div|
242 (mod 2 5) 2 ; (2 / 5) * 5 + (2 mod 5) = 0 * 5 + 2 = 2
243 (mod 2 -5) -3 ; (2 / -5) * (-5) + (2 mod -5) = (-1) * (-5) + (-3) = 2
244 (mod -2 5) 3 ; (-2 / 5) * 5 + (-2 mod 5) = (-1) * 5 + 3 = -2
245 (mod -2 -5) -2 ; (-2 / -5) * (-5) + (-2 mod -5) = 0 * (-5) + (-2) = -2
247 ; num = 0, div != 0
248 (mod 0 3) 0 ; (0 / 3) * 3 + (0 mod 3) = 0 * 3 + 0 = 0
249 (mod 0 -3) 0
250 )
251 )
253 ;; rem & quot
254 ;; http://en.wikipedia.org/wiki/Remainder
256 (deftest test-rem
257 ; wrong number of args
258 (is (thrown? IllegalArgumentException (rem)))
259 (is (thrown? IllegalArgumentException (rem 1)))
260 (is (thrown? IllegalArgumentException (rem 3 2 1)))
262 ; divide by zero
263 (is (thrown? ArithmeticException (rem 9 0)))
264 (is (thrown? ArithmeticException (rem 0 0)))
266 (are [x y] (= x y)
267 (rem 4 2) 0
268 (rem 3 2) 1
269 (rem 6 4) 2
270 (rem 0 5) 0
272 (rem 2 1/2) 0
273 (rem 2/3 1/2) 1/6
274 (rem 1 2/3) 1/3
276 (rem 4.0 2.0) 0.0
277 (rem 4.5 2.0) 0.5
279 ; |num| > |div|, num != k * div
280 (rem 42 5) 2 ; (8 * 5) + 2 == 42
281 (rem 42 -5) 2 ; (-8 * -5) + 2 == 42
282 (rem -42 5) -2 ; (-8 * 5) + -2 == -42
283 (rem -42 -5) -2 ; (8 * -5) + -2 == -42
285 ; |num| > |div|, num = k * div
286 (rem 9 3) 0
287 (rem 9 -3) 0
288 (rem -9 3) 0
289 (rem -9 -3) 0
291 ; |num| < |div|
292 (rem 2 5) 2
293 (rem 2 -5) 2
294 (rem -2 5) -2
295 (rem -2 -5) -2
297 ; num = 0, div != 0
298 (rem 0 3) 0
299 (rem 0 -3) 0
300 )
301 )
303 (deftest test-quot
304 ; wrong number of args
305 (is (thrown? IllegalArgumentException (quot)))
306 (is (thrown? IllegalArgumentException (quot 1)))
307 (is (thrown? IllegalArgumentException (quot 3 2 1)))
309 ; divide by zero
310 (is (thrown? ArithmeticException (quot 9 0)))
311 (is (thrown? ArithmeticException (quot 0 0)))
313 (are [x y] (= x y)
314 (quot 4 2) 2
315 (quot 3 2) 1
316 (quot 6 4) 1
317 (quot 0 5) 0
319 (quot 2 1/2) 4
320 (quot 2/3 1/2) 1
321 (quot 1 2/3) 1
323 (quot 4.0 2.0) 2.0
324 (quot 4.5 2.0) 2.0
326 ; |num| > |div|, num != k * div
327 (quot 42 5) 8 ; (8 * 5) + 2 == 42
328 (quot 42 -5) -8 ; (-8 * -5) + 2 == 42
329 (quot -42 5) -8 ; (-8 * 5) + -2 == -42
330 (quot -42 -5) 8 ; (8 * -5) + -2 == -42
332 ; |num| > |div|, num = k * div
333 (quot 9 3) 3
334 (quot 9 -3) -3
335 (quot -9 3) -3
336 (quot -9 -3) 3
338 ; |num| < |div|
339 (quot 2 5) 0
340 (quot 2 -5) 0
341 (quot -2 5) 0
342 (quot -2 -5) 0
344 ; num = 0, div != 0
345 (quot 0 3) 0
346 (quot 0 -3) 0
347 )
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- expt
397 "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-left
402 (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-right
414 (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 ;; arrays
428 (deftest test-array-types
429 (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-ratios
441 (is (= (denominator 1/2) 2))
442 (is (= (numerator 1/2) 1))
443 (is (= (bigint (/ 100000000000000000000 3)) 33333333333333333333))
444 (is (= (long 10000000000000000000/3) 3333333333333333333)))