;;;; bitwise-utils-test -*- Scheme -*- ;;;; Kon Lovett, Apr '20 (import test) (test-begin "Bitwise Utils") ;;; (import bitwise-utils) (import (chicken bitwise)) (define (hex->number x) (string->number x 16)) (define (number->hex x) (number->string x 16)) (define (negate x) (* -1 x)) ;(bitwise-join #t 23 '()) (test #b+01111111 (bitwise-mask 7)) (test #b+01111111 (bitwise-mask 7 #t)) (test #b-10000000 (bitwise-mask 7 #f)) (test #b-100 (logical-shift-right #b-1000 1)) (test #b+100 (logical-shift-right #b+1000 1)) (test #b-101 (logical-shift-right #b-1011 1)) (test #b+101 (logical-shift-right #b+1011 1)) (test #x-2c (logical-shift-right #x-b0 2)) (test #x+2c (logical-shift-right #x+b0 2)) (test #x-2c000000000000000000000000044000 (logical-shift-right #x-b0000000000000000000000000110000 2)) (test #x+2c000000000000000000000000044000 (logical-shift-right #x+b0000000000000000000000000110000 2)) (test #b+0111111100 (logical-shift-left (bitwise-mask 7) 2)) (test #b+0111111100 (logical-shift-left (bitwise-mask 7 #t) 2)) (test #b-1000000000 (logical-shift-left (bitwise-mask 7 #f) 2)) (test 1023 (string-length (number->string (bitwise-mask 1023) 2))) (test #b101101 (bitwise-join #b10 #b0000001 #b101)) (test #b110011000000001 (bitwise-join #b1100 #b1100000000 #b1)) (test #xb0000000000000000000000000110000deadbeef (bitwise-join #xb0000000000000000000000000110000 #xdeadbeef)) ;mask effectively #b1..11000000011 (#b-111111101) (test #b-111111101 (bitwise-join (bitwise-mask 7 #f) #b11)) (test #b110011000000001 (bitwise-and #b110011010110101 (bitwise-join (bitwise-mask 7 #f) #b11))) (test #b110011000000001 (bitwise-and #b110011010110101 #b111111000000011)) (test '(0) (bitwise-split 0 5)) (test '(5) (bitwise-split 5 0)) (test '(0) (bitwise-split 0 0)) (test '("a" "b" "c" "d" "e" "f") (map number->hex (bitwise-split #xabcdef 4))) (test '("-a" "-b" "-c" "-d" "-e" "-f") (map number->hex (bitwise-split #x-abcdef 4))) ;NOTE not bitwise-join bitwise-split due to sign (test #xabcdef (apply bitwise-join (map hex->number '("a" "b" "c" "d" "e" "f")))) (test #x-abcdef (negate (apply bitwise-join (map (o negate hex->number) '("-a" "-b" "-c" "-d" "-e" "-f"))))) (test '("b0000000" "0" "110000deadbeef") (map number->hex (bitwise-split #xb0000000000000000000000000110000deadbeef 64))) (test '("-b0000000" "0" "-110000deadbeef") (map number->hex (bitwise-split #x-b0000000000000000000000000110000deadbeef 64))) (test 29 (bitwise-count #xb0000000000000000000000000110000deadbeef)) (test 28 (bitwise-count #x-b0000000000000000000000000110000deadbeef)) ;;; (test-end "Bitwise Utils") (test-exit)