| 1 |
(library (tests bignums) |
| 2 |
(export run-tests) |
| 3 |
(import (ikarus) (tests framework)) |
| 4 |
|
| 5 |
|
| 6 |
(define (run-tests) |
| 7 |
(test-bignums) |
| 8 |
(test-bignum-conversion) |
| 9 |
(test-bitwise-bit-count) |
| 10 |
(test-bignum-length)) |
| 11 |
|
| 12 |
|
| 13 |
(define (test-bignum-conversion) |
| 14 |
(define (test x) |
| 15 |
(define (test1 x prefix radix) |
| 16 |
(let ([s (string-append prefix |
| 17 |
(number->string x radix))]) |
| 18 |
(assert (equal? x (read (open-string-input-port s)))))) |
| 19 |
(test1 x "#x" 16) |
| 20 |
(test1 x "#o" 8) |
| 21 |
(test1 x "#b" 2)) |
| 22 |
(test #b11111111111111111111111111111111111111111111111111) |
| 23 |
(test #b1111111111111111111111111111111111111111) |
| 24 |
(test 39487932748923498234) |
| 25 |
(test #b-11111111111111111111111111111111111111111111111111) |
| 26 |
(test #b-1111111111111111111111111111111111111111) |
| 27 |
(test -39487932748923498234)) |
| 28 |
|
| 29 |
(define (test-bignum-length) |
| 30 |
(define (ref ei) |
| 31 |
(do ((result 0 (+ result 1)) |
| 32 |
(bits (if (negative? ei) |
| 33 |
(bitwise-not ei) |
| 34 |
ei) |
| 35 |
(bitwise-arithmetic-shift bits -1))) |
| 36 |
((zero? bits) |
| 37 |
result))) |
| 38 |
(define (test n) |
| 39 |
(let ([n0 (bitwise-length n)] |
| 40 |
[n1 (ref n)]) |
| 41 |
(unless (= n0 n1) |
| 42 |
(error 'test-bignum-length "mismatch" |
| 43 |
(format "#x~x" n) n0 n1)))) |
| 44 |
(test #xF) |
| 45 |
(test #xFF) |
| 46 |
(test #xFFF) |
| 47 |
(test #xFFFF) |
| 48 |
(test #xFFFFF) |
| 49 |
(test #xFFFFFF) |
| 50 |
(test #xFFFFFFF) |
| 51 |
(test #xFFFFFFFF) |
| 52 |
(test #xFFFFFFFFF) |
| 53 |
(test #xFFFFFFFFFF) |
| 54 |
(test #xFFFFFFFFFFF) |
| 55 |
(test #xFFFFFFFFFFFF) |
| 56 |
(test #xFFFFFFFFFFFFF) |
| 57 |
(test #xFFFFFFFFFFFFFF) |
| 58 |
(test #xFFFFFFFFFFFFFFF) |
| 59 |
(test #xFFFFFFFFFFFFFFFF) |
| 60 |
(test #x-F) |
| 61 |
(test #x-FF) |
| 62 |
(test #x-FFF) |
| 63 |
(test #x-FFFF) |
| 64 |
(test #x-FFFFF) |
| 65 |
(test #x-FFFFFF) |
| 66 |
(test #x-FFFFFFF) |
| 67 |
(test #x-FFFFFFFF) |
| 68 |
(test #x-FFFFFFFFF) |
| 69 |
(test #x-FFFFFFFFFF) |
| 70 |
(test #x-FFFFFFFFFFF) |
| 71 |
(test #x-FFFFFFFFFFFF) |
| 72 |
(test #x-FFFFFFFFFFFFF) |
| 73 |
(test #x-FFFFFFFFFFFFFF) |
| 74 |
(test #x-FFFFFFFFFFFFFFF) |
| 75 |
(test #x-FFFFFFFFFFFFFFFF) |
| 76 |
|
| 77 |
(test #xE) |
| 78 |
(test #xFE) |
| 79 |
(test #xFFE) |
| 80 |
(test #xFFFE) |
| 81 |
(test #xFFFFE) |
| 82 |
(test #xFFFFFE) |
| 83 |
(test #xFFFFFFE) |
| 84 |
(test #xFFFFFFFE) |
| 85 |
(test #xFFFFFFFFE) |
| 86 |
(test #xFFFFFFFFFE) |
| 87 |
(test #xFFFFFFFFFFE) |
| 88 |
(test #xFFFFFFFFFFFE) |
| 89 |
(test #xFFFFFFFFFFFFE) |
| 90 |
(test #xFFFFFFFFFFFFFE) |
| 91 |
(test #xFFFFFFFFFFFFFFE) |
| 92 |
(test #xFFFFFFFFFFFFFFFE) |
| 93 |
(test #x-E) |
| 94 |
(test #x-FE) |
| 95 |
(test #x-FFE) |
| 96 |
(test #x-FFFE) |
| 97 |
(test #x-FFFFE) |
| 98 |
(test #x-FFFFFE) |
| 99 |
(test #x-FFFFFFE) |
| 100 |
(test #x-FFFFFFFE) |
| 101 |
(test #x-FFFFFFFFE) |
| 102 |
(test #x-FFFFFFFFFE) |
| 103 |
(test #x-FFFFFFFFFFE) |
| 104 |
(test #x-FFFFFFFFFFFE) |
| 105 |
(test #x-FFFFFFFFFFFFE) |
| 106 |
(test #x-FFFFFFFFFFFFFE) |
| 107 |
(test #x-FFFFFFFFFFFFFFE) |
| 108 |
(test #x-FFFFFFFFFFFFFFFE) |
| 109 |
|
| 110 |
(test #x1) |
| 111 |
(test #x1F) |
| 112 |
(test #x1FF) |
| 113 |
(test #x1FFF) |
| 114 |
(test #x1FFFF) |
| 115 |
(test #x1FFFFF) |
| 116 |
(test #x1FFFFFF) |
| 117 |
(test #x1FFFFFFF) |
| 118 |
(test #x1FFFFFFFF) |
| 119 |
(test #x1FFFFFFFFF) |
| 120 |
(test #x1FFFFFFFFFF) |
| 121 |
(test #x1FFFFFFFFFFF) |
| 122 |
(test #x1FFFFFFFFFFFF) |
| 123 |
(test #x1FFFFFFFFFFFFF) |
| 124 |
(test #x1FFFFFFFFFFFFFF) |
| 125 |
(test #x1FFFFFFFFFFFFFFF) |
| 126 |
(test #x-1) |
| 127 |
(test #x-1F) |
| 128 |
(test #x-1FF) |
| 129 |
(test #x-1FFF) |
| 130 |
(test #x-1FFFF) |
| 131 |
(test #x-1FFFFF) |
| 132 |
(test #x-1FFFFFF) |
| 133 |
(test #x-1FFFFFFF) |
| 134 |
(test #x-1FFFFFFFF) |
| 135 |
(test #x-1FFFFFFFFF) |
| 136 |
(test #x-1FFFFFFFFFF) |
| 137 |
(test #x-1FFFFFFFFFFF) |
| 138 |
(test #x-1FFFFFFFFFFFF) |
| 139 |
(test #x-1FFFFFFFFFFFFF) |
| 140 |
(test #x-1FFFFFFFFFFFFFF) |
| 141 |
(test #x-1FFFFFFFFFFFFFFF) |
| 142 |
|
| 143 |
(test #x1) |
| 144 |
(test #x10) |
| 145 |
(test #x100) |
| 146 |
(test #x1000) |
| 147 |
(test #x10000) |
| 148 |
(test #x100000) |
| 149 |
(test #x1000000) |
| 150 |
(test #x10000000) |
| 151 |
(test #x100000000) |
| 152 |
(test #x1000000000) |
| 153 |
(test #x10000000000) |
| 154 |
(test #x100000000000) |
| 155 |
(test #x1000000000000) |
| 156 |
(test #x10000000000000) |
| 157 |
(test #x100000000000000) |
| 158 |
(test #x1000000000000000) |
| 159 |
(test #x-1) |
| 160 |
(test #x-10) |
| 161 |
(test #x-100) |
| 162 |
(test #x-1000) |
| 163 |
(test #x-10000) |
| 164 |
(test #x-100000) |
| 165 |
(test #x-1000000) |
| 166 |
(test #x-10000000) |
| 167 |
(test #x-100000000) |
| 168 |
(test #x-1000000000) |
| 169 |
(test #x-10000000000) |
| 170 |
(test #x-100000000000) |
| 171 |
(test #x-1000000000000) |
| 172 |
(test #x-10000000000000) |
| 173 |
(test #x-100000000000000) |
| 174 |
(test #x-1000000000000000) |
| 175 |
|
| 176 |
(test #x1) |
| 177 |
(test #x11) |
| 178 |
(test #x101) |
| 179 |
(test #x1001) |
| 180 |
(test #x10001) |
| 181 |
(test #x100001) |
| 182 |
(test #x1000001) |
| 183 |
(test #x10000001) |
| 184 |
(test #x100000001) |
| 185 |
(test #x1000000001) |
| 186 |
(test #x10000000001) |
| 187 |
(test #x100000000001) |
| 188 |
(test #x1000000000001) |
| 189 |
(test #x10000000000001) |
| 190 |
(test #x100000000000001) |
| 191 |
(test #x1000000000000001) |
| 192 |
(test #x-1) |
| 193 |
(test #x-11) |
| 194 |
(test #x-101) |
| 195 |
(test #x-1001) |
| 196 |
(test #x-10001) |
| 197 |
(test #x-100001) |
| 198 |
(test #x-1000001) |
| 199 |
(test #x-10000001) |
| 200 |
(test #x-100000001) |
| 201 |
(test #x-1000000001) |
| 202 |
(test #x-10000000001) |
| 203 |
(test #x-100000000001) |
| 204 |
(test #x-1000000000001) |
| 205 |
(test #x-10000000000001) |
| 206 |
(test #x-100000000000001) |
| 207 |
(test #x-1000000000000001)) |
| 208 |
|
| 209 |
|
| 210 |
(define (test-bitwise-bit-count) |
| 211 |
(define (test n) |
| 212 |
(define (pos-count-bits n) |
| 213 |
(if (zero? n) |
| 214 |
0 |
| 215 |
(let ([c (count-bits (bitwise-arithmetic-shift-right n 1))]) |
| 216 |
(if (even? n) c (+ c 1))))) |
| 217 |
(define (count-bits n) |
| 218 |
(if (>= n 0) |
| 219 |
(pos-count-bits n) |
| 220 |
(bitwise-not (pos-count-bits (bitwise-not n))))) |
| 221 |
(let ([bc0 (bitwise-bit-count n)] |
| 222 |
[bc1 (count-bits n)]) |
| 223 |
(unless (= bc0 bc1) |
| 224 |
(error 'test-bitcount "failed/expected/got" n bc1 bc0)))) |
| 225 |
(define (test-fx count n inc) |
| 226 |
(when (fixnum? n) |
| 227 |
(when (zero? (fxlogand count #xFFFF)) |
| 228 |
(printf "bitwise-bit-count ~s\n" n)) |
| 229 |
(test n) |
| 230 |
(test-fx (+ count 1) (+ n inc) inc))) |
| 231 |
(if (= (fixnum-width) 30) |
| 232 |
(test-fx 0 (least-fixnum) #xFF) |
| 233 |
(test-fx 0 (least-fixnum) #xFF00000000)) |
| 234 |
(test 28472347823493290482390849023840928390482309480923840923840983) |
| 235 |
(test -847234234903290482390849023840928390482309480923840923840983)) |
| 236 |
|
| 237 |
(define-tests test-bignums |
| 238 |
; first, some simple quotients |
| 239 |
[(lambda (x) (= x 101)) (quotient 348972 3434)] |
| 240 |
[(lambda (x) (= x -101)) (quotient -348972 3434)] |
| 241 |
[(lambda (x) (= x -101)) (quotient 348972 -3434)] |
| 242 |
[(lambda (x) (= x 101)) (quotient -348972 -3434)] |
| 243 |
; then bump first argument to a small bignum: |
| 244 |
[(lambda (x) (= x 2255760)) (quotient 536870912 238)] |
| 245 |
[(lambda (x) (= x -2255760)) (quotient -536870912 238)] |
| 246 |
[(lambda (x) (= x -2255760)) (quotient 536870912 -238)] |
| 247 |
[(lambda (x) (= x 2255760)) (quotient -536870912 -238)] |
| 248 |
; then bump first argument to a big bignum: |
| 249 |
[(lambda (x) (= x 1652556267336712615)) |
| 250 |
(quotient 536870912238479837489374 324873)] |
| 251 |
[(lambda (x) (= x -1652556267336712615)) |
| 252 |
(quotient -536870912238479837489374 324873)] |
| 253 |
[(lambda (x) (= x -1652556267336712615)) |
| 254 |
(quotient 536870912238479837489374 -324873)] |
| 255 |
[(lambda (x) (= x 1652556267336712615)) |
| 256 |
(quotient -536870912238479837489374 -324873)] |
| 257 |
; then make both arguments bignums, but result fixnum: |
| 258 |
[(lambda (x) (= x 165)) |
| 259 |
(quotient 536870912238479837489374 3248732398479823749283)] |
| 260 |
[(lambda (x) (= x -165)) |
| 261 |
(quotient -536870912238479837489374 3248732398479823749283)] |
| 262 |
[(lambda (x) (= x -165)) |
| 263 |
(quotient 536870912238479837489374 -3248732398479823749283)] |
| 264 |
[(lambda (x) (= x 165)) |
| 265 |
(quotient -536870912238479837489374 -3248732398479823749283)] |
| 266 |
; then both arguments and result are all bignums: |
| 267 |
[(lambda (x) (= x 1652555047284588078)) |
| 268 |
(quotient 5368709122384798374893743894798327498234 3248732398479823749283)] |
| 269 |
[(lambda (x) (= x -1652555047284588078)) |
| 270 |
(quotient -5368709122384798374893743894798327498234 3248732398479823749283)] |
| 271 |
[(lambda (x) (= x -1652555047284588078)) |
| 272 |
(quotient 5368709122384798374893743894798327498234 -3248732398479823749283)] |
| 273 |
[(lambda (x) (= x 1652555047284588078)) |
| 274 |
(quotient -5368709122384798374893743894798327498234 -3248732398479823749283)] |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
[(lambda (x) (= x 23)) (remainder 23 349839489348)] |
| 280 |
[(lambda (x) (= x -23)) (remainder -23 349839489348)] |
| 281 |
[(lambda (x) (= x 23)) (remainder 23 -349839489348)] |
| 282 |
[(lambda (x) (= x -23)) (remainder -23 -349839489348)] |
| 283 |
|
| 284 |
|
| 285 |
;;; Next, modulo |
| 286 |
; first, some simple arguments |
| 287 |
[(lambda (x) (= x 2138)) (modulo 348972 3434)] |
| 288 |
[(lambda (x) (= x 1296)) (modulo -348972 3434)] |
| 289 |
[(lambda (x) (= x -1296)) (modulo 348972 -3434)] |
| 290 |
[(lambda (x) (= x -2138)) (modulo -348972 -3434)] |
| 291 |
; then bignum second argument: can be done with +/- |
| 292 |
[(lambda (x) (= x 349839489325)) (modulo -23 349839489348)] |
| 293 |
[(lambda (x) (= x -23)) (modulo -23 -349839489348)] |
| 294 |
[(lambda (x) (= x 23)) (modulo 23 349839489348)] |
| 295 |
[(lambda (x) (= x -349839489325)) (modulo 23 -349839489348)] |
| 296 |
|
| 297 |
; then bump first argument to a small bignum: |
| 298 |
[(lambda (x) (= x 32)) (remainder 536870912 238)] |
| 299 |
[(lambda (x) (= x -32)) (remainder -536870912 238)] |
| 300 |
[(lambda (x) (= x 32)) (remainder 536870912 -238)] |
| 301 |
[(lambda (x) (= x -32)) (remainder -536870912 -238)] |
| 302 |
|
| 303 |
[(lambda (x) (= x 32)) (modulo 536870912 238)] |
| 304 |
[(lambda (x) (= x 206)) (modulo -536870912 238)] |
| 305 |
[(lambda (x) (= x -206)) (modulo 536870912 -238)] |
| 306 |
[(lambda (x) (= x -32)) (modulo -536870912 -238)] |
| 307 |
; then bump first argument to a big bignum: |
| 308 |
[(lambda (x) (= x 116479)) |
| 309 |
(modulo 536870912238479837489374 324873)] |
| 310 |
[(lambda (x) (= x 208394)) |
| 311 |
(modulo -536870912238479837489374 324873)] |
| 312 |
[(lambda (x) (= x -208394)) |
| 313 |
(modulo 536870912238479837489374 -324873)] |
| 314 |
[(lambda (x) (= x -116479)) |
| 315 |
(modulo -536870912238479837489374 -324873)] |
| 316 |
; then make both arguments bignums |
| 317 |
[(lambda (x) (= x 830066489308918857679)) |
| 318 |
(modulo 536870912238479837489374 3248732398479823749283)] |
| 319 |
[(lambda (x) (= x -2418665909170904891604)) |
| 320 |
(modulo 536870912238479837489374 -3248732398479823749283)] |
| 321 |
[(lambda (x) (= x 2418665909170904891604)) |
| 322 |
(modulo -536870912238479837489374 3248732398479823749283)] |
| 323 |
[(lambda (x) (= x -830066489308918857679)) |
| 324 |
(modulo -536870912238479837489374 -3248732398479823749283)] |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
[(lambda (x) (= x -13)) (bitwise-not 12)] |
| 330 |
[(lambda (x) (= x 11)) (bitwise-not -12)] |
| 331 |
[(lambda (x) (= x 0)) (bitwise-not -1)] |
| 332 |
[(lambda (x) (= x -1)) (bitwise-not 0)] |
| 333 |
[(lambda (x) (= x (least-fixnum))) (bitwise-not (greatest-fixnum))] |
| 334 |
[(lambda (x) (= x (greatest-fixnum))) (bitwise-not (least-fixnum))] |
| 335 |
|
| 336 |
[(lambda (x) (= x -38947389478348937489375)) |
| 337 |
(bitwise-not 38947389478348937489374)] |
| 338 |
[(lambda (x) (= x -22300745198530623141535718272648361505980416)) |
| 339 |
(bitwise-not #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)] |
| 340 |
[(lambda (x) (= x 38947389478348937489374)) |
| 341 |
(bitwise-not -38947389478348937489375)] |
| 342 |
[(lambda (x) (= x 22300745198530623141535718272648361505980414)) |
| 343 |
(bitwise-not #x-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)] |
| 344 |
[(lambda (x) (= x -340282366920938463463374607431768211456)) |
| 345 |
(bitwise-not #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)] |
| 346 |
[(lambda (x) (= x 340282366920938463463374607431768211454)) |
| 347 |
(bitwise-not #x-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)] |
| 348 |
[(lambda (x) (= x -79228162514264337593543950337)) |
| 349 |
(bitwise-not #x1000000000000000000000000)] |
| 350 |
[(lambda (x) (= x 79228162514264337593543950335)) |
| 351 |
(bitwise-not #x-1000000000000000000000000)] |
| 352 |
|
| 353 |
|
| 354 |
)) |
| 355 |
|
| 356 |
|