1 | ;-*- Mode: Lisp -*- |
---|
2 | ;;;; Author: Paul Dietz |
---|
3 | ;;;; Created: Mon Sep 1 20:16:42 2003 |
---|
4 | ;;;; Contains: Tests of ABS |
---|
5 | |
---|
6 | (in-package :cl-test) |
---|
7 | |
---|
8 | (compile-and-load "numbers-aux.lsp") |
---|
9 | |
---|
10 | (deftest abs.error.1 |
---|
11 | (signals-error (abs) program-error) |
---|
12 | t) |
---|
13 | |
---|
14 | (deftest abs.error.2 |
---|
15 | (signals-error (abs 0 0) program-error) |
---|
16 | t) |
---|
17 | |
---|
18 | (deftest abs.error.3 |
---|
19 | (signals-error (abs 0 nil nil) program-error) |
---|
20 | t) |
---|
21 | |
---|
22 | (deftest abs.1 |
---|
23 | (loop for x in *numbers* |
---|
24 | for a = (abs x) |
---|
25 | always (and (realp a) (not (minusp a)))) |
---|
26 | t) |
---|
27 | |
---|
28 | (deftest abs.2 |
---|
29 | (loop for x = (random-fixnum) |
---|
30 | for a = (abs x) |
---|
31 | repeat 10000 |
---|
32 | unless (if (plusp x) (eql x a) (eql (- x) a)) |
---|
33 | collect (list x a)) |
---|
34 | nil) |
---|
35 | |
---|
36 | (deftest abs.3 |
---|
37 | (let ((bound (ash 1 300))) |
---|
38 | (loop for x = (random-from-interval bound) |
---|
39 | for a = (abs x) |
---|
40 | repeat 10000 |
---|
41 | unless (if (plusp x) (eql x a) (eql (- x) a)) |
---|
42 | collect (list x a))) |
---|
43 | nil) |
---|
44 | |
---|
45 | (deftest abs.4 |
---|
46 | (loop for num = (random-fixnum) |
---|
47 | for den = (random-fixnum) |
---|
48 | for den2 = (if (zerop den) 1 den) |
---|
49 | for r = (/ num den) |
---|
50 | for a = (abs r) |
---|
51 | repeat 10000 |
---|
52 | unless (if (>= r 0) (eql r a) (eql (- r) a)) |
---|
53 | collect (list num den2 r a)) |
---|
54 | nil) |
---|
55 | |
---|
56 | (deftest abs.5 |
---|
57 | (let ((bound (ash 1 210))) |
---|
58 | (loop |
---|
59 | for num = (random-from-interval bound) |
---|
60 | for den = (random-from-interval bound) |
---|
61 | for den2 = (if (zerop den) 1 den) |
---|
62 | for r = (/ num den) |
---|
63 | for a = (abs r) |
---|
64 | repeat 10000 |
---|
65 | unless (if (>= r 0) (eql r a) (eql (- r) a)) |
---|
66 | collect (list num den2 r a))) |
---|
67 | nil) |
---|
68 | |
---|
69 | (deftest abs.6 |
---|
70 | (let ((bound (float (ash 1 11) 1.0s0))) |
---|
71 | (loop |
---|
72 | for x = (random-from-interval bound) |
---|
73 | for a = (abs x) |
---|
74 | repeat 10000 |
---|
75 | unless (if (minusp x) |
---|
76 | (eql (- x) a) |
---|
77 | (eql x a)) |
---|
78 | collect (list x a))) |
---|
79 | nil) |
---|
80 | |
---|
81 | (deftest abs.7 |
---|
82 | (let ((bound (float (ash 1 22) 1.0f0))) |
---|
83 | (loop |
---|
84 | for x = (random-from-interval bound) |
---|
85 | for a = (abs x) |
---|
86 | repeat 10000 |
---|
87 | unless (if (minusp x) |
---|
88 | (eql (- x) a) |
---|
89 | (eql x a)) |
---|
90 | collect (list x a))) |
---|
91 | nil) |
---|
92 | |
---|
93 | (deftest abs.8 |
---|
94 | (let ((bound (float (ash 1 48) 1.0d0))) |
---|
95 | (loop |
---|
96 | for x = (random-from-interval bound) |
---|
97 | for a = (abs x) |
---|
98 | repeat 10000 |
---|
99 | unless (if (minusp x) |
---|
100 | (eql (- x) a) |
---|
101 | (eql x a)) |
---|
102 | collect (list x a))) |
---|
103 | nil) |
---|
104 | |
---|
105 | (deftest abs.9 |
---|
106 | (let ((bound (float (ash 1 48) 1.0l0))) |
---|
107 | (loop |
---|
108 | for x = (random-from-interval bound) |
---|
109 | for a = (abs x) |
---|
110 | repeat 10000 |
---|
111 | unless (if (minusp x) |
---|
112 | (eql (- x) a) |
---|
113 | (eql x a)) |
---|
114 | collect (list x a))) |
---|
115 | nil) |
---|
116 | |
---|
117 | ;;; The example on the abs page says that (abs -0.0) should be -0,0. |
---|
118 | ;;; However, FABS on the x86 returns 0.0 for that. Since the examples |
---|
119 | ;;; in the hyperspec are not normative, the following four tests |
---|
120 | ;;; have been commented out. |
---|
121 | |
---|
122 | ;;; (deftest abs.10 |
---|
123 | ;;; (abs -0.0s0) |
---|
124 | ;;; -0.0s0) |
---|
125 | ;;; |
---|
126 | ;;; (deftest abs.11 |
---|
127 | ;;; (abs -0.0f0) |
---|
128 | ;;; -0.0f0) |
---|
129 | ;;; |
---|
130 | ;;; (deftest abs.12 |
---|
131 | ;;; (abs -0.0d0) |
---|
132 | ;;; -0.0d0) |
---|
133 | ;;; |
---|
134 | ;;; (deftest abs.13 |
---|
135 | ;;; (abs -0.0l0) |
---|
136 | ;;; -0.0l0) |
---|
137 | |
---|
138 | ;;; Complex numbers |
---|
139 | |
---|
140 | (deftest abs.14 |
---|
141 | (let ((result (abs #c(3 4)))) |
---|
142 | (=t result 5)) |
---|
143 | t) |
---|
144 | |
---|
145 | (deftest abs.15 |
---|
146 | (let ((result (abs #c(-3 4)))) |
---|
147 | (=t result 5)) |
---|
148 | t) |
---|
149 | |
---|
150 | (deftest abs.16 |
---|
151 | (let ((result (abs #c(3 -4)))) |
---|
152 | (=t result 5)) |
---|
153 | t) |
---|
154 | |
---|
155 | (deftest abs.17 |
---|
156 | (let ((result (abs #c(-3 -4)))) |
---|
157 | (=t result 5)) |
---|
158 | t) |
---|
159 | |
---|
160 | (deftest abs.18 |
---|
161 | (abs #c(3.0s0 4.0s0)) |
---|
162 | 5.0s0) |
---|
163 | |
---|
164 | (deftest abs.19 |
---|
165 | (abs #c(3.0f0 -4.0f0)) |
---|
166 | 5.0f0) |
---|
167 | |
---|
168 | (deftest abs.20 |
---|
169 | (abs #c(-3.0d0 4.0d0)) |
---|
170 | 5.0d0) |
---|
171 | |
---|
172 | (deftest abs.21 |
---|
173 | (abs #c(-3.0l0 4.0l0)) |
---|
174 | 5.0l0) |
---|
175 | |
---|
176 | (deftest abs.22 |
---|
177 | (macrolet ((%m (z) z)) |
---|
178 | (abs (expand-in-current-env (%m -4)))) |
---|
179 | 4) |
---|