*** empty log message ***
[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"
18ee5de9 807 exception:numerical-overflow
24360e11
KR
808 (modulo-expt 17 23 0))
809
810 (pass-if-exception
811 "Proper exception when result not invertible"
18ee5de9 812 exception:numerical-overflow
24360e11
KR
813 (modulo-expt 10 -1 48))
814
815 (pass-if-exception
816 "Proper exception with wrong type argument"
18ee5de9 817 exception:wrong-type-arg
24360e11
KR
818 (modulo-expt "Sam" 23 10))
819
820 (pass-if-exception
821 "Proper exception with wrong type argument"
18ee5de9 822 exception:wrong-type-arg
24360e11
KR
823 (modulo-expt 17 9.9 10))
824
825 (pass-if-exception
826 "Proper exception with wrong type argument"
18ee5de9 827 exception:wrong-type-arg
24360e11
KR
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)))
d39a7b58
MV
1039 (pass-if (= 1.3 (num->str->num 1.3 10)))
1040
eb73f94b
MV
1041 ;; XXX - some results depend on whether Guile is compiled optimzed
1042 ;; or not. It is clearly undesirable to have number->string to be
1043 ;; influenced by this.
1044
d39a7b58 1045 (pass-if (string=? (number->string 35.25 36) "Z.9"))
eb73f94b
MV
1046 (pass-if (or (string=? (number->string 0.25 2) "0.01")
1047 (string=? (number->string 0.25 2) "0.010")))
d39a7b58
MV
1048 (pass-if (string=? (number->string 255.0625 16) "FF.1"))
1049 (pass-if (string=? (number->string (/ 1 3) 3) "1/10"))
eb73f94b
MV
1050 (pass-if (or (string=? (number->string 11.33333333333333333 12)
1051 "B.4")
1052 (string=? (number->string 11.33333333333333333 12)
1053 "B.400000000000009")))
1054 (pass-if (or (string=? (number->string 1.324e44 16)
1055 "5.EFE0A14FAFEe24")
1056 (string=? (number->string 1.324e44 16)
1057 "5.EFE0A14FAFDF8e24")))))
7c24e528 1058
f29b3454
DH
1059;;;
1060;;; string->number
1061;;;
1062
2f4a254a
DH
1063(with-test-prefix "string->number"
1064
1065 (pass-if "string->number"
1066 (documented? string->number))
1067
1068 (pass-if "non number strings"
1069 (for-each (lambda (x) (if (string->number x) (throw 'fail)))
569c483b 1070 '("" "q" "1q" "6+7iq" "8+9q" "10+11" "13+" "18@19q" "20@q" "23@"
2f4a254a 1071 "+25iq" "26i" "-q" "-iq" "i" "5#.0" "8/" "10#11" ".#" "."
569c483b 1072 "#o.2" "3.4q" "15.16e17q" "18.19e+q" ".q" ".17#18" "10q" "#b2"
2f4a254a
DH
1073 "#b3" "#b4" "#b5" "#b6" "#b7" "#b8" "#b9" "#ba" "#bb" "#bc"
1074 "#bd" "#be" "#bf" "#q" "#b#b1" "#o#o1" "#d#d1" "#x#x1" "#e#e1"
1075 "#i#i1" "12@12+0i"))
1076 #t)
1077
b7d9b1cf
DH
1078 (pass-if "valid number strings"
1079 (for-each (lambda (couple)
1080 (apply
1081 (lambda (x y)
9dd9857f
MV
1082 (let ((xx (string->number x)))
1083 (if (or (eq? xx #f) (not (eqv? xx y)))
ca2b31fe
MV
1084 (begin
1085 (pk x y)
1086 (throw 'fail)))))
b7d9b1cf
DH
1087 couple))
1088 `(;; Radix:
1089 ("#b0" 0) ("#B0" 0) ("#b1" 1) ("#B1" 1) ("#o0" 0) ("#O0" 0)
1090 ("#o1" 1) ("#O1" 1) ("#o2" 2) ("#O2" 2) ("#o3" 3) ("#O3" 3)
1091 ("#o4" 4) ("#O4" 4) ("#o5" 5) ("#O5" 5) ("#o6" 6) ("#O6" 6)
1092 ("#o7" 7) ("#O7" 7) ("#d0" 0) ("#D0" 0) ("#d1" 1) ("#D1" 1)
1093 ("#d2" 2) ("#D2" 2) ("#d3" 3) ("#D3" 3) ("#d4" 4) ("#D4" 4)
1094 ("#d5" 5) ("#D5" 5) ("#d6" 6) ("#D6" 6) ("#d7" 7) ("#D7" 7)
1095 ("#d8" 8) ("#D8" 8) ("#d9" 9) ("#D9" 9)
1096 ("#xa" 10) ("#Xa" 10) ("#xb" 11) ("#Xb" 11)
1097 ("#xc" 12) ("#Xc" 12) ("#xd" 13) ("#Xd" 13)
1098 ("#xe" 14) ("#Xe" 14) ("#xf" 15) ("#Xf" 15)
1099 ("#b1010" 10)
1100 ("#o12345670" 2739128)
1101 ("#d1234567890" 1234567890)
1102 ("#x1234567890abcdef" 1311768467294899695)
1103 ;; Exactness:
ca2b31fe 1104 ("#e1" 1) ("#e1.2" 12/10)
9dd9857f 1105 ("#i1.1" 1.1) ("#i1" 1.0)
b7d9b1cf
DH
1106 ;; Integers:
1107 ("1" ,(1+ 0)) ("23" ,(+ 9 9 5)) ("-1" ,(- 0 1))
1108 ("-45" ,(- 0 45)) ("2#" 20.0) ("2##" 200.0) ("12##" 1200.0)
1109 ("#b#i100" 4.0)
9dd9857f
MV
1110 ;; Fractions:
1111 ("1/1" 1) ("1/2" 1/2) ("-1/2" -1/2) ("1#/1" 10.0)
1112 ("10/1#" 1.0) ("1#/1#" 1.0) ("#e9/10" 9/10) ("#e10/1#" 1)
b7d9b1cf
DH
1113 ("#i6/8" 0.75) ("#i1/1" 1.0)
1114 ;; Decimal numbers:
1115 ;; * <uinteger 10> <suffix>
1116 ("1e2" 100.0) ("1E2" 100.0) ("1s2" 100.0) ("1S2" 100.0)
1117 ("1f2" 100.0) ("1F2" 100.0) ("1d2" 100.0) ("1D2" 100.0)
1118 ("1l2" 100.0) ("1L2" 100.0) ("1e+2" 100.0) ("1e-2" 0.01)
1119 ;; * . <digit 10>+ #* <suffix>
1120 (".1" .1) (".0123456789" 123456789e-10) (".16#" 0.16)
1121 (".0123456789e10" 123456789.0) (".16#e3" 160.0) ("#d.3" 0.3)
1122 ;; * <digit 10>+ . <digit 10>* #* <suffix>
1123 ("3." ,(exact->inexact 3)) ("3.e0" ,(exact->inexact 3))
1124 ("3.1" ,(exact->inexact 31/10)) ("3.1e0" 3.1) ("3.1#" 3.1)
1125 ("3.1#e0" 3.1)
1126 ;; * <digit 10>+ #+ . #* <suffix>
1127 ("3#." 30.0) ("3#.e0" 30.0) ("3#.#" 30.0) ("3#.#e0" 30.0)
1128 ;; Complex:
1129 ("1@0" 1.0) ("1@+0" 1.0) ("1@-0" 1.0)
1130 ("2+3i" ,(+ 2 (* 3 +i))) ("4-5i" ,(- 4 (* 5 +i)))
1131 ("1+i" 1+1i) ("1-i" 1-1i) ("+1i" 0+1i) ("-1i" 0-1i)
1132 ("+i" +1i) ("-i" -1i)))
1133 #t)
1134
2f4a254a
DH
1135 (pass-if-exception "exponent too big"
1136 exception:out-of-range
1137 (string->number "12.13e141414")))
1138
f29b3454
DH
1139;;;
1140;;; number?
1141;;;
1142
7c24e528
RB
1143(with-test-prefix "number?"
1144 (pass-if (documented? number?))
1145 (pass-if (number? 0))
1146 (pass-if (number? 7))
1147 (pass-if (number? -7))
1148 (pass-if (number? 1.3))
1149 (pass-if (number? (+ 1 fixnum-max)))
1150 (pass-if (number? (- 1 fixnum-min)))
1151 (pass-if (number? 3+4i))
4d332f19
DH
1152 (pass-if (not (number? #\a)))
1153 (pass-if (not (number? "a")))
1154 (pass-if (not (number? (make-vector 0))))
1155 (pass-if (not (number? (cons 1 2))))
1156 (pass-if (not (number? #t)))
1157 (pass-if (not (number? (lambda () #t))))
1158 (pass-if (not (number? (current-input-port)))))
7c24e528 1159
f29b3454
DH
1160;;;
1161;;; complex?
1162;;;
1163
7c24e528
RB
1164(with-test-prefix "complex?"
1165 (pass-if (documented? complex?))
1166 (pass-if (complex? 0))
1167 (pass-if (complex? 7))
1168 (pass-if (complex? -7))
1169 (pass-if (complex? (+ 1 fixnum-max)))
1170 (pass-if (complex? (- 1 fixnum-min)))
1171 (pass-if (complex? 1.3))
1172 (pass-if (complex? 3+4i))
4d332f19
DH
1173 (pass-if (not (complex? #\a)))
1174 (pass-if (not (complex? "a")))
1175 (pass-if (not (complex? (make-vector 0))))
1176 (pass-if (not (complex? (cons 1 2))))
1177 (pass-if (not (complex? #t)))
1178 (pass-if (not (complex? (lambda () #t))))
1179 (pass-if (not (complex? (current-input-port)))))
7c24e528 1180
f29b3454
DH
1181;;;
1182;;; real?
1183;;;
1184
7c24e528
RB
1185(with-test-prefix "real?"
1186 (pass-if (documented? real?))
1187 (pass-if (real? 0))
1188 (pass-if (real? 7))
1189 (pass-if (real? -7))
1190 (pass-if (real? (+ 1 fixnum-max)))
1191 (pass-if (real? (- 1 fixnum-min)))
1192 (pass-if (real? 1.3))
4d332f19
DH
1193 (pass-if (not (real? 3+4i)))
1194 (pass-if (not (real? #\a)))
1195 (pass-if (not (real? "a")))
1196 (pass-if (not (real? (make-vector 0))))
1197 (pass-if (not (real? (cons 1 2))))
1198 (pass-if (not (real? #t)))
1199 (pass-if (not (real? (lambda () #t))))
1200 (pass-if (not (real? (current-input-port)))))
7c24e528 1201
f29b3454 1202;;;
7c24e528 1203;;; rational? (same as real? right now)
f29b3454
DH
1204;;;
1205
7c24e528
RB
1206(with-test-prefix "rational?"
1207 (pass-if (documented? rational?))
1208 (pass-if (rational? 0))
1209 (pass-if (rational? 7))
1210 (pass-if (rational? -7))
1211 (pass-if (rational? (+ 1 fixnum-max)))
1212 (pass-if (rational? (- 1 fixnum-min)))
1213 (pass-if (rational? 1.3))
4d332f19
DH
1214 (pass-if (not (rational? 3+4i)))
1215 (pass-if (not (rational? #\a)))
1216 (pass-if (not (rational? "a")))
1217 (pass-if (not (rational? (make-vector 0))))
1218 (pass-if (not (rational? (cons 1 2))))
1219 (pass-if (not (rational? #t)))
1220 (pass-if (not (rational? (lambda () #t))))
1221 (pass-if (not (rational? (current-input-port)))))
7c24e528 1222
f29b3454
DH
1223;;;
1224;;; integer?
1225;;;
1226
7c24e528
RB
1227(with-test-prefix "integer?"
1228 (pass-if (documented? integer?))
1229 (pass-if (integer? 0))
1230 (pass-if (integer? 7))
1231 (pass-if (integer? -7))
1232 (pass-if (integer? (+ 1 fixnum-max)))
1233 (pass-if (integer? (- 1 fixnum-min)))
1234 (pass-if (and (= 3+0i (round 3+0i)) (integer? 3+0i)))
1235 (pass-if (and (= 1.0 (round 1.0)) (integer? 1.0)))
4d332f19 1236 (pass-if (not (integer? 1.3)))
e456f08e
KR
1237 (pass-if (integer? +inf.0))
1238 (pass-if (integer? -inf.0))
c1122753 1239 (pass-if (not (integer? +nan.0)))
4d332f19
DH
1240 (pass-if (not (integer? 3+4i)))
1241 (pass-if (not (integer? #\a)))
1242 (pass-if (not (integer? "a")))
1243 (pass-if (not (integer? (make-vector 0))))
1244 (pass-if (not (integer? (cons 1 2))))
1245 (pass-if (not (integer? #t)))
1246 (pass-if (not (integer? (lambda () #t))))
1247 (pass-if (not (integer? (current-input-port)))))
7c24e528 1248
f29b3454
DH
1249;;;
1250;;; inexact?
1251;;;
1252
7c24e528
RB
1253(with-test-prefix "inexact?"
1254 (pass-if (documented? inexact?))
4d332f19
DH
1255 (pass-if (not (inexact? 0)))
1256 (pass-if (not (inexact? 7)))
1257 (pass-if (not (inexact? -7)))
1258 (pass-if (not (inexact? (+ 1 fixnum-max))))
1259 (pass-if (not (inexact? (- 1 fixnum-min))))
7c24e528
RB
1260 (pass-if (inexact? 1.3))
1261 (pass-if (inexact? 3.1+4.2i))
ca2b31fe
MV
1262 (pass-if-exception "char"
1263 exception:wrong-type-arg
1264 (not (inexact? #\a)))
1265 (pass-if-exception "string"
1266 exception:wrong-type-arg
1267 (not (inexact? "a")))
1268 (pass-if-exception "vector"
1269 exception:wrong-type-arg
1270 (not (inexact? (make-vector 0))))
1271 (pass-if-exception "cons"
1272 exception:wrong-type-arg
1273 (not (inexact? (cons 1 2))))
1274 (pass-if-exception "bool"
1275 exception:wrong-type-arg
1276 (not (inexact? #t)))
1277 (pass-if-exception "procedure"
1278 exception:wrong-type-arg
1279 (not (inexact? (lambda () #t))))
1280 (pass-if-exception "port"
1281 exception:wrong-type-arg
1282 (not (inexact? (current-input-port)))))
7c24e528 1283
47ae1f0e
DH
1284;;;
1285;;; equal?
1286;;;
1287
1288(with-test-prefix "equal?"
1289 (pass-if (documented? equal?))
1290 (pass-if (equal? 0 0))
1291 (pass-if (equal? 7 7))
1292 (pass-if (equal? -7 -7))
1293 (pass-if (equal? (+ 1 fixnum-max) (+ 1 fixnum-max)))
1294 (pass-if (equal? (- fixnum-min 1) (- fixnum-min 1)))
1295 (pass-if (not (equal? 0 1)))
1296 (pass-if (not (equal? fixnum-max (+ 1 fixnum-max))))
1297 (pass-if (not (equal? (+ 1 fixnum-max) fixnum-max)))
1298 (pass-if (not (equal? (+ 1 fixnum-max) (+ 2 fixnum-max))))
1299 (pass-if (not (equal? fixnum-min (- fixnum-min 1))))
1300 (pass-if (not (equal? (- fixnum-min 1) fixnum-min)))
1301 (pass-if (not (equal? (- fixnum-min 1) (- fixnum-min 2))))
1302 (pass-if (not (equal? (+ fixnum-max 1) (- fixnum-min 1))))
1303
1304 (pass-if (not (equal? (ash 1 256) +inf.0)))
1305 (pass-if (not (equal? +inf.0 (ash 1 256))))
1306 (pass-if (not (equal? (ash 1 256) -inf.0)))
1307 (pass-if (not (equal? -inf.0 (ash 1 256))))
1308
1309 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
1310 ;; sure we've avoided that
1311 (pass-if (not (equal? (ash 1 1024) +inf.0)))
1312 (pass-if (not (equal? +inf.0 (ash 1 1024))))
1313 (pass-if (not (equal? (- (ash 1 1024)) -inf.0)))
1314 (pass-if (not (equal? -inf.0 (- (ash 1 1024)))))
1315
1316 (pass-if (not (equal? +nan.0 +nan.0)))
1317 (pass-if (not (equal? 0 +nan.0)))
1318 (pass-if (not (equal? +nan.0 0)))
1319 (pass-if (not (equal? 1 +nan.0)))
1320 (pass-if (not (equal? +nan.0 1)))
1321 (pass-if (not (equal? -1 +nan.0)))
1322 (pass-if (not (equal? +nan.0 -1)))
1323
1324 (pass-if (not (equal? (ash 1 256) +nan.0)))
1325 (pass-if (not (equal? +nan.0 (ash 1 256))))
1326 (pass-if (not (equal? (- (ash 1 256)) +nan.0)))
1327 (pass-if (not (equal? +nan.0 (- (ash 1 256)))))
1328
1329 (pass-if (not (equal? (ash 1 8192) +nan.0)))
1330 (pass-if (not (equal? +nan.0 (ash 1 8192))))
1331 (pass-if (not (equal? (- (ash 1 8192)) +nan.0)))
1332 (pass-if (not (equal? +nan.0 (- (ash 1 8192)))))
1333
1334 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
1335 ;; sure we've avoided that
1336 (pass-if (not (equal? (ash 3 1023) +nan.0)))
1337 (pass-if (not (equal? +nan.0 (ash 3 1023)))))
1338
f29b3454
DH
1339;;;
1340;;; =
1341;;;
1342
7c24e528
RB
1343(with-test-prefix "="
1344 (expect-fail (documented? =))
1345 (pass-if (= 0 0))
1346 (pass-if (= 7 7))
1347 (pass-if (= -7 -7))
1348 (pass-if (= (+ 1 fixnum-max) (+ 1 fixnum-max)))
47ae1f0e 1349 (pass-if (= (- fixnum-min 1) (- fixnum-min 1)))
4d332f19
DH
1350 (pass-if (not (= 0 1)))
1351 (pass-if (not (= fixnum-max (+ 1 fixnum-max))))
1352 (pass-if (not (= (+ 1 fixnum-max) fixnum-max)))
47ae1f0e 1353 (pass-if (not (= (+ 1 fixnum-max) (+ 2 fixnum-max))))
4d332f19
DH
1354 (pass-if (not (= fixnum-min (- fixnum-min 1))))
1355 (pass-if (not (= (- fixnum-min 1) fixnum-min)))
47ae1f0e 1356 (pass-if (not (= (- fixnum-min 1) (- fixnum-min 2))))
4d332f19 1357 (pass-if (not (= (+ fixnum-max 1) (- fixnum-min 1))))
2cfcaed5 1358
adda36ed
KR
1359 (pass-if (not (= (ash 1 256) +inf.0)))
1360 (pass-if (not (= +inf.0 (ash 1 256))))
1361 (pass-if (not (= (ash 1 256) -inf.0)))
1362 (pass-if (not (= -inf.0 (ash 1 256))))
1363
1364 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
1365 ;; sure we've avoided that
1366 (pass-if (not (= (ash 1 1024) +inf.0)))
1367 (pass-if (not (= +inf.0 (ash 1 1024))))
1368 (pass-if (not (= (- (ash 1 1024)) -inf.0)))
1369 (pass-if (not (= -inf.0 (- (ash 1 1024)))))
1370
2cfcaed5
KR
1371 (pass-if (not (= +nan.0 +nan.0)))
1372 (pass-if (not (= 0 +nan.0)))
1373 (pass-if (not (= +nan.0 0)))
1374 (pass-if (not (= 1 +nan.0)))
1375 (pass-if (not (= +nan.0 1)))
1376 (pass-if (not (= -1 +nan.0)))
1377 (pass-if (not (= +nan.0 -1)))
1378
1379 (pass-if (not (= (ash 1 256) +nan.0)))
1380 (pass-if (not (= +nan.0 (ash 1 256))))
1381 (pass-if (not (= (- (ash 1 256)) +nan.0)))
1382 (pass-if (not (= +nan.0 (- (ash 1 256)))))
1383
1384 (pass-if (not (= (ash 1 8192) +nan.0)))
1385 (pass-if (not (= +nan.0 (ash 1 8192))))
1386 (pass-if (not (= (- (ash 1 8192)) +nan.0)))
1387 (pass-if (not (= +nan.0 (- (ash 1 8192)))))
1388
1389 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
1390 ;; sure we've avoided that
1391 (pass-if (not (= (ash 3 1023) +nan.0)))
2a8b5e04
KR
1392 (pass-if (not (= +nan.0 (ash 3 1023))))
1393
1394 (pass-if (= 1/2 0.5))
1395 (pass-if (not (= 1/3 0.333333333333333333333333333333333)))
1396 (pass-if (not (= 2/3 0.5)))
1397 (pass-if (not (= 0.5 (+ 1/2 (/ 1 (ash 1 1000))))))
1398
1399 (pass-if (= 1/2 0.5+0i))
1400 (pass-if (not (= 0.333333333333333333333333333333333 1/3)))
1401 (pass-if (not (= 2/3 0.5+0i)))
1402 (pass-if (not (= 1/2 0+0.5i)))
1403
1404 (pass-if (= 0.5 1/2))
1405 (pass-if (not (= 0.5 2/3)))
1406 (pass-if (not (= (+ 1/2 (/ 1 (ash 1 1000))) 0.5)))
1407
1408 (pass-if (= 0.5+0i 1/2))
1409 (pass-if (not (= 0.5+0i 2/3)))
1410 (pass-if (not (= 0+0.5i 1/2))))
7c24e528 1411
de142bea
DH
1412;;;
1413;;; <
1414;;;
1415
1416(with-test-prefix "<"
1417
de142bea 1418 (expect-fail "documented?"
cb18f2a8 1419 (documented? <))
de142bea 1420
de142bea
DH
1421 (with-test-prefix "(< 0 n)"
1422
1423 (pass-if "n = 0"
1424 (not (< 0 0)))
1425
1426 (pass-if "n = 0.0"
1427 (not (< 0 0.0)))
1428
1429 (pass-if "n = 1"
1430 (< 0 1))
1431
1432 (pass-if "n = 1.0"
1433 (< 0 1.0))
1434
1435 (pass-if "n = -1"
1436 (not (< 0 -1)))
1437
1438 (pass-if "n = -1.0"
1439 (not (< 0 -1.0)))
1440
21e39e8f
DH
1441 (pass-if "n = fixnum-max"
1442 (< 0 fixnum-max))
1443
1444 (pass-if "n = fixnum-max + 1"
1445 (< 0 (+ fixnum-max 1)))
1446
1447 (pass-if "n = fixnum-min"
1448 (not (< 0 fixnum-min)))
de142bea 1449
21e39e8f
DH
1450 (pass-if "n = fixnum-min - 1"
1451 (not (< 0 (- fixnum-min 1)))))
1452
de142bea
DH
1453 (with-test-prefix "(< 0.0 n)"
1454
1455 (pass-if "n = 0"
1456 (not (< 0.0 0)))
1457
1458 (pass-if "n = 0.0"
1459 (not (< 0.0 0.0)))
1460
1461 (pass-if "n = 1"
1462 (< 0.0 1))
1463
1464 (pass-if "n = 1.0"
1465 (< 0.0 1.0))
1466
1467 (pass-if "n = -1"
1468 (not (< 0.0 -1)))
1469
1470 (pass-if "n = -1.0"
1471 (not (< 0.0 -1.0)))
1472
21e39e8f
DH
1473 (pass-if "n = fixnum-max"
1474 (< 0.0 fixnum-max))
1475
1476 (pass-if "n = fixnum-max + 1"
1477 (< 0.0 (+ fixnum-max 1)))
de142bea 1478
21e39e8f
DH
1479 (pass-if "n = fixnum-min"
1480 (not (< 0.0 fixnum-min)))
1481
1482 (pass-if "n = fixnum-min - 1"
1483 (not (< 0.0 (- fixnum-min 1)))))
1484
1485 (with-test-prefix "(< 1 n)"
de142bea 1486
21e39e8f 1487 (pass-if "n = 0"
de142bea
DH
1488 (not (< 1 0)))
1489
21e39e8f
DH
1490 (pass-if "n = 0.0"
1491 (not (< 1 0.0)))
1492
1493 (pass-if "n = 1"
1494 (not (< 1 1)))
1495
de142bea 1496 (pass-if "n = 1.0"
21e39e8f
DH
1497 (not (< 1 1.0)))
1498
1499 (pass-if "n = -1"
1500 (not (< 1 -1)))
1501
1502 (pass-if "n = -1.0"
1503 (not (< 1 -1.0)))
1504
1505 (pass-if "n = fixnum-max"
1506 (< 1 fixnum-max))
1507
1508 (pass-if "n = fixnum-max + 1"
1509 (< 1 (+ fixnum-max 1)))
1510
1511 (pass-if "n = fixnum-min"
1512 (not (< 1 fixnum-min)))
1513
1514 (pass-if "n = fixnum-min - 1"
1515 (not (< 1 (- fixnum-min 1)))))
1516
1517 (with-test-prefix "(< 1.0 n)"
1518
1519 (pass-if "n = 0"
de142bea
DH
1520 (not (< 1.0 0)))
1521
21e39e8f
DH
1522 (pass-if "n = 0.0"
1523 (not (< 1.0 0.0)))
1524
1525 (pass-if "n = 1"
1526 (not (< 1.0 1)))
1527
1528 (pass-if "n = 1.0"
1529 (not (< 1.0 1.0)))
1530
de142bea 1531 (pass-if "n = -1"
21e39e8f
DH
1532 (not (< 1.0 -1)))
1533
1534 (pass-if "n = -1.0"
1535 (not (< 1.0 -1.0)))
1536
1537 (pass-if "n = fixnum-max"
1538 (< 1.0 fixnum-max))
1539
1540 (pass-if "n = fixnum-max + 1"
1541 (< 1.0 (+ fixnum-max 1)))
1542
1543 (pass-if "n = fixnum-min"
1544 (not (< 1.0 fixnum-min)))
1545
1546 (pass-if "n = fixnum-min - 1"
1547 (not (< 1.0 (- fixnum-min 1)))))
1548
1549 (with-test-prefix "(< -1 n)"
1550
1551 (pass-if "n = 0"
de142bea
DH
1552 (< -1 0))
1553
21e39e8f
DH
1554 (pass-if "n = 0.0"
1555 (< -1 0.0))
1556
1557 (pass-if "n = 1"
1558 (< -1 1))
1559
1560 (pass-if "n = 1.0"
1561 (< -1 1.0))
1562
1563 (pass-if "n = -1"
1564 (not (< -1 -1)))
1565
de142bea 1566 (pass-if "n = -1.0"
21e39e8f
DH
1567 (not (< -1 -1.0)))
1568
1569 (pass-if "n = fixnum-max"
1570 (< -1 fixnum-max))
1571
1572 (pass-if "n = fixnum-max + 1"
1573 (< -1 (+ fixnum-max 1)))
1574
1575 (pass-if "n = fixnum-min"
1576 (not (< -1 fixnum-min)))
1577
1578 (pass-if "n = fixnum-min - 1"
1579 (not (< -1 (- fixnum-min 1)))))
1580
1581 (with-test-prefix "(< -1.0 n)"
1582
1583 (pass-if "n = 0"
de142bea
DH
1584 (< -1.0 0))
1585
21e39e8f
DH
1586 (pass-if "n = 0.0"
1587 (< -1.0 0.0))
1588
1589 (pass-if "n = 1"
1590 (< -1.0 1))
1591
1592 (pass-if "n = 1.0"
1593 (< -1.0 1.0))
1594
1595 (pass-if "n = -1"
1596 (not (< -1.0 -1)))
1597
1598 (pass-if "n = -1.0"
1599 (not (< -1.0 -1.0)))
1600
1601 (pass-if "n = fixnum-max"
1602 (< -1.0 fixnum-max))
1603
1604 (pass-if "n = fixnum-max + 1"
1605 (< -1.0 (+ fixnum-max 1)))
de142bea 1606
21e39e8f
DH
1607 (pass-if "n = fixnum-min"
1608 (not (< -1.0 fixnum-min)))
1609
1610 (pass-if "n = fixnum-min - 1"
1611 (not (< -1.0 (- fixnum-min 1)))))
1612
1613 (with-test-prefix "(< fixnum-max n)"
1614
1615 (pass-if "n = 0"
1616 (not (< fixnum-max 0)))
1617
1618 (pass-if "n = 0.0"
1619 (not (< fixnum-max 0.0)))
de142bea
DH
1620
1621 (pass-if "n = 1"
21e39e8f 1622 (not (< fixnum-max 1)))
de142bea
DH
1623
1624 (pass-if "n = 1.0"
21e39e8f 1625 (not (< fixnum-max 1.0)))
de142bea
DH
1626
1627 (pass-if "n = -1"
21e39e8f 1628 (not (< fixnum-max -1)))
de142bea
DH
1629
1630 (pass-if "n = -1.0"
21e39e8f 1631 (not (< fixnum-max -1.0)))
de142bea 1632
21e39e8f
DH
1633 (pass-if "n = fixnum-max"
1634 (not (< fixnum-max fixnum-max)))
de142bea 1635
21e39e8f
DH
1636 (pass-if "n = fixnum-max + 1"
1637 (< fixnum-max (+ fixnum-max 1)))
1638
1639 (pass-if "n = fixnum-min"
1640 (not (< fixnum-max fixnum-min)))
1641
1642 (pass-if "n = fixnum-min - 1"
1643 (not (< fixnum-max (- fixnum-min 1)))))
1644
1645 (with-test-prefix "(< (+ fixnum-max 1) n)"
1646
1647 (pass-if "n = 0"
1648 (not (< (+ fixnum-max 1) 0)))
1649
1650 (pass-if "n = 0.0"
1651 (not (< (+ fixnum-max 1) 0.0)))
de142bea
DH
1652
1653 (pass-if "n = 1"
21e39e8f 1654 (not (< (+ fixnum-max 1) 1)))
de142bea
DH
1655
1656 (pass-if "n = 1.0"
21e39e8f 1657 (not (< (+ fixnum-max 1) 1.0)))
de142bea
DH
1658
1659 (pass-if "n = -1"
21e39e8f 1660 (not (< (+ fixnum-max 1) -1)))
de142bea
DH
1661
1662 (pass-if "n = -1.0"
21e39e8f 1663 (not (< (+ fixnum-max 1) -1.0)))
de142bea 1664
21e39e8f
DH
1665 (pass-if "n = fixnum-max"
1666 (not (< (+ fixnum-max 1) fixnum-max)))
de142bea 1667
21e39e8f
DH
1668 (pass-if "n = fixnum-max + 1"
1669 (not (< (+ fixnum-max 1) (+ fixnum-max 1))))
de142bea 1670
21e39e8f
DH
1671 (pass-if "n = fixnum-min"
1672 (not (< (+ fixnum-max 1) fixnum-min)))
1673
1674 (pass-if "n = fixnum-min - 1"
1675 (not (< (+ fixnum-max 1) (- fixnum-min 1)))))
1676
1677 (with-test-prefix "(< fixnum-min n)"
1678
1679 (pass-if "n = 0"
1680 (< fixnum-min 0))
1681
1682 (pass-if "n = 0.0"
1683 (< fixnum-min 0.0))
de142bea
DH
1684
1685 (pass-if "n = 1"
21e39e8f 1686 (< fixnum-min 1))
de142bea
DH
1687
1688 (pass-if "n = 1.0"
21e39e8f 1689 (< fixnum-min 1.0))
de142bea
DH
1690
1691 (pass-if "n = -1"
21e39e8f 1692 (< fixnum-min -1))
de142bea
DH
1693
1694 (pass-if "n = -1.0"
21e39e8f 1695 (< fixnum-min -1.0))
de142bea 1696
21e39e8f
DH
1697 (pass-if "n = fixnum-max"
1698 (< fixnum-min fixnum-max))
1699
1700 (pass-if "n = fixnum-max + 1"
1701 (< fixnum-min (+ fixnum-max 1)))
de142bea 1702
21e39e8f
DH
1703 (pass-if "n = fixnum-min"
1704 (not (< fixnum-min fixnum-min)))
de142bea 1705
21e39e8f
DH
1706 (pass-if "n = fixnum-min - 1"
1707 (not (< fixnum-min (- fixnum-min 1)))))
1708
1709 (with-test-prefix "(< (- fixnum-min 1) n)"
1710
1711 (pass-if "n = 0"
1712 (< (- fixnum-min 1) 0))
1713
1714 (pass-if "n = 0.0"
1715 (< (- fixnum-min 1) 0.0))
1716
1717 (pass-if "n = 1"
1718 (< (- fixnum-min 1) 1))
1719
1720 (pass-if "n = 1.0"
1721 (< (- fixnum-min 1) 1.0))
de142bea
DH
1722
1723 (pass-if "n = -1"
21e39e8f 1724 (< (- fixnum-min 1) -1))
de142bea
DH
1725
1726 (pass-if "n = -1.0"
21e39e8f
DH
1727 (< (- fixnum-min 1) -1.0))
1728
1729 (pass-if "n = fixnum-max"
1730 (< (- fixnum-min 1) fixnum-max))
1731
1732 (pass-if "n = fixnum-max + 1"
1733 (< (- fixnum-min 1) (+ fixnum-max 1)))
1734
1735 (pass-if "n = fixnum-min"
1736 (< (- fixnum-min 1) fixnum-min))
1737
1738 (pass-if "n = fixnum-min - 1"
2cfcaed5
KR
1739 (not (< (- fixnum-min 1) (- fixnum-min 1)))))
1740
adda36ed
KR
1741 (pass-if (< (ash 1 256) +inf.0))
1742 (pass-if (not (< +inf.0 (ash 1 256))))
1743 (pass-if (not (< (ash 1 256) -inf.0)))
1744 (pass-if (< -inf.0 (ash 1 256)))
1745
1746 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
1747 ;; sure we've avoided that
1748 (pass-if (< (1- (ash 1 1024)) +inf.0))
1749 (pass-if (< (ash 1 1024) +inf.0))
1750 (pass-if (< (1+ (ash 1 1024)) +inf.0))
1751 (pass-if (not (< +inf.0 (1- (ash 1 1024)))))
1752 (pass-if (not (< +inf.0 (ash 1 1024))))
1753 (pass-if (not (< +inf.0 (1+ (ash 1 1024)))))
1754 (pass-if (< -inf.0 (- (1- (ash 1 1024)))))
1755 (pass-if (< -inf.0 (- (ash 1 1024))))
1756 (pass-if (< -inf.0 (- (1+ (ash 1 1024)))))
1757 (pass-if (not (< (- (1- (ash 1 1024))) -inf.0)))
1758 (pass-if (not (< (- (ash 1 1024)) -inf.0)))
1759 (pass-if (not (< (- (1+ (ash 1 1024))) -inf.0)))
1760
2cfcaed5
KR
1761 (pass-if (not (< +nan.0 +nan.0)))
1762 (pass-if (not (< 0 +nan.0)))
1763 (pass-if (not (< +nan.0 0)))
1764 (pass-if (not (< 1 +nan.0)))
1765 (pass-if (not (< +nan.0 1)))
1766 (pass-if (not (< -1 +nan.0)))
1767 (pass-if (not (< +nan.0 -1)))
1768
1769 (pass-if (not (< (ash 1 256) +nan.0)))
1770 (pass-if (not (< +nan.0 (ash 1 256))))
1771 (pass-if (not (< (- (ash 1 256)) +nan.0)))
1772 (pass-if (not (< +nan.0 (- (ash 1 256)))))
1773
1774 (pass-if (not (< (ash 1 8192) +nan.0)))
1775 (pass-if (not (< +nan.0 (ash 1 8192))))
1776 (pass-if (not (< (- (ash 1 8192)) +nan.0)))
1777 (pass-if (not (< +nan.0 (- (ash 1 8192)))))
1778
1779 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
1780 ;; sure we've avoided that
1781 (pass-if (not (< (ash 3 1023) +nan.0)))
1782 (pass-if (not (< (1+ (ash 3 1023)) +nan.0)))
1783 (pass-if (not (< (1- (ash 3 1023)) +nan.0)))
1784 (pass-if (not (< +nan.0 (ash 3 1023))))
1785 (pass-if (not (< +nan.0 (1+ (ash 3 1023)))))
fe89421e
KR
1786 (pass-if (not (< +nan.0 (1- (ash 3 1023)))))
1787
1788 (with-test-prefix "inum/frac"
1789 (pass-if (< 2 9/4))
1790 (pass-if (< -2 9/4))
1791 (pass-if (< -2 7/4))
1792 (pass-if (< -2 -7/4))
1793 (pass-if (eq? #f (< 2 7/4)))
1794 (pass-if (eq? #f (< 2 -7/4)))
1795 (pass-if (eq? #f (< 2 -9/4)))
1796 (pass-if (eq? #f (< -2 -9/4))))
1797
1798 (with-test-prefix "bignum/frac"
1799 (let ((x (ash 1 2048)))
1800 (pass-if (< x (* 4/3 x)))
1801 (pass-if (< (- x) (* 4/3 x)))
1802 (pass-if (< (- x) (* 2/3 x)))
1803 (pass-if (< (- x) (* -2/3 x)))
1804 (pass-if (eq? #f (< x (* 2/3 x))))
1805 (pass-if (eq? #f (< x (* -2/3 x))))
1806 (pass-if (eq? #f (< x (* -4/3 x))))
1807 (pass-if (eq? #f (< (- x) (* -4/3 x))))))
1808
1809 (with-test-prefix "flonum/frac"
1810 (pass-if (< 0.75 4/3))
1811 (pass-if (< -0.75 4/3))
1812 (pass-if (< -0.75 2/3))
1813 (pass-if (< -0.75 -2/3))
1814 (pass-if (eq? #f (< 0.75 2/3)))
1815 (pass-if (eq? #f (< 0.75 -2/3)))
1816 (pass-if (eq? #f (< 0.75 -4/3)))
1817 (pass-if (eq? #f (< -0.75 -4/3)))
1818
1819 (pass-if (< -inf.0 4/3))
1820 (pass-if (< -inf.0 -4/3))
1821 (pass-if (eq? #f (< +inf.0 4/3)))
1822 (pass-if (eq? #f (< +inf.0 -4/3)))
1823
1824 (pass-if (eq? #f (< +nan.0 4/3)))
1825 (pass-if (eq? #f (< +nan.0 -4/3))))
1826
1827 (with-test-prefix "frac/inum"
1828 (pass-if (< 7/4 2))
1829 (pass-if (< -7/4 2))
1830 (pass-if (< -9/4 2))
1831 (pass-if (< -9/4 -2))
1832 (pass-if (eq? #f (< 9/4 2)))
1833 (pass-if (eq? #f (< 9/4 -2)))
1834 (pass-if (eq? #f (< 7/4 -2)))
1835 (pass-if (eq? #f (< -7/4 -2))))
1836
1837 (with-test-prefix "frac/bignum"
1838 (let ((x (ash 1 2048)))
1839 (pass-if (< (* 2/3 x) x))
1840 (pass-if (< (* -2/3 x) x))
1841 (pass-if (< (* -4/3 x) x))
1842 (pass-if (< (* -4/3 x) (- x)))
1843 (pass-if (eq? #f (< (* 4/3 x) x)))
1844 (pass-if (eq? #f (< (* 4/3 x) (- x))))
1845 (pass-if (eq? #f (< (* 2/3 x) (- x))))
1846 (pass-if (eq? #f (< (* -2/3 x) (- x))))))
1847
1848 (with-test-prefix "frac/flonum"
1849 (pass-if (< 2/3 0.75))
1850 (pass-if (< -2/3 0.75))
1851 (pass-if (< -4/3 0.75))
1852 (pass-if (< -4/3 -0.75))
1853 (pass-if (eq? #f (< 4/3 0.75)))
1854 (pass-if (eq? #f (< 4/3 -0.75)))
1855 (pass-if (eq? #f (< 2/3 -0.75)))
1856 (pass-if (eq? #f (< -2/3 -0.75)))
1857
1858 (pass-if (< 4/3 +inf.0))
1859 (pass-if (< -4/3 +inf.0))
1860 (pass-if (eq? #f (< 4/3 -inf.0)))
1861 (pass-if (eq? #f (< -4/3 -inf.0)))
1862
1863 (pass-if (eq? #f (< 4/3 +nan.0)))
1864 (pass-if (eq? #f (< -4/3 +nan.0))))
1865
1866 (with-test-prefix "frac/frac"
1867 (pass-if (< 2/3 6/7))
1868 (pass-if (< -2/3 6/7))
1869 (pass-if (< -4/3 6/7))
1870 (pass-if (< -4/3 -6/7))
1871 (pass-if (eq? #f (< 4/3 6/7)))
1872 (pass-if (eq? #f (< 4/3 -6/7)))
1873 (pass-if (eq? #f (< 2/3 -6/7)))
1874 (pass-if (eq? #f (< -2/3 -6/7)))))
f29b3454
DH
1875
1876;;;
1877;;; >
1878;;;
1879
7c24e528
RB
1880;; currently not tested -- implementation is trivial
1881;; (> x y) is implemented as (< y x)
1882;; FIXME: tests should probably be added in case we change implementation.
1883
f29b3454
DH
1884;;;
1885;;; <=
1886;;;
1887
7c24e528
RB
1888;; currently not tested -- implementation is trivial
1889;; (<= x y) is implemented as (not (< y x))
1890;; FIXME: tests should probably be added in case we change implementation.
1891
f29b3454
DH
1892;;;
1893;;; >=
1894;;;
1895
7c24e528
RB
1896;; currently not tested -- implementation is trivial
1897;; (>= x y) is implemented as (not (< x y))
1898;; FIXME: tests should probably be added in case we change implementation.
1899
f29b3454
DH
1900;;;
1901;;; zero?
1902;;;
1903
7c24e528
RB
1904(with-test-prefix "zero?"
1905 (expect-fail (documented? zero?))
1906 (pass-if (zero? 0))
4d332f19
DH
1907 (pass-if (not (zero? 7)))
1908 (pass-if (not (zero? -7)))
1909 (pass-if (not (zero? (+ 1 fixnum-max))))
1910 (pass-if (not (zero? (- 1 fixnum-min))))
1911 (pass-if (not (zero? 1.3)))
1912 (pass-if (not (zero? 3.1+4.2i))))
7c24e528 1913
f29b3454
DH
1914;;;
1915;;; positive?
1916;;;
1917
7c24e528
RB
1918(with-test-prefix "positive?"
1919 (expect-fail (documented? positive?))
1920 (pass-if (positive? 1))
1921 (pass-if (positive? (+ fixnum-max 1)))
1922 (pass-if (positive? 1.3))
4d332f19
DH
1923 (pass-if (not (positive? 0)))
1924 (pass-if (not (positive? -1)))
1925 (pass-if (not (positive? (- fixnum-min 1))))
1926 (pass-if (not (positive? -1.3))))
7c24e528 1927
f29b3454
DH
1928;;;
1929;;; negative?
1930;;;
1931
7c24e528
RB
1932(with-test-prefix "negative?"
1933 (expect-fail (documented? negative?))
4d332f19
DH
1934 (pass-if (not (negative? 1)))
1935 (pass-if (not (negative? (+ fixnum-max 1))))
1936 (pass-if (not (negative? 1.3)))
1937 (pass-if (not (negative? 0)))
7c24e528
RB
1938 (pass-if (negative? -1))
1939 (pass-if (negative? (- fixnum-min 1)))
1940 (pass-if (negative? -1.3)))
1941
f29b3454
DH
1942;;;
1943;;; max
1944;;;
1945
adda36ed 1946(with-test-prefix "max"
593a4c2f
KR
1947 (pass-if-exception "no args" exception:wrong-num-args
1948 (max))
1949
1950 (pass-if-exception "one complex" exception:wrong-type-arg
1951 (max 1+i))
1952
1953 (pass-if-exception "inum/complex" exception:wrong-type-arg
1954 (max 123 1+i))
1955 (pass-if-exception "big/complex" exception:wrong-type-arg
1956 (max 9999999999999999999999999999999999999999 1+i))
1957 (pass-if-exception "real/complex" exception:wrong-type-arg
1958 (max 123.0 1+i))
1959 (pass-if-exception "frac/complex" exception:wrong-type-arg
1960 (max 123/456 1+i))
1961
1962 (pass-if-exception "complex/inum" exception:wrong-type-arg
1963 (max 1+i 123))
1964 (pass-if-exception "complex/big" exception:wrong-type-arg
1965 (max 1+i 9999999999999999999999999999999999999999))
1966 (pass-if-exception "complex/real" exception:wrong-type-arg
1967 (max 1+i 123.0))
1968 (pass-if-exception "complex/frac" exception:wrong-type-arg
1969 (max 1+i 123/456))
1970
adda36ed
KR
1971 (let ((big*2 (* fixnum-max 2))
1972 (big*3 (* fixnum-max 3))
1973 (big*4 (* fixnum-max 4))
1974 (big*5 (* fixnum-max 5)))
501da403 1975
2530518e
KR
1976 (with-test-prefix "inum / frac"
1977 (pass-if (= 3 (max 3 5/2)))
1978 (pass-if (= 5/2 (max 2 5/2))))
1979
1980 (with-test-prefix "frac / inum"
1981 (pass-if (= 3 (max 5/2 3)))
1982 (pass-if (= 5/2 (max 5/2 2))))
1983
23d77957
KR
1984 (with-test-prefix "inum / real"
1985 (pass-if (nan? (max 123 +nan.0))))
1986
1987 (with-test-prefix "real / inum"
1988 (pass-if (nan? (max +nan.0 123))))
1989
2530518e
KR
1990 (with-test-prefix "big / frac"
1991 (pass-if (= big*2 (max big*2 5/2)))
1992 (pass-if (= 5/2 (max (- big*2) 5/2))))
1993
1994 (with-test-prefix "frac / big"
1995 (pass-if (= big*2 (max 5/2 big*2)))
1996 (pass-if (= 5/2 (max 5/2 (- big*2)))))
1997
23d77957
KR
1998 (with-test-prefix "big / real"
1999 (pass-if (nan? (max big*5 +nan.0)))
2000 (pass-if (= big*5 (max big*5 -inf.0)))
2001 (pass-if (= +inf.0 (max big*5 +inf.0)))
2002 (pass-if (= 1.0 (max (- big*5) 1.0)))
2003 (pass-if (inexact? (max big*5 1.0)))
2004 (pass-if (= (exact->inexact big*5) (max big*5 1.0))))
2005
2006 (with-test-prefix "real / big"
2007 (pass-if (nan? (max +nan.0 big*5)))
2008 (pass-if (= +inf.0 (max +inf.0 big*5)))
2009 (pass-if (= big*5 (max -inf.0 big*5)))
2010 (pass-if (= 1.0 (max 1.0 (- big*5))))
2011 (pass-if (inexact? (max 1.0 big*5)))
2012 (pass-if (= (exact->inexact big*5) (max 1.0 big*5))))
2013
2530518e
KR
2014 (with-test-prefix "frac / frac"
2015 (pass-if (= 2/3 (max 1/2 2/3)))
2016 (pass-if (= 2/3 (max 2/3 1/2)))
2017 (pass-if (= -1/2 (max -1/2 -2/3)))
2018 (pass-if (= -1/2 (max -2/3 -1/2))))
2019
23d77957
KR
2020 (with-test-prefix "real / real"
2021 (pass-if (nan? (max 123.0 +nan.0)))
2022 (pass-if (nan? (max +nan.0 123.0)))
2023 (pass-if (nan? (max +nan.0 +nan.0)))
2024 (pass-if (= 456.0 (max 123.0 456.0)))
2025 (pass-if (= 456.0 (max 456.0 123.0)))))
adda36ed
KR
2026
2027 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
2028 ;; sure we've avoided that
2029 (for-each (lambda (b)
2030 (pass-if (list b +inf.0)
2031 (= +inf.0 (max b +inf.0)))
2032 (pass-if (list +inf.0 b)
2033 (= +inf.0 (max b +inf.0)))
2034 (pass-if (list b -inf.0)
23d77957 2035 (= (exact->inexact b) (max b -inf.0)))
adda36ed 2036 (pass-if (list -inf.0 b)
23d77957 2037 (= (exact->inexact b) (max b -inf.0))))
adda36ed
KR
2038 (list (1- (ash 1 1024))
2039 (ash 1 1024)
2040 (1+ (ash 1 1024))
2041 (- (1- (ash 1 1024)))
2042 (- (ash 1 1024))
501da403
KR
2043 (- (1+ (ash 1 1024)))))
2044
2045 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
2046 ;; sure we've avoided that
2047 (pass-if (nan? (max (ash 1 2048) +nan.0)))
2048 (pass-if (nan? (max +nan.0 (ash 1 2048)))))
adda36ed 2049
f29b3454
DH
2050;;;
2051;;; min
2052;;;
2053
7c24e528
RB
2054;; FIXME: unfinished...
2055
2056(with-test-prefix "min"
593a4c2f
KR
2057 (pass-if-exception "no args" exception:wrong-num-args
2058 (min))
2059
2060 (pass-if-exception "one complex" exception:wrong-type-arg
2061 (min 1+i))
2062
2063 (pass-if-exception "inum/complex" exception:wrong-type-arg
2064 (min 123 1+i))
2065 (pass-if-exception "big/complex" exception:wrong-type-arg
2066 (min 9999999999999999999999999999999999999999 1+i))
2067 (pass-if-exception "real/complex" exception:wrong-type-arg
2068 (min 123.0 1+i))
2069 (pass-if-exception "frac/complex" exception:wrong-type-arg
2070 (min 123/456 1+i))
2071
2072 (pass-if-exception "complex/inum" exception:wrong-type-arg
2073 (min 1+i 123))
2074 (pass-if-exception "complex/big" exception:wrong-type-arg
2075 (min 1+i 9999999999999999999999999999999999999999))
2076 (pass-if-exception "complex/real" exception:wrong-type-arg
2077 (min 1+i 123.0))
2078 (pass-if-exception "complex/frac" exception:wrong-type-arg
2079 (min 1+i 123/456))
2080
7c24e528
RB
2081 (let ((big*2 (* fixnum-max 2))
2082 (big*3 (* fixnum-max 3))
2083 (big*4 (* fixnum-max 4))
2084 (big*5 (* fixnum-max 5)))
23d77957 2085
7f703e0d 2086 (expect-fail (documented? min))
7c24e528
RB
2087 (pass-if (= 1 (min 7 3 1 5)))
2088 (pass-if (= 1 (min 1 7 3 5)))
2089 (pass-if (= 1 (min 7 3 5 1)))
2090 (pass-if (= -7 (min 2 3 4 -2 5 -7 1 -1 4 2)))
2091 (pass-if (= -7 (min -7 2 3 4 -2 5 1 -1 4 2)))
2092 (pass-if (= -7 (min 2 3 4 -2 5 1 -1 4 2 -7)))
2093 (pass-if (= big*2 (min big*3 big*5 big*2 big*4)))
2094 (pass-if (= big*2 (min big*2 big*3 big*5 big*4)))
2095 (pass-if (= big*2 (min big*3 big*5 big*4 big*2)))
2096 (pass-if
2097 (= (- fixnum-min 1) (min 2 4 (- fixnum-min 1) 3 (* 2 fixnum-max))))
2098 (pass-if
2099 (= (- fixnum-min 1) (min (- fixnum-min 1) 2 4 3 (* 2 fixnum-max))))
2100 (pass-if
adda36ed 2101 (= (- fixnum-min 1) (min 2 4 3 (* 2 fixnum-max) (- fixnum-min 1))))
23d77957 2102
2530518e
KR
2103 (with-test-prefix "inum / frac"
2104 (pass-if (= 5/2 (min 3 5/2)))
2105 (pass-if (= 2 (min 2 5/2))))
2106
2107 (with-test-prefix "frac / inum"
2108 (pass-if (= 5/2 (min 5/2 3)))
2109 (pass-if (= 2 (min 5/2 2))))
2110
23d77957
KR
2111 (with-test-prefix "inum / real"
2112 (pass-if (nan? (min 123 +nan.0))))
2113
2114 (with-test-prefix "real / inum"
2115 (pass-if (nan? (min +nan.0 123))))
2116
2530518e
KR
2117 (with-test-prefix "big / frac"
2118 (pass-if (= 5/2 (min big*2 5/2)))
2119 (pass-if (= (- big*2) (min (- big*2) 5/2))))
2120
2121 (with-test-prefix "frac / big"
2122 (pass-if (= 5/2 (min 5/2 big*2)))
2123 (pass-if (= (- big*2) (min 5/2 (- big*2)))))
2124
23d77957
KR
2125 (with-test-prefix "big / real"
2126 (pass-if (nan? (min big*5 +nan.0)))
2127 (pass-if (= big*5 (min big*5 +inf.0)))
2128 (pass-if (= -inf.0 (min big*5 -inf.0)))
2129 (pass-if (= 1.0 (min big*5 1.0)))
2130 (pass-if (inexact? (min (- big*5) 1.0)))
2131 (pass-if (= (exact->inexact (- big*5)) (min (- big*5) 1.0))))
2132
2133 (with-test-prefix "real / big"
2134 (pass-if (nan? (min +nan.0 big*5)))
2135 (pass-if (= big*5 (min +inf.0 big*5)))
2136 (pass-if (= -inf.0 (min -inf.0 big*5)))
2137 (pass-if (= 1.0 (min 1.0 big*5)))
2138 (pass-if (inexact? (min 1.0 (- big*5))))
2139 (pass-if (= (exact->inexact (- big*5)) (min 1.0 (- big*5)))))
2140
2530518e
KR
2141 (with-test-prefix "frac / frac"
2142 (pass-if (= 1/2 (min 1/2 2/3)))
2143 (pass-if (= 1/2 (min 2/3 1/2)))
2144 (pass-if (= -2/3 (min -1/2 -2/3)))
2145 (pass-if (= -2/3 (min -2/3 -1/2))))
2146
23d77957
KR
2147 (with-test-prefix "real / real"
2148 (pass-if (nan? (min 123.0 +nan.0)))
2149 (pass-if (nan? (min +nan.0 123.0)))
2150 (pass-if (nan? (min +nan.0 +nan.0)))
2151 (pass-if (= 123.0 (min 123.0 456.0)))
2152 (pass-if (= 123.0 (min 456.0 123.0)))))
2153
2154
adda36ed
KR
2155 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
2156 ;; sure we've avoided that
2157 (for-each (lambda (b)
2158 (pass-if (list b +inf.0)
23d77957 2159 (= (exact->inexact b) (min b +inf.0)))
adda36ed 2160 (pass-if (list +inf.0 b)
23d77957 2161 (= (exact->inexact b) (min b +inf.0)))
adda36ed
KR
2162 (pass-if (list b -inf.0)
2163 (= -inf.0 (min b -inf.0)))
2164 (pass-if (list -inf.0 b)
2165 (= -inf.0 (min b -inf.0))))
2166 (list (1- (ash 1 1024))
2167 (ash 1 1024)
2168 (1+ (ash 1 1024))
2169 (- (1- (ash 1 1024)))
2170 (- (ash 1 1024))
501da403
KR
2171 (- (1+ (ash 1 1024)))))
2172
2173 ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
2174 ;; sure we've avoided that
2175 (pass-if (nan? (min (- (ash 1 2048)) (- +nan.0))))
2176 (pass-if (nan? (min (- +nan.0) (- (ash 1 2048))))))
adda36ed 2177
f29b3454
DH
2178;;;
2179;;; +
2180;;;
2181
2182(with-test-prefix "+"
2183
2184 (expect-fail "documented?"
2185 (documented? +))
2186
2187 (with-test-prefix "wrong type argument"
2188
2189 (pass-if-exception "1st argument string"
2190 exception:wrong-type-arg
2191 (+ "1" 2))
2192
2193 (pass-if-exception "2nd argument bool"
2194 exception:wrong-type-arg
2195 (+ 1 #f))))
2196;;;
2197;;; -
2198;;;
2199
072e6de2
KR
2200(with-test-prefix "-"
2201
2202 (pass-if "-inum - +bignum"
2203 (= #x-100000000000000000000000000000001
ef016629
KR
2204 (- -1 #x100000000000000000000000000000000)))
2205
2206 (pass-if "big - inum"
2207 (= #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
2208 (- #x100000000000000000000000000000000 1)))
2209
2210 (pass-if "big - -inum"
2211 (= #x100000000000000000000000000000001
2212 (- #x100000000000000000000000000000000 -1))))
072e6de2 2213
f29b3454
DH
2214;;;
2215;;; *
2216;;;
2217
65ea251e
KR
2218(with-test-prefix "*"
2219
2220 (pass-if "complex * bignum"
2221 (let ((big (ash 1 90)))
2222 (= (make-rectangular big big)
2223 (* 1+1i big)))))
2224
f29b3454
DH
2225;;;
2226;;; /
2227;;;
2228
1b3a7932
DH
2229(with-test-prefix "/"
2230
2231 (expect-fail "documented?"
2232 (documented? /))
2233
2234 (with-test-prefix "division by zero"
2235
2236 (pass-if-exception "(/ 0)"
2237 exception:numerical-overflow
2238 (/ 0))
2239
cdf52e3d
MV
2240 (pass-if "(/ 0.0)"
2241 (= +inf.0 (/ 0.0)))
80074d77 2242
1b3a7932
DH
2243 (pass-if-exception "(/ 1 0)"
2244 exception:numerical-overflow
80074d77
DH
2245 (/ 1 0))
2246
cdf52e3d
MV
2247 (pass-if "(/ 1 0.0)"
2248 (= +inf.0 (/ 1 0.0)))
80074d77
DH
2249
2250 (pass-if-exception "(/ bignum 0)"
2251 exception:numerical-overflow
2252 (/ (+ fixnum-max 1) 0))
2253
cdf52e3d
MV
2254 (pass-if "(/ bignum 0.0)"
2255 (= +inf.0 (/ (+ fixnum-max 1) 0.0)))
80074d77
DH
2256
2257 (pass-if-exception "(/ 1.0 0)"
2258 exception:numerical-overflow
2259 (/ 1.0 0))
2260
cdf52e3d
MV
2261 (pass-if "(/ 1.0 0.0)"
2262 (= +inf.0 (/ 1.0 0.0)))
80074d77
DH
2263
2264 (pass-if-exception "(/ +i 0)"
2265 exception:numerical-overflow
2266 (/ +i 0))
2267
cdf52e3d
MV
2268 (pass-if "(/ +i 0.0)"
2269 (= +inf.0 (imag-part (/ +i 0.0)))))
469b963c
MV
2270
2271 (with-test-prefix "complex division"
2272
2273 (pass-if "(/ 3+4i)"
2274 (= (/ 3+4i) 0.12-0.16i))
2275
2276 (pass-if "(/ 4+3i)"
2277 (= (/ 4+3i) 0.16-0.12i))
2278
2279 (pass-if "(/ 25+125i 3+4i)"
2280 (= (/ 25+125i 3+4i) 23.0+11.0i))
2281
2282 (pass-if "(/ 25+125i 4+3i)"
2283 (= (/ 25+125i 4+3i) 19.0+17.0i))
2284
2285 (pass-if "(/ 25 3+4i)"
2286 (= (/ 25 3+4i) 3.0-4.0i))
2287
2288 (pass-if "(/ 25 4+3i)"
2289 (= (/ 25 4+3i) 4.0-3.0i))
2290
2291 (pass-if "(/ 1e200+1e200i)"
2292 (= (/ 1e200+1e200i) 5.0e-201-5.0e-201i))))
1b3a7932 2293
f29b3454
DH
2294;;;
2295;;; truncate
2296;;;
2297
14a6784c
KR
2298(with-test-prefix "truncate"
2299 (pass-if (= 1 (truncate 1.75)))
2300 (pass-if (= 1 (truncate 1.5)))
2301 (pass-if (= 1 (truncate 1.25)))
2302 (pass-if (= 0 (truncate 0.75)))
2303 (pass-if (= 0 (truncate 0.5)))
2304 (pass-if (= 0 (truncate 0.0)))
2305 (pass-if (= 0 (truncate -0.5)))
2306 (pass-if (= -1 (truncate -1.25)))
2307 (pass-if (= -1 (truncate -1.5))))
2308
f29b3454
DH
2309;;;
2310;;; round
2311;;;
2312
14a6784c
KR
2313(with-test-prefix "round"
2314 (pass-if (= 2 (round 1.75)))
2315 (pass-if (= 2 (round 1.5)))
2316 (pass-if (= 1 (round 1.25)))
2317 (pass-if (= 1 (round 0.75)))
2318 (pass-if (= 0 (round 0.5)))
2319 (pass-if (= 0 (round 0.0)))
2320 (pass-if (= 0 (round -0.5)))
2321 (pass-if (= -1 (round -1.25)))
abff733b
KR
2322 (pass-if (= -2 (round -1.5)))
2323
2324 (with-test-prefix "inum"
2325 (pass-if "0"
2326 (and (= 0 (round 0))
2327 (exact? (round 0))))
2328
2329 (pass-if "1"
2330 (and (= 1 (round 1))
2331 (exact? (round 1))))
2332
2333 (pass-if "-1"
2334 (and (= -1 (round -1))
2335 (exact? (round -1)))))
2336
2337 (with-test-prefix "bignum"
2338 (let ((x (1+ most-positive-fixnum)))
2339 (pass-if "(1+ most-positive-fixnum)"
2340 (and (= x (round x))
2341 (exact? (round x)))))
2342
2343 (let ((x (1- most-negative-fixnum)))
2344 (pass-if "(1- most-negative-fixnum)"
2345 (and (= x (round x))
2346 (exact? (round x))))))
2347
2348 (with-test-prefix "real"
2349 (pass-if "0.0"
2350 (and (= 0.0 (round 0.0))
2351 (inexact? (round 0.0))))
2352
2353 (pass-if "1.0"
2354 (and (= 1.0 (round 1.0))
2355 (inexact? (round 1.0))))
2356
2357 (pass-if "-1.0"
2358 (and (= -1.0 (round -1.0))
2359 (inexact? (round -1.0))))
2360
2361 (pass-if "-3.1"
2362 (and (= -3.0 (round -3.1))
2363 (inexact? (round -3.1))))
2364
2365 (pass-if "3.1"
2366 (and (= 3.0 (round 3.1))
2367 (inexact? (round 3.1))))
2368
2369 (pass-if "3.9"
2370 (and (= 4.0 (round 3.9))
2371 (inexact? (round 3.9))))
2372
2373 (pass-if "-3.9"
2374 (and (= -4.0 (round -3.9))
2375 (inexact? (round -3.9))))
2376
2377 (pass-if "1.5"
2378 (and (= 2.0 (round 1.5))
2379 (inexact? (round 1.5))))
2380
2381 (pass-if "2.5"
2382 (and (= 2.0 (round 2.5))
2383 (inexact? (round 2.5))))
2384
2385 (pass-if "3.5"
2386 (and (= 4.0 (round 3.5))
2387 (inexact? (round 3.5))))
2388
2389 (pass-if "-1.5"
2390 (and (= -2.0 (round -1.5))
2391 (inexact? (round -1.5))))
2392
2393 (pass-if "-2.5"
2394 (and (= -2.0 (round -2.5))
2395 (inexact? (round -2.5))))
2396
2397 (pass-if "-3.5"
2398 (and (= -4.0 (round -3.5))
2399 (inexact? (round -3.5))))
2400
2401 ;; prior to guile 1.6.5, on an IEEE system an inexact 2^53-1 (ie. a
2402 ;; float with mantissa all ones) came out as 2^53 from `round' (except
2403 ;; on i386 and m68k systems using the coprocessor and optimizing, where
2404 ;; extra precision hid the problem)
2405 (pass-if "2^53-1"
2406 (let ((x (exact->inexact (1- (ash 1 53)))))
2407 (and (= x (round x))
2408 (inexact? (round x)))))
2409 (pass-if "-(2^53-1)"
2410 (let ((x (exact->inexact (- (1- (ash 1 53))))))
2411 (and (= x (round x))
2412 (inexact? (round x)))))))
14a6784c 2413
f29b3454
DH
2414;;;
2415;;; exact->inexact
2416;;;
2417
a1fb3b1c
KR
2418(with-test-prefix "exact->inexact"
2419
2420 ;; Test "(exact->inexact n)", expect "want".
2421 ;; "i" is a index, for diagnostic purposes.
2422 (define (try-i i n want)
2423 (with-test-prefix (list i n want)
2424 (with-test-prefix "pos"
2425 (let ((got (exact->inexact n)))
2426 (pass-if "inexact?" (inexact? got))
2427 (pass-if (list "=" got) (= want got))))
2428 (set! n (- n))
2429 (set! want (- want))
2430 (with-test-prefix "neg"
2431 (let ((got (exact->inexact n)))
2432 (pass-if "inexact?" (inexact? got))
2433 (pass-if (list "=" got) (= want got))))))
2434
2435 (with-test-prefix "2^i, no round"
2436 (do ((i 0 (1+ i))
2437 (n 1 (* 2 n))
2438 (want 1.0 (* 2.0 want)))
2439 ((> i 100))
2440 (try-i i n want)))
2441
2442 (with-test-prefix "2^i+1, no round"
2443 (do ((i 1 (1+ i))
2444 (n 3 (1- (* 2 n)))
2445 (want 3.0 (- (* 2.0 want) 1.0)))
2446 ((>= i dbl-mant-dig))
2447 (try-i i n want)))
2448
2449 (with-test-prefix "(2^i+1)*2^100, no round"
2450 (do ((i 1 (1+ i))
2451 (n 3 (1- (* 2 n)))
2452 (want 3.0 (- (* 2.0 want) 1.0)))
2453 ((>= i dbl-mant-dig))
2454 (try-i i (ash n 100) (ash-flo want 100))))
2455
2456 ;; bit pattern: 1111....11100.00
2457 ;; <-mantdig-><-i->
2458 ;;
2459 (with-test-prefix "mantdig ones then zeros, no rounding"
2460 (do ((i 0 (1+ i))
2461 (n (- (ash 1 dbl-mant-dig) 1) (* 2 n))
2462 (want (- (ash-flo 1.0 dbl-mant-dig) 1.0) (* 2.0 want)))
2463 ((> i 100))
2464 (try-i i n want)))
2465
2466 ;; bit pattern: 1111....111011..1
2467 ;; <-mantdig-> <-i->
2468 ;; This sort of value was incorrectly rounded upwards in Guile 1.6.4 when
2469 ;; i >= 11 (that's when the total is 65 or more bits).
2470 ;;
2471 (with-test-prefix "mantdig ones then 011..11, round down"
2472 (do ((i 0 (1+ i))
2473 (n (- (ash 1 (+ 1 dbl-mant-dig)) 2) (+ 1 (* 2 n)))
2474 (want (- (ash-flo 1.0 (+ 1 dbl-mant-dig)) 2.0) (* 2.0 want)))
2475 ((> i 100))
2476 (try-i i n want)))
2477
2478 ;; bit pattern: 1111....111100..001
2479 ;; <-mantdig-> <--i->
2480 ;;
2481 (with-test-prefix "mantdig ones then 100..001, round up"
2482 (do ((i 0 (1+ i))
2483 (n (- (ash 1 (+ 2 dbl-mant-dig)) 1) (1- (* 2 n)))
2484 (want (ash-flo 1.0 (+ 2 dbl-mant-dig)) (* 2.0 want)))
2485 ((> i 100))
2486 (try-i i n want)))
2487
2488 ;; bit pattern: 1000....000100..001
2489 ;; <-mantdig-> <--i->
2490 ;;
2491 (with-test-prefix "2^mantdig then 100..001, round up"
2492 (do ((i 0 (1+ i))
2493 (n (- (ash 1 (+ 2 dbl-mant-dig)) 1) (1- (* 2 n)))
2494 (want (+ (ash-flo 1.0 (+ 2 dbl-mant-dig)) 4.0) (* 2.0 want)))
2495 ((> i 100))
2496 (try-i i n want))))
2497
f29b3454
DH
2498;;;
2499;;; floor
2500;;;
2501
2502;;;
2503;;; ceiling
2504;;;
2505
46f2c0f1
RB
2506;;;
2507;;; expt
2508;;;
2509
2510(with-test-prefix "expt"
2511 (pass-if "(= 1 (expt 0 0))" (= 1 (expt 0 0)))
2512 (pass-if "(= 1 (expt 0 0.0))" (= 1 (expt 0 0.0)))
2513 (pass-if "(= 1 (expt 0.0 0))" (= 1 (expt 0.0 0)))
2514 (pass-if "(= 1 (expt 0.0 0.0))" (= 1 (expt 0.0 0.0))))
2515
14a6784c
KR
2516;;;
2517;;; asinh
2518;;;
2519
2520(with-test-prefix "asinh"
2521 (pass-if (= 0 (asinh 0))))
2522
2523;;;
2524;;; acosh
2525;;;
2526
2527(with-test-prefix "acosh"
2528 (pass-if (= 0 (acosh 1))))
2529
2530;;;
2531;;; atanh
2532;;;
2533
2534(with-test-prefix "atanh"
2535 (pass-if (= 0 (atanh 0))))
2536
f29b3454
DH
2537;;;
2538;;; make-rectangular
2539;;;
2540
2541;;;
2542;;; make-polar
2543;;;
2544
d40681ec
KR
2545(with-test-prefix "make-polar"
2546 (define pi 3.14159265358979323846)
2547 (define (almost= x y)
2548 (> 0.01 (magnitude (- x y))))
2549
2550 (pass-if (= 0 (make-polar 0 0)))
2551 (pass-if (= 0 (make-polar 0 123.456)))
2552 (pass-if (= 1 (make-polar 1 0)))
2553 (pass-if (= -1 (make-polar -1 0)))
2554
2555 (pass-if (almost= 0+i (make-polar 1 (* 0.5 pi))))
2556 (pass-if (almost= -1 (make-polar 1 (* 1.0 pi))))
2557 (pass-if (almost= 0-i (make-polar 1 (* 1.5 pi))))
2558 (pass-if (almost= 1 (make-polar 1 (* 2.0 pi)))))
2559
f29b3454
DH
2560;;;
2561;;; real-part
2562;;;
2563
2564;;;
2565;;; imag-part
2566;;;
2567
2568;;;
2569;;; magnitude
2570;;;
2571
d40681ec
KR
2572(with-test-prefix "magnitude"
2573 (pass-if (= 0 (magnitude 0)))
2574 (pass-if (= 1 (magnitude 1)))
2575 (pass-if (= 1 (magnitude -1)))
2576 (pass-if (= 1 (magnitude 0+i)))
2577 (pass-if (= 1 (magnitude 0-i)))
2578 (pass-if (= 5 (magnitude 3+4i)))
2579 (pass-if (= 5 (magnitude 3-4i)))
2580 (pass-if (= 5 (magnitude -3+4i)))
2581 (pass-if (= 5 (magnitude -3-4i))))
2582
f29b3454
DH
2583;;;
2584;;; angle
2585;;;
2586
cfc9fc1c
KR
2587(with-test-prefix "angle"
2588 (define pi 3.14159265358979323846)
2589 (define (almost= x y)
2590 (> 0.01 (magnitude (- x y))))
2591
2592 (pass-if "inum +ve" (= 0 (angle 1)))
2593 (pass-if "inum -ve" (almost= pi (angle -1)))
2594
2595 (pass-if "bignum +ve" (= 0 (angle (1+ fixnum-max))))
2596 (pass-if "bignum -ve" (almost= pi (angle (1- fixnum-min))))
2597
2598 (pass-if "flonum +ve" (= 0 (angle 1.5)))
2599 (pass-if "flonum -ve" (almost= pi (angle -1.5))))
2600
f29b3454
DH
2601;;;
2602;;; inexact->exact
2603;;;
300c6a76 2604
1259cb26
KR
2605(with-test-prefix "inexact->exact"
2606
9dd9857f 2607 (pass-if-exception "+inf" exception:out-of-range
a409f865 2608 (inexact->exact +inf.0))
1259cb26 2609
9dd9857f 2610 (pass-if-exception "-inf" exception:out-of-range
a409f865 2611 (inexact->exact -inf.0))
1259cb26 2612
9dd9857f 2613 (pass-if-exception "nan" exception:out-of-range
a409f865 2614 (inexact->exact +nan.0))
1259cb26
KR
2615
2616 (with-test-prefix "2.0**i to exact and back"
2617 (do ((i 0 (1+ i))
2618 (n 1.0 (* 2.0 n)))
2619 ((> i 100))
2620 (pass-if (list i n)
2621 (= n (inexact->exact (exact->inexact n)))))))
2622
c1122753
KR
2623;;;
2624;;; integer-expt
2625;;;
2626
2627(with-test-prefix "integer-expt"
2628
2629 (pass-if-exception "2^+inf" exception:wrong-type-arg
2630 (integer-expt 2 +inf.0))
2631 (pass-if-exception "2^-inf" exception:wrong-type-arg
2632 (integer-expt 2 -inf.0))
2633 (pass-if-exception "2^nan" exception:wrong-type-arg
2634 (integer-expt 2 +nan.0)))
2635
a04a3604
KR
2636;;;
2637;;; integer-length
2638;;;
2639
2640(with-test-prefix "integer-length"
2641
2642 (with-test-prefix "-2^i, ...11100..00"
2643 (do ((n -1 (ash n 1))
2644 (i 0 (1+ i)))
2645 ((> i 256))
2646 (pass-if (list n "expect" i)
2647 (= i (integer-length n)))))
2648
2649 (with-test-prefix "-2^i+1 ...11100..01"
2650 (do ((n -3 (logxor 3 (ash n 1)))
2651 (i 2 (1+ i)))
2652 ((> i 256))
2653 (pass-if n
2654 (= i (integer-length n)))))
2655
2656 (with-test-prefix "-2^i-1 ...111011..11"
2657 (do ((n -2 (1+ (ash n 1)))
2658 (i 1 (1+ i)))
2659 ((> i 256))
2660 (pass-if n
2661 (= i (integer-length n))))))
2662
abff733b
KR
2663;;;
2664;;; logbit?
2665;;;
2666
2667(with-test-prefix "logbit?"
2668 (pass-if (eq? #f (logbit? 0 0)))
2669 (pass-if (eq? #f (logbit? 1 0)))
2670 (pass-if (eq? #f (logbit? 31 0)))
2671 (pass-if (eq? #f (logbit? 32 0)))
2672 (pass-if (eq? #f (logbit? 33 0)))
2673 (pass-if (eq? #f (logbit? 63 0)))
2674 (pass-if (eq? #f (logbit? 64 0)))
2675 (pass-if (eq? #f (logbit? 65 0)))
2676
2677 ;; prior to guile 1.6.5, testing bit 32, 64 etc of value 1 would wrap
2678 ;; around and return #t where it ought to be #f
2679 (pass-if (eq? #t (logbit? 0 1)))
2680 (pass-if (eq? #f (logbit? 1 1)))
2681 (pass-if (eq? #f (logbit? 31 1)))
2682 (pass-if (eq? #f (logbit? 32 1)))
2683 (pass-if (eq? #f (logbit? 33 1)))
2684 (pass-if (eq? #f (logbit? 63 1)))
2685 (pass-if (eq? #f (logbit? 64 1)))
2686 (pass-if (eq? #f (logbit? 65 1)))
2687 (pass-if (eq? #f (logbit? 128 1)))
2688
2689 (pass-if (eq? #t (logbit? 0 -1)))
2690 (pass-if (eq? #t (logbit? 1 -1)))
2691 (pass-if (eq? #t (logbit? 31 -1)))
2692 (pass-if (eq? #t (logbit? 32 -1)))
2693 (pass-if (eq? #t (logbit? 33 -1)))
2694 (pass-if (eq? #t (logbit? 63 -1)))
2695 (pass-if (eq? #t (logbit? 64 -1)))
2696 (pass-if (eq? #t (logbit? 65 -1))))
2697
300c6a76
KR
2698;;;
2699;;; logcount
2700;;;
2701
2702(with-test-prefix "logcount"
2703
2704 (with-test-prefix "-2^i, meaning ...11100..00"
2705 (do ((n -1 (ash n 1))
2706 (i 0 (1+ i)))
2707 ((> i 256))
795c0bae
KR
2708 (pass-if n
2709 (= i (logcount n)))))
2710
2711 (with-test-prefix "2^i"
2712 (do ((n 1 (ash n 1))
2713 (i 0 (1+ i)))
2714 ((> i 256))
2715 (pass-if n
2716 (= 1 (logcount n)))))
2717
2718 (with-test-prefix "2^i-1"
2719 (do ((n 0 (1+ (ash n 1)))
2720 (i 0 (1+ i)))
2721 ((> i 256))
300c6a76
KR
2722 (pass-if n
2723 (= i (logcount n))))))
795c0bae 2724
1ec2dd6f
KR
2725;;;
2726;;; lognot
2727;;;
2728
2729(with-test-prefix "lognot"
2730 (pass-if (= -1 (lognot 0)))
2731 (pass-if (= 0 (lognot -1)))
2732 (pass-if (= -2 (lognot 1)))
2733 (pass-if (= 1 (lognot -2)))
2734
2735 (pass-if (= #x-100000000000000000000000000000000
2736 (lognot #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)))
2737 (pass-if (= #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
2738 (lognot #x-100000000000000000000000000000000))))