Fix comment
[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 (test (= (max 1 2 3 4 5) 5))
12
13 ;;; MIN
14 (test (= (min 1)     1))
15 (test (= (min 1 2 3) 1))
16 (test (= (min 3 2 1) 1))
17 (test (= (min 9 3 8 7 6 3 3) 3))
18
19 ;;; EVENP
20 (test      (evenp  2))
21 (test      (evenp -2))
22 (test (not (evenp  1)))
23 (test      (evenp  0))
24
25 ;;; ODDP
26 (test      (oddp  3))
27 (test      (oddp -3))
28 (test (not (oddp  2)))
29 (test (not (oddp  0)))
30
31 ;;; +, -, *, /
32 ;;; The builtin definition of these is variadic, but the function definition
33 ;;; should be as well. So, test it using MAPCAR
34 (let* ((a '(1 2))
35        (b a)
36        (c a))
37   (test (equal (mapcar #'+ a b c) '( 3  6)))
38   (test (equal (mapcar #'- a b c) '(-1 -2)))
39   (test (equal (mapcar #'* a b c) '( 1  8)))
40   ;; This test will need to be changed when rationals are introduced
41   (test (equal (mapcar #'/ a b c) '( 1  0.5))))
42
43 ;;; >, >=, =, <, <=, /=
44 ;;; As above, we need to make sure the function is called, not the builtin
45 (let ((a '(1 3 1 2 1))
46       (b '(2 2 2 2 1))
47       (c '(3 1 2 1 1)))
48   (test (equal (mapcar #'>  a b c) '(nil   t nil nil nil)))
49   (test (equal (mapcar #'>= a b c) '(nil   t nil   t   t)))
50   (test (equal (mapcar #'=  a b c) '(nil nil nil nil   t)))
51   (test (equal (mapcar #'<  a b c) '(  t nil nil nil nil)))
52   (test (equal (mapcar #'<= a b c) '(  t nil   t nil   t)))
53   (test (equal (mapcar #'/= a b c) '(  t   t nil nil nil))))
54
55 ;;; INTEGERP
56 (test (integerp  1))
57 (test (integerp -1))
58 (test (integerp  0))
59
60 ;;; FLOATP
61
62 ;; It is a known bug. Javascript does not distinguish between floats
63 ;; and integers, and we represent both numbers in the same way. So 1
64 ;; == 1.0 and integer and float types are not disjoint.
65 (expected-failure (floatp 1.0)) 
66
67 (test             (floatp    1.1))
68 (test             (floatp    pi))
69 (test             (floatp (- pi)))
70 (test        (not (floatp    1)))
71
72 ;;; GCD
73 (test (= 0 (gcd)))
74 (test (= 6 (gcd 60 42)))
75 (test (= 1 (gcd 3333 -33 101)))
76 (test (= 11 (gcd 3333 -33 1002001)))
77 (test (= 7 (gcd 91 -49)))
78 (test (= 7 (gcd 63 -42 35)))
79 (test (= 5 (gcd 5)))
80 (test (= 4 (gcd -4)))
81
82 ;;; LCM
83 (test (= 10 (lcm 10)))
84 (test (= 150 (lcm 25 30)))
85 (test (= 360 (lcm -24 18 10)))
86 (test (= 70 (lcm 14 35)))
87 (test (= 0 (lcm 0 5)))
88 (test (= 60 (lcm 1 2 3 4 5 6)))