Add SIGSYS.
[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
23d77957
KR
1955 (with-test-prefix "inum / real"
1956 (pass-if (nan? (max 123 +nan.0))))
1957
1958 (with-test-prefix "real / inum"
1959 (pass-if (nan? (max +nan.0 123))))
1960
1961 (with-test-prefix "big / real"
1962 (pass-if (nan? (max big*5 +nan.0)))
1963 (pass-if (= big*5 (max big*5 -inf.0)))
1964 (pass-if (= +inf.0 (max big*5 +inf.0)))
1965 (pass-if (= 1.0 (max (- big*5) 1.0)))
1966 (pass-if (inexact? (max big*5 1.0)))
1967 (pass-if (= (exact->inexact big*5) (max big*5 1.0))))
1968
1969 (with-test-prefix "real / big"
1970 (pass-if (nan? (max +nan.0 big*5)))
1971 (pass-if (= +inf.0 (max +inf.0 big*5)))
1972 (pass-if (= big*5 (max -inf.0 big*5)))
1973 (pass-if (= 1.0 (max 1.0 (- big*5))))
1974 (pass-if (inexact? (max 1.0 big*5)))
1975 (pass-if (= (exact->inexact big*5) (max 1.0 big*5))))
1976
1977 (with-test-prefix "real / real"
1978 (pass-if (nan? (max 123.0 +nan.0)))
1979 (pass-if (nan? (max +nan.0 123.0)))
1980 (pass-if (nan? (max +nan.0 +nan.0)))
1981 (pass-if (= 456.0 (max 123.0 456.0)))
1982 (pass-if (= 456.0 (max 456.0 123.0)))))
adda36ed
KR
1983
1984 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
1985 ;; sure we've avoided that
1986 (for-each (lambda (b)
1987 (pass-if (list b +inf.0)
1988 (= +inf.0 (max b +inf.0)))
1989 (pass-if (list +inf.0 b)
1990 (= +inf.0 (max b +inf.0)))
1991 (pass-if (list b -inf.0)
23d77957 1992 (= (exact->inexact b) (max b -inf.0)))
adda36ed 1993 (pass-if (list -inf.0 b)
23d77957 1994 (= (exact->inexact b) (max b -inf.0))))
adda36ed
KR
1995 (list (1- (ash 1 1024))
1996 (ash 1 1024)
1997 (1+ (ash 1 1024))
1998 (- (1- (ash 1 1024)))
1999 (- (ash 1 1024))
501da403
KR
2000 (- (1+ (ash 1 1024)))))
2001
2002 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
2003 ;; sure we've avoided that
2004 (pass-if (nan? (max (ash 1 2048) +nan.0)))
2005 (pass-if (nan? (max +nan.0 (ash 1 2048)))))
adda36ed 2006
f29b3454
DH
2007;;;
2008;;; min
2009;;;
2010
7c24e528
RB
2011;; FIXME: unfinished...
2012
2013(with-test-prefix "min"
593a4c2f
KR
2014 (pass-if-exception "no args" exception:wrong-num-args
2015 (min))
2016
2017 (pass-if-exception "one complex" exception:wrong-type-arg
2018 (min 1+i))
2019
2020 (pass-if-exception "inum/complex" exception:wrong-type-arg
2021 (min 123 1+i))
2022 (pass-if-exception "big/complex" exception:wrong-type-arg
2023 (min 9999999999999999999999999999999999999999 1+i))
2024 (pass-if-exception "real/complex" exception:wrong-type-arg
2025 (min 123.0 1+i))
2026 (pass-if-exception "frac/complex" exception:wrong-type-arg
2027 (min 123/456 1+i))
2028
2029 (pass-if-exception "complex/inum" exception:wrong-type-arg
2030 (min 1+i 123))
2031 (pass-if-exception "complex/big" exception:wrong-type-arg
2032 (min 1+i 9999999999999999999999999999999999999999))
2033 (pass-if-exception "complex/real" exception:wrong-type-arg
2034 (min 1+i 123.0))
2035 (pass-if-exception "complex/frac" exception:wrong-type-arg
2036 (min 1+i 123/456))
2037
7c24e528
RB
2038 (let ((big*2 (* fixnum-max 2))
2039 (big*3 (* fixnum-max 3))
2040 (big*4 (* fixnum-max 4))
2041 (big*5 (* fixnum-max 5)))
23d77957 2042
7c24e528
RB
2043 (expect-fail (documented? max))
2044 (pass-if (= 1 (min 7 3 1 5)))
2045 (pass-if (= 1 (min 1 7 3 5)))
2046 (pass-if (= 1 (min 7 3 5 1)))
2047 (pass-if (= -7 (min 2 3 4 -2 5 -7 1 -1 4 2)))
2048 (pass-if (= -7 (min -7 2 3 4 -2 5 1 -1 4 2)))
2049 (pass-if (= -7 (min 2 3 4 -2 5 1 -1 4 2 -7)))
2050 (pass-if (= big*2 (min big*3 big*5 big*2 big*4)))
2051 (pass-if (= big*2 (min big*2 big*3 big*5 big*4)))
2052 (pass-if (= big*2 (min big*3 big*5 big*4 big*2)))
2053 (pass-if
2054 (= (- fixnum-min 1) (min 2 4 (- fixnum-min 1) 3 (* 2 fixnum-max))))
2055 (pass-if
2056 (= (- fixnum-min 1) (min (- fixnum-min 1) 2 4 3 (* 2 fixnum-max))))
2057 (pass-if
adda36ed 2058 (= (- fixnum-min 1) (min 2 4 3 (* 2 fixnum-max) (- fixnum-min 1))))
23d77957
KR
2059
2060 (with-test-prefix "inum / real"
2061 (pass-if (nan? (min 123 +nan.0))))
2062
2063 (with-test-prefix "real / inum"
2064 (pass-if (nan? (min +nan.0 123))))
2065
2066 (with-test-prefix "big / real"
2067 (pass-if (nan? (min big*5 +nan.0)))
2068 (pass-if (= big*5 (min big*5 +inf.0)))
2069 (pass-if (= -inf.0 (min big*5 -inf.0)))
2070 (pass-if (= 1.0 (min big*5 1.0)))
2071 (pass-if (inexact? (min (- big*5) 1.0)))
2072 (pass-if (= (exact->inexact (- big*5)) (min (- big*5) 1.0))))
2073
2074 (with-test-prefix "real / big"
2075 (pass-if (nan? (min +nan.0 big*5)))
2076 (pass-if (= big*5 (min +inf.0 big*5)))
2077 (pass-if (= -inf.0 (min -inf.0 big*5)))
2078 (pass-if (= 1.0 (min 1.0 big*5)))
2079 (pass-if (inexact? (min 1.0 (- big*5))))
2080 (pass-if (= (exact->inexact (- big*5)) (min 1.0 (- big*5)))))
2081
2082 (with-test-prefix "real / real"
2083 (pass-if (nan? (min 123.0 +nan.0)))
2084 (pass-if (nan? (min +nan.0 123.0)))
2085 (pass-if (nan? (min +nan.0 +nan.0)))
2086 (pass-if (= 123.0 (min 123.0 456.0)))
2087 (pass-if (= 123.0 (min 456.0 123.0)))))
2088
2089
adda36ed
KR
2090 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
2091 ;; sure we've avoided that
2092 (for-each (lambda (b)
2093 (pass-if (list b +inf.0)
23d77957 2094 (= (exact->inexact b) (min b +inf.0)))
adda36ed 2095 (pass-if (list +inf.0 b)
23d77957 2096 (= (exact->inexact b) (min b +inf.0)))
adda36ed
KR
2097 (pass-if (list b -inf.0)
2098 (= -inf.0 (min b -inf.0)))
2099 (pass-if (list -inf.0 b)
2100 (= -inf.0 (min b -inf.0))))
2101 (list (1- (ash 1 1024))
2102 (ash 1 1024)
2103 (1+ (ash 1 1024))
2104 (- (1- (ash 1 1024)))
2105 (- (ash 1 1024))
501da403
KR
2106 (- (1+ (ash 1 1024)))))
2107
2108 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
2109 ;; sure we've avoided that
2110 (pass-if (nan? (min (- (ash 1 2048)) (- +nan.0))))
2111 (pass-if (nan? (min (- +nan.0) (- (ash 1 2048))))))
adda36ed 2112
f29b3454
DH
2113;;;
2114;;; +
2115;;;
2116
2117(with-test-prefix "+"
2118
2119 (expect-fail "documented?"
2120 (documented? +))
2121
2122 (with-test-prefix "wrong type argument"
2123
2124 (pass-if-exception "1st argument string"
2125 exception:wrong-type-arg
2126 (+ "1" 2))
2127
2128 (pass-if-exception "2nd argument bool"
2129 exception:wrong-type-arg
2130 (+ 1 #f))))
2131;;;
2132;;; -
2133;;;
2134
072e6de2
KR
2135(with-test-prefix "-"
2136
2137 (pass-if "-inum - +bignum"
2138 (= #x-100000000000000000000000000000001
ef016629
KR
2139 (- -1 #x100000000000000000000000000000000)))
2140
2141 (pass-if "big - inum"
2142 (= #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
2143 (- #x100000000000000000000000000000000 1)))
2144
2145 (pass-if "big - -inum"
2146 (= #x100000000000000000000000000000001
2147 (- #x100000000000000000000000000000000 -1))))
072e6de2 2148
f29b3454
DH
2149;;;
2150;;; *
2151;;;
2152
65ea251e
KR
2153(with-test-prefix "*"
2154
2155 (pass-if "complex * bignum"
2156 (let ((big (ash 1 90)))
2157 (= (make-rectangular big big)
2158 (* 1+1i big)))))
2159
f29b3454
DH
2160;;;
2161;;; /
2162;;;
2163
1b3a7932
DH
2164(with-test-prefix "/"
2165
2166 (expect-fail "documented?"
2167 (documented? /))
2168
2169 (with-test-prefix "division by zero"
2170
2171 (pass-if-exception "(/ 0)"
2172 exception:numerical-overflow
2173 (/ 0))
2174
cdf52e3d
MV
2175 (pass-if "(/ 0.0)"
2176 (= +inf.0 (/ 0.0)))
80074d77 2177
1b3a7932
DH
2178 (pass-if-exception "(/ 1 0)"
2179 exception:numerical-overflow
80074d77
DH
2180 (/ 1 0))
2181
cdf52e3d
MV
2182 (pass-if "(/ 1 0.0)"
2183 (= +inf.0 (/ 1 0.0)))
80074d77
DH
2184
2185 (pass-if-exception "(/ bignum 0)"
2186 exception:numerical-overflow
2187 (/ (+ fixnum-max 1) 0))
2188
cdf52e3d
MV
2189 (pass-if "(/ bignum 0.0)"
2190 (= +inf.0 (/ (+ fixnum-max 1) 0.0)))
80074d77
DH
2191
2192 (pass-if-exception "(/ 1.0 0)"
2193 exception:numerical-overflow
2194 (/ 1.0 0))
2195
cdf52e3d
MV
2196 (pass-if "(/ 1.0 0.0)"
2197 (= +inf.0 (/ 1.0 0.0)))
80074d77
DH
2198
2199 (pass-if-exception "(/ +i 0)"
2200 exception:numerical-overflow
2201 (/ +i 0))
2202
cdf52e3d
MV
2203 (pass-if "(/ +i 0.0)"
2204 (= +inf.0 (imag-part (/ +i 0.0)))))
469b963c
MV
2205
2206 (with-test-prefix "complex division"
2207
2208 (pass-if "(/ 3+4i)"
2209 (= (/ 3+4i) 0.12-0.16i))
2210
2211 (pass-if "(/ 4+3i)"
2212 (= (/ 4+3i) 0.16-0.12i))
2213
2214 (pass-if "(/ 25+125i 3+4i)"
2215 (= (/ 25+125i 3+4i) 23.0+11.0i))
2216
2217 (pass-if "(/ 25+125i 4+3i)"
2218 (= (/ 25+125i 4+3i) 19.0+17.0i))
2219
2220 (pass-if "(/ 25 3+4i)"
2221 (= (/ 25 3+4i) 3.0-4.0i))
2222
2223 (pass-if "(/ 25 4+3i)"
2224 (= (/ 25 4+3i) 4.0-3.0i))
2225
2226 (pass-if "(/ 1e200+1e200i)"
2227 (= (/ 1e200+1e200i) 5.0e-201-5.0e-201i))))
1b3a7932 2228
f29b3454
DH
2229;;;
2230;;; truncate
2231;;;
2232
14a6784c
KR
2233(with-test-prefix "truncate"
2234 (pass-if (= 1 (truncate 1.75)))
2235 (pass-if (= 1 (truncate 1.5)))
2236 (pass-if (= 1 (truncate 1.25)))
2237 (pass-if (= 0 (truncate 0.75)))
2238 (pass-if (= 0 (truncate 0.5)))
2239 (pass-if (= 0 (truncate 0.0)))
2240 (pass-if (= 0 (truncate -0.5)))
2241 (pass-if (= -1 (truncate -1.25)))
2242 (pass-if (= -1 (truncate -1.5))))
2243
f29b3454
DH
2244;;;
2245;;; round
2246;;;
2247
14a6784c
KR
2248(with-test-prefix "round"
2249 (pass-if (= 2 (round 1.75)))
2250 (pass-if (= 2 (round 1.5)))
2251 (pass-if (= 1 (round 1.25)))
2252 (pass-if (= 1 (round 0.75)))
2253 (pass-if (= 0 (round 0.5)))
2254 (pass-if (= 0 (round 0.0)))
2255 (pass-if (= 0 (round -0.5)))
2256 (pass-if (= -1 (round -1.25)))
2257 (pass-if (= -2 (round -1.5))))
2258
f29b3454
DH
2259;;;
2260;;; exact->inexact
2261;;;
2262
a1fb3b1c
KR
2263(with-test-prefix "exact->inexact"
2264
2265 ;; Test "(exact->inexact n)", expect "want".
2266 ;; "i" is a index, for diagnostic purposes.
2267 (define (try-i i n want)
2268 (with-test-prefix (list i n want)
2269 (with-test-prefix "pos"
2270 (let ((got (exact->inexact n)))
2271 (pass-if "inexact?" (inexact? got))
2272 (pass-if (list "=" got) (= want got))))
2273 (set! n (- n))
2274 (set! want (- want))
2275 (with-test-prefix "neg"
2276 (let ((got (exact->inexact n)))
2277 (pass-if "inexact?" (inexact? got))
2278 (pass-if (list "=" got) (= want got))))))
2279
2280 (with-test-prefix "2^i, no round"
2281 (do ((i 0 (1+ i))
2282 (n 1 (* 2 n))
2283 (want 1.0 (* 2.0 want)))
2284 ((> i 100))
2285 (try-i i n want)))
2286
2287 (with-test-prefix "2^i+1, no round"
2288 (do ((i 1 (1+ i))
2289 (n 3 (1- (* 2 n)))
2290 (want 3.0 (- (* 2.0 want) 1.0)))
2291 ((>= i dbl-mant-dig))
2292 (try-i i n want)))
2293
2294 (with-test-prefix "(2^i+1)*2^100, no round"
2295 (do ((i 1 (1+ i))
2296 (n 3 (1- (* 2 n)))
2297 (want 3.0 (- (* 2.0 want) 1.0)))
2298 ((>= i dbl-mant-dig))
2299 (try-i i (ash n 100) (ash-flo want 100))))
2300
2301 ;; bit pattern: 1111....11100.00
2302 ;; <-mantdig-><-i->
2303 ;;
2304 (with-test-prefix "mantdig ones then zeros, no rounding"
2305 (do ((i 0 (1+ i))
2306 (n (- (ash 1 dbl-mant-dig) 1) (* 2 n))
2307 (want (- (ash-flo 1.0 dbl-mant-dig) 1.0) (* 2.0 want)))
2308 ((> i 100))
2309 (try-i i n want)))
2310
2311 ;; bit pattern: 1111....111011..1
2312 ;; <-mantdig-> <-i->
2313 ;; This sort of value was incorrectly rounded upwards in Guile 1.6.4 when
2314 ;; i >= 11 (that's when the total is 65 or more bits).
2315 ;;
2316 (with-test-prefix "mantdig ones then 011..11, round down"
2317 (do ((i 0 (1+ i))
2318 (n (- (ash 1 (+ 1 dbl-mant-dig)) 2) (+ 1 (* 2 n)))
2319 (want (- (ash-flo 1.0 (+ 1 dbl-mant-dig)) 2.0) (* 2.0 want)))
2320 ((> i 100))
2321 (try-i i n want)))
2322
2323 ;; bit pattern: 1111....111100..001
2324 ;; <-mantdig-> <--i->
2325 ;;
2326 (with-test-prefix "mantdig ones then 100..001, round up"
2327 (do ((i 0 (1+ i))
2328 (n (- (ash 1 (+ 2 dbl-mant-dig)) 1) (1- (* 2 n)))
2329 (want (ash-flo 1.0 (+ 2 dbl-mant-dig)) (* 2.0 want)))
2330 ((> i 100))
2331 (try-i i n want)))
2332
2333 ;; bit pattern: 1000....000100..001
2334 ;; <-mantdig-> <--i->
2335 ;;
2336 (with-test-prefix "2^mantdig then 100..001, round up"
2337 (do ((i 0 (1+ i))
2338 (n (- (ash 1 (+ 2 dbl-mant-dig)) 1) (1- (* 2 n)))
2339 (want (+ (ash-flo 1.0 (+ 2 dbl-mant-dig)) 4.0) (* 2.0 want)))
2340 ((> i 100))
2341 (try-i i n want))))
2342
f29b3454
DH
2343;;;
2344;;; floor
2345;;;
2346
2347;;;
2348;;; ceiling
2349;;;
2350
46f2c0f1
RB
2351;;;
2352;;; expt
2353;;;
2354
2355(with-test-prefix "expt"
2356 (pass-if "(= 1 (expt 0 0))" (= 1 (expt 0 0)))
2357 (pass-if "(= 1 (expt 0 0.0))" (= 1 (expt 0 0.0)))
2358 (pass-if "(= 1 (expt 0.0 0))" (= 1 (expt 0.0 0)))
2359 (pass-if "(= 1 (expt 0.0 0.0))" (= 1 (expt 0.0 0.0))))
2360
14a6784c
KR
2361;;;
2362;;; asinh
2363;;;
2364
2365(with-test-prefix "asinh"
2366 (pass-if (= 0 (asinh 0))))
2367
2368;;;
2369;;; acosh
2370;;;
2371
2372(with-test-prefix "acosh"
2373 (pass-if (= 0 (acosh 1))))
2374
2375;;;
2376;;; atanh
2377;;;
2378
2379(with-test-prefix "atanh"
2380 (pass-if (= 0 (atanh 0))))
2381
f29b3454
DH
2382;;;
2383;;; make-rectangular
2384;;;
2385
2386;;;
2387;;; make-polar
2388;;;
2389
d40681ec
KR
2390(with-test-prefix "make-polar"
2391 (define pi 3.14159265358979323846)
2392 (define (almost= x y)
2393 (> 0.01 (magnitude (- x y))))
2394
2395 (pass-if (= 0 (make-polar 0 0)))
2396 (pass-if (= 0 (make-polar 0 123.456)))
2397 (pass-if (= 1 (make-polar 1 0)))
2398 (pass-if (= -1 (make-polar -1 0)))
2399
2400 (pass-if (almost= 0+i (make-polar 1 (* 0.5 pi))))
2401 (pass-if (almost= -1 (make-polar 1 (* 1.0 pi))))
2402 (pass-if (almost= 0-i (make-polar 1 (* 1.5 pi))))
2403 (pass-if (almost= 1 (make-polar 1 (* 2.0 pi)))))
2404
f29b3454
DH
2405;;;
2406;;; real-part
2407;;;
2408
2409;;;
2410;;; imag-part
2411;;;
2412
2413;;;
2414;;; magnitude
2415;;;
2416
d40681ec
KR
2417(with-test-prefix "magnitude"
2418 (pass-if (= 0 (magnitude 0)))
2419 (pass-if (= 1 (magnitude 1)))
2420 (pass-if (= 1 (magnitude -1)))
2421 (pass-if (= 1 (magnitude 0+i)))
2422 (pass-if (= 1 (magnitude 0-i)))
2423 (pass-if (= 5 (magnitude 3+4i)))
2424 (pass-if (= 5 (magnitude 3-4i)))
2425 (pass-if (= 5 (magnitude -3+4i)))
2426 (pass-if (= 5 (magnitude -3-4i))))
2427
f29b3454
DH
2428;;;
2429;;; angle
2430;;;
2431
cfc9fc1c
KR
2432(with-test-prefix "angle"
2433 (define pi 3.14159265358979323846)
2434 (define (almost= x y)
2435 (> 0.01 (magnitude (- x y))))
2436
2437 (pass-if "inum +ve" (= 0 (angle 1)))
2438 (pass-if "inum -ve" (almost= pi (angle -1)))
2439
2440 (pass-if "bignum +ve" (= 0 (angle (1+ fixnum-max))))
2441 (pass-if "bignum -ve" (almost= pi (angle (1- fixnum-min))))
2442
2443 (pass-if "flonum +ve" (= 0 (angle 1.5)))
2444 (pass-if "flonum -ve" (almost= pi (angle -1.5))))
2445
f29b3454
DH
2446;;;
2447;;; inexact->exact
2448;;;
300c6a76 2449
1259cb26
KR
2450(with-test-prefix "inexact->exact"
2451
9dd9857f 2452 (pass-if-exception "+inf" exception:out-of-range
a409f865 2453 (inexact->exact +inf.0))
1259cb26 2454
9dd9857f 2455 (pass-if-exception "-inf" exception:out-of-range
a409f865 2456 (inexact->exact -inf.0))
1259cb26 2457
9dd9857f 2458 (pass-if-exception "nan" exception:out-of-range
a409f865 2459 (inexact->exact +nan.0))
1259cb26
KR
2460
2461 (with-test-prefix "2.0**i to exact and back"
2462 (do ((i 0 (1+ i))
2463 (n 1.0 (* 2.0 n)))
2464 ((> i 100))
2465 (pass-if (list i n)
2466 (= n (inexact->exact (exact->inexact n)))))))
2467
a04a3604
KR
2468;;;
2469;;; integer-length
2470;;;
2471
2472(with-test-prefix "integer-length"
2473
2474 (with-test-prefix "-2^i, ...11100..00"
2475 (do ((n -1 (ash n 1))
2476 (i 0 (1+ i)))
2477 ((> i 256))
2478 (pass-if (list n "expect" i)
2479 (= i (integer-length n)))))
2480
2481 (with-test-prefix "-2^i+1 ...11100..01"
2482 (do ((n -3 (logxor 3 (ash n 1)))
2483 (i 2 (1+ i)))
2484 ((> i 256))
2485 (pass-if n
2486 (= i (integer-length n)))))
2487
2488 (with-test-prefix "-2^i-1 ...111011..11"
2489 (do ((n -2 (1+ (ash n 1)))
2490 (i 1 (1+ i)))
2491 ((> i 256))
2492 (pass-if n
2493 (= i (integer-length n))))))
2494
300c6a76
KR
2495;;;
2496;;; logcount
2497;;;
2498
2499(with-test-prefix "logcount"
2500
2501 (with-test-prefix "-2^i, meaning ...11100..00"
2502 (do ((n -1 (ash n 1))
2503 (i 0 (1+ i)))
2504 ((> i 256))
795c0bae
KR
2505 (pass-if n
2506 (= i (logcount n)))))
2507
2508 (with-test-prefix "2^i"
2509 (do ((n 1 (ash n 1))
2510 (i 0 (1+ i)))
2511 ((> i 256))
2512 (pass-if n
2513 (= 1 (logcount n)))))
2514
2515 (with-test-prefix "2^i-1"
2516 (do ((n 0 (1+ (ash n 1)))
2517 (i 0 (1+ i)))
2518 ((> i 256))
300c6a76
KR
2519 (pass-if n
2520 (= i (logcount n))))))
795c0bae 2521
1ec2dd6f
KR
2522;;;
2523;;; lognot
2524;;;
2525
2526(with-test-prefix "lognot"
2527 (pass-if (= -1 (lognot 0)))
2528 (pass-if (= 0 (lognot -1)))
2529 (pass-if (= -2 (lognot 1)))
2530 (pass-if (= 1 (lognot -2)))
2531
2532 (pass-if (= #x-100000000000000000000000000000000
2533 (lognot #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)))
2534 (pass-if (= #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
2535 (lognot #x-100000000000000000000000000000000))))