| 1 |
(library (tests bitwise) |
| 2 |
(export run-tests) |
| 3 |
(import (ikarus) (tests framework)) |
| 4 |
|
| 5 |
|
| 6 |
(define (test-base-case op i0 i1 r) |
| 7 |
(assert (= (op i0 i1) r))) |
| 8 |
|
| 9 |
(define (test-base-cases) |
| 10 |
|
| 11 |
(test-base-case bitwise-and 0 0 0) |
| 12 |
(test-base-case bitwise-and 0 1 0) |
| 13 |
(test-base-case bitwise-and 1 0 0) |
| 14 |
(test-base-case bitwise-and 1 1 1) |
| 15 |
|
| 16 |
(test-base-case bitwise-ior 0 0 0) |
| 17 |
(test-base-case bitwise-ior 0 1 1) |
| 18 |
(test-base-case bitwise-ior 1 0 1) |
| 19 |
(test-base-case bitwise-ior 1 1 1) |
| 20 |
|
| 21 |
(test-base-case bitwise-xor 0 0 0) |
| 22 |
(test-base-case bitwise-xor 0 1 1) |
| 23 |
(test-base-case bitwise-xor 1 0 1) |
| 24 |
(test-base-case bitwise-xor 1 1 0)) |
| 25 |
|
| 26 |
(define (generate-numbers) |
| 27 |
(define N 68) |
| 28 |
(define (n* n i) |
| 29 |
(if (zero? i) |
| 30 |
'() |
| 31 |
(cons n (n* (bitwise-arithmetic-shift-left n 1) (- i 1))))) |
| 32 |
(append |
| 33 |
(n* 1 N) |
| 34 |
(n* -1 N) |
| 35 |
(map sub1 (n* 1 N)) |
| 36 |
(map sub1 (n* -1 N)) |
| 37 |
(map add1 (n* 1 N)) |
| 38 |
(map add1 (n* -1 N)))) |
| 39 |
|
| 40 |
|
| 41 |
(define (one-bit n) |
| 42 |
(if (even? n) 0 1)) |
| 43 |
|
| 44 |
(define (unit? n) |
| 45 |
(or (= n 0) (= n -1))) |
| 46 |
|
| 47 |
(define (trusted op n1 n2) |
| 48 |
(if (and (unit? n1) (unit? n2)) |
| 49 |
(op n1 n2) |
| 50 |
(+ (one-bit (op (one-bit n1) (one-bit n2))) |
| 51 |
(bitwise-arithmetic-shift-left |
| 52 |
(trusted op |
| 53 |
(bitwise-arithmetic-shift-right n1 1) |
| 54 |
(bitwise-arithmetic-shift-right n2 1)) |
| 55 |
1)))) |
| 56 |
|
| 57 |
(define (test-case op) |
| 58 |
(define ls (generate-numbers)) |
| 59 |
(define id 0) |
| 60 |
(for-each |
| 61 |
(lambda (n1) |
| 62 |
(for-each |
| 63 |
(lambda (n2) |
| 64 |
(let ([r0 (op n1 n2)] |
| 65 |
[r1 (trusted op n1 n2)]) |
| 66 |
(unless (= r0 r1) |
| 67 |
(printf "id=~s ~x ~x ~x ~x\n" id n1 n2 r0 r1) |
| 68 |
(error 'test-bitwise-op |
| 69 |
"mismatch/op/a0/a1/got/expected" op n1 n2 r0 r1)) |
| 70 |
(set! id (+ id 1)))) |
| 71 |
ls)) |
| 72 |
ls)) |
| 73 |
|
| 74 |
(define (test-other-cases) |
| 75 |
(test-case bitwise-and) |
| 76 |
(test-case bitwise-ior) |
| 77 |
(test-case bitwise-xor) |
| 78 |
) |
| 79 |
|
| 80 |
|
| 81 |
(define (run-tests) |
| 82 |
(test-base-cases) |
| 83 |
(test-other-cases))) |
| 84 |
|
| 85 |
|