0.8.14.28:
[sbcl/lichteblau.git] / tests / arith.impure.lisp
blobe4c7e7d6b46b331554bbefddc8ad9c018ec4ca2d
1 ;;;; arithmetic tests with side effects
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.
14 (load "assertoid.lisp")
15 (use-package "ASSERTOID")
17 (defmacro define-compiled-fun (fun name)
18 `(progn
19 (declaim (notinline ,name))
20 (defun ,name (&rest args)
21 (declare (optimize safety))
22 (case (length args)
23 (0 (,fun))
24 (1 (,fun (car args)))
25 (2 (,fun (car args) (cadr args)))
26 (t (apply #',fun args))))))
28 (define-compiled-fun min compiled-min)
29 (define-compiled-fun max compiled-max)
30 (define-compiled-fun + compiled-+)
31 (define-compiled-fun * compiled-*)
32 (define-compiled-fun logand compiled-logand)
33 (define-compiled-fun logior compiled-logior)
34 (define-compiled-fun logxor compiled-logxor)
36 (assert (null (ignore-errors (compiled-min '(1 2 3)))))
37 (assert (= (compiled-min -1) -1))
38 (assert (null (ignore-errors (compiled-min 1 #(1 2 3)))))
39 (assert (= (compiled-min 10 11) 10))
40 (assert (null (ignore-errors (compiled-min (find-package "CL") -5.0))))
41 (assert (= (compiled-min 5.0 -3) -3))
42 (assert (null (ignore-errors (compiled-max #c(4 3)))))
43 (assert (= (compiled-max 0) 0))
44 (assert (null (ignore-errors (compiled-max "MIX" 3))))
45 (assert (= (compiled-max -1 10.0) 10.0))
46 (assert (null (ignore-errors (compiled-max 3 #'max))))
47 (assert (= (compiled-max -3 0) 0))
49 (assert (null (ignore-errors (compiled-+ "foo"))))
50 (assert (= (compiled-+ 3f0) 3f0))
51 (assert (null (ignore-errors (compiled-+ 1 #p"tmp"))))
52 (assert (= (compiled-+ 1 2) 3))
53 (assert (null (ignore-errors (compiled-+ '(1 2 3) 3))))
54 (assert (= (compiled-+ 3f0 4f0) 7f0))
55 (assert (null (ignore-errors (compiled-* "foo"))))
56 (assert (= (compiled-* 3f0) 3f0))
57 (assert (null (ignore-errors (compiled-* 1 #p"tmp"))))
58 (assert (= (compiled-* 1 2) 2))
59 (assert (null (ignore-errors (compiled-* '(1 2 3) 3))))
60 (assert (= (compiled-* 3f0 4f0) 12f0))
62 (assert (null (ignore-errors (compiled-logand #(1)))))
63 (assert (= (compiled-logand 1) 1))
64 (assert (null (ignore-errors (compiled-logior 3f0))))
65 (assert (= (compiled-logior 4) 4))
66 (assert (null (ignore-errors (compiled-logxor #c(2 3)))))
67 (assert (= (compiled-logxor -6) -6))
69 (assert (raises-error? (coerce (expt 10 1000) 'single-float) type-error))
71 (defun are-we-getting-ash-right (x y)
72 (declare (optimize speed)
73 (type (unsigned-byte 32) x)
74 (type (integer -40 0) y))
75 (ash x y))
76 (defun what-about-with-constants (x)
77 (declare (optimize speed) (type (unsigned-byte 32) x))
78 (ash x -32))
80 (dotimes (i 41)
81 (assert (= (are-we-getting-ash-right (1- (ash 1 32)) (- i))
82 (if (< i 32)
83 (1- (ash 1 (- 32 i)))
84 0))))
85 (assert (= (what-about-with-constants (1- (ash 1 32))) 0))
87 (defun one-more-test-case-to-catch-sparc (x y)
88 (declare (optimize speed (safety 0))
89 (type (unsigned-byte 32) x) (type (integer -40 2) y))
90 (the (unsigned-byte 32) (ash x y)))
91 (assert (= (one-more-test-case-to-catch-sparc (1- (ash 1 32)) -40) 0))
93 (defun 64-bit-logcount (x)
94 (declare (optimize speed) (type (unsigned-byte 54) x))
95 (logcount x))
96 (assert (= (64-bit-logcount (1- (ash 1 24))) 24))
97 (assert (= (64-bit-logcount (1- (ash 1 32))) 32))
98 (assert (= (64-bit-logcount (1- (ash 1 48))) 48))
99 (assert (= (64-bit-logcount (1- (ash 1 54))) 54))
101 (sb-ext:quit :unix-status 104)