Added test-round.
[bpt/guile.git] / test-suite / tests / numbers.test
CommitLineData
de142bea 1;;;; numbers.test --- tests guile's numbers -*- scheme -*-
593a4c2f 2;;;; Copyright (C) 2000, 2001, 2003, 2004 Free Software Foundation, Inc.
de142bea 3;;;;
73be1d9e
MV
4;;;; This library is free software; you can redistribute it and/or
5;;;; modify it under the terms of the GNU Lesser General Public
6;;;; License as published by the Free Software Foundation; either
7;;;; version 2.1 of the License, or (at your option) any later version.
8;;;;
9;;;; This library is distributed in the hope that it will be useful,
de142bea 10;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
11;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12;;;; Lesser General Public License for more details.
13;;;;
14;;;; You should have received a copy of the GNU Lesser General Public
15;;;; License along with this library; if not, write to the Free Software
16;;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
de142bea 17
a1fb3b1c
KR
18(define-module (test-suite test-numbers)
19 #:use-module (test-suite lib)
20 #:use-module (ice-9 documentation))
de142bea 21
de142bea
DH
22;;;
23;;; miscellaneous
24;;;
25
1b3a7932
DH
26(define exception:numerical-overflow
27 (cons 'numerical-overflow "^Numerical overflow"))
28
cb18f2a8 29(define (documented? object)
5c96bc39 30 (not (not (object-documentation object))))
de142bea 31
8b7838b5
RB
32(define fixnum-bit
33 (inexact->exact (+ (/ (log (+ most-positive-fixnum 1)) (log 2)) 1)))
34
21e39e8f
DH
35(define fixnum-min most-negative-fixnum)
36(define fixnum-max most-positive-fixnum)
de142bea 37
a1fb3b1c
KR
38;; Divine the number of bits in the mantissa of a flonum.
39;; We look for when 2.0^i+1.0 gets rounded, ie. the difference between that
40;; value and 2.0^k is not 1.0.
41;; Of course this assumes flonums have a fixed precision mantissa, but
42;; that's the case now and probably into the forseeable future.
43;; On an IEEE system, which means pretty much everywhere, the value here is
44;; the usual 53.
45;;
46(define dbl-mant-dig
47 (let more ((i 1)
48 (d 2.0))
49 (if (> i 1024)
50 (error "Oops, cannot determine number of bits in mantissa of inexact"))
51 (let* ((sum (+ 1.0 d))
52 (diff (- sum d)))
53 (if (= diff 1.0)
54 (more (1+ i) (* 2.0 d))
55 i))))
56
57;; like ash, but working on a flonum
58(define (ash-flo x n)
59 (while (> n 0)
60 (set! x (* 2.0 x))
61 (set! n (1- n)))
62 (while (< n 0)
63 (set! x (* 0.5 x))
64 (set! n (1+ n)))
65 x)
66
67
de142bea
DH
68;;;
69;;; exact?
70;;;
71
72(with-test-prefix "exact?"
73
de142bea 74 (pass-if "documented?"
cb18f2a8 75 (documented? exact?))
de142bea 76
21e39e8f 77 (with-test-prefix "integers"
de142bea 78
21e39e8f
DH
79 (pass-if "0"
80 (exact? 0))
de142bea 81
21e39e8f
DH
82 (pass-if "fixnum-max"
83 (exact? fixnum-max))
de142bea 84
21e39e8f
DH
85 (pass-if "fixnum-max + 1"
86 (exact? (+ fixnum-max 1)))
de142bea 87
21e39e8f
DH
88 (pass-if "fixnum-min"
89 (exact? fixnum-min))
de142bea 90
21e39e8f
DH
91 (pass-if "fixnum-min - 1"
92 (exact? (- fixnum-min 1))))
93
94 (with-test-prefix "reals"
95
96 ;; (FIXME: need better examples.)
97
98 (pass-if "sqrt (fixnum-max^2 - 1)"
99 (eq? #f (exact? (sqrt (- (expt fixnum-max 2) 1)))))
100
101 (pass-if "sqrt ((fixnum-max+1)^2 - 1)"
102 (eq? #f (exact? (sqrt (- (expt (+ fixnum-max 1) 2) 1)))))))
de142bea
DH
103
104;;;
105;;; odd?
106;;;
107
7c24e528
RB
108(with-test-prefix "odd?"
109 (pass-if (documented? odd?))
110 (pass-if (odd? 1))
111 (pass-if (odd? -1))
4d332f19
DH
112 (pass-if (not (odd? 0)))
113 (pass-if (not (odd? 2)))
114 (pass-if (not (odd? -2)))
7c24e528 115 (pass-if (odd? (+ (* 2 fixnum-max) 1)))
4d332f19 116 (pass-if (not (odd? (* 2 fixnum-max))))
7c24e528 117 (pass-if (odd? (- (* 2 fixnum-min) 1)))
4d332f19 118 (pass-if (not (odd? (* 2 fixnum-min)))))
de142bea
DH
119
120;;;
121;;; even?
122;;;
123
7c24e528
RB
124(with-test-prefix "even?"
125 (pass-if (documented? even?))
126 (pass-if (even? 2))
127 (pass-if (even? -2))
128 (pass-if (even? 0))
4d332f19
DH
129 (pass-if (not (even? 1)))
130 (pass-if (not (even? -1)))
131 (pass-if (not (even? (+ (* 2 fixnum-max) 1))))
7c24e528 132 (pass-if (even? (* 2 fixnum-max)))
4d332f19 133 (pass-if (not (even? (- (* 2 fixnum-min) 1))))
7c24e528 134 (pass-if (even? (* 2 fixnum-min))))
de142bea
DH
135
136;;;
7c24e528
RB
137;;; inf? and inf
138;;;
139
140(with-test-prefix "inf?"
141 (pass-if (documented? inf?))
142 (pass-if (inf? (inf)))
143 ;; FIXME: what are the expected behaviors?
144 ;; (pass-if (inf? (/ 1.0 0.0))
145 ;; (pass-if (inf? (/ 1 0.0))
4d332f19
DH
146 (pass-if (not (inf? 0)))
147 (pass-if (not (inf? 42.0)))
148 (pass-if (not (inf? (+ fixnum-max 1))))
149 (pass-if (not (inf? (- fixnum-min 1)))))
7c24e528
RB
150
151;;;
152;;; nan? and nan
de142bea
DH
153;;;
154
7c24e528
RB
155(with-test-prefix "nan?"
156 (pass-if (documented? nan?))
157 (pass-if (nan? (nan)))
158 ;; FIXME: other ways we should be able to generate NaN?
4d332f19
DH
159 (pass-if (not (nan? 0)))
160 (pass-if (not (nan? 42.0)))
161 (pass-if (not (nan? (+ fixnum-max 1))))
162 (pass-if (not (nan? (- fixnum-min 1)))))
de142bea 163
7c24e528
RB
164;;;
165;;; abs
166;;;
167
168(with-test-prefix "abs"
169 (pass-if (documented? abs))
170 (pass-if (zero? (abs 0)))
171 (pass-if (= 1 (abs 1)))
172 (pass-if (= 1 (abs -1)))
173 (pass-if (= (+ fixnum-max 1) (abs (+ fixnum-max 1))))
174 (pass-if (= (+ (- fixnum-min) 1) (abs (- fixnum-min 1))))
76903a31
KR
175 (pass-if (= 0.0 (abs 0.0)))
176 (pass-if (= 1.0 (abs 1.0)))
177 (pass-if (= 1.0 (abs -1.0)))
178 (pass-if (nan? (abs +nan.0)))
179 (pass-if (= +inf.0 (abs +inf.0)))
180 (pass-if (= +inf.0 (abs -inf.0))))
181
de142bea
DH
182;;;
183;;; quotient
184;;;
185
186(with-test-prefix "quotient"
187
de142bea 188 (expect-fail "documented?"
cb18f2a8 189 (documented? quotient))
de142bea 190
de142bea
DH
191 (with-test-prefix "0 / n"
192
193 (pass-if "n = 1"
194 (eqv? 0 (quotient 0 1)))
195
196 (pass-if "n = -1"
197 (eqv? 0 (quotient 0 -1)))
198
21e39e8f
DH
199 (pass-if "n = 2"
200 (eqv? 0 (quotient 0 2)))
201
202 (pass-if "n = fixnum-max"
203 (eqv? 0 (quotient 0 fixnum-max)))
204
205 (pass-if "n = fixnum-max + 1"
206 (eqv? 0 (quotient 0 (+ fixnum-max 1))))
207
208 (pass-if "n = fixnum-min"
209 (eqv? 0 (quotient 0 fixnum-min)))
210
211 (pass-if "n = fixnum-min - 1"
212 (eqv? 0 (quotient 0 (- fixnum-min 1)))))
de142bea 213
21e39e8f 214 (with-test-prefix "1 / n"
de142bea
DH
215
216 (pass-if "n = 1"
217 (eqv? 1 (quotient 1 1)))
218
219 (pass-if "n = -1"
21e39e8f
DH
220 (eqv? -1 (quotient 1 -1)))
221
222 (pass-if "n = 2"
223 (eqv? 0 (quotient 1 2)))
de142bea 224
21e39e8f
DH
225 (pass-if "n = fixnum-max"
226 (eqv? 0 (quotient 1 fixnum-max)))
de142bea 227
21e39e8f
DH
228 (pass-if "n = fixnum-max + 1"
229 (eqv? 0 (quotient 1 (+ fixnum-max 1))))
de142bea 230
21e39e8f
DH
231 (pass-if "n = fixnum-min"
232 (eqv? 0 (quotient 1 fixnum-min)))
233
234 (pass-if "n = fixnum-min - 1"
235 (eqv? 0 (quotient 1 (- fixnum-min 1)))))
236
237 (with-test-prefix "-1 / n"
de142bea
DH
238
239 (pass-if "n = 1"
21e39e8f 240 (eqv? -1 (quotient -1 1)))
de142bea
DH
241
242 (pass-if "n = -1"
243 (eqv? 1 (quotient -1 -1)))
244
21e39e8f
DH
245 (pass-if "n = 2"
246 (eqv? 0 (quotient -1 2)))
247
248 (pass-if "n = fixnum-max"
249 (eqv? 0 (quotient -1 fixnum-max)))
250
251 (pass-if "n = fixnum-max + 1"
252 (eqv? 0 (quotient -1 (+ fixnum-max 1))))
253
254 (pass-if "n = fixnum-min"
255 (eqv? 0 (quotient -1 fixnum-min)))
256
257 (pass-if "n = fixnum-min - 1"
258 (eqv? 0 (quotient -1 (- fixnum-min 1)))))
259
260 (with-test-prefix "fixnum-max / n"
261
262 (pass-if "n = 1"
263 (eqv? fixnum-max (quotient fixnum-max 1)))
264
265 (pass-if "n = -1"
266 (eqv? (- fixnum-max) (quotient fixnum-max -1)))
267
268 (pass-if "n = 2"
269 (eqv? fixnum-max (+ (* (quotient fixnum-max 2) 2) 1)))
270
271 (pass-if "n = fixnum-max"
272 (eqv? 1 (quotient fixnum-max fixnum-max)))
273
274 (pass-if "n = fixnum-max + 1"
275 (eqv? 0 (quotient fixnum-max (+ fixnum-max 1))))
276
277 (pass-if "n = fixnum-min"
278 (eqv? 0 (quotient fixnum-max fixnum-min)))
279
280 (pass-if "n = fixnum-min - 1"
281 (eqv? 0 (quotient fixnum-max (- fixnum-min 1)))))
282
283 (with-test-prefix "(fixnum-max + 1) / n"
284
285 (pass-if "n = 1"
286 (eqv? (+ fixnum-max 1) (quotient (+ fixnum-max 1) 1)))
287
288 (pass-if "n = -1"
289 (eqv? (- (+ fixnum-max 1)) (quotient (+ fixnum-max 1) -1)))
290
291 (pass-if "n = 2"
292 (eqv? (+ fixnum-max 1) (* (quotient (+ fixnum-max 1) 2) 2)))
293
294 (pass-if "n = fixnum-max"
295 (eqv? 1 (quotient (+ fixnum-max 1) fixnum-max)))
296
297 (pass-if "n = fixnum-max + 1"
298 (eqv? 1 (quotient (+ fixnum-max 1) (+ fixnum-max 1))))
299
300 (pass-if "n = fixnum-min"
301 (eqv? -1 (quotient (+ fixnum-max 1) fixnum-min)))
302
303 (pass-if "n = fixnum-min - 1"
304 (eqv? 0 (quotient (+ fixnum-max 1) (- fixnum-min 1)))))
305
306 (with-test-prefix "fixnum-min / n"
307
308 (pass-if "n = 1"
309 (eqv? fixnum-min (quotient fixnum-min 1)))
310
311 (pass-if "n = -1"
312 (eqv? (- fixnum-min) (quotient fixnum-min -1)))
313
314 (pass-if "n = 2"
315 (eqv? fixnum-min (* (quotient fixnum-min 2) 2)))
316
317 (pass-if "n = fixnum-max"
318 (eqv? -1 (quotient fixnum-min fixnum-max)))
319
320 (pass-if "n = fixnum-max + 1"
321 (eqv? -1 (quotient fixnum-min (+ fixnum-max 1))))
322
323 (pass-if "n = fixnum-min"
324 (eqv? 1 (quotient fixnum-min fixnum-min)))
325
326 (pass-if "n = fixnum-min - 1"
ad22fe7c
KR
327 (eqv? 0 (quotient fixnum-min (- fixnum-min 1))))
328
329 (pass-if "n = - fixnum-min - 1"
330 (eqv? -1 (quotient fixnum-min (1- (- fixnum-min)))))
331
332 ;; special case, normally inum/big is zero
333 (pass-if "n = - fixnum-min"
334 (eqv? -1 (quotient fixnum-min (- fixnum-min))))
335
336 (pass-if "n = - fixnum-min + 1"
337 (eqv? 0 (quotient fixnum-min (1+ (- fixnum-min))))))
21e39e8f
DH
338
339 (with-test-prefix "(fixnum-min - 1) / n"
340
341 (pass-if "n = 1"
342 (eqv? (- fixnum-min 1) (quotient (- fixnum-min 1) 1)))
343
344 (pass-if "n = -1"
345 (eqv? (- (- fixnum-min 1)) (quotient (- fixnum-min 1) -1)))
346
347 (pass-if "n = 2"
348 (eqv? fixnum-min (* (quotient (- fixnum-min 1) 2) 2)))
349
350 (pass-if "n = fixnum-max"
351 (eqv? -1 (quotient (- fixnum-min 1) fixnum-max)))
352
353 (pass-if "n = fixnum-max + 1"
354 (eqv? -1 (quotient (- fixnum-min 1) (+ fixnum-max 1))))
355
356 (pass-if "n = fixnum-min"
357 (eqv? 1 (quotient (- fixnum-min 1) fixnum-min)))
358
359 (pass-if "n = fixnum-min - 1"
360 (eqv? 1 (quotient (- fixnum-min 1) (- fixnum-min 1)))))
de142bea
DH
361
362 ;; Positive dividend and divisor
363
364 (pass-if "35 / 7"
365 (eqv? 5 (quotient 35 7)))
366
367 ;; Negative dividend, positive divisor
368
369 (pass-if "-35 / 7"
370 (eqv? -5 (quotient -35 7)))
371
372 ;; Positive dividend, negative divisor
373
374 (pass-if "35 / -7"
375 (eqv? -5 (quotient 35 -7)))
376
377 ;; Negative dividend and divisor
378
379 (pass-if "-35 / -7"
380 (eqv? 5 (quotient -35 -7)))
381
382 ;; Are numerical overflows detected correctly?
383
80074d77
DH
384 (with-test-prefix "division by zero"
385
386 (pass-if-exception "(quotient 1 0)"
387 exception:numerical-overflow
388 (quotient 1 0))
389
390 (pass-if-exception "(quotient bignum 0)"
391 exception:numerical-overflow
392 (quotient (+ fixnum-max 1) 0)))
393
de142bea
DH
394 ;; Are wrong type arguments detected correctly?
395
396 )
397
398;;;
399;;; remainder
400;;;
401
402(with-test-prefix "remainder"
403
de142bea 404 (expect-fail "documented?"
cb18f2a8 405 (documented? remainder))
de142bea 406
de142bea
DH
407 (with-test-prefix "0 / n"
408
409 (pass-if "n = 1"
410 (eqv? 0 (remainder 0 1)))
411
412 (pass-if "n = -1"
413 (eqv? 0 (remainder 0 -1)))
414
21e39e8f
DH
415 (pass-if "n = fixnum-max"
416 (eqv? 0 (remainder 0 fixnum-max)))
417
418 (pass-if "n = fixnum-max + 1"
419 (eqv? 0 (remainder 0 (+ fixnum-max 1))))
420
421 (pass-if "n = fixnum-min"
422 (eqv? 0 (remainder 0 fixnum-min)))
de142bea 423
21e39e8f
DH
424 (pass-if "n = fixnum-min - 1"
425 (eqv? 0 (remainder 0 (- fixnum-min 1)))))
de142bea 426
21e39e8f 427 (with-test-prefix "1 / n"
de142bea
DH
428
429 (pass-if "n = 1"
430 (eqv? 0 (remainder 1 1)))
431
432 (pass-if "n = -1"
21e39e8f
DH
433 (eqv? 0 (remainder 1 -1)))
434
435 (pass-if "n = fixnum-max"
436 (eqv? 1 (remainder 1 fixnum-max)))
de142bea 437
21e39e8f
DH
438 (pass-if "n = fixnum-max + 1"
439 (eqv? 1 (remainder 1 (+ fixnum-max 1))))
de142bea 440
21e39e8f
DH
441 (pass-if "n = fixnum-min"
442 (eqv? 1 (remainder 1 fixnum-min)))
de142bea 443
21e39e8f
DH
444 (pass-if "n = fixnum-min - 1"
445 (eqv? 1 (remainder 1 (- fixnum-min 1)))))
446
447 (with-test-prefix "-1 / n"
de142bea
DH
448
449 (pass-if "n = 1"
21e39e8f 450 (eqv? 0 (remainder -1 1)))
de142bea
DH
451
452 (pass-if "n = -1"
453 (eqv? 0 (remainder -1 -1)))
454
21e39e8f
DH
455 (pass-if "n = fixnum-max"
456 (eqv? -1 (remainder -1 fixnum-max)))
457
458 (pass-if "n = fixnum-max + 1"
459 (eqv? -1 (remainder -1 (+ fixnum-max 1))))
460
461 (pass-if "n = fixnum-min"
462 (eqv? -1 (remainder -1 fixnum-min)))
463
464 (pass-if "n = fixnum-min - 1"
465 (eqv? -1 (remainder -1 (- fixnum-min 1)))))
466
467 (with-test-prefix "fixnum-max / n"
468
469 (pass-if "n = 1"
470 (eqv? 0 (remainder fixnum-max 1)))
471
472 (pass-if "n = -1"
473 (eqv? 0 (remainder fixnum-max -1)))
474
475 (pass-if "n = fixnum-max"
476 (eqv? 0 (remainder fixnum-max fixnum-max)))
477
478 (pass-if "n = fixnum-max + 1"
479 (eqv? fixnum-max (remainder fixnum-max (+ fixnum-max 1))))
480
481 (pass-if "n = fixnum-min"
482 (eqv? fixnum-max (remainder fixnum-max fixnum-min)))
483
484 (pass-if "n = fixnum-min - 1"
485 (eqv? fixnum-max (remainder fixnum-max (- fixnum-min 1)))))
486
487 (with-test-prefix "(fixnum-max + 1) / n"
488
489 (pass-if "n = 1"
490 (eqv? 0 (remainder (+ fixnum-max 1) 1)))
491
492 (pass-if "n = -1"
493 (eqv? 0 (remainder (+ fixnum-max 1) -1)))
494
495 (pass-if "n = fixnum-max"
496 (eqv? 1 (remainder (+ fixnum-max 1) fixnum-max)))
497
498 (pass-if "n = fixnum-max + 1"
499 (eqv? 0 (remainder (+ fixnum-max 1) (+ fixnum-max 1))))
500
501 (pass-if "n = fixnum-min"
502 (eqv? 0 (remainder (+ fixnum-max 1) fixnum-min)))
503
504 (pass-if "n = fixnum-min - 1"
505 (eqv? (+ fixnum-max 1) (remainder (+ fixnum-max 1) (- fixnum-min 1)))))
506
507 (with-test-prefix "fixnum-min / n"
508
509 (pass-if "n = 1"
510 (eqv? 0 (remainder fixnum-min 1)))
511
512 (pass-if "n = -1"
513 (eqv? 0 (remainder fixnum-min -1)))
514
515 (pass-if "n = fixnum-max"
516 (eqv? -1 (remainder fixnum-min fixnum-max)))
517
518 (pass-if "n = fixnum-max + 1"
519 (eqv? 0 (remainder fixnum-min (+ fixnum-max 1))))
520
521 (pass-if "n = fixnum-min"
522 (eqv? 0 (remainder fixnum-min fixnum-min)))
523
524 (pass-if "n = fixnum-min - 1"
ad22fe7c
KR
525 (eqv? fixnum-min (remainder fixnum-min (- fixnum-min 1))))
526
527 (pass-if "n = - fixnum-min - 1"
528 (eqv? -1 (remainder fixnum-min (1- (- fixnum-min)))))
529
530 ;; special case, normally inum%big is the inum
531 (pass-if "n = - fixnum-min"
532 (eqv? 0 (remainder fixnum-min (- fixnum-min))))
533
534 (pass-if "n = - fixnum-min + 1"
535 (eqv? fixnum-min (remainder fixnum-min (1+ (- fixnum-min))))))
21e39e8f
DH
536
537 (with-test-prefix "(fixnum-min - 1) / n"
538
539 (pass-if "n = 1"
540 (eqv? 0 (remainder (- fixnum-min 1) 1)))
541
542 (pass-if "n = -1"
543 (eqv? 0 (remainder (- fixnum-min 1) -1)))
544
545 (pass-if "n = fixnum-max"
546 (eqv? -2 (remainder (- fixnum-min 1) fixnum-max)))
547
548 (pass-if "n = fixnum-max + 1"
549 (eqv? -1 (remainder (- fixnum-min 1) (+ fixnum-max 1))))
550
551 (pass-if "n = fixnum-min"
552 (eqv? -1 (remainder (- fixnum-min 1) fixnum-min)))
553
554 (pass-if "n = fixnum-min - 1"
555 (eqv? 0 (remainder (- fixnum-min 1) (- fixnum-min 1)))))
de142bea
DH
556
557 ;; Positive dividend and divisor
558
559 (pass-if "35 / 7"
560 (eqv? 0 (remainder 35 7)))
561
562 ;; Negative dividend, positive divisor
563
564 (pass-if "-35 / 7"
565 (eqv? 0 (remainder -35 7)))
566
567 ;; Positive dividend, negative divisor
568
569 (pass-if "35 / -7"
570 (eqv? 0 (remainder 35 -7)))
571
572 ;; Negative dividend and divisor
573
574 (pass-if "-35 / -7"
575 (eqv? 0 (remainder -35 -7)))
576
577 ;; Are numerical overflows detected correctly?
578
80074d77
DH
579 (with-test-prefix "division by zero"
580
581 (pass-if-exception "(remainder 1 0)"
582 exception:numerical-overflow
583 (remainder 1 0))
584
585 (pass-if-exception "(remainder bignum 0)"
586 exception:numerical-overflow
587 (remainder (+ fixnum-max 1) 0)))
588
de142bea
DH
589 ;; Are wrong type arguments detected correctly?
590
591 )
592
593;;;
594;;; modulo
595;;;
596
597(with-test-prefix "modulo"
598
de142bea 599 (expect-fail "documented?"
cb18f2a8 600 (documented? modulo))
de142bea 601
de142bea
DH
602 (with-test-prefix "0 % n"
603
604 (pass-if "n = 1"
605 (eqv? 0 (modulo 0 1)))
606
607 (pass-if "n = -1"
608 (eqv? 0 (modulo 0 -1)))
609
21e39e8f
DH
610 (pass-if "n = fixnum-max"
611 (eqv? 0 (modulo 0 fixnum-max)))
612
613 (pass-if "n = fixnum-max + 1"
614 (eqv? 0 (modulo 0 (+ fixnum-max 1))))
de142bea 615
21e39e8f
DH
616 (pass-if "n = fixnum-min"
617 (eqv? 0 (modulo 0 fixnum-min)))
de142bea 618
21e39e8f
DH
619 (pass-if "n = fixnum-min - 1"
620 (eqv? 0 (modulo 0 (- fixnum-min 1)))))
621
622 (with-test-prefix "1 % n"
de142bea
DH
623
624 (pass-if "n = 1"
625 (eqv? 0 (modulo 1 1)))
626
627 (pass-if "n = -1"
21e39e8f 628 (eqv? 0 (modulo 1 -1)))
de142bea 629
21e39e8f
DH
630 (pass-if "n = fixnum-max"
631 (eqv? 1 (modulo 1 fixnum-max)))
de142bea 632
21e39e8f
DH
633 (pass-if "n = fixnum-max + 1"
634 (eqv? 1 (modulo 1 (+ fixnum-max 1))))
de142bea 635
21e39e8f
DH
636 (pass-if "n = fixnum-min"
637 (eqv? (+ fixnum-min 1) (modulo 1 fixnum-min)))
638
639 (pass-if "n = fixnum-min - 1"
640 (eqv? fixnum-min (modulo 1 (- fixnum-min 1)))))
641
642 (with-test-prefix "-1 % n"
de142bea
DH
643
644 (pass-if "n = 1"
21e39e8f 645 (eqv? 0 (modulo -1 1)))
de142bea
DH
646
647 (pass-if "n = -1"
648 (eqv? 0 (modulo -1 -1)))
649
21e39e8f
DH
650 (pass-if "n = fixnum-max"
651 (eqv? (- fixnum-max 1) (modulo -1 fixnum-max)))
652
653 (pass-if "n = fixnum-max + 1"
654 (eqv? fixnum-max (modulo -1 (+ fixnum-max 1))))
655
656 (pass-if "n = fixnum-min"
657 (eqv? -1 (modulo -1 fixnum-min)))
658
659 (pass-if "n = fixnum-min - 1"
660 (eqv? -1 (modulo -1 (- fixnum-min 1)))))
661
662 (with-test-prefix "fixnum-max % n"
663
664 (pass-if "n = 1"
665 (eqv? 0 (modulo fixnum-max 1)))
666
667 (pass-if "n = -1"
668 (eqv? 0 (modulo fixnum-max -1)))
669
670 (pass-if "n = fixnum-max"
671 (eqv? 0 (modulo fixnum-max fixnum-max)))
672
673 (pass-if "n = fixnum-max + 1"
674 (eqv? fixnum-max (modulo fixnum-max (+ fixnum-max 1))))
675
676 (pass-if "n = fixnum-min"
677 (eqv? -1 (modulo fixnum-max fixnum-min)))
678
679 (pass-if "n = fixnum-min - 1"
680 (eqv? -2 (modulo fixnum-max (- fixnum-min 1)))))
681
682 (with-test-prefix "(fixnum-max + 1) % n"
683
684 (pass-if "n = 1"
685 (eqv? 0 (modulo (+ fixnum-max 1) 1)))
686
687 (pass-if "n = -1"
688 (eqv? 0 (modulo (+ fixnum-max 1) -1)))
689
690 (pass-if "n = fixnum-max"
691 (eqv? 1 (modulo (+ fixnum-max 1) fixnum-max)))
692
693 (pass-if "n = fixnum-max + 1"
694 (eqv? 0 (modulo (+ fixnum-max 1) (+ fixnum-max 1))))
695
696 (pass-if "n = fixnum-min"
697 (eqv? 0 (modulo (+ fixnum-max 1) fixnum-min)))
698
699 (pass-if "n = fixnum-min - 1"
700 (eqv? -1 (modulo (+ fixnum-max 1) (- fixnum-min 1)))))
701
702 (with-test-prefix "fixnum-min % n"
703
704 (pass-if "n = 1"
705 (eqv? 0 (modulo fixnum-min 1)))
706
707 (pass-if "n = -1"
708 (eqv? 0 (modulo fixnum-min -1)))
709
710 (pass-if "n = fixnum-max"
711 (eqv? (- fixnum-max 1) (modulo fixnum-min fixnum-max)))
712
713 (pass-if "n = fixnum-max + 1"
714 (eqv? 0 (modulo fixnum-min (+ fixnum-max 1))))
715
716 (pass-if "n = fixnum-min"
717 (eqv? 0 (modulo fixnum-min fixnum-min)))
718
719 (pass-if "n = fixnum-min - 1"
720 (eqv? fixnum-min (modulo fixnum-min (- fixnum-min 1)))))
721
722 (with-test-prefix "(fixnum-min - 1) % n"
723
724 (pass-if "n = 1"
725 (eqv? 0 (modulo (- fixnum-min 1) 1)))
726
727 (pass-if "n = -1"
728 (eqv? 0 (modulo (- fixnum-min 1) -1)))
729
730 (pass-if "n = fixnum-max"
731 (eqv? (- fixnum-max 2) (modulo (- fixnum-min 1) fixnum-max)))
732
733 (pass-if "n = fixnum-max + 1"
734 (eqv? fixnum-max (modulo (- fixnum-min 1) (+ fixnum-max 1))))
735
736 (pass-if "n = fixnum-min"
737 (eqv? -1 (modulo (- fixnum-min 1) fixnum-min)))
738
739 (pass-if "n = fixnum-min - 1"
740 (eqv? 0 (modulo (- fixnum-min 1) (- fixnum-min 1)))))
de142bea
DH
741
742 ;; Positive dividend and divisor
743
744 (pass-if "13 % 4"
745 (eqv? 1 (modulo 13 4)))
746
747 (pass-if "2177452800 % 86400"
748 (eqv? 0 (modulo 2177452800 86400)))
749
750 ;; Negative dividend, positive divisor
751
752 (pass-if "-13 % 4"
753 (eqv? 3 (modulo -13 4)))
754
755 (pass-if "-2177452800 % 86400"
756 (eqv? 0 (modulo -2177452800 86400)))
757
758 ;; Positive dividend, negative divisor
759
760 (pass-if "13 % -4"
761 (eqv? -3 (modulo 13 -4)))
762
763 (pass-if "2177452800 % -86400"
764 (eqv? 0 (modulo 2177452800 -86400)))
765
766 ;; Negative dividend and divisor
767
768 (pass-if "-13 % -4"
769 (eqv? -1 (modulo -13 -4)))
770
771 (pass-if "-2177452800 % -86400"
772 (eqv? 0 (modulo -2177452800 -86400)))
773
774 ;; Are numerical overflows detected correctly?
775
80074d77
DH
776 (with-test-prefix "division by zero"
777
778 (pass-if-exception "(modulo 1 0)"
779 exception:numerical-overflow
780 (modulo 1 0))
781
782 (pass-if-exception "(modulo bignum 0)"
783 exception:numerical-overflow
784 (modulo (+ fixnum-max 1) 0)))
785
de142bea
DH
786 ;; Are wrong type arguments detected correctly?
787
788 )
789
24360e11
KR
790;;;
791;;; modulo-expt
792;;;
793
794(with-test-prefix "modulo-expt"
795 (pass-if (= 1 (modulo-expt 17 23 47)))
796
797 (pass-if (= 1 (modulo-expt 17 -23 47)))
798
799 (pass-if (= 17 (modulo-expt 17 -22 47)))
800
801 (pass-if (= 36 (modulo-expt 17 22 47)))
802
803 (pass-if (= 183658794479969134816674175082294846241553725240 (modulo-expt 111122223333444455556666 111122223333444455556666 1153478690012629968439432872520758982731022934717)))
804
805 (pass-if-exception
806 "Proper exception with 0 modulus"
807 (cons 'numerical-overflow "")
808 (modulo-expt 17 23 0))
809
810 (pass-if-exception
811 "Proper exception when result not invertible"
812 (cons 'numerical-overflow "")
813 (modulo-expt 10 -1 48))
814
815 (pass-if-exception
816 "Proper exception with wrong type argument"
817 (cons 'wrong-type-arg "")
818 (modulo-expt "Sam" 23 10))
819
820 (pass-if-exception
821 "Proper exception with wrong type argument"
822 (cons 'wrong-type-arg "")
823 (modulo-expt 17 9.9 10))
824
825 (pass-if-exception
826 "Proper exception with wrong type argument"
827 (cons 'wrong-type-arg "")
828 (modulo-expt 17 23 'Ethel)))
829
de142bea
DH
830;;;
831;;; gcd
832;;;
833
834(with-test-prefix "gcd"
835
de142bea 836 (expect-fail "documented?"
cb18f2a8 837 (documented? gcd))
de142bea 838
de142bea
DH
839 (with-test-prefix "(0 n)"
840
21e39e8f
DH
841 (pass-if "n = 0"
842 (eqv? 0 (gcd 0 0)))
843
de142bea
DH
844 (pass-if "n = 1"
845 (eqv? 1 (gcd 0 1)))
846
847 (pass-if "n = -1"
848 (eqv? 1 (gcd 0 -1)))
849
21e39e8f
DH
850 (pass-if "n = fixnum-max"
851 (eqv? fixnum-max (gcd 0 fixnum-max)))
de142bea 852
21e39e8f
DH
853 (pass-if "n = fixnum-max + 1"
854 (eqv? (+ fixnum-max 1) (gcd 0 (+ fixnum-max 1))))
de142bea 855
21e39e8f
DH
856 (pass-if "n = fixnum-min"
857 (eqv? (- fixnum-min) (gcd 0 fixnum-min)))
de142bea 858
21e39e8f
DH
859 (pass-if "n = fixnum-min - 1"
860 (eqv? (- (- fixnum-min 1)) (gcd 0 (- fixnum-min 1)))))
861
db386f80
KR
862 (with-test-prefix "(n 0)"
863
864 (pass-if "n = 2^128 * fixnum-max"
865 (eqv? (ash fixnum-max 128) (gcd (ash fixnum-max 128) 0))))
866
21e39e8f
DH
867 (with-test-prefix "(1 n)"
868
869 (pass-if "n = 0"
de142bea
DH
870 (eqv? 1 (gcd 1 0)))
871
21e39e8f
DH
872 (pass-if "n = 1"
873 (eqv? 1 (gcd 1 1)))
874
de142bea 875 (pass-if "n = -1"
21e39e8f
DH
876 (eqv? 1 (gcd 1 -1)))
877
878 (pass-if "n = fixnum-max"
879 (eqv? 1 (gcd 1 fixnum-max)))
880
881 (pass-if "n = fixnum-max + 1"
882 (eqv? 1 (gcd 1 (+ fixnum-max 1))))
883
884 (pass-if "n = fixnum-min"
885 (eqv? 1 (gcd 1 fixnum-min)))
886
887 (pass-if "n = fixnum-min - 1"
888 (eqv? 1 (gcd 1 (- fixnum-min 1)))))
889
890 (with-test-prefix "(-1 n)"
891
892 (pass-if "n = 0"
de142bea
DH
893 (eqv? 1 (gcd -1 0)))
894
21e39e8f
DH
895 (pass-if "n = 1"
896 (eqv? 1 (gcd -1 1)))
de142bea 897
21e39e8f
DH
898 (pass-if "n = -1"
899 (eqv? 1 (gcd -1 -1)))
de142bea 900
21e39e8f
DH
901 (pass-if "n = fixnum-max"
902 (eqv? 1 (gcd -1 fixnum-max)))
903
904 (pass-if "n = fixnum-max + 1"
905 (eqv? 1 (gcd -1 (+ fixnum-max 1))))
906
907 (pass-if "n = fixnum-min"
908 (eqv? 1 (gcd -1 fixnum-min)))
909
910 (pass-if "n = fixnum-min - 1"
911 (eqv? 1 (gcd -1 (- fixnum-min 1)))))
912
913 (with-test-prefix "(fixnum-max n)"
914
915 (pass-if "n = 0"
916 (eqv? fixnum-max (gcd fixnum-max 0)))
de142bea
DH
917
918 (pass-if "n = 1"
21e39e8f 919 (eqv? 1 (gcd fixnum-max 1)))
de142bea
DH
920
921 (pass-if "n = -1"
21e39e8f
DH
922 (eqv? 1 (gcd fixnum-max -1)))
923
924 (pass-if "n = fixnum-max"
925 (eqv? fixnum-max (gcd fixnum-max fixnum-max)))
de142bea 926
21e39e8f
DH
927 (pass-if "n = fixnum-max + 1"
928 (eqv? 1 (gcd fixnum-max (+ fixnum-max 1))))
de142bea 929
21e39e8f
DH
930 (pass-if "n = fixnum-min"
931 (eqv? 1 (gcd fixnum-max fixnum-min)))
de142bea 932
21e39e8f
DH
933 (pass-if "n = fixnum-min - 1"
934 (eqv? 1 (gcd fixnum-max (- fixnum-min 1)))))
935
936 (with-test-prefix "((+ fixnum-max 1) n)"
937
938 (pass-if "n = 0"
939 (eqv? (+ fixnum-max 1) (gcd (+ fixnum-max 1) 0)))
940
941 (pass-if "n = 1"
942 (eqv? 1 (gcd (+ fixnum-max 1) 1)))
de142bea
DH
943
944 (pass-if "n = -1"
21e39e8f 945 (eqv? 1 (gcd (+ fixnum-max 1) -1)))
de142bea 946
21e39e8f
DH
947 (pass-if "n = fixnum-max"
948 (eqv? 1 (gcd (+ fixnum-max 1) fixnum-max)))
de142bea 949
21e39e8f
DH
950 (pass-if "n = fixnum-max + 1"
951 (eqv? (+ fixnum-max 1) (gcd (+ fixnum-max 1) (+ fixnum-max 1))))
de142bea 952
21e39e8f
DH
953 (pass-if "n = fixnum-min"
954 (eqv? (+ fixnum-max 1) (gcd (+ fixnum-max 1) fixnum-min)))
955
956 (pass-if "n = fixnum-min - 1"
957 (eqv? 1 (gcd (+ fixnum-max 1) (- fixnum-min 1)))))
958
959 (with-test-prefix "(fixnum-min n)"
960
961 (pass-if "n = 0"
962 (eqv? (- fixnum-min) (gcd fixnum-min 0)))
963
964 (pass-if "n = 1"
965 (eqv? 1 (gcd fixnum-min 1)))
de142bea
DH
966
967 (pass-if "n = -1"
21e39e8f
DH
968 (eqv? 1 (gcd fixnum-min -1)))
969
970 (pass-if "n = fixnum-max"
971 (eqv? 1 (gcd fixnum-min fixnum-max)))
972
973 (pass-if "n = fixnum-max + 1"
974 (eqv? (+ fixnum-max 1) (gcd fixnum-min (+ fixnum-max 1))))
de142bea 975
21e39e8f
DH
976 (pass-if "n = fixnum-min"
977 (eqv? (- fixnum-min) (gcd fixnum-min fixnum-min)))
978
979 (pass-if "n = fixnum-min - 1"
980 (eqv? 1 (gcd fixnum-min (- fixnum-min 1)))))
981
982 (with-test-prefix "((- fixnum-min 1) n)"
983
984 (pass-if "n = 0"
985 (eqv? (- (- fixnum-min 1)) (gcd (- fixnum-min 1) 0)))
986
987 (pass-if "n = 1"
988 (eqv? 1 (gcd (- fixnum-min 1) 1)))
989
990 (pass-if "n = -1"
991 (eqv? 1 (gcd (- fixnum-min 1) -1)))
992
993 (pass-if "n = fixnum-max"
994 (eqv? 1 (gcd (- fixnum-min 1) fixnum-max)))
995
996 (pass-if "n = fixnum-max + 1"
997 (eqv? 1 (gcd (- fixnum-min 1) (+ fixnum-max 1))))
998
999 (pass-if "n = fixnum-min"
1000 (eqv? 1 (gcd (- fixnum-min 1) fixnum-min)))
1001
1002 (pass-if "n = fixnum-min - 1"
1003 (eqv? (- (- fixnum-min 1)) (gcd (- fixnum-min 1) (- fixnum-min 1)))))
de142bea
DH
1004
1005 ;; Are wrong type arguments detected correctly?
1006
1007 )
1008
f29b3454
DH
1009;;;
1010;;; lcm
1011;;;
1012
7c24e528
RB
1013(with-test-prefix "lcm"
1014 ;; FIXME: more tests?
1015 ;; (some of these are already in r4rs.test)
1016 (expect-fail (documented? lcm))
1017 (pass-if (= (lcm) 1))
1018 (pass-if (= (lcm 32 -36) 288))
1019 (let ((big-n 115792089237316195423570985008687907853269984665640564039457584007913129639936) ; 2 ^ 256
1020 (lcm-of-big-n-and-11 1273712981610478149659280835095566986385969831322046204434033424087044426039296))
1021 (pass-if (= lcm-of-big-n-and-11 (lcm big-n 11)))
1022 (pass-if (= lcm-of-big-n-and-11 (lcm 11 big-n 11)))))
1023
f29b3454
DH
1024;;;
1025;;; number->string
1026;;;
1027
7c24e528
RB
1028(with-test-prefix "number->string"
1029 (let ((num->str->num
1030 (lambda (n radix)
1031 (string->number (number->string n radix) radix))))
1032
1033 (pass-if (documented? number->string))
1034 (pass-if (string=? (number->string 0) "0"))
1035 (pass-if (string=? (number->string 171) "171"))
1036 (pass-if (= (+ fixnum-max 1) (num->str->num (+ fixnum-max 1) 10)))
1037 (pass-if (= (- fixnum-min 1) (num->str->num (- fixnum-min 1) 10)))
1038 (pass-if (= (inf) (num->str->num (inf) 10)))
1039 (pass-if (= 1.3 (num->str->num 1.3 10)))))
1040
f29b3454
DH
1041;;;
1042;;; string->number
1043;;;
1044
2f4a254a
DH
1045(with-test-prefix "string->number"
1046
1047 (pass-if "string->number"
1048 (documented? string->number))
1049
1050 (pass-if "non number strings"
1051 (for-each (lambda (x) (if (string->number x) (throw 'fail)))
569c483b 1052 '("" "q" "1q" "6+7iq" "8+9q" "10+11" "13+" "18@19q" "20@q" "23@"
2f4a254a 1053 "+25iq" "26i" "-q" "-iq" "i" "5#.0" "8/" "10#11" ".#" "."
569c483b 1054 "#o.2" "3.4q" "15.16e17q" "18.19e+q" ".q" ".17#18" "10q" "#b2"
2f4a254a
DH
1055 "#b3" "#b4" "#b5" "#b6" "#b7" "#b8" "#b9" "#ba" "#bb" "#bc"
1056 "#bd" "#be" "#bf" "#q" "#b#b1" "#o#o1" "#d#d1" "#x#x1" "#e#e1"
1057 "#i#i1" "12@12+0i"))
1058 #t)
1059
b7d9b1cf
DH
1060 (pass-if "valid number strings"
1061 (for-each (lambda (couple)
1062 (apply
1063 (lambda (x y)
9dd9857f
MV
1064 (let ((xx (string->number x)))
1065 (if (or (eq? xx #f) (not (eqv? xx y)))
ca2b31fe
MV
1066 (begin
1067 (pk x y)
1068 (throw 'fail)))))
b7d9b1cf
DH
1069 couple))
1070 `(;; Radix:
1071 ("#b0" 0) ("#B0" 0) ("#b1" 1) ("#B1" 1) ("#o0" 0) ("#O0" 0)
1072 ("#o1" 1) ("#O1" 1) ("#o2" 2) ("#O2" 2) ("#o3" 3) ("#O3" 3)
1073 ("#o4" 4) ("#O4" 4) ("#o5" 5) ("#O5" 5) ("#o6" 6) ("#O6" 6)
1074 ("#o7" 7) ("#O7" 7) ("#d0" 0) ("#D0" 0) ("#d1" 1) ("#D1" 1)
1075 ("#d2" 2) ("#D2" 2) ("#d3" 3) ("#D3" 3) ("#d4" 4) ("#D4" 4)
1076 ("#d5" 5) ("#D5" 5) ("#d6" 6) ("#D6" 6) ("#d7" 7) ("#D7" 7)
1077 ("#d8" 8) ("#D8" 8) ("#d9" 9) ("#D9" 9)
1078 ("#xa" 10) ("#Xa" 10) ("#xb" 11) ("#Xb" 11)
1079 ("#xc" 12) ("#Xc" 12) ("#xd" 13) ("#Xd" 13)
1080 ("#xe" 14) ("#Xe" 14) ("#xf" 15) ("#Xf" 15)
1081 ("#b1010" 10)
1082 ("#o12345670" 2739128)
1083 ("#d1234567890" 1234567890)
1084 ("#x1234567890abcdef" 1311768467294899695)
1085 ;; Exactness:
ca2b31fe 1086 ("#e1" 1) ("#e1.2" 12/10)
9dd9857f 1087 ("#i1.1" 1.1) ("#i1" 1.0)
b7d9b1cf
DH
1088 ;; Integers:
1089 ("1" ,(1+ 0)) ("23" ,(+ 9 9 5)) ("-1" ,(- 0 1))
1090 ("-45" ,(- 0 45)) ("2#" 20.0) ("2##" 200.0) ("12##" 1200.0)
1091 ("#b#i100" 4.0)
9dd9857f
MV
1092 ;; Fractions:
1093 ("1/1" 1) ("1/2" 1/2) ("-1/2" -1/2) ("1#/1" 10.0)
1094 ("10/1#" 1.0) ("1#/1#" 1.0) ("#e9/10" 9/10) ("#e10/1#" 1)
b7d9b1cf
DH
1095 ("#i6/8" 0.75) ("#i1/1" 1.0)
1096 ;; Decimal numbers:
1097 ;; * <uinteger 10> <suffix>
1098 ("1e2" 100.0) ("1E2" 100.0) ("1s2" 100.0) ("1S2" 100.0)
1099 ("1f2" 100.0) ("1F2" 100.0) ("1d2" 100.0) ("1D2" 100.0)
1100 ("1l2" 100.0) ("1L2" 100.0) ("1e+2" 100.0) ("1e-2" 0.01)
1101 ;; * . <digit 10>+ #* <suffix>
1102 (".1" .1) (".0123456789" 123456789e-10) (".16#" 0.16)
1103 (".0123456789e10" 123456789.0) (".16#e3" 160.0) ("#d.3" 0.3)
1104 ;; * <digit 10>+ . <digit 10>* #* <suffix>
1105 ("3." ,(exact->inexact 3)) ("3.e0" ,(exact->inexact 3))
1106 ("3.1" ,(exact->inexact 31/10)) ("3.1e0" 3.1) ("3.1#" 3.1)
1107 ("3.1#e0" 3.1)
1108 ;; * <digit 10>+ #+ . #* <suffix>
1109 ("3#." 30.0) ("3#.e0" 30.0) ("3#.#" 30.0) ("3#.#e0" 30.0)
1110 ;; Complex:
1111 ("1@0" 1.0) ("1@+0" 1.0) ("1@-0" 1.0)
1112 ("2+3i" ,(+ 2 (* 3 +i))) ("4-5i" ,(- 4 (* 5 +i)))
1113 ("1+i" 1+1i) ("1-i" 1-1i) ("+1i" 0+1i) ("-1i" 0-1i)
1114 ("+i" +1i) ("-i" -1i)))
1115 #t)
1116
2f4a254a
DH
1117 (pass-if-exception "exponent too big"
1118 exception:out-of-range
1119 (string->number "12.13e141414")))
1120
f29b3454
DH
1121;;;
1122;;; number?
1123;;;
1124
7c24e528
RB
1125(with-test-prefix "number?"
1126 (pass-if (documented? number?))
1127 (pass-if (number? 0))
1128 (pass-if (number? 7))
1129 (pass-if (number? -7))
1130 (pass-if (number? 1.3))
1131 (pass-if (number? (+ 1 fixnum-max)))
1132 (pass-if (number? (- 1 fixnum-min)))
1133 (pass-if (number? 3+4i))
4d332f19
DH
1134 (pass-if (not (number? #\a)))
1135 (pass-if (not (number? "a")))
1136 (pass-if (not (number? (make-vector 0))))
1137 (pass-if (not (number? (cons 1 2))))
1138 (pass-if (not (number? #t)))
1139 (pass-if (not (number? (lambda () #t))))
1140 (pass-if (not (number? (current-input-port)))))
7c24e528 1141
f29b3454
DH
1142;;;
1143;;; complex?
1144;;;
1145
7c24e528
RB
1146(with-test-prefix "complex?"
1147 (pass-if (documented? complex?))
1148 (pass-if (complex? 0))
1149 (pass-if (complex? 7))
1150 (pass-if (complex? -7))
1151 (pass-if (complex? (+ 1 fixnum-max)))
1152 (pass-if (complex? (- 1 fixnum-min)))
1153 (pass-if (complex? 1.3))
1154 (pass-if (complex? 3+4i))
4d332f19
DH
1155 (pass-if (not (complex? #\a)))
1156 (pass-if (not (complex? "a")))
1157 (pass-if (not (complex? (make-vector 0))))
1158 (pass-if (not (complex? (cons 1 2))))
1159 (pass-if (not (complex? #t)))
1160 (pass-if (not (complex? (lambda () #t))))
1161 (pass-if (not (complex? (current-input-port)))))
7c24e528 1162
f29b3454
DH
1163;;;
1164;;; real?
1165;;;
1166
7c24e528
RB
1167(with-test-prefix "real?"
1168 (pass-if (documented? real?))
1169 (pass-if (real? 0))
1170 (pass-if (real? 7))
1171 (pass-if (real? -7))
1172 (pass-if (real? (+ 1 fixnum-max)))
1173 (pass-if (real? (- 1 fixnum-min)))
1174 (pass-if (real? 1.3))
4d332f19
DH
1175 (pass-if (not (real? 3+4i)))
1176 (pass-if (not (real? #\a)))
1177 (pass-if (not (real? "a")))
1178 (pass-if (not (real? (make-vector 0))))
1179 (pass-if (not (real? (cons 1 2))))
1180 (pass-if (not (real? #t)))
1181 (pass-if (not (real? (lambda () #t))))
1182 (pass-if (not (real? (current-input-port)))))
7c24e528 1183
f29b3454 1184;;;
7c24e528 1185;;; rational? (same as real? right now)
f29b3454
DH
1186;;;
1187
7c24e528
RB
1188(with-test-prefix "rational?"
1189 (pass-if (documented? rational?))
1190 (pass-if (rational? 0))
1191 (pass-if (rational? 7))
1192 (pass-if (rational? -7))
1193 (pass-if (rational? (+ 1 fixnum-max)))
1194 (pass-if (rational? (- 1 fixnum-min)))
1195 (pass-if (rational? 1.3))
4d332f19
DH
1196 (pass-if (not (rational? 3+4i)))
1197 (pass-if (not (rational? #\a)))
1198 (pass-if (not (rational? "a")))
1199 (pass-if (not (rational? (make-vector 0))))
1200 (pass-if (not (rational? (cons 1 2))))
1201 (pass-if (not (rational? #t)))
1202 (pass-if (not (rational? (lambda () #t))))
1203 (pass-if (not (rational? (current-input-port)))))
7c24e528 1204
f29b3454
DH
1205;;;
1206;;; integer?
1207;;;
1208
7c24e528
RB
1209(with-test-prefix "integer?"
1210 (pass-if (documented? integer?))
1211 (pass-if (integer? 0))
1212 (pass-if (integer? 7))
1213 (pass-if (integer? -7))
1214 (pass-if (integer? (+ 1 fixnum-max)))
1215 (pass-if (integer? (- 1 fixnum-min)))
1216 (pass-if (and (= 3+0i (round 3+0i)) (integer? 3+0i)))
1217 (pass-if (and (= 1.0 (round 1.0)) (integer? 1.0)))
4d332f19
DH
1218 (pass-if (not (integer? 1.3)))
1219 (pass-if (not (integer? 3+4i)))
1220 (pass-if (not (integer? #\a)))
1221 (pass-if (not (integer? "a")))
1222 (pass-if (not (integer? (make-vector 0))))
1223 (pass-if (not (integer? (cons 1 2))))
1224 (pass-if (not (integer? #t)))
1225 (pass-if (not (integer? (lambda () #t))))
1226 (pass-if (not (integer? (current-input-port)))))
7c24e528 1227
f29b3454
DH
1228;;;
1229;;; inexact?
1230;;;
1231
7c24e528
RB
1232(with-test-prefix "inexact?"
1233 (pass-if (documented? inexact?))
4d332f19
DH
1234 (pass-if (not (inexact? 0)))
1235 (pass-if (not (inexact? 7)))
1236 (pass-if (not (inexact? -7)))
1237 (pass-if (not (inexact? (+ 1 fixnum-max))))
1238 (pass-if (not (inexact? (- 1 fixnum-min))))
7c24e528
RB
1239 (pass-if (inexact? 1.3))
1240 (pass-if (inexact? 3.1+4.2i))
ca2b31fe
MV
1241 (pass-if-exception "char"
1242 exception:wrong-type-arg
1243 (not (inexact? #\a)))
1244 (pass-if-exception "string"
1245 exception:wrong-type-arg
1246 (not (inexact? "a")))
1247 (pass-if-exception "vector"
1248 exception:wrong-type-arg
1249 (not (inexact? (make-vector 0))))
1250 (pass-if-exception "cons"
1251 exception:wrong-type-arg
1252 (not (inexact? (cons 1 2))))
1253 (pass-if-exception "bool"
1254 exception:wrong-type-arg
1255 (not (inexact? #t)))
1256 (pass-if-exception "procedure"
1257 exception:wrong-type-arg
1258 (not (inexact? (lambda () #t))))
1259 (pass-if-exception "port"
1260 exception:wrong-type-arg
1261 (not (inexact? (current-input-port)))))
7c24e528 1262
47ae1f0e
DH
1263;;;
1264;;; equal?
1265;;;
1266
1267(with-test-prefix "equal?"
1268 (pass-if (documented? equal?))
1269 (pass-if (equal? 0 0))
1270 (pass-if (equal? 7 7))
1271 (pass-if (equal? -7 -7))
1272 (pass-if (equal? (+ 1 fixnum-max) (+ 1 fixnum-max)))
1273 (pass-if (equal? (- fixnum-min 1) (- fixnum-min 1)))
1274 (pass-if (not (equal? 0 1)))
1275 (pass-if (not (equal? fixnum-max (+ 1 fixnum-max))))
1276 (pass-if (not (equal? (+ 1 fixnum-max) fixnum-max)))
1277 (pass-if (not (equal? (+ 1 fixnum-max) (+ 2 fixnum-max))))
1278 (pass-if (not (equal? fixnum-min (- fixnum-min 1))))
1279 (pass-if (not (equal? (- fixnum-min 1) fixnum-min)))
1280 (pass-if (not (equal? (- fixnum-min 1) (- fixnum-min 2))))
1281 (pass-if (not (equal? (+ fixnum-max 1) (- fixnum-min 1))))
1282
1283 (pass-if (not (equal? (ash 1 256) +inf.0)))
1284 (pass-if (not (equal? +inf.0 (ash 1 256))))
1285 (pass-if (not (equal? (ash 1 256) -inf.0)))
1286 (pass-if (not (equal? -inf.0 (ash 1 256))))
1287
1288 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
1289 ;; sure we've avoided that
1290 (pass-if (not (equal? (ash 1 1024) +inf.0)))
1291 (pass-if (not (equal? +inf.0 (ash 1 1024))))
1292 (pass-if (not (equal? (- (ash 1 1024)) -inf.0)))
1293 (pass-if (not (equal? -inf.0 (- (ash 1 1024)))))
1294
1295 (pass-if (not (equal? +nan.0 +nan.0)))
1296 (pass-if (not (equal? 0 +nan.0)))
1297 (pass-if (not (equal? +nan.0 0)))
1298 (pass-if (not (equal? 1 +nan.0)))
1299 (pass-if (not (equal? +nan.0 1)))
1300 (pass-if (not (equal? -1 +nan.0)))
1301 (pass-if (not (equal? +nan.0 -1)))
1302
1303 (pass-if (not (equal? (ash 1 256) +nan.0)))
1304 (pass-if (not (equal? +nan.0 (ash 1 256))))
1305 (pass-if (not (equal? (- (ash 1 256)) +nan.0)))
1306 (pass-if (not (equal? +nan.0 (- (ash 1 256)))))
1307
1308 (pass-if (not (equal? (ash 1 8192) +nan.0)))
1309 (pass-if (not (equal? +nan.0 (ash 1 8192))))
1310 (pass-if (not (equal? (- (ash 1 8192)) +nan.0)))
1311 (pass-if (not (equal? +nan.0 (- (ash 1 8192)))))
1312
1313 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
1314 ;; sure we've avoided that
1315 (pass-if (not (equal? (ash 3 1023) +nan.0)))
1316 (pass-if (not (equal? +nan.0 (ash 3 1023)))))
1317
f29b3454
DH
1318;;;
1319;;; =
1320;;;
1321
7c24e528
RB
1322(with-test-prefix "="
1323 (expect-fail (documented? =))
1324 (pass-if (= 0 0))
1325 (pass-if (= 7 7))
1326 (pass-if (= -7 -7))
1327 (pass-if (= (+ 1 fixnum-max) (+ 1 fixnum-max)))
47ae1f0e 1328 (pass-if (= (- fixnum-min 1) (- fixnum-min 1)))
4d332f19
DH
1329 (pass-if (not (= 0 1)))
1330 (pass-if (not (= fixnum-max (+ 1 fixnum-max))))
1331 (pass-if (not (= (+ 1 fixnum-max) fixnum-max)))
47ae1f0e 1332 (pass-if (not (= (+ 1 fixnum-max) (+ 2 fixnum-max))))
4d332f19
DH
1333 (pass-if (not (= fixnum-min (- fixnum-min 1))))
1334 (pass-if (not (= (- fixnum-min 1) fixnum-min)))
47ae1f0e 1335 (pass-if (not (= (- fixnum-min 1) (- fixnum-min 2))))
4d332f19 1336 (pass-if (not (= (+ fixnum-max 1) (- fixnum-min 1))))
2cfcaed5 1337
adda36ed
KR
1338 (pass-if (not (= (ash 1 256) +inf.0)))
1339 (pass-if (not (= +inf.0 (ash 1 256))))
1340 (pass-if (not (= (ash 1 256) -inf.0)))
1341 (pass-if (not (= -inf.0 (ash 1 256))))
1342
1343 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
1344 ;; sure we've avoided that
1345 (pass-if (not (= (ash 1 1024) +inf.0)))
1346 (pass-if (not (= +inf.0 (ash 1 1024))))
1347 (pass-if (not (= (- (ash 1 1024)) -inf.0)))
1348 (pass-if (not (= -inf.0 (- (ash 1 1024)))))
1349
2cfcaed5
KR
1350 (pass-if (not (= +nan.0 +nan.0)))
1351 (pass-if (not (= 0 +nan.0)))
1352 (pass-if (not (= +nan.0 0)))
1353 (pass-if (not (= 1 +nan.0)))
1354 (pass-if (not (= +nan.0 1)))
1355 (pass-if (not (= -1 +nan.0)))
1356 (pass-if (not (= +nan.0 -1)))
1357
1358 (pass-if (not (= (ash 1 256) +nan.0)))
1359 (pass-if (not (= +nan.0 (ash 1 256))))
1360 (pass-if (not (= (- (ash 1 256)) +nan.0)))
1361 (pass-if (not (= +nan.0 (- (ash 1 256)))))
1362
1363 (pass-if (not (= (ash 1 8192) +nan.0)))
1364 (pass-if (not (= +nan.0 (ash 1 8192))))
1365 (pass-if (not (= (- (ash 1 8192)) +nan.0)))
1366 (pass-if (not (= +nan.0 (- (ash 1 8192)))))
1367
1368 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
1369 ;; sure we've avoided that
1370 (pass-if (not (= (ash 3 1023) +nan.0)))
2a8b5e04
KR
1371 (pass-if (not (= +nan.0 (ash 3 1023))))
1372
1373 (pass-if (= 1/2 0.5))
1374 (pass-if (not (= 1/3 0.333333333333333333333333333333333)))
1375 (pass-if (not (= 2/3 0.5)))
1376 (pass-if (not (= 0.5 (+ 1/2 (/ 1 (ash 1 1000))))))
1377
1378 (pass-if (= 1/2 0.5+0i))
1379 (pass-if (not (= 0.333333333333333333333333333333333 1/3)))
1380 (pass-if (not (= 2/3 0.5+0i)))
1381 (pass-if (not (= 1/2 0+0.5i)))
1382
1383 (pass-if (= 0.5 1/2))
1384 (pass-if (not (= 0.5 2/3)))
1385 (pass-if (not (= (+ 1/2 (/ 1 (ash 1 1000))) 0.5)))
1386
1387 (pass-if (= 0.5+0i 1/2))
1388 (pass-if (not (= 0.5+0i 2/3)))
1389 (pass-if (not (= 0+0.5i 1/2))))
7c24e528 1390
de142bea
DH
1391;;;
1392;;; <
1393;;;
1394
1395(with-test-prefix "<"
1396
de142bea 1397 (expect-fail "documented?"
cb18f2a8 1398 (documented? <))
de142bea 1399
de142bea
DH
1400 (with-test-prefix "(< 0 n)"
1401
1402 (pass-if "n = 0"
1403 (not (< 0 0)))
1404
1405 (pass-if "n = 0.0"
1406 (not (< 0 0.0)))
1407
1408 (pass-if "n = 1"
1409 (< 0 1))
1410
1411 (pass-if "n = 1.0"
1412 (< 0 1.0))
1413
1414 (pass-if "n = -1"
1415 (not (< 0 -1)))
1416
1417 (pass-if "n = -1.0"
1418 (not (< 0 -1.0)))
1419
21e39e8f
DH
1420 (pass-if "n = fixnum-max"
1421 (< 0 fixnum-max))
1422
1423 (pass-if "n = fixnum-max + 1"
1424 (< 0 (+ fixnum-max 1)))
1425
1426 (pass-if "n = fixnum-min"
1427 (not (< 0 fixnum-min)))
de142bea 1428
21e39e8f
DH
1429 (pass-if "n = fixnum-min - 1"
1430 (not (< 0 (- fixnum-min 1)))))
1431
de142bea
DH
1432 (with-test-prefix "(< 0.0 n)"
1433
1434 (pass-if "n = 0"
1435 (not (< 0.0 0)))
1436
1437 (pass-if "n = 0.0"
1438 (not (< 0.0 0.0)))
1439
1440 (pass-if "n = 1"
1441 (< 0.0 1))
1442
1443 (pass-if "n = 1.0"
1444 (< 0.0 1.0))
1445
1446 (pass-if "n = -1"
1447 (not (< 0.0 -1)))
1448
1449 (pass-if "n = -1.0"
1450 (not (< 0.0 -1.0)))
1451
21e39e8f
DH
1452 (pass-if "n = fixnum-max"
1453 (< 0.0 fixnum-max))
1454
1455 (pass-if "n = fixnum-max + 1"
1456 (< 0.0 (+ fixnum-max 1)))
de142bea 1457
21e39e8f
DH
1458 (pass-if "n = fixnum-min"
1459 (not (< 0.0 fixnum-min)))
1460
1461 (pass-if "n = fixnum-min - 1"
1462 (not (< 0.0 (- fixnum-min 1)))))
1463
1464 (with-test-prefix "(< 1 n)"
de142bea 1465
21e39e8f 1466 (pass-if "n = 0"
de142bea
DH
1467 (not (< 1 0)))
1468
21e39e8f
DH
1469 (pass-if "n = 0.0"
1470 (not (< 1 0.0)))
1471
1472 (pass-if "n = 1"
1473 (not (< 1 1)))
1474
de142bea 1475 (pass-if "n = 1.0"
21e39e8f
DH
1476 (not (< 1 1.0)))
1477
1478 (pass-if "n = -1"
1479 (not (< 1 -1)))
1480
1481 (pass-if "n = -1.0"
1482 (not (< 1 -1.0)))
1483
1484 (pass-if "n = fixnum-max"
1485 (< 1 fixnum-max))
1486
1487 (pass-if "n = fixnum-max + 1"
1488 (< 1 (+ fixnum-max 1)))
1489
1490 (pass-if "n = fixnum-min"
1491 (not (< 1 fixnum-min)))
1492
1493 (pass-if "n = fixnum-min - 1"
1494 (not (< 1 (- fixnum-min 1)))))
1495
1496 (with-test-prefix "(< 1.0 n)"
1497
1498 (pass-if "n = 0"
de142bea
DH
1499 (not (< 1.0 0)))
1500
21e39e8f
DH
1501 (pass-if "n = 0.0"
1502 (not (< 1.0 0.0)))
1503
1504 (pass-if "n = 1"
1505 (not (< 1.0 1)))
1506
1507 (pass-if "n = 1.0"
1508 (not (< 1.0 1.0)))
1509
de142bea 1510 (pass-if "n = -1"
21e39e8f
DH
1511 (not (< 1.0 -1)))
1512
1513 (pass-if "n = -1.0"
1514 (not (< 1.0 -1.0)))
1515
1516 (pass-if "n = fixnum-max"
1517 (< 1.0 fixnum-max))
1518
1519 (pass-if "n = fixnum-max + 1"
1520 (< 1.0 (+ fixnum-max 1)))
1521
1522 (pass-if "n = fixnum-min"
1523 (not (< 1.0 fixnum-min)))
1524
1525 (pass-if "n = fixnum-min - 1"
1526 (not (< 1.0 (- fixnum-min 1)))))
1527
1528 (with-test-prefix "(< -1 n)"
1529
1530 (pass-if "n = 0"
de142bea
DH
1531 (< -1 0))
1532
21e39e8f
DH
1533 (pass-if "n = 0.0"
1534 (< -1 0.0))
1535
1536 (pass-if "n = 1"
1537 (< -1 1))
1538
1539 (pass-if "n = 1.0"
1540 (< -1 1.0))
1541
1542 (pass-if "n = -1"
1543 (not (< -1 -1)))
1544
de142bea 1545 (pass-if "n = -1.0"
21e39e8f
DH
1546 (not (< -1 -1.0)))
1547
1548 (pass-if "n = fixnum-max"
1549 (< -1 fixnum-max))
1550
1551 (pass-if "n = fixnum-max + 1"
1552 (< -1 (+ fixnum-max 1)))
1553
1554 (pass-if "n = fixnum-min"
1555 (not (< -1 fixnum-min)))
1556
1557 (pass-if "n = fixnum-min - 1"
1558 (not (< -1 (- fixnum-min 1)))))
1559
1560 (with-test-prefix "(< -1.0 n)"
1561
1562 (pass-if "n = 0"
de142bea
DH
1563 (< -1.0 0))
1564
21e39e8f
DH
1565 (pass-if "n = 0.0"
1566 (< -1.0 0.0))
1567
1568 (pass-if "n = 1"
1569 (< -1.0 1))
1570
1571 (pass-if "n = 1.0"
1572 (< -1.0 1.0))
1573
1574 (pass-if "n = -1"
1575 (not (< -1.0 -1)))
1576
1577 (pass-if "n = -1.0"
1578 (not (< -1.0 -1.0)))
1579
1580 (pass-if "n = fixnum-max"
1581 (< -1.0 fixnum-max))
1582
1583 (pass-if "n = fixnum-max + 1"
1584 (< -1.0 (+ fixnum-max 1)))
de142bea 1585
21e39e8f
DH
1586 (pass-if "n = fixnum-min"
1587 (not (< -1.0 fixnum-min)))
1588
1589 (pass-if "n = fixnum-min - 1"
1590 (not (< -1.0 (- fixnum-min 1)))))
1591
1592 (with-test-prefix "(< fixnum-max n)"
1593
1594 (pass-if "n = 0"
1595 (not (< fixnum-max 0)))
1596
1597 (pass-if "n = 0.0"
1598 (not (< fixnum-max 0.0)))
de142bea
DH
1599
1600 (pass-if "n = 1"
21e39e8f 1601 (not (< fixnum-max 1)))
de142bea
DH
1602
1603 (pass-if "n = 1.0"
21e39e8f 1604 (not (< fixnum-max 1.0)))
de142bea
DH
1605
1606 (pass-if "n = -1"
21e39e8f 1607 (not (< fixnum-max -1)))
de142bea
DH
1608
1609 (pass-if "n = -1.0"
21e39e8f 1610 (not (< fixnum-max -1.0)))
de142bea 1611
21e39e8f
DH
1612 (pass-if "n = fixnum-max"
1613 (not (< fixnum-max fixnum-max)))
de142bea 1614
21e39e8f
DH
1615 (pass-if "n = fixnum-max + 1"
1616 (< fixnum-max (+ fixnum-max 1)))
1617
1618 (pass-if "n = fixnum-min"
1619 (not (< fixnum-max fixnum-min)))
1620
1621 (pass-if "n = fixnum-min - 1"
1622 (not (< fixnum-max (- fixnum-min 1)))))
1623
1624 (with-test-prefix "(< (+ fixnum-max 1) n)"
1625
1626 (pass-if "n = 0"
1627 (not (< (+ fixnum-max 1) 0)))
1628
1629 (pass-if "n = 0.0"
1630 (not (< (+ fixnum-max 1) 0.0)))
de142bea
DH
1631
1632 (pass-if "n = 1"
21e39e8f 1633 (not (< (+ fixnum-max 1) 1)))
de142bea
DH
1634
1635 (pass-if "n = 1.0"
21e39e8f 1636 (not (< (+ fixnum-max 1) 1.0)))
de142bea
DH
1637
1638 (pass-if "n = -1"
21e39e8f 1639 (not (< (+ fixnum-max 1) -1)))
de142bea
DH
1640
1641 (pass-if "n = -1.0"
21e39e8f 1642 (not (< (+ fixnum-max 1) -1.0)))
de142bea 1643
21e39e8f
DH
1644 (pass-if "n = fixnum-max"
1645 (not (< (+ fixnum-max 1) fixnum-max)))
de142bea 1646
21e39e8f
DH
1647 (pass-if "n = fixnum-max + 1"
1648 (not (< (+ fixnum-max 1) (+ fixnum-max 1))))
de142bea 1649
21e39e8f
DH
1650 (pass-if "n = fixnum-min"
1651 (not (< (+ fixnum-max 1) fixnum-min)))
1652
1653 (pass-if "n = fixnum-min - 1"
1654 (not (< (+ fixnum-max 1) (- fixnum-min 1)))))
1655
1656 (with-test-prefix "(< fixnum-min n)"
1657
1658 (pass-if "n = 0"
1659 (< fixnum-min 0))
1660
1661 (pass-if "n = 0.0"
1662 (< fixnum-min 0.0))
de142bea
DH
1663
1664 (pass-if "n = 1"
21e39e8f 1665 (< fixnum-min 1))
de142bea
DH
1666
1667 (pass-if "n = 1.0"
21e39e8f 1668 (< fixnum-min 1.0))
de142bea
DH
1669
1670 (pass-if "n = -1"
21e39e8f 1671 (< fixnum-min -1))
de142bea
DH
1672
1673 (pass-if "n = -1.0"
21e39e8f 1674 (< fixnum-min -1.0))
de142bea 1675
21e39e8f
DH
1676 (pass-if "n = fixnum-max"
1677 (< fixnum-min fixnum-max))
1678
1679 (pass-if "n = fixnum-max + 1"
1680 (< fixnum-min (+ fixnum-max 1)))
de142bea 1681
21e39e8f
DH
1682 (pass-if "n = fixnum-min"
1683 (not (< fixnum-min fixnum-min)))
de142bea 1684
21e39e8f
DH
1685 (pass-if "n = fixnum-min - 1"
1686 (not (< fixnum-min (- fixnum-min 1)))))
1687
1688 (with-test-prefix "(< (- fixnum-min 1) n)"
1689
1690 (pass-if "n = 0"
1691 (< (- fixnum-min 1) 0))
1692
1693 (pass-if "n = 0.0"
1694 (< (- fixnum-min 1) 0.0))
1695
1696 (pass-if "n = 1"
1697 (< (- fixnum-min 1) 1))
1698
1699 (pass-if "n = 1.0"
1700 (< (- fixnum-min 1) 1.0))
de142bea
DH
1701
1702 (pass-if "n = -1"
21e39e8f 1703 (< (- fixnum-min 1) -1))
de142bea
DH
1704
1705 (pass-if "n = -1.0"
21e39e8f
DH
1706 (< (- fixnum-min 1) -1.0))
1707
1708 (pass-if "n = fixnum-max"
1709 (< (- fixnum-min 1) fixnum-max))
1710
1711 (pass-if "n = fixnum-max + 1"
1712 (< (- fixnum-min 1) (+ fixnum-max 1)))
1713
1714 (pass-if "n = fixnum-min"
1715 (< (- fixnum-min 1) fixnum-min))
1716
1717 (pass-if "n = fixnum-min - 1"
2cfcaed5
KR
1718 (not (< (- fixnum-min 1) (- fixnum-min 1)))))
1719
adda36ed
KR
1720 (pass-if (< (ash 1 256) +inf.0))
1721 (pass-if (not (< +inf.0 (ash 1 256))))
1722 (pass-if (not (< (ash 1 256) -inf.0)))
1723 (pass-if (< -inf.0 (ash 1 256)))
1724
1725 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
1726 ;; sure we've avoided that
1727 (pass-if (< (1- (ash 1 1024)) +inf.0))
1728 (pass-if (< (ash 1 1024) +inf.0))
1729 (pass-if (< (1+ (ash 1 1024)) +inf.0))
1730 (pass-if (not (< +inf.0 (1- (ash 1 1024)))))
1731 (pass-if (not (< +inf.0 (ash 1 1024))))
1732 (pass-if (not (< +inf.0 (1+ (ash 1 1024)))))
1733 (pass-if (< -inf.0 (- (1- (ash 1 1024)))))
1734 (pass-if (< -inf.0 (- (ash 1 1024))))
1735 (pass-if (< -inf.0 (- (1+ (ash 1 1024)))))
1736 (pass-if (not (< (- (1- (ash 1 1024))) -inf.0)))
1737 (pass-if (not (< (- (ash 1 1024)) -inf.0)))
1738 (pass-if (not (< (- (1+ (ash 1 1024))) -inf.0)))
1739
2cfcaed5
KR
1740 (pass-if (not (< +nan.0 +nan.0)))
1741 (pass-if (not (< 0 +nan.0)))
1742 (pass-if (not (< +nan.0 0)))
1743 (pass-if (not (< 1 +nan.0)))
1744 (pass-if (not (< +nan.0 1)))
1745 (pass-if (not (< -1 +nan.0)))
1746 (pass-if (not (< +nan.0 -1)))
1747
1748 (pass-if (not (< (ash 1 256) +nan.0)))
1749 (pass-if (not (< +nan.0 (ash 1 256))))
1750 (pass-if (not (< (- (ash 1 256)) +nan.0)))
1751 (pass-if (not (< +nan.0 (- (ash 1 256)))))
1752
1753 (pass-if (not (< (ash 1 8192) +nan.0)))
1754 (pass-if (not (< +nan.0 (ash 1 8192))))
1755 (pass-if (not (< (- (ash 1 8192)) +nan.0)))
1756 (pass-if (not (< +nan.0 (- (ash 1 8192)))))
1757
1758 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
1759 ;; sure we've avoided that
1760 (pass-if (not (< (ash 3 1023) +nan.0)))
1761 (pass-if (not (< (1+ (ash 3 1023)) +nan.0)))
1762 (pass-if (not (< (1- (ash 3 1023)) +nan.0)))
1763 (pass-if (not (< +nan.0 (ash 3 1023))))
1764 (pass-if (not (< +nan.0 (1+ (ash 3 1023)))))
fe89421e
KR
1765 (pass-if (not (< +nan.0 (1- (ash 3 1023)))))
1766
1767 (with-test-prefix "inum/frac"
1768 (pass-if (< 2 9/4))
1769 (pass-if (< -2 9/4))
1770 (pass-if (< -2 7/4))
1771 (pass-if (< -2 -7/4))
1772 (pass-if (eq? #f (< 2 7/4)))
1773 (pass-if (eq? #f (< 2 -7/4)))
1774 (pass-if (eq? #f (< 2 -9/4)))
1775 (pass-if (eq? #f (< -2 -9/4))))
1776
1777 (with-test-prefix "bignum/frac"
1778 (let ((x (ash 1 2048)))
1779 (pass-if (< x (* 4/3 x)))
1780 (pass-if (< (- x) (* 4/3 x)))
1781 (pass-if (< (- x) (* 2/3 x)))
1782 (pass-if (< (- x) (* -2/3 x)))
1783 (pass-if (eq? #f (< x (* 2/3 x))))
1784 (pass-if (eq? #f (< x (* -2/3 x))))
1785 (pass-if (eq? #f (< x (* -4/3 x))))
1786 (pass-if (eq? #f (< (- x) (* -4/3 x))))))
1787
1788 (with-test-prefix "flonum/frac"
1789 (pass-if (< 0.75 4/3))
1790 (pass-if (< -0.75 4/3))
1791 (pass-if (< -0.75 2/3))
1792 (pass-if (< -0.75 -2/3))
1793 (pass-if (eq? #f (< 0.75 2/3)))
1794 (pass-if (eq? #f (< 0.75 -2/3)))
1795 (pass-if (eq? #f (< 0.75 -4/3)))
1796 (pass-if (eq? #f (< -0.75 -4/3)))
1797
1798 (pass-if (< -inf.0 4/3))
1799 (pass-if (< -inf.0 -4/3))
1800 (pass-if (eq? #f (< +inf.0 4/3)))
1801 (pass-if (eq? #f (< +inf.0 -4/3)))
1802
1803 (pass-if (eq? #f (< +nan.0 4/3)))
1804 (pass-if (eq? #f (< +nan.0 -4/3))))
1805
1806 (with-test-prefix "frac/inum"
1807 (pass-if (< 7/4 2))
1808 (pass-if (< -7/4 2))
1809 (pass-if (< -9/4 2))
1810 (pass-if (< -9/4 -2))
1811 (pass-if (eq? #f (< 9/4 2)))
1812 (pass-if (eq? #f (< 9/4 -2)))
1813 (pass-if (eq? #f (< 7/4 -2)))
1814 (pass-if (eq? #f (< -7/4 -2))))
1815
1816 (with-test-prefix "frac/bignum"
1817 (let ((x (ash 1 2048)))
1818 (pass-if (< (* 2/3 x) x))
1819 (pass-if (< (* -2/3 x) x))
1820 (pass-if (< (* -4/3 x) x))
1821 (pass-if (< (* -4/3 x) (- x)))
1822 (pass-if (eq? #f (< (* 4/3 x) x)))
1823 (pass-if (eq? #f (< (* 4/3 x) (- x))))
1824 (pass-if (eq? #f (< (* 2/3 x) (- x))))
1825 (pass-if (eq? #f (< (* -2/3 x) (- x))))))
1826
1827 (with-test-prefix "frac/flonum"
1828 (pass-if (< 2/3 0.75))
1829 (pass-if (< -2/3 0.75))
1830 (pass-if (< -4/3 0.75))
1831 (pass-if (< -4/3 -0.75))
1832 (pass-if (eq? #f (< 4/3 0.75)))
1833 (pass-if (eq? #f (< 4/3 -0.75)))
1834 (pass-if (eq? #f (< 2/3 -0.75)))
1835 (pass-if (eq? #f (< -2/3 -0.75)))
1836
1837 (pass-if (< 4/3 +inf.0))
1838 (pass-if (< -4/3 +inf.0))
1839 (pass-if (eq? #f (< 4/3 -inf.0)))
1840 (pass-if (eq? #f (< -4/3 -inf.0)))
1841
1842 (pass-if (eq? #f (< 4/3 +nan.0)))
1843 (pass-if (eq? #f (< -4/3 +nan.0))))
1844
1845 (with-test-prefix "frac/frac"
1846 (pass-if (< 2/3 6/7))
1847 (pass-if (< -2/3 6/7))
1848 (pass-if (< -4/3 6/7))
1849 (pass-if (< -4/3 -6/7))
1850 (pass-if (eq? #f (< 4/3 6/7)))
1851 (pass-if (eq? #f (< 4/3 -6/7)))
1852 (pass-if (eq? #f (< 2/3 -6/7)))
1853 (pass-if (eq? #f (< -2/3 -6/7)))))
f29b3454
DH
1854
1855;;;
1856;;; >
1857;;;
1858
7c24e528
RB
1859;; currently not tested -- implementation is trivial
1860;; (> x y) is implemented as (< y x)
1861;; FIXME: tests should probably be added in case we change implementation.
1862
f29b3454
DH
1863;;;
1864;;; <=
1865;;;
1866
7c24e528
RB
1867;; currently not tested -- implementation is trivial
1868;; (<= x y) is implemented as (not (< y x))
1869;; FIXME: tests should probably be added in case we change implementation.
1870
f29b3454
DH
1871;;;
1872;;; >=
1873;;;
1874
7c24e528
RB
1875;; currently not tested -- implementation is trivial
1876;; (>= x y) is implemented as (not (< x y))
1877;; FIXME: tests should probably be added in case we change implementation.
1878
f29b3454
DH
1879;;;
1880;;; zero?
1881;;;
1882
7c24e528
RB
1883(with-test-prefix "zero?"
1884 (expect-fail (documented? zero?))
1885 (pass-if (zero? 0))
4d332f19
DH
1886 (pass-if (not (zero? 7)))
1887 (pass-if (not (zero? -7)))
1888 (pass-if (not (zero? (+ 1 fixnum-max))))
1889 (pass-if (not (zero? (- 1 fixnum-min))))
1890 (pass-if (not (zero? 1.3)))
1891 (pass-if (not (zero? 3.1+4.2i))))
7c24e528 1892
f29b3454
DH
1893;;;
1894;;; positive?
1895;;;
1896
7c24e528
RB
1897(with-test-prefix "positive?"
1898 (expect-fail (documented? positive?))
1899 (pass-if (positive? 1))
1900 (pass-if (positive? (+ fixnum-max 1)))
1901 (pass-if (positive? 1.3))
4d332f19
DH
1902 (pass-if (not (positive? 0)))
1903 (pass-if (not (positive? -1)))
1904 (pass-if (not (positive? (- fixnum-min 1))))
1905 (pass-if (not (positive? -1.3))))
7c24e528 1906
f29b3454
DH
1907;;;
1908;;; negative?
1909;;;
1910
7c24e528
RB
1911(with-test-prefix "negative?"
1912 (expect-fail (documented? negative?))
4d332f19
DH
1913 (pass-if (not (negative? 1)))
1914 (pass-if (not (negative? (+ fixnum-max 1))))
1915 (pass-if (not (negative? 1.3)))
1916 (pass-if (not (negative? 0)))
7c24e528
RB
1917 (pass-if (negative? -1))
1918 (pass-if (negative? (- fixnum-min 1)))
1919 (pass-if (negative? -1.3)))
1920
f29b3454
DH
1921;;;
1922;;; max
1923;;;
1924
adda36ed 1925(with-test-prefix "max"
593a4c2f
KR
1926 (pass-if-exception "no args" exception:wrong-num-args
1927 (max))
1928
1929 (pass-if-exception "one complex" exception:wrong-type-arg
1930 (max 1+i))
1931
1932 (pass-if-exception "inum/complex" exception:wrong-type-arg
1933 (max 123 1+i))
1934 (pass-if-exception "big/complex" exception:wrong-type-arg
1935 (max 9999999999999999999999999999999999999999 1+i))
1936 (pass-if-exception "real/complex" exception:wrong-type-arg
1937 (max 123.0 1+i))
1938 (pass-if-exception "frac/complex" exception:wrong-type-arg
1939 (max 123/456 1+i))
1940
1941 (pass-if-exception "complex/inum" exception:wrong-type-arg
1942 (max 1+i 123))
1943 (pass-if-exception "complex/big" exception:wrong-type-arg
1944 (max 1+i 9999999999999999999999999999999999999999))
1945 (pass-if-exception "complex/real" exception:wrong-type-arg
1946 (max 1+i 123.0))
1947 (pass-if-exception "complex/frac" exception:wrong-type-arg
1948 (max 1+i 123/456))
1949
adda36ed
KR
1950 (let ((big*2 (* fixnum-max 2))
1951 (big*3 (* fixnum-max 3))
1952 (big*4 (* fixnum-max 4))
1953 (big*5 (* fixnum-max 5)))
501da403 1954
2530518e
KR
1955 (with-test-prefix "inum / frac"
1956 (pass-if (= 3 (max 3 5/2)))
1957 (pass-if (= 5/2 (max 2 5/2))))
1958
1959 (with-test-prefix "frac / inum"
1960 (pass-if (= 3 (max 5/2 3)))
1961 (pass-if (= 5/2 (max 5/2 2))))
1962
23d77957
KR
1963 (with-test-prefix "inum / real"
1964 (pass-if (nan? (max 123 +nan.0))))
1965
1966 (with-test-prefix "real / inum"
1967 (pass-if (nan? (max +nan.0 123))))
1968
2530518e
KR
1969 (with-test-prefix "big / frac"
1970 (pass-if (= big*2 (max big*2 5/2)))
1971 (pass-if (= 5/2 (max (- big*2) 5/2))))
1972
1973 (with-test-prefix "frac / big"
1974 (pass-if (= big*2 (max 5/2 big*2)))
1975 (pass-if (= 5/2 (max 5/2 (- big*2)))))
1976
23d77957
KR
1977 (with-test-prefix "big / real"
1978 (pass-if (nan? (max big*5 +nan.0)))
1979 (pass-if (= big*5 (max big*5 -inf.0)))
1980 (pass-if (= +inf.0 (max big*5 +inf.0)))
1981 (pass-if (= 1.0 (max (- big*5) 1.0)))
1982 (pass-if (inexact? (max big*5 1.0)))
1983 (pass-if (= (exact->inexact big*5) (max big*5 1.0))))
1984
1985 (with-test-prefix "real / big"
1986 (pass-if (nan? (max +nan.0 big*5)))
1987 (pass-if (= +inf.0 (max +inf.0 big*5)))
1988 (pass-if (= big*5 (max -inf.0 big*5)))
1989 (pass-if (= 1.0 (max 1.0 (- big*5))))
1990 (pass-if (inexact? (max 1.0 big*5)))
1991 (pass-if (= (exact->inexact big*5) (max 1.0 big*5))))
1992
2530518e
KR
1993 (with-test-prefix "frac / frac"
1994 (pass-if (= 2/3 (max 1/2 2/3)))
1995 (pass-if (= 2/3 (max 2/3 1/2)))
1996 (pass-if (= -1/2 (max -1/2 -2/3)))
1997 (pass-if (= -1/2 (max -2/3 -1/2))))
1998
23d77957
KR
1999 (with-test-prefix "real / real"
2000 (pass-if (nan? (max 123.0 +nan.0)))
2001 (pass-if (nan? (max +nan.0 123.0)))
2002 (pass-if (nan? (max +nan.0 +nan.0)))
2003 (pass-if (= 456.0 (max 123.0 456.0)))
2004 (pass-if (= 456.0 (max 456.0 123.0)))))
adda36ed
KR
2005
2006 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
2007 ;; sure we've avoided that
2008 (for-each (lambda (b)
2009 (pass-if (list b +inf.0)
2010 (= +inf.0 (max b +inf.0)))
2011 (pass-if (list +inf.0 b)
2012 (= +inf.0 (max b +inf.0)))
2013 (pass-if (list b -inf.0)
23d77957 2014 (= (exact->inexact b) (max b -inf.0)))
adda36ed 2015 (pass-if (list -inf.0 b)
23d77957 2016 (= (exact->inexact b) (max b -inf.0))))
adda36ed
KR
2017 (list (1- (ash 1 1024))
2018 (ash 1 1024)
2019 (1+ (ash 1 1024))
2020 (- (1- (ash 1 1024)))
2021 (- (ash 1 1024))
501da403
KR
2022 (- (1+ (ash 1 1024)))))
2023
2024 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
2025 ;; sure we've avoided that
2026 (pass-if (nan? (max (ash 1 2048) +nan.0)))
2027 (pass-if (nan? (max +nan.0 (ash 1 2048)))))
adda36ed 2028
f29b3454
DH
2029;;;
2030;;; min
2031;;;
2032
7c24e528
RB
2033;; FIXME: unfinished...
2034
2035(with-test-prefix "min"
593a4c2f
KR
2036 (pass-if-exception "no args" exception:wrong-num-args
2037 (min))
2038
2039 (pass-if-exception "one complex" exception:wrong-type-arg
2040 (min 1+i))
2041
2042 (pass-if-exception "inum/complex" exception:wrong-type-arg
2043 (min 123 1+i))
2044 (pass-if-exception "big/complex" exception:wrong-type-arg
2045 (min 9999999999999999999999999999999999999999 1+i))
2046 (pass-if-exception "real/complex" exception:wrong-type-arg
2047 (min 123.0 1+i))
2048 (pass-if-exception "frac/complex" exception:wrong-type-arg
2049 (min 123/456 1+i))
2050
2051 (pass-if-exception "complex/inum" exception:wrong-type-arg
2052 (min 1+i 123))
2053 (pass-if-exception "complex/big" exception:wrong-type-arg
2054 (min 1+i 9999999999999999999999999999999999999999))
2055 (pass-if-exception "complex/real" exception:wrong-type-arg
2056 (min 1+i 123.0))
2057 (pass-if-exception "complex/frac" exception:wrong-type-arg
2058 (min 1+i 123/456))
2059
7c24e528
RB
2060 (let ((big*2 (* fixnum-max 2))
2061 (big*3 (* fixnum-max 3))
2062 (big*4 (* fixnum-max 4))
2063 (big*5 (* fixnum-max 5)))
23d77957 2064
7f703e0d 2065 (expect-fail (documented? min))
7c24e528
RB
2066 (pass-if (= 1 (min 7 3 1 5)))
2067 (pass-if (= 1 (min 1 7 3 5)))
2068 (pass-if (= 1 (min 7 3 5 1)))
2069 (pass-if (= -7 (min 2 3 4 -2 5 -7 1 -1 4 2)))
2070 (pass-if (= -7 (min -7 2 3 4 -2 5 1 -1 4 2)))
2071 (pass-if (= -7 (min 2 3 4 -2 5 1 -1 4 2 -7)))
2072 (pass-if (= big*2 (min big*3 big*5 big*2 big*4)))
2073 (pass-if (= big*2 (min big*2 big*3 big*5 big*4)))
2074 (pass-if (= big*2 (min big*3 big*5 big*4 big*2)))
2075 (pass-if
2076 (= (- fixnum-min 1) (min 2 4 (- fixnum-min 1) 3 (* 2 fixnum-max))))
2077 (pass-if
2078 (= (- fixnum-min 1) (min (- fixnum-min 1) 2 4 3 (* 2 fixnum-max))))
2079 (pass-if
adda36ed 2080 (= (- fixnum-min 1) (min 2 4 3 (* 2 fixnum-max) (- fixnum-min 1))))
23d77957 2081
2530518e
KR
2082 (with-test-prefix "inum / frac"
2083 (pass-if (= 5/2 (min 3 5/2)))
2084 (pass-if (= 2 (min 2 5/2))))
2085
2086 (with-test-prefix "frac / inum"
2087 (pass-if (= 5/2 (min 5/2 3)))
2088 (pass-if (= 2 (min 5/2 2))))
2089
23d77957
KR
2090 (with-test-prefix "inum / real"
2091 (pass-if (nan? (min 123 +nan.0))))
2092
2093 (with-test-prefix "real / inum"
2094 (pass-if (nan? (min +nan.0 123))))
2095
2530518e
KR
2096 (with-test-prefix "big / frac"
2097 (pass-if (= 5/2 (min big*2 5/2)))
2098 (pass-if (= (- big*2) (min (- big*2) 5/2))))
2099
2100 (with-test-prefix "frac / big"
2101 (pass-if (= 5/2 (min 5/2 big*2)))
2102 (pass-if (= (- big*2) (min 5/2 (- big*2)))))
2103
23d77957
KR
2104 (with-test-prefix "big / real"
2105 (pass-if (nan? (min big*5 +nan.0)))
2106 (pass-if (= big*5 (min big*5 +inf.0)))
2107 (pass-if (= -inf.0 (min big*5 -inf.0)))
2108 (pass-if (= 1.0 (min big*5 1.0)))
2109 (pass-if (inexact? (min (- big*5) 1.0)))
2110 (pass-if (= (exact->inexact (- big*5)) (min (- big*5) 1.0))))
2111
2112 (with-test-prefix "real / big"
2113 (pass-if (nan? (min +nan.0 big*5)))
2114 (pass-if (= big*5 (min +inf.0 big*5)))
2115 (pass-if (= -inf.0 (min -inf.0 big*5)))
2116 (pass-if (= 1.0 (min 1.0 big*5)))
2117 (pass-if (inexact? (min 1.0 (- big*5))))
2118 (pass-if (= (exact->inexact (- big*5)) (min 1.0 (- big*5)))))
2119
2530518e
KR
2120 (with-test-prefix "frac / frac"
2121 (pass-if (= 1/2 (min 1/2 2/3)))
2122 (pass-if (= 1/2 (min 2/3 1/2)))
2123 (pass-if (= -2/3 (min -1/2 -2/3)))
2124 (pass-if (= -2/3 (min -2/3 -1/2))))
2125
23d77957
KR
2126 (with-test-prefix "real / real"
2127 (pass-if (nan? (min 123.0 +nan.0)))
2128 (pass-if (nan? (min +nan.0 123.0)))
2129 (pass-if (nan? (min +nan.0 +nan.0)))
2130 (pass-if (= 123.0 (min 123.0 456.0)))
2131 (pass-if (= 123.0 (min 456.0 123.0)))))
2132
2133
adda36ed
KR
2134 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
2135 ;; sure we've avoided that
2136 (for-each (lambda (b)
2137 (pass-if (list b +inf.0)
23d77957 2138 (= (exact->inexact b) (min b +inf.0)))
adda36ed 2139 (pass-if (list +inf.0 b)
23d77957 2140 (= (exact->inexact b) (min b +inf.0)))
adda36ed
KR
2141 (pass-if (list b -inf.0)
2142 (= -inf.0 (min b -inf.0)))
2143 (pass-if (list -inf.0 b)
2144 (= -inf.0 (min b -inf.0))))
2145 (list (1- (ash 1 1024))
2146 (ash 1 1024)
2147 (1+ (ash 1 1024))
2148 (- (1- (ash 1 1024)))
2149 (- (ash 1 1024))
501da403
KR
2150 (- (1+ (ash 1 1024)))))
2151
2152 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
2153 ;; sure we've avoided that
2154 (pass-if (nan? (min (- (ash 1 2048)) (- +nan.0))))
2155 (pass-if (nan? (min (- +nan.0) (- (ash 1 2048))))))
adda36ed 2156
f29b3454
DH
2157;;;
2158;;; +
2159;;;
2160
2161(with-test-prefix "+"
2162
2163 (expect-fail "documented?"
2164 (documented? +))
2165
2166 (with-test-prefix "wrong type argument"
2167
2168 (pass-if-exception "1st argument string"
2169 exception:wrong-type-arg
2170 (+ "1" 2))
2171
2172 (pass-if-exception "2nd argument bool"
2173 exception:wrong-type-arg
2174 (+ 1 #f))))
2175;;;
2176;;; -
2177;;;
2178
072e6de2
KR
2179(with-test-prefix "-"
2180
2181 (pass-if "-inum - +bignum"
2182 (= #x-100000000000000000000000000000001
ef016629
KR
2183 (- -1 #x100000000000000000000000000000000)))
2184
2185 (pass-if "big - inum"
2186 (= #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
2187 (- #x100000000000000000000000000000000 1)))
2188
2189 (pass-if "big - -inum"
2190 (= #x100000000000000000000000000000001
2191 (- #x100000000000000000000000000000000 -1))))
072e6de2 2192
f29b3454
DH
2193;;;
2194;;; *
2195;;;
2196
65ea251e
KR
2197(with-test-prefix "*"
2198
2199 (pass-if "complex * bignum"
2200 (let ((big (ash 1 90)))
2201 (= (make-rectangular big big)
2202 (* 1+1i big)))))
2203
f29b3454
DH
2204;;;
2205;;; /
2206;;;
2207
1b3a7932
DH
2208(with-test-prefix "/"
2209
2210 (expect-fail "documented?"
2211 (documented? /))
2212
2213 (with-test-prefix "division by zero"
2214
2215 (pass-if-exception "(/ 0)"
2216 exception:numerical-overflow
2217 (/ 0))
2218
cdf52e3d
MV
2219 (pass-if "(/ 0.0)"
2220 (= +inf.0 (/ 0.0)))
80074d77 2221
1b3a7932
DH
2222 (pass-if-exception "(/ 1 0)"
2223 exception:numerical-overflow
80074d77
DH
2224 (/ 1 0))
2225
cdf52e3d
MV
2226 (pass-if "(/ 1 0.0)"
2227 (= +inf.0 (/ 1 0.0)))
80074d77
DH
2228
2229 (pass-if-exception "(/ bignum 0)"
2230 exception:numerical-overflow
2231 (/ (+ fixnum-max 1) 0))
2232
cdf52e3d
MV
2233 (pass-if "(/ bignum 0.0)"
2234 (= +inf.0 (/ (+ fixnum-max 1) 0.0)))
80074d77
DH
2235
2236 (pass-if-exception "(/ 1.0 0)"
2237 exception:numerical-overflow
2238 (/ 1.0 0))
2239
cdf52e3d
MV
2240 (pass-if "(/ 1.0 0.0)"
2241 (= +inf.0 (/ 1.0 0.0)))
80074d77
DH
2242
2243 (pass-if-exception "(/ +i 0)"
2244 exception:numerical-overflow
2245 (/ +i 0))
2246
cdf52e3d
MV
2247 (pass-if "(/ +i 0.0)"
2248 (= +inf.0 (imag-part (/ +i 0.0)))))
469b963c
MV
2249
2250 (with-test-prefix "complex division"
2251
2252 (pass-if "(/ 3+4i)"
2253 (= (/ 3+4i) 0.12-0.16i))
2254
2255 (pass-if "(/ 4+3i)"
2256 (= (/ 4+3i) 0.16-0.12i))
2257
2258 (pass-if "(/ 25+125i 3+4i)"
2259 (= (/ 25+125i 3+4i) 23.0+11.0i))
2260
2261 (pass-if "(/ 25+125i 4+3i)"
2262 (= (/ 25+125i 4+3i) 19.0+17.0i))
2263
2264 (pass-if "(/ 25 3+4i)"
2265 (= (/ 25 3+4i) 3.0-4.0i))
2266
2267 (pass-if "(/ 25 4+3i)"
2268 (= (/ 25 4+3i) 4.0-3.0i))
2269
2270 (pass-if "(/ 1e200+1e200i)"
2271 (= (/ 1e200+1e200i) 5.0e-201-5.0e-201i))))
1b3a7932 2272
f29b3454
DH
2273;;;
2274;;; truncate
2275;;;
2276
14a6784c
KR
2277(with-test-prefix "truncate"
2278 (pass-if (= 1 (truncate 1.75)))
2279 (pass-if (= 1 (truncate 1.5)))
2280 (pass-if (= 1 (truncate 1.25)))
2281 (pass-if (= 0 (truncate 0.75)))
2282 (pass-if (= 0 (truncate 0.5)))
2283 (pass-if (= 0 (truncate 0.0)))
2284 (pass-if (= 0 (truncate -0.5)))
2285 (pass-if (= -1 (truncate -1.25)))
2286 (pass-if (= -1 (truncate -1.5))))
2287
f29b3454
DH
2288;;;
2289;;; round
2290;;;
2291
14a6784c
KR
2292(with-test-prefix "round"
2293 (pass-if (= 2 (round 1.75)))
2294 (pass-if (= 2 (round 1.5)))
2295 (pass-if (= 1 (round 1.25)))
2296 (pass-if (= 1 (round 0.75)))
2297 (pass-if (= 0 (round 0.5)))
2298 (pass-if (= 0 (round 0.0)))
2299 (pass-if (= 0 (round -0.5)))
2300 (pass-if (= -1 (round -1.25)))
abff733b
KR
2301 (pass-if (= -2 (round -1.5)))
2302
2303 (with-test-prefix "inum"
2304 (pass-if "0"
2305 (and (= 0 (round 0))
2306 (exact? (round 0))))
2307
2308 (pass-if "1"
2309 (and (= 1 (round 1))
2310 (exact? (round 1))))
2311
2312 (pass-if "-1"
2313 (and (= -1 (round -1))
2314 (exact? (round -1)))))
2315
2316 (with-test-prefix "bignum"
2317 (let ((x (1+ most-positive-fixnum)))
2318 (pass-if "(1+ most-positive-fixnum)"
2319 (and (= x (round x))
2320 (exact? (round x)))))
2321
2322 (let ((x (1- most-negative-fixnum)))
2323 (pass-if "(1- most-negative-fixnum)"
2324 (and (= x (round x))
2325 (exact? (round x))))))
2326
2327 (with-test-prefix "real"
2328 (pass-if "0.0"
2329 (and (= 0.0 (round 0.0))
2330 (inexact? (round 0.0))))
2331
2332 (pass-if "1.0"
2333 (and (= 1.0 (round 1.0))
2334 (inexact? (round 1.0))))
2335
2336 (pass-if "-1.0"
2337 (and (= -1.0 (round -1.0))
2338 (inexact? (round -1.0))))
2339
2340 (pass-if "-3.1"
2341 (and (= -3.0 (round -3.1))
2342 (inexact? (round -3.1))))
2343
2344 (pass-if "3.1"
2345 (and (= 3.0 (round 3.1))
2346 (inexact? (round 3.1))))
2347
2348 (pass-if "3.9"
2349 (and (= 4.0 (round 3.9))
2350 (inexact? (round 3.9))))
2351
2352 (pass-if "-3.9"
2353 (and (= -4.0 (round -3.9))
2354 (inexact? (round -3.9))))
2355
2356 (pass-if "1.5"
2357 (and (= 2.0 (round 1.5))
2358 (inexact? (round 1.5))))
2359
2360 (pass-if "2.5"
2361 (and (= 2.0 (round 2.5))
2362 (inexact? (round 2.5))))
2363
2364 (pass-if "3.5"
2365 (and (= 4.0 (round 3.5))
2366 (inexact? (round 3.5))))
2367
2368 (pass-if "-1.5"
2369 (and (= -2.0 (round -1.5))
2370 (inexact? (round -1.5))))
2371
2372 (pass-if "-2.5"
2373 (and (= -2.0 (round -2.5))
2374 (inexact? (round -2.5))))
2375
2376 (pass-if "-3.5"
2377 (and (= -4.0 (round -3.5))
2378 (inexact? (round -3.5))))
2379
2380 ;; prior to guile 1.6.5, on an IEEE system an inexact 2^53-1 (ie. a
2381 ;; float with mantissa all ones) came out as 2^53 from `round' (except
2382 ;; on i386 and m68k systems using the coprocessor and optimizing, where
2383 ;; extra precision hid the problem)
2384 (pass-if "2^53-1"
2385 (let ((x (exact->inexact (1- (ash 1 53)))))
2386 (and (= x (round x))
2387 (inexact? (round x)))))
2388 (pass-if "-(2^53-1)"
2389 (let ((x (exact->inexact (- (1- (ash 1 53))))))
2390 (and (= x (round x))
2391 (inexact? (round x)))))))
14a6784c 2392
f29b3454
DH
2393;;;
2394;;; exact->inexact
2395;;;
2396
a1fb3b1c
KR
2397(with-test-prefix "exact->inexact"
2398
2399 ;; Test "(exact->inexact n)", expect "want".
2400 ;; "i" is a index, for diagnostic purposes.
2401 (define (try-i i n want)
2402 (with-test-prefix (list i n want)
2403 (with-test-prefix "pos"
2404 (let ((got (exact->inexact n)))
2405 (pass-if "inexact?" (inexact? got))
2406 (pass-if (list "=" got) (= want got))))
2407 (set! n (- n))
2408 (set! want (- want))
2409 (with-test-prefix "neg"
2410 (let ((got (exact->inexact n)))
2411 (pass-if "inexact?" (inexact? got))
2412 (pass-if (list "=" got) (= want got))))))
2413
2414 (with-test-prefix "2^i, no round"
2415 (do ((i 0 (1+ i))
2416 (n 1 (* 2 n))
2417 (want 1.0 (* 2.0 want)))
2418 ((> i 100))
2419 (try-i i n want)))
2420
2421 (with-test-prefix "2^i+1, no round"
2422 (do ((i 1 (1+ i))
2423 (n 3 (1- (* 2 n)))
2424 (want 3.0 (- (* 2.0 want) 1.0)))
2425 ((>= i dbl-mant-dig))
2426 (try-i i n want)))
2427
2428 (with-test-prefix "(2^i+1)*2^100, no round"
2429 (do ((i 1 (1+ i))
2430 (n 3 (1- (* 2 n)))
2431 (want 3.0 (- (* 2.0 want) 1.0)))
2432 ((>= i dbl-mant-dig))
2433 (try-i i (ash n 100) (ash-flo want 100))))
2434
2435 ;; bit pattern: 1111....11100.00
2436 ;; <-mantdig-><-i->
2437 ;;
2438 (with-test-prefix "mantdig ones then zeros, no rounding"
2439 (do ((i 0 (1+ i))
2440 (n (- (ash 1 dbl-mant-dig) 1) (* 2 n))
2441 (want (- (ash-flo 1.0 dbl-mant-dig) 1.0) (* 2.0 want)))
2442 ((> i 100))
2443 (try-i i n want)))
2444
2445 ;; bit pattern: 1111....111011..1
2446 ;; <-mantdig-> <-i->
2447 ;; This sort of value was incorrectly rounded upwards in Guile 1.6.4 when
2448 ;; i >= 11 (that's when the total is 65 or more bits).
2449 ;;
2450 (with-test-prefix "mantdig ones then 011..11, round down"
2451 (do ((i 0 (1+ i))
2452 (n (- (ash 1 (+ 1 dbl-mant-dig)) 2) (+ 1 (* 2 n)))
2453 (want (- (ash-flo 1.0 (+ 1 dbl-mant-dig)) 2.0) (* 2.0 want)))
2454 ((> i 100))
2455 (try-i i n want)))
2456
2457 ;; bit pattern: 1111....111100..001
2458 ;; <-mantdig-> <--i->
2459 ;;
2460 (with-test-prefix "mantdig ones then 100..001, round up"
2461 (do ((i 0 (1+ i))
2462 (n (- (ash 1 (+ 2 dbl-mant-dig)) 1) (1- (* 2 n)))
2463 (want (ash-flo 1.0 (+ 2 dbl-mant-dig)) (* 2.0 want)))
2464 ((> i 100))
2465 (try-i i n want)))
2466
2467 ;; bit pattern: 1000....000100..001
2468 ;; <-mantdig-> <--i->
2469 ;;
2470 (with-test-prefix "2^mantdig then 100..001, round up"
2471 (do ((i 0 (1+ i))
2472 (n (- (ash 1 (+ 2 dbl-mant-dig)) 1) (1- (* 2 n)))
2473 (want (+ (ash-flo 1.0 (+ 2 dbl-mant-dig)) 4.0) (* 2.0 want)))
2474 ((> i 100))
2475 (try-i i n want))))
2476
f29b3454
DH
2477;;;
2478;;; floor
2479;;;
2480
2481;;;
2482;;; ceiling
2483;;;
2484
46f2c0f1
RB
2485;;;
2486;;; expt
2487;;;
2488
2489(with-test-prefix "expt"
2490 (pass-if "(= 1 (expt 0 0))" (= 1 (expt 0 0)))
2491 (pass-if "(= 1 (expt 0 0.0))" (= 1 (expt 0 0.0)))
2492 (pass-if "(= 1 (expt 0.0 0))" (= 1 (expt 0.0 0)))
2493 (pass-if "(= 1 (expt 0.0 0.0))" (= 1 (expt 0.0 0.0))))
2494
14a6784c
KR
2495;;;
2496;;; asinh
2497;;;
2498
2499(with-test-prefix "asinh"
2500 (pass-if (= 0 (asinh 0))))
2501
2502;;;
2503;;; acosh
2504;;;
2505
2506(with-test-prefix "acosh"
2507 (pass-if (= 0 (acosh 1))))
2508
2509;;;
2510;;; atanh
2511;;;
2512
2513(with-test-prefix "atanh"
2514 (pass-if (= 0 (atanh 0))))
2515
f29b3454
DH
2516;;;
2517;;; make-rectangular
2518;;;
2519
2520;;;
2521;;; make-polar
2522;;;
2523
d40681ec
KR
2524(with-test-prefix "make-polar"
2525 (define pi 3.14159265358979323846)
2526 (define (almost= x y)
2527 (> 0.01 (magnitude (- x y))))
2528
2529 (pass-if (= 0 (make-polar 0 0)))
2530 (pass-if (= 0 (make-polar 0 123.456)))
2531 (pass-if (= 1 (make-polar 1 0)))
2532 (pass-if (= -1 (make-polar -1 0)))
2533
2534 (pass-if (almost= 0+i (make-polar 1 (* 0.5 pi))))
2535 (pass-if (almost= -1 (make-polar 1 (* 1.0 pi))))
2536 (pass-if (almost= 0-i (make-polar 1 (* 1.5 pi))))
2537 (pass-if (almost= 1 (make-polar 1 (* 2.0 pi)))))
2538
f29b3454
DH
2539;;;
2540;;; real-part
2541;;;
2542
2543;;;
2544;;; imag-part
2545;;;
2546
2547;;;
2548;;; magnitude
2549;;;
2550
d40681ec
KR
2551(with-test-prefix "magnitude"
2552 (pass-if (= 0 (magnitude 0)))
2553 (pass-if (= 1 (magnitude 1)))
2554 (pass-if (= 1 (magnitude -1)))
2555 (pass-if (= 1 (magnitude 0+i)))
2556 (pass-if (= 1 (magnitude 0-i)))
2557 (pass-if (= 5 (magnitude 3+4i)))
2558 (pass-if (= 5 (magnitude 3-4i)))
2559 (pass-if (= 5 (magnitude -3+4i)))
2560 (pass-if (= 5 (magnitude -3-4i))))
2561
f29b3454
DH
2562;;;
2563;;; angle
2564;;;
2565
cfc9fc1c
KR
2566(with-test-prefix "angle"
2567 (define pi 3.14159265358979323846)
2568 (define (almost= x y)
2569 (> 0.01 (magnitude (- x y))))
2570
2571 (pass-if "inum +ve" (= 0 (angle 1)))
2572 (pass-if "inum -ve" (almost= pi (angle -1)))
2573
2574 (pass-if "bignum +ve" (= 0 (angle (1+ fixnum-max))))
2575 (pass-if "bignum -ve" (almost= pi (angle (1- fixnum-min))))
2576
2577 (pass-if "flonum +ve" (= 0 (angle 1.5)))
2578 (pass-if "flonum -ve" (almost= pi (angle -1.5))))
2579
f29b3454
DH
2580;;;
2581;;; inexact->exact
2582;;;
300c6a76 2583
1259cb26
KR
2584(with-test-prefix "inexact->exact"
2585
9dd9857f 2586 (pass-if-exception "+inf" exception:out-of-range
a409f865 2587 (inexact->exact +inf.0))
1259cb26 2588
9dd9857f 2589 (pass-if-exception "-inf" exception:out-of-range
a409f865 2590 (inexact->exact -inf.0))
1259cb26 2591
9dd9857f 2592 (pass-if-exception "nan" exception:out-of-range
a409f865 2593 (inexact->exact +nan.0))
1259cb26
KR
2594
2595 (with-test-prefix "2.0**i to exact and back"
2596 (do ((i 0 (1+ i))
2597 (n 1.0 (* 2.0 n)))
2598 ((> i 100))
2599 (pass-if (list i n)
2600 (= n (inexact->exact (exact->inexact n)))))))
2601
a04a3604
KR
2602;;;
2603;;; integer-length
2604;;;
2605
2606(with-test-prefix "integer-length"
2607
2608 (with-test-prefix "-2^i, ...11100..00"
2609 (do ((n -1 (ash n 1))
2610 (i 0 (1+ i)))
2611 ((> i 256))
2612 (pass-if (list n "expect" i)
2613 (= i (integer-length n)))))
2614
2615 (with-test-prefix "-2^i+1 ...11100..01"
2616 (do ((n -3 (logxor 3 (ash n 1)))
2617 (i 2 (1+ i)))
2618 ((> i 256))
2619 (pass-if n
2620 (= i (integer-length n)))))
2621
2622 (with-test-prefix "-2^i-1 ...111011..11"
2623 (do ((n -2 (1+ (ash n 1)))
2624 (i 1 (1+ i)))
2625 ((> i 256))
2626 (pass-if n
2627 (= i (integer-length n))))))
2628
abff733b
KR
2629;;;
2630;;; logbit?
2631;;;
2632
2633(with-test-prefix "logbit?"
2634 (pass-if (eq? #f (logbit? 0 0)))
2635 (pass-if (eq? #f (logbit? 1 0)))
2636 (pass-if (eq? #f (logbit? 31 0)))
2637 (pass-if (eq? #f (logbit? 32 0)))
2638 (pass-if (eq? #f (logbit? 33 0)))
2639 (pass-if (eq? #f (logbit? 63 0)))
2640 (pass-if (eq? #f (logbit? 64 0)))
2641 (pass-if (eq? #f (logbit? 65 0)))
2642
2643 ;; prior to guile 1.6.5, testing bit 32, 64 etc of value 1 would wrap
2644 ;; around and return #t where it ought to be #f
2645 (pass-if (eq? #t (logbit? 0 1)))
2646 (pass-if (eq? #f (logbit? 1 1)))
2647 (pass-if (eq? #f (logbit? 31 1)))
2648 (pass-if (eq? #f (logbit? 32 1)))
2649 (pass-if (eq? #f (logbit? 33 1)))
2650 (pass-if (eq? #f (logbit? 63 1)))
2651 (pass-if (eq? #f (logbit? 64 1)))
2652 (pass-if (eq? #f (logbit? 65 1)))
2653 (pass-if (eq? #f (logbit? 128 1)))
2654
2655 (pass-if (eq? #t (logbit? 0 -1)))
2656 (pass-if (eq? #t (logbit? 1 -1)))
2657 (pass-if (eq? #t (logbit? 31 -1)))
2658 (pass-if (eq? #t (logbit? 32 -1)))
2659 (pass-if (eq? #t (logbit? 33 -1)))
2660 (pass-if (eq? #t (logbit? 63 -1)))
2661 (pass-if (eq? #t (logbit? 64 -1)))
2662 (pass-if (eq? #t (logbit? 65 -1))))
2663
300c6a76
KR
2664;;;
2665;;; logcount
2666;;;
2667
2668(with-test-prefix "logcount"
2669
2670 (with-test-prefix "-2^i, meaning ...11100..00"
2671 (do ((n -1 (ash n 1))
2672 (i 0 (1+ i)))
2673 ((> i 256))
795c0bae
KR
2674 (pass-if n
2675 (= i (logcount n)))))
2676
2677 (with-test-prefix "2^i"
2678 (do ((n 1 (ash n 1))
2679 (i 0 (1+ i)))
2680 ((> i 256))
2681 (pass-if n
2682 (= 1 (logcount n)))))
2683
2684 (with-test-prefix "2^i-1"
2685 (do ((n 0 (1+ (ash n 1)))
2686 (i 0 (1+ i)))
2687 ((> i 256))
300c6a76
KR
2688 (pass-if n
2689 (= i (logcount n))))))
795c0bae 2690
1ec2dd6f
KR
2691;;;
2692;;; lognot
2693;;;
2694
2695(with-test-prefix "lognot"
2696 (pass-if (= -1 (lognot 0)))
2697 (pass-if (= 0 (lognot -1)))
2698 (pass-if (= -2 (lognot 1)))
2699 (pass-if (= 1 (lognot -2)))
2700
2701 (pass-if (= #x-100000000000000000000000000000000
2702 (lognot #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)))
2703 (pass-if (= #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
2704 (lognot #x-100000000000000000000000000000000))))