0.7.13.13:
[sbcl.git] / tests / arith.pure.lisp
1 ;;;; arithmetic tests with no side effects
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
8 ;;;; from CMU CL.
9 ;;;; 
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
13
14 (cl:in-package :cl-user)
15
16 ;;; Once upon a time, in the process of porting CMUCL's SPARC backend
17 ;;; to SBCL, multiplications were excitingly broken.  While it's
18 ;;; unlikely that anything with such fundamental arithmetic errors as
19 ;;; these are going to get this far, it's probably worth checking.
20 (macrolet ((test (op res1 res2)
21              `(progn
22                (assert (= (,op 4 2) ,res1))
23                (assert (= (,op 2 4) ,res2))
24                (assert (= (funcall (compile nil (lambda (x y) (,op x y))) 4 2) 
25                         ,res1))
26                (assert (= (funcall (compile nil (lambda (x y) (,op x y))) 2 4) 
27                         ,res2)))))
28   (test + 6 6)
29   (test - 2 -2)
30   (test * 8 8)
31   (test / 2 1/2)
32   (test expt 16 16))
33
34 ;;; In a bug reported by Wolfhard Buss on cmucl-imp 2002-06-18 (BUG
35 ;;; 184), sbcl didn't catch all divisions by zero, notably divisions
36 ;;; of bignums and ratios by 0.  Fixed in sbcl-0.7.6.13.
37 (assert (raises-error? (/ 2/3 0) division-by-zero))
38 (assert (raises-error? (/ (1+ most-positive-fixnum) 0) division-by-zero))
39
40 ;;; In a bug reported by Raymond Toy on cmucl-imp 2002-07-18, (COERCE
41 ;;; <RATIONAL> '(COMPLEX FLOAT)) was failing to return a complex
42 ;;; float; a patch was given by Wolfhard Buss cmucl-imp 2002-07-19.
43 (assert (= (coerce 1 '(complex float)) #c(1.0 0.0)))
44 (assert (= (coerce 1/2 '(complex float)) #c(0.5 0.0)))
45 (assert (= (coerce 1.0d0 '(complex float)) #c(1.0d0 0.0d0)))
46
47 ;;; COERCE also sometimes failed to verify that a particular coercion
48 ;;; was possible (in particular coercing rationals to bounded float
49 ;;; types.
50 (assert (raises-error? (coerce 1 '(float 2.0 3.0)) type-error))
51 (assert (raises-error? (coerce 1 '(single-float -1.0 0.0)) type-error))
52 (assert (eql (coerce 1 '(single-float -1.0 2.0)) 1.0))
53
54 ;;; ANSI says MIN and MAX should signal TYPE-ERROR if any argument
55 ;;; isn't REAL. SBCL 0.7.7 didn't in the 1-arg case. (reported as a
56 ;;; bug in CMU CL on #lisp IRC by lrasinen 2002-09-01)
57 #||
58
59 FIXME: These tests would be good to have. But although, in
60 sbcl-0.7.7.2x, (NULL (IGNORE-ERRORS (MIN 1 #(1 2 3)))) returns T, the
61 ASSERTion fails, probably in something related to bug #194.
62
63 (assert (null (ignore-errors (min '(1 2 3)))))
64 (assert (= (min -1) -1))
65 (assert (null (ignore-errors (min 1 #(1 2 3)))))
66 (assert (= (min 10 11) 10))
67 (assert (null (ignore-errors (min (find-package "CL") -5.0))))
68 (assert (= (min 5.0 -3) -3))
69 (assert (null (ignore-errors (max #c(4 3)))))
70 (assert (= (max 0) 0))
71 (assert (null (ignore-errors (max "MIX" 3))))
72 (assert (= (max -1 10.0) 10.0))
73 (assert (null (ignore-errors (max 3 #'max))))
74 (assert (= (max -3 0) 0))
75 ||#
76
77 ;;; (CEILING x 2^k) was optimized incorrectly
78 (loop for divisor in '(-4 4)
79    for ceiler = (compile nil `(lambda (x)
80                                 (declare (fixnum x))
81                                 (declare (optimize (speed 3)))
82                                 (ceiling x ,divisor)))
83    do (loop for i from -5 to 5
84          for exact-q = (/ i divisor)
85          do (multiple-value-bind (q r)
86                 (funcall ceiler i)
87               (assert (= (+ (* q divisor) r) i))
88               (assert (<= exact-q q))
89               (assert (< q (1+ exact-q))))))