f58473fe1ea5074692fd93b095ba49b7be95d877
[jscl.git] / tests / numbers.lisp
1 ;;;; Tests for numeric functions
2
3 ; ABS
4 (test (= (abs  3) 3))
5 (test (= (abs -3) 3))
6
7 ; MAX
8 (test (= (max 1)     1))
9 (test (= (max 1 2 3) 3))
10 (test (= (max 3 2 1) 3))
11
12 ; MIN
13 (test (= (min 1)     1))
14 (test (= (min 1 2 3) 1))
15 (test (= (min 3 2 1) 1))
16
17 ; EVENP
18 (test      (evenp  2))
19 (test      (evenp -2))
20 (test (not (evenp  1)))
21 (test      (evenp  0))
22
23 ; ODDP
24 (test      (oddp  3))
25 (test      (oddp -3))
26 (test (not (oddp  2)))
27 (test (not (oddp  0)))
28
29 ; +, -, *, /
30 ; The builtin definition of these is variadic, but the function definition
31 ; should be as well. So, test it using MAPCAR
32 (let* ((a '(1 2))
33        (b a)
34        (c a))
35   (test (equal (mapcar #'+ a b c) '( 3  6)))
36   (test (equal (mapcar #'- a b c) '(-1 -2)))
37   (test (equal (mapcar #'* a b c) '( 1  8)))
38   ; This test will need to be changed when rationals are introduced
39   (test (equal (mapcar #'/ a b c) '( 1  0.5))))
40
41 ; >, >=, =, <, <=, /=
42 ; As above, we need to make sure the function is called, not the builtin
43 (let ((a '(1 3 1 2 1))
44       (b '(2 2 2 2 1))
45       (c '(3 1 2 1 1)))
46   (test (equal (mapcar #'>  a b c) '(nil   t nil nil nil)))
47   (test (equal (mapcar #'>= a b c) '(nil   t nil   t   t)))
48   (test (equal (mapcar #'=  a b c) '(nil nil nil nil   t)))
49   (test (equal (mapcar #'<  a b c) '(  t nil nil nil nil)))
50   (test (equal (mapcar #'<= a b c) '(  t nil   t nil   t)))
51   (test (equal (mapcar #'/= a b c) '(  t   t nil nil nil))))
52
53 ; INTEGERP
54 (test (integerp  1))
55 (test (integerp -1))
56 (test (integerp  0))
57
58 ; FLOATP
59 (expected-failure (floatp    1.0)) ; The reader reads 1.0 as an int 
60 (test             (floatp    1.1))
61 (test             (floatp    pi))
62 (test             (floatp (- pi)))
63 (test        (not (floatp    1)))