From eae3fbe7dedfed35e58f2da6f2f52d3eb4ab6acf Mon Sep 17 00:00:00 2001 From: Koz Ross Date: Tue, 31 Jan 2017 19:30:00 +1300 Subject: [PATCH 1/2] Adding tests for SRFI 60 --- test.txt | 1 + tests/srfi-60-tests.scm | 141 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 test.txt create mode 100644 tests/srfi-60-tests.scm diff --git a/test.txt b/test.txt new file mode 100644 index 00000000..b5754e20 --- /dev/null +++ b/test.txt @@ -0,0 +1 @@ +ok \ No newline at end of file diff --git a/tests/srfi-60-tests.scm b/tests/srfi-60-tests.scm new file mode 100644 index 00000000..638989c2 --- /dev/null +++ b/tests/srfi-60-tests.scm @@ -0,0 +1,141 @@ +(import + (scheme base) + (srfi 60) + (scheme cyclone test)) + +(define a #b1100) +(define b #b1010) +(define c #b1110) + +(define (dec->bin x) (number->string x 2)) + +(define (test-commutative op) + (test (op a b) (op b a))) + +(define (test-associative op) + (test (op a (op b c)) (op (op a b) c))) + +(test-group + "logand" + (test "1000" (dec->bin (logand a b))) + (test "1000" (dec->bin (bitwise-and a b))) + (test-commutative logand) + (test-commutative bitwise-and) + (test-associative logand) + (test-associative bitwise-and)) + +(test-group + "logior" + (test "1110" (dec->bin (logior a b))) + (test "1110" (dec->bin (bitwise-ior a b))) + (test-commutative logior) + (test-commutative bitwise-ior) + (test-associative logior) + (test-associative bitwise-ior)) + +(test-group + "logxor" + (test "110" (dec->bin (logxor a b))) + (test "110" (dec->bin (bitwise-xor a b))) + (test-commutative logxor) + (test-commutative bitwise-xor) + (test-associative logxor) + (test-associative bitwise-xor)) + +(test-group + "lognot" + (test "-10000001" (dec->bin (lognot #b10000000))) + (test "-10000001" (dec->bin (bitwise-not #b10000000))) + (test "-1" (dec->bin (lognot #b0))) + (test "-1" (dec->bin (bitwise-not #b0)))) + +(test-group + "logtest" + (do + ((i 1 (when (= i j) (+ 1 i))) + (j 0) (if (= i j) 0 (+ 1 j))) + ((= i 1024)) + (test (not (zero? (logand i j))) (logtest i j)) + (test (not (zero? (logand i j))) (any-bits-set? i j)))) + +(test-group + "logcount" + (test 4 (logcount #b10101010)) + (test 4 (bit-count #b10101010)) + (test 0 (logcount 0)) + (test 0 (bit-count 0)) + (test 1 (logcount -2)) + (test 1 (bit-count -2))) + +(test-group + "integer-length" + (test 8 (integer-length #b10101010)) + (test 0 (integer-length 0)) + (test 4 (integer-length #b1111))) + +(define fsb-results #u8(-1, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4)) + +(test-group + "log2-binary-factors" + (do + ((i 0 (+ i 1))) + ((= i 17)) + (let ((res (bytevector-u8-ref fsb-results i))) + (test res (log2-binary-factors i)) + (test res (first-set-bit i)) + (test res (log2-binary (- i))) + (test res (first-set-bit (- i)))))) + +(test-group + "logbit" + (test-assert (logbit? 0 #b1101)) + (test-assert (bit-set? 0 #b1101)) + (test-not (logbit? 1 #b1101)) + (test-not (bit-set? 0 #b1101)) + (test-assert (logbit? 2 #b1101)) + (test-assert (bit-set? 2 #b1101)) + (test-assert (logbit? 3 #b1101)) + (test-assert (bit-set? 3 #b1101)) + (test-not (logbit? 4 #b1101)) + (test-not (bit-set? 4 #b1101))) + +(test-group + "copy-bit" + (test "1" (dec->bin (copy-bit 0 0 #t))) + (test "100" (dec->bin (copy-bit 2 0 #t))) + (test "1011" (dec->bin (copy-bit 2 #b1111 #f)))) + +(test-group + "bit-field" + (test "1010" (dec->bin (bit-field #b1101101010 0 4))) + (test "10110" (dec->bin (bit-field #b1101101010 4 9)))) + +(test-group + "copy-bit-field" + (test "1101100000" (dec->bin (copy-bit-field #b1101101010 0 0 4))) + (test "1101101111" (dec->bin (copy-bit-field #b1101101010 -1 0 4))) + (test "110100111110000" (dec->bin (copy-bit-field #b110100100010000 -1 5 9)))) + +(test-group + "ash" + (test "1000" (dec->bin (ash #b1 3))) + (test "1000" (dec->bin (arithmetic-shift #b1 3))) + (test "101" (dec->bin (ash #b1010 -1))) + (test "101" (dec->bin (arithmetic-shift #b1010 -1)))) + +(test-group + "rotate-bit-field" + (test "10" (dec->bin (rotate-bit-field #b0100 3 0 4))) + (test "10" (dec->bin (rotate-bit-field #b0100 -1 0 4))) + (test "110100010010000" (dec->bin (rotate-bit-field #b110100100010000 + -1 + 5 + 9))) + (test "110100000110000" (dec->bin (rotate-bit-field #b110100100010000 + 1 + 5 + 9)))) + +(test-group + "reverse-bit-field" + (test "e5" (number->string (reverse-bit-field #xa7 0 8) 16))) From 7f36cdc708284a6c037ab1e0f1c9f6b21c7abfbc Mon Sep 17 00:00:00 2001 From: Koz Ross Date: Tue, 31 Jan 2017 19:36:23 +1300 Subject: [PATCH 2/2] Fixing comma derp --- tests/srfi-60-tests.scm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/srfi-60-tests.scm b/tests/srfi-60-tests.scm index 638989c2..04c7b427 100644 --- a/tests/srfi-60-tests.scm +++ b/tests/srfi-60-tests.scm @@ -73,7 +73,7 @@ (test 0 (integer-length 0)) (test 4 (integer-length #b1111))) -(define fsb-results #u8(-1, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4)) +(define fsb-results #u8(-1 0 1 0 2 0 1 0 3 0 1 0 2 0 1 0 4)) (test-group "log2-binary-factors"