Debian Codegraph @

src/tests/normal-tests/complex.lsp

raw
1
2 (in-package :cl-test)
3 (suite 'complex)
4
5 ;;; Test suite is for testing complex types (added as part of adding
6 ;;; complex float as a native type).
7
8 (test complex.0001.upgradex-complex-part-type
9 (signals error (upgraded-complex-part-type 'string))
10 (mapc (lambda (upgraded-typespec typespec)
11 (is (eql upgraded-typespec (upgraded-complex-part-type typespec))
12 "upgraded-complex-part-type of ~s should be ~s, is ~s"
13 typespec upgraded-typespec (upgraded-complex-part-type typespec)))
14 #-complex-float (make-list 10 :initial-element 'real)
15 #+complex-float (append (make-list 5 :initial-element 'rational)
16 '(float single-float single-float double-float long-float))
17 '(rational integer fixnum (integer 0 1) ratio
18 float short-float single-float double-float long-float)))
19
20 (test complex.0002.type-of
21 (mapc (lambda (type element)
22 (is (equal type (type-of element)) "(type-of ~s) not equal to ~s, is ~s"
23 element type (type-of element)))
24 #-complex-float (make-list 6 :initial-element '(complex real))
25 #+complex-float '((complex rational)
26 (complex rational)
27 (complex single-float)
28 (complex single-float)
29 (complex double-float)
30 (complex long-float))
31 (list #c(1 2)
32 #c(1/2 3/2)
33 #c(1.2 3.2)
34 #c(1.2s0 3.2s0)
35 #c(1.2d0 3.2d0)
36 #c(1.2l0 3.2l0))))
37
38 ;;; ensures that common arithmetic operations doesn't fail
39 (defmacro without-fpe-traps (&body body)
40 `(let ((bits (si:trap-fpe 'cl:last t)))
41 (unwind-protect
42 (progn (si:trap-fpe t nil)
43 ,@body)
44 (si:trap-fpe bits t))))
45
46 (defmacro should-signal (type &body body)
47 `(handler-case (progn ,@body)
48 (:no-error (&rest args)
49 (finishes (error "should signal ~s" ',type)))
50 (,type () t)))
51
52 (test complex.0003.arith
53 (without-fpe-traps
54 (flet ((check-op-1 (op)
55 "Check if 1-argument operation succeeds."
56 (mapc op *complexes*))
57 (check-op-2 (op)
58 "Check if pair-wise operation succeeds."
59 (mapc (lambda (elt)
60 (mapc (lambda (set)
61 (dolist (v set)
62 (funcall op elt v)
63 (funcall op v elt)))
64 (list *integers* *floats* *ratios* *complexes*)))
65 *complexes*)))
66 (finishes
67 (mapc (lambda (op) (finishes (check-op-1 op)))
68 (list #'conjugate #'realpart #'imagpart ; complexes
69 #'atan #'sin #'sinh #'tan #'tanh #'cos #'cosh ; trigonometry
70 #'- #'+ #'* #'/ #'1- #'1+ ; basic artih
71 #'abs #'exp #'log #'sqrt ; exponentials
72 #'= #'/= #'zerop ; predicates
73 ))
74 "complex valid operations crashed")
75 ;; operations without meaning for complex
76 (finishes
77 (mapc (lambda (op) (should-signal type-error (check-op-1 op)))
78 (list #'ceiling #'floor #'minusp #'plusp #'round #'truncate
79 #'< #'<= #'> #'>= #'min #'max)))
80 ;; trigonometry (atan2 corner cases)
81 (check-op-1 (lambda (elt) (should-signal error (atan elt 42))))
82 (check-op-1 (lambda (elt) (should-signal error (atan elt #c(1 1)))))
83 (check-op-1 (lambda (elt) (should-signal error (atan elt #c(1.0 1.0)))))
84 (finishes (check-op-2 #'+))
85 (finishes (check-op-2 #'-))
86 (finishes (check-op-2 #'*))
87 (finishes (check-op-2 (lambda (elt1 elt2)
88 (unless (zerop elt2)
89 (/ elt1 elt2)))))
90 ;; See https://gitlab.com/embeddable-common-lisp/ecl/issues/485
91 (finishes (check-op-2 (lambda (elt1 elt2)
92 (when (and (< (realpart elt2) 4096)
93 (> (realpart elt2) -4095))
94 (unless (and (zerop elt1)
95 (not (zerop elt2))
96 (not (plusp (realpart elt2))))
97 (expt elt1 elt2))))))
98 ;; comparison
99 (mapc (lambda (op)
100 (check-op-2 (lambda (elt1 elt2)
101 (should-signal type-error (funcall op elt1 elt2)))))
102 (list #'max #'min #'< #'<= #'> #'>= #'>))
103 (finishes (check-op-2 #'=) "=<2> complex operation crashed")
104 (finishes (check-op-2 #'/=) "/=<2> complex operation crashed"))))
105
106 (test complex.0004.contagion
107 (mapc (lambda (num type)
108 (is (typep num type) "~s is not typep to ~s (type-of is ~s)" num type (type-of num)))
109 (list #c(1/4 0) #c(1/4 1) #c(1 0.0) #c(1.0 0)
110 #c(0.0s0 0.0s0) #c(0.0d0 1) #c(0.0 0.0d0) #c(0.0l0 0.0d0)
111 (+ 1.0 #c(1 2)) (+ 1.0 #c(1 2.0d0)) (+ 1.0 #c(1 0)))
112 (list 'rational '(complex rational) '(complex single-float) '(complex single-float)
113 '(complex single-float) '(complex double-float) '(complex double-float)
114 '(complex long-float) '(complex single-float) '(complex double-float) 'single-float)) )
115
116 (test complex.0005.conjugate ; also tests imagpart / realpart
117 (mapcar (lambda (c1 c2)
118 (and (= (realpart c1) (realpart c2))
119 (= (imagpart c1) (- (imagpart c2)))))
120 *complexes*
121 (mapcar #'conjugate *complexes*)))
122
123 (test complex.0006.predicates
124 (mapcar (lambda (c)
125 (is (numberp c))
126 (is (complexp c))
127 (is (not (realp c))))
128 *complexes*))
129
130 ;;; These tests are made explicitly for complex-float numbers.
131 #+complex-float
132 (let* ((ss1 0.0s0) (ss2 -0.0s0)
133 (sf1 1.1) (sf2 -42.3)
134 (sd1 0.0d0) (sd2 +0.2d0)
135 (sl1 -2.4l0) (sl2 -3.2l0)
136 (cf0 (si:complex-float ss1 ss2))
137 (cf1 (si:complex-float sf1 sf2))
138 (cf2 (si:complex-float sd1 sd2))
139 (cf3 (si:complex-float sl1 sl2))
140 (all-cfloats (list cf0 cf1 cf2 cf3))
141 (rf0 #c(1 1)))
142
143 (test cfloat.0000.type
144 (is (every (lambda (elt) (typep elt 'number)) all-cfloats))
145 (is (every (lambda (elt) (typep elt 'complex)) all-cfloats))
146 (is (not (some (lambda (elt) (typep elt 'float)) all-cfloats)))
147 (is (every (lambda (elt) (complexp elt)) all-cfloats))
148 (is (not (some (lambda (elt) (realp elt)) all-cfloats)))
149 (is (not (some (lambda (elt) (floatp elt)) all-cfloats)))
150 (is (not (some (lambda (elt) (rationalp elt)) all-cfloats)))
151 (is (subtypep 'si:complex-single-float 'si:complex-float))
152 (is (subtypep 'si:complex-double-float 'si:complex-float))
153 (is (subtypep 'si:complex-long-float 'si:complex-float))
154 (is (subtypep 'si:complex-float 'complex))
155 (is (subtypep 'si:complex-float 'number))
156 (is (not (subtypep 'si:complex-float 'float)))
157 (is (not (subtypep 'si:complex-float 'rational)))
158 (is (not (typep cf1 'si:complex-double-float)))
159 (is (not (typep cf1 'si:complex-long-float)))
160 (is (not (typep cf2 'si:complex-single-float)))
161 (is (not (typep cf2 'si:complex-long-float)))
162 (is (not (typep cf3 'si:complex-single-float)))
163 (is (not (typep cf3 'si:complex-double-float))))
164
165 (test cfloat.0001.predicate
166 (is (every #'si:complex-float-p all-cfloats))
167 (is (not (some #'si:complex-float-p (list nil #\c 3 sf1 rf0)))))
168
169 ;; all numerical operators which doesn't apply to complex
170 (test cfloat.0002.invalid-type
171 ;; unary operators
172 (let ((operations (list #'plusp #'minusp #'max #'min #'evenp #'oddp
173 #'> #'>= #'< #'<= #'lcm #'isqrt #'cis #'complex
174 #'numerator #'denominator #'rationalize #'float)))
175 (mapc (lambda (op)
176 (mapc (lambda (num)
177 (signals type-error (funcall op num) "~a" op))
178 all-cfloats))
179 operations))
180 ;; nary operators (implement me!)
181 )
182
183 (test cfloat.0003.zerop
184 (mapc (lambda (num)
185 (if (and (zerop (realpart num))
186 (zerop (imagpart num)))
187 (is (zerop num))
188 (is (not (zerop num)))))
189 (list* (si:complex-float 0.0s0 0.0s0)
190 (si:complex-float 0.0d0 0.0d0)
191 (si:complex-float 0.0l0 0.0l0)
192 all-cfloats)))
193
194 (test cfloat.0004.realpart/imagpart
195 (mapc (lambda (num parts)
196 (is (= (car parts) (realpart num)))
197 (is (= (cdr parts) (imagpart num))))
198 all-cfloats
199 (list (cons ss1 ss2)
200 (cons sf1 sf2)
201 (cons sd1 sd2)
202 ;; TDD is great! I did put "long" in instead of "long double".
203 (cons sl1 sl2))))
204
205 (test cfloat.0005.conjugate
206 (mapc (lambda (num parts)
207 (is (= (car parts) (realpart num)))
208 (is (= (- (cdr parts)) (imagpart num))))
209 (mapcar #'conjugate all-cfloats)
210 (list (cons ss1 ss2)
211 (cons sf1 sf2)
212 (cons sd1 sd2)
213 ;; TDD is great! I did put "long" in instead of "long double".
214 (cons sl1 sl2))))
215
216 (test cfloat.0006.abs
217 (is (every (lambda (num) (typep (abs num) 'real)) all-cfloats))
218 (mapc (lambda (elt)
219 (let ((imag (coerce 0 (type-of elt))))
220 (is (= (abs elt) (abs (si:complex-float elt imag))))))
221 (list -4.1234s0 +1.000103 +0.1243d0 -2.1234l0)))
222
223 (test cfloat.0007.exponentiation
224 ;; exp, sqrt, log and log1p
225 (finishes (mapc (lambda (cf) (exp cf)) all-cfloats) "exp1")
226 (finishes (mapc (lambda (cf) (sqrt cf)) all-cfloats) "sqrt")
227 (finishes (mapc (lambda (cf)
228 #+floating-point-exceptions
229 (if (zerop cf)
230 (signals division-by-zero (log cf))
231 (log cf))
232 #-floating-point-exceptions
233 (log cf))
234 all-cfloats) "log1")
235 (finishes (mapc (lambda (cf) (si:log1p cf)) all-cfloats) "log1p")
236 ;; log operations on floats should give corresponding cfloat type
237 (mapc (lambda (num)
238 ;; we assume all only negative or complex are passed here
239 (let ((result-type (typecase num
240 (si:complex-single-float 'si:complex-single-float)
241 (si:complex-double-float 'si:complex-double-float)
242 (si:complex-long-float 'si:complex-long-float)
243 (short-float 'si:complex-single-float)
244 (single-float 'si:complex-single-float)
245 (double-float 'si:complex-double-float)
246 (long-float 'si:complex-long-float)
247 (otherwise 'si:complex-single-float))))
248 (is (typep (log num) result-type) "log(~s) is not of type ~s" num result-type)
249 (is (typep (si:log1p num) result-type) "log(~s) is not of type ~s" num result-type)))
250 (list -1.12s0 -1.12 -1.12d0 -1.12l0 -4 #c(1/3 3)
251 (1- most-negative-fixnum) cf1 cf2 cf3)))
252
253 (test cfloat.0008.trig/hyper.1 ; only 1-argument variants
254 ;; trigonometric
255 (finishes (mapc (lambda (cf) (sin cf)) all-cfloats) "sin")
256 (finishes (mapc (lambda (cf) (cos cf)) all-cfloats) "cos")
257 (finishes (mapc (lambda (cf) (tan cf)) all-cfloats) "tan")
258 ;; trigonometric arcus
259 (finishes (mapc (lambda (cf) (asin cf)) all-cfloats) "asin")
260 (finishes (mapc (lambda (cf) (acos cf)) all-cfloats) "acos")
261 (finishes (mapc (lambda (cf) (atan cf)) all-cfloats) "atan")
262 ;; hyperbolic
263 (finishes (mapc (lambda (cf) (sinh cf)) all-cfloats) "sinh")
264 (finishes (mapc (lambda (cf) (cosh cf)) all-cfloats) "cosh")
265 (finishes (mapc (lambda (cf) (tanh cf)) all-cfloats) "tanh")
266 ;; hyperbolic arcus
267 (finishes (mapc (lambda (cf) (asinh cf)) all-cfloats) "asinh")
268 (finishes (mapc (lambda (cf) (acosh cf)) all-cfloats) "acosh")
269 (finishes (mapc (lambda (cf) (atanh cf)) all-cfloats) "atonh"))
270
271 (test cfloat.0008.make-complex
272 (signals type-error (complex #c(1.0 1.0) 14))
273 (signals type-error (complex 14 #c(1.0 1.0)))
274 (signals type-error (complex 14 "foobar"))
275 (is (typep (complex 1 0) 'fixnum))
276 (is (typep (complex 1 1) '(complex rational)))
277 (is (typep (complex 0 1) '(complex rational)))
278 (is (typep (complex (1+ most-positive-fixnum) 1/4) '(complex rational)))
279 (is (typep (complex 1/4 (1+ most-positive-fixnum)) '(complex rational)))
280 (is (typep (complex 1.4 (1+ most-positive-fixnum)) '(complex single-float)))
281 (is (typep (complex (1+ most-positive-fixnum) 1.4) '(complex single-float)))
282 (is (typep (complex 1.4d0 1) '(complex double-float)))
283 (is (typep (complex 1.4d0 1.0l0) '(complex long-float)))
284 (is (typep (complex 1.4 1.0d0) '(complex double-float))))
285
286 (test cfloat.0009.c-sharp-reader
287 ;; reader conses complex-floats (instead of a generic type)
288 (is (typep #c(1.0s0 2.3) 'si:complex-single-float))
289 (is (typep #c(1.0d0 2.3s0) 'si:complex-double-float))
290 (is (typep #c(1.0d0 2.3l0) 'si:complex-long-float))
291 (is (typep #c(1 2.3l0) 'si:complex-long-float))
292 (is (typep #c(1.0 0) 'si:complex-float))
293 (is (not (typep #c(1 1) 'si:complex-float)))
294 (is (not (typep #c(0 1) 'si:complex-float)))
295 (is (not (typep #c(1 0) 'si:complex-float))))
296
297 ;; Date: 2019-12-30
298 ;; URL: https://gitlab.com/embeddable-common-lisp/ecl/issues/547
299 ;; From: Karsten Poeck
300 ;; Fixed: 2019-12-30 (Daniel Kochmański)
301 ;; Description:
302 ;;
303 ;; (EXPT #C(1.0 0.0) 2) causes unrecoverable error.
304 ;;
305 (test csfloat.0010.issue-547
306 (finishes (expt #c(1.0 0.0) 2))))