| 1 |
|
| 2 |
(library (tests fixnums) |
| 3 |
(export run-tests) |
| 4 |
(import (ikarus)) |
| 5 |
|
| 6 |
|
| 7 |
(define (run-tests) |
| 8 |
(test-fxdiv-and-mod) |
| 9 |
(test-fxdiv0-and-mod0) |
| 10 |
(test-fxlength) |
| 11 |
(test-fxcarry)) |
| 12 |
|
| 13 |
|
| 14 |
(define (test-fxcarry) |
| 15 |
(define (fx*/carry-reference fx1 fx2 fx3) |
| 16 |
(let* ([s (+ (* fx1 fx2) fx3)] |
| 17 |
[s0 (mod0 s (expt 2 (fixnum-width)))] |
| 18 |
[s1 (div0 s (expt 2 (fixnum-width)))]) |
| 19 |
(values s0 s1))) |
| 20 |
(define (fx+/carry-reference fx1 fx2 fx3) |
| 21 |
(let* ([s (+ (+ fx1 fx2) fx3)] |
| 22 |
[s0 (mod0 s (expt 2 (fixnum-width)))] |
| 23 |
[s1 (div0 s (expt 2 (fixnum-width)))]) |
| 24 |
(values s0 s1))) |
| 25 |
(define (fx-/carry-reference fx1 fx2 fx3) |
| 26 |
(let* ([s (- (- fx1 fx2) fx3)] |
| 27 |
[s0 (mod0 s (expt 2 (fixnum-width)))] |
| 28 |
[s1 (div0 s (expt 2 (fixnum-width)))]) |
| 29 |
(values s0 s1))) |
| 30 |
(define (test name fxop/carry fxop/carry-reference fx1 fx2 fx3) |
| 31 |
(let-values ([(s0 s1) (fxop/carry fx1 fx2 fx3)] |
| 32 |
[(s2 s3) (fxop/carry-reference fx1 fx2 fx3)]) |
| 33 |
(unless (fx= s0 s2) |
| 34 |
(error name "failed (value1) on ~s ~s ~s, got ~s, should be ~s" |
| 35 |
fx1 fx2 fx3 s0 s2)) |
| 36 |
(unless (fx= s1 s3) |
| 37 |
(error name "failed (value2) on ~s ~s ~s, got ~s, should be ~s" |
| 38 |
fx1 fx2 fx3 s1 s3)))) |
| 39 |
(define ls |
| 40 |
(list 0 1 2 -1 -2 38734 -3843 2484598 -348732487 (greatest-fixnum) (least-fixnum))) |
| 41 |
(printf "[~s: test-fxcarry] " (expt (length ls) 3)) |
| 42 |
(for-each |
| 43 |
(lambda (fx1) |
| 44 |
(for-each |
| 45 |
(lambda (fx2) |
| 46 |
(for-each |
| 47 |
(lambda (fx3) |
| 48 |
(test 'fx*/carry fx*/carry fx*/carry-reference fx1 fx2 fx3) |
| 49 |
(test 'fx+/carry fx+/carry fx+/carry-reference fx1 fx2 fx3) |
| 50 |
(test 'fx-/carry fx-/carry fx-/carry-reference fx1 fx2 fx3)) |
| 51 |
ls)) |
| 52 |
ls)) |
| 53 |
ls) |
| 54 |
) |
| 55 |
|
| 56 |
|
| 57 |
(define (test-fxdiv-and-mod) |
| 58 |
(define (test x1 x2) |
| 59 |
(let-values ([(d m) (fxdiv-and-mod x1 x2)]) |
| 60 |
(printf "(fxdiv-and-mod ~s ~s) = ~s ~s\n" x1 x2 d m) |
| 61 |
(assert (= d (fxdiv x1 x2))) |
| 62 |
(assert (= m (fxmod x1 x2))) |
| 63 |
(assert (<= 0 m)) |
| 64 |
(assert (< m (abs x2))) |
| 65 |
(assert (= x1 (+ (* d x2) m))))) |
| 66 |
|
| 67 |
(test +17 +3) |
| 68 |
(test +17 -3) |
| 69 |
(test -17 +3) |
| 70 |
(test -17 -3) |
| 71 |
(test +16 +3) |
| 72 |
(test +16 -3) |
| 73 |
(test -16 +3) |
| 74 |
(test -16 -3) |
| 75 |
(test +15 +3) |
| 76 |
(test +15 -3) |
| 77 |
(test -15 +3) |
| 78 |
(test -15 -3) |
| 79 |
(test +10 +4) |
| 80 |
(test +10 -4) |
| 81 |
(test -10 +4) |
| 82 |
(test -10 -4)) |
| 83 |
|
| 84 |
|
| 85 |
(define (test-fxdiv0-and-mod0) |
| 86 |
(define (test x1 x2) |
| 87 |
(let-values ([(d m) (fxdiv0-and-mod0 x1 x2)]) |
| 88 |
(printf "(fxdiv0-and-mod0 ~s ~s) = ~s ~s\n" x1 x2 d m) |
| 89 |
(assert (= d (fxdiv0 x1 x2))) |
| 90 |
(assert (= m (fxmod0 x1 x2))) |
| 91 |
(assert (<= (- (abs (/ x2 2))) m)) |
| 92 |
(assert (< m (abs (/ x2 2)))) |
| 93 |
(assert (= x1 (+ (* d x2) m))))) |
| 94 |
(test +17 +3) |
| 95 |
(test +17 -3) |
| 96 |
(test -17 +3) |
| 97 |
(test -17 -3) |
| 98 |
(test +16 +3) |
| 99 |
(test +16 -3) |
| 100 |
(test -16 +3) |
| 101 |
(test -16 -3) |
| 102 |
(test +15 +3) |
| 103 |
(test +15 -3) |
| 104 |
(test -15 +3) |
| 105 |
(test -15 -3) |
| 106 |
(test +10 +4) |
| 107 |
(test +10 -4) |
| 108 |
(test -10 +4) |
| 109 |
(test -10 -4) |
| 110 |
|
| 111 |
(test (least-fixnum) +1) |
| 112 |
;(test (least-fixnum) -1) ;; overflows |
| 113 |
(test (greatest-fixnum) +1) |
| 114 |
(test (greatest-fixnum) -1) |
| 115 |
(test (least-fixnum) +2) |
| 116 |
(test (least-fixnum) -2) |
| 117 |
(test (greatest-fixnum) +2) |
| 118 |
(test (greatest-fixnum) -2) |
| 119 |
|
| 120 |
(test 0 (least-fixnum)) |
| 121 |
(test 0 (greatest-fixnum)) |
| 122 |
(test +1 (least-fixnum)) |
| 123 |
(test +1 (greatest-fixnum)) |
| 124 |
(test -1 (least-fixnum)) |
| 125 |
(test -1 (greatest-fixnum)) |
| 126 |
(test +2 (least-fixnum)) |
| 127 |
(test +2 (greatest-fixnum)) |
| 128 |
(test -2 (least-fixnum)) |
| 129 |
(test -2 (greatest-fixnum)) |
| 130 |
|
| 131 |
(test (least-fixnum) (least-fixnum)) |
| 132 |
(test (greatest-fixnum) (least-fixnum)) |
| 133 |
(test (least-fixnum) (greatest-fixnum)) |
| 134 |
(test (greatest-fixnum) (greatest-fixnum))) |
| 135 |
|
| 136 |
|
| 137 |
(define (test-fxlength) |
| 138 |
(define (test x) |
| 139 |
(define (bitlen x) |
| 140 |
(if (zero? x) |
| 141 |
0 |
| 142 |
(+ 1 (bitlen (bitwise-arithmetic-shift-right x 1))))) |
| 143 |
(define (len x) |
| 144 |
(if (< x 0) |
| 145 |
(bitlen (bitwise-not x)) |
| 146 |
(bitlen x))) |
| 147 |
(let ([c0 (len x)] |
| 148 |
[c1 (fxlength x)]) |
| 149 |
(unless (= c0 c1) |
| 150 |
(error 'test-fxlength "failed/expected/got" x c0 c1)))) |
| 151 |
(define (test-fx count n inc) |
| 152 |
(when (fixnum? n) |
| 153 |
(when (zero? (fxlogand count #xFFFF)) |
| 154 |
(printf "bitwise-bit-count ~s\n" n)) |
| 155 |
(test n) |
| 156 |
(test-fx (+ count 1) (+ n inc) inc))) |
| 157 |
(test 0) |
| 158 |
(test 1) |
| 159 |
(test 2) |
| 160 |
(test 3) |
| 161 |
(test -1) |
| 162 |
(test -2) |
| 163 |
(test -3) |
| 164 |
(if (= (fixnum-width) 30) |
| 165 |
(test-fx 0 (least-fixnum) #xFF) |
| 166 |
(test-fx 0 (least-fixnum) #xFF00000000)) ) |
| 167 |
) |
| 168 |
|