Debian Codegraph @

scheme/tests/numerics.ss

raw
1
2 (library (tests numerics)
3 (export run-tests)
4 (import (ikarus))
5
6 (define (test-rounding)
7 (define (test-round x)
8 (let ([rx (round x)])
9 (unless (integer? rx)
10 (error 'test-round "not an integer result for" x rx))
11 (let ([diff (abs (- (abs x) (abs rx)))])
12 (cond
13 [(= diff 1/2)
14 (unless (even? rx)
15 (error 'test-round "non-even rounding for" x rx))]
16 [else
17 (unless (< diff 1/2)
18 (error 'test-round "rounding the wrong way for" x rx))]))))
19 (test-round -251/100)
20 (test-round -250/100)
21 (test-round -249/100)
22 (test-round +251/100)
23 (test-round +250/100)
24 (test-round +249/100)
25
26 (test-round -151/100)
27 (test-round -150/100)
28 (test-round -149/100)
29 (test-round +151/100)
30 (test-round +150/100)
31 (test-round +149/100)
32
33 (test-round -351/100)
34 (test-round -350/100)
35 (test-round -349/100)
36 (test-round +351/100)
37 (test-round +350/100)
38 (test-round +349/100)
39
40 (test-round +0.00)
41 (test-round -0.00)
42 (test-round +0.49)
43 (test-round -0.49)
44 (test-round +1.49)
45 (test-round -1.49)
46 (test-round +2.49)
47 (test-round -2.49)
48 (test-round +3.49)
49 (test-round -3.49)
50 (test-round +0.51)
51 (test-round -0.51)
52 (test-round +1.51)
53 (test-round -1.51)
54 (test-round +2.51)
55 (test-round -2.51)
56 (test-round +3.51)
57 (test-round -3.51)
58 (test-round +0.50)
59 (test-round -0.50)
60 (test-round +1.50)
61 (test-round -1.50)
62 (test-round +2.50)
63 (test-round -2.50)
64 (test-round +3.50)
65 (test-round -3.50)
66
67
68
69 )
70
71 (define (test-eqv)
72 (define (test-eqv x y result)
73 (unless (eqv? (eqv? x y) result)
74 (error 'test-eqv "failed" x y result)))
75 (test-eqv 0 0 #t)
76 (test-eqv 0.0 0 #f)
77 (test-eqv 0 0.0 #f)
78 (test-eqv 0.0 0.0 #t)
79 (test-eqv 0.0 -0.0 #f)
80 (test-eqv -0.0 0.0 #f)
81 (test-eqv -0.0 -0.0 #t)
82 ;;; ikarus extensions, not guaranteed by R6RS
83 (test-eqv +nan.0 +nan.0 #t)
84 (test-eqv (/ +inf.0 +inf.0) (/ -inf.0 +inf.0) #t)
85 (test-eqv +nan.0 (string->number "+nan.0") #t))
86
87 (define (test-exact-integer-sqrt)
88 (define (f i j inc)
89 (when (< i j)
90 (let-values ([(s r) (exact-integer-sqrt i)])
91 (unless (and (= (+ (* s s) r) i)
92 (< i (* (+ s 1) (+ s 1))))
93 (error 'exact-integer-sqrt "wrong result" i))
94 (f (+ i inc) j inc))))
95 (f 0 10000 1)
96 (f 0 536870911 10000)
97 (f 0 536870911000 536870911))
98
99
100 (define (run-tests)
101 (test-rounding)
102 (test-exact-integer-sqrt)
103 (test-eqv))
104 )