1 | ;-*- Mode: Lisp -*- |
---|
2 | ;;;; Author: Paul Dietz |
---|
3 | ;;;; Created: Tue Sep 2 19:36:22 2003 |
---|
4 | ;;;; Contains: Tests of EXPT |
---|
5 | |
---|
6 | (in-package :cl-test) |
---|
7 | |
---|
8 | ;;; Error tests |
---|
9 | |
---|
10 | (deftest expt.error.1 |
---|
11 | (signals-error (expt) program-error) |
---|
12 | t) |
---|
13 | |
---|
14 | (deftest expt.error.2 |
---|
15 | (signals-error (expt 1 1 1) program-error) |
---|
16 | t) |
---|
17 | |
---|
18 | (deftest expt.error.3 |
---|
19 | (signals-error (expt 1 1 nil nil) program-error) |
---|
20 | t) |
---|
21 | |
---|
22 | (deftest expt.error.4 |
---|
23 | (signals-error (expt most-positive-short-float 2) floating-point-overflow) |
---|
24 | t) |
---|
25 | |
---|
26 | (deftest expt.error.5 |
---|
27 | (signals-error (expt most-positive-single-float 2) floating-point-overflow) |
---|
28 | t) |
---|
29 | |
---|
30 | (deftest expt.error.6 |
---|
31 | (signals-error (expt most-positive-double-float 2) floating-point-overflow) |
---|
32 | t) |
---|
33 | |
---|
34 | (deftest expt.error.7 |
---|
35 | (signals-error (expt most-positive-long-float 2) floating-point-overflow) |
---|
36 | t) |
---|
37 | |
---|
38 | #+bogus-test |
---|
39 | (deftest expt.error.8 |
---|
40 | (signals-error (expt least-positive-short-float 2) floating-point-underflow) |
---|
41 | t) |
---|
42 | |
---|
43 | #+bogus-test |
---|
44 | (deftest expt.error.9 |
---|
45 | (signals-error (expt least-positive-single-float 2) floating-point-underflow) |
---|
46 | t) |
---|
47 | |
---|
48 | #+bogus-test |
---|
49 | (deftest expt.error.10 |
---|
50 | (signals-error (expt least-positive-double-float 2) floating-point-underflow) |
---|
51 | t) |
---|
52 | |
---|
53 | #+bogus-test |
---|
54 | (deftest expt.error.11 |
---|
55 | (signals-error (expt least-positive-long-float 2) floating-point-underflow) |
---|
56 | t) |
---|
57 | |
---|
58 | |
---|
59 | |
---|
60 | |
---|
61 | ;;; Non-error tests |
---|
62 | |
---|
63 | (deftest expt.1 |
---|
64 | (expt 0 0) |
---|
65 | 1) |
---|
66 | |
---|
67 | (deftest expt.2 |
---|
68 | (loop for i from -1000 to 1000 |
---|
69 | always (eql (expt i 0) 1)) |
---|
70 | t) |
---|
71 | |
---|
72 | (deftest expt.3 |
---|
73 | (loop for i = (random 1.0s3) |
---|
74 | repeat 1000 |
---|
75 | always (eql (expt i 0) 1.0s0)) |
---|
76 | t) |
---|
77 | |
---|
78 | (deftest expt.4 |
---|
79 | (loop for i = (random 1.0f6) |
---|
80 | repeat 1000 |
---|
81 | always (eql (expt i 0) 1.0f0)) |
---|
82 | t) |
---|
83 | |
---|
84 | (deftest expt.5 |
---|
85 | (loop for i = (random 1.0d10) |
---|
86 | repeat 1000 |
---|
87 | always (eql (expt i 0) 1.0d0)) |
---|
88 | t) |
---|
89 | |
---|
90 | (deftest expt.6 |
---|
91 | (loop for i = (random 1.0l10) |
---|
92 | repeat 1000 |
---|
93 | always (eql (expt i 0) 1.0l0)) |
---|
94 | t) |
---|
95 | |
---|
96 | (deftest expt.7 |
---|
97 | (loop for i from -1000 to 1000 |
---|
98 | for c = (complex i i) |
---|
99 | always (eql (expt c 0) 1)) |
---|
100 | t) |
---|
101 | |
---|
102 | (deftest expt.8 |
---|
103 | (loop for i = (random 1.0s3) |
---|
104 | for c = (complex i i) |
---|
105 | repeat 1000 |
---|
106 | always (eql (expt c 0) #c(1.0s0 0.0s0))) |
---|
107 | t) |
---|
108 | |
---|
109 | (deftest expt.9 |
---|
110 | (loop for i = (random 1.0f6) |
---|
111 | for c = (complex i i) |
---|
112 | repeat 1000 |
---|
113 | always (eql (expt c 0) #c(1.0f0 0.0f0))) |
---|
114 | t) |
---|
115 | |
---|
116 | (deftest expt.10 |
---|
117 | (loop for i = (random 1.0d10) |
---|
118 | for c = (complex i i) |
---|
119 | repeat 1000 |
---|
120 | always (eql (expt c 0) #c(1.0d0 0.0d0))) |
---|
121 | t) |
---|
122 | |
---|
123 | (deftest expt.11 |
---|
124 | (loop for i = (random 1.0l10) |
---|
125 | for c = (complex i i) |
---|
126 | repeat 1000 |
---|
127 | always (eql (expt c 0) #c(1.0l0 0.0l0))) |
---|
128 | t) |
---|
129 | |
---|
130 | (deftest expt.12 |
---|
131 | (loop for x in *numbers* |
---|
132 | unless (or (floatp (realpart x)) |
---|
133 | (eql (expt x 1) x)) |
---|
134 | collect x) |
---|
135 | nil) |
---|
136 | |
---|
137 | (deftest expt.13 |
---|
138 | (loop for x in *rationals* |
---|
139 | unless (and (eql (expt x 2) (* x x)) |
---|
140 | (or (zerop x) |
---|
141 | (eql (expt x -1) (/ x)))) |
---|
142 | collect x) |
---|
143 | nil) |
---|
144 | |
---|
145 | (deftest expt.14 |
---|
146 | (expt #c(0 2) 2) |
---|
147 | -4) |
---|
148 | |
---|
149 | (deftest expt.15 |
---|
150 | (expt #c(1 1) 2) |
---|
151 | #c(0 2)) |
---|
152 | |
---|
153 | (deftest expt.16 |
---|
154 | (expt #c(1/2 1/3) 3) |
---|
155 | #c(-1/24 23/108)) |
---|
156 | |
---|
157 | (deftest expt.17 |
---|
158 | (expt #c(1 1) -2) |
---|
159 | #c(0 -1/2)) |
---|
160 | |
---|
161 | (deftest expt.18 |
---|
162 | (loop |
---|
163 | for zero in '(0.0s0 0.0f0 0.0d0 0.0l0) |
---|
164 | always |
---|
165 | (loop for i from -1000 to 1000 |
---|
166 | always (or (zerop i) |
---|
167 | (eql (expt i zero) (float 1 zero))))) |
---|
168 | t) |
---|
169 | |
---|
170 | (deftest expt.19 |
---|
171 | (loop |
---|
172 | for zero in '(0.0s0 0.0f0 0.0d0 0.0l0) |
---|
173 | always |
---|
174 | (loop for i from -1000 to 1000 |
---|
175 | always (or (zerop i) |
---|
176 | (eql (expt (float i 0.0s0) zero) (float 1 zero))))) |
---|
177 | t) |
---|
178 | |
---|
179 | (deftest expt.20 |
---|
180 | (loop |
---|
181 | for zero in '(0.0f0 0.0d0 0.0l0) |
---|
182 | always |
---|
183 | (loop for i from -1000 to 1000 |
---|
184 | always (or (zerop i) |
---|
185 | (eql (expt (float i 0.0f0) zero) (float 1 zero))))) |
---|
186 | t) |
---|
187 | |
---|
188 | (deftest expt.21 |
---|
189 | (loop |
---|
190 | for zero in '(0.0d0 0.0l0) |
---|
191 | always |
---|
192 | (loop for i from -1000 to 1000 |
---|
193 | always (or (zerop i) |
---|
194 | (eql (expt (float i 0.0d0) zero) (float 1 zero))))) |
---|
195 | t) |
---|
196 | |
---|
197 | (deftest expt.22 |
---|
198 | (expt 2.0f0 0.0s0) |
---|
199 | 1.0f0) |
---|
200 | |
---|
201 | (deftest expt.23 |
---|
202 | (expt 2.0d0 0.0s0) |
---|
203 | 1.0d0) |
---|
204 | |
---|
205 | (deftest expt.24 |
---|
206 | (expt 2.0l0 0.0s0) |
---|
207 | 1.0l0) |
---|
208 | |
---|
209 | (deftest expt.25 |
---|
210 | (expt 2.0d0 0.0f0) |
---|
211 | 1.0d0) |
---|
212 | |
---|
213 | (deftest expt.26 |
---|
214 | (expt 2.0l0 0.0f0) |
---|
215 | 1.0l0) |
---|
216 | |
---|
217 | (deftest expt.27 |
---|
218 | (expt 2.0l0 0.0d0) |
---|
219 | 1.0l0) |
---|
220 | |
---|
221 | (deftest expt.28 |
---|
222 | (<= (realpart (expt -8 1/3)) 0.0) |
---|
223 | nil) |
---|
224 | |
---|
225 | #| |
---|
226 | ;;; FIXME |
---|
227 | ;;; I need to think more about how to do approximate float |
---|
228 | ;;; equality in a principled way. |
---|
229 | |
---|
230 | (deftest expt.29 |
---|
231 | (loop for bound in '(1.0s4 1.0f6 1.0d8 1.0l8) |
---|
232 | for ebound in (list short-float-epsilon single-float-epsilon |
---|
233 | double-float-epsilon long-float-epsilon) |
---|
234 | for ebound2 = (max (* 2 ebound) (/ bound)) |
---|
235 | nconc |
---|
236 | (loop for x = (1+ (random 1.0f6)) |
---|
237 | for s1 = (sqrt x) |
---|
238 | for s2 = (expt x 1/2) |
---|
239 | for error = (/ (abs (- s2 s2)) x) |
---|
240 | repeat 1000 |
---|
241 | unless (< error ebound2) |
---|
242 | collect (list x s1 s2))) |
---|
243 | nil) |
---|
244 | |
---|
245 | (deftest expt.30 |
---|
246 | (loop for bound in '(1.0s4 1.0f6 1.0d8 1.0l8) |
---|
247 | for ebound in (list short-float-epsilon single-float-epsilon |
---|
248 | double-float-epsilon long-float-epsilon) |
---|
249 | for ebound2 = (max (* 2 ebound) (/ bound)) |
---|
250 | nconc |
---|
251 | (loop for x = (- (1+ (random 1.0f6))) |
---|
252 | for s1 = (sqrt x) |
---|
253 | for s2 = (expt x 1/2) |
---|
254 | for error = (/ (abs (- s2 s2)) x) |
---|
255 | repeat 1000 |
---|
256 | unless (< error ebound2) |
---|
257 | collect (list x s1 s2))) |
---|
258 | nil) |
---|
259 | |# |
---|