Add basic tests for the binary module

I've also added the Binary module to Core.carp.
This commit is contained in:
Scott Olsen 2020-02-18 15:34:41 -05:00
parent 3c95c4baeb
commit edee6d9dc9
2 changed files with 146 additions and 0 deletions

View File

@ -47,3 +47,4 @@
(load "Map.carp")
(load "Heap.carp")
(load "Sort.carp")
(load "Binary.carp")

145
test/binary.carp Normal file
View File

@ -0,0 +1,145 @@
(use Binary)
(load "Test.carp")
(use Test)
(deftest test
;; int16 tests
(assert-equal test
(Uint16.from-long 2051l)
(unsafe-bytes->int16 (Binary.Order.LittleEndian) &[3b 8b])
"Unsafe little endian unsafe-bytes->int16 works as expected.")
(assert-equal test
(Uint16.from-long 776l)
(unsafe-bytes->int16 (Binary.Order.BigEndian) &[3b 8b])
"Unsafe big endian unsafe-bytes->int16 works as expected.")
(assert-equal test
&(Maybe.Just (Uint16.from-long 2051l))
&(bytes->int16 (Binary.Order.LittleEndian) &[3b 8b])
"Unsafe little endian bytes->int16 works as expected.")
(assert-equal test
&(Maybe.Just (Uint16.from-long 776l))
&(bytes->int16 (Binary.Order.BigEndian) &[3b 8b])
"Unsafe big endian bytes->int16 works as expected.")
(assert-equal test
&(Maybe.Nothing)
&(bytes->int16 (Binary.Order.LittleEndian) &[3b])
"bytes->int16 returns Nothing on insufficient data.")
(assert-equal test
&[(Uint16.from-long 2051l) (Uint16.from-long 776l)]
&(unsafe-bytes->int16-seq (Binary.Order.LittleEndian) &[3b 8b 8b 3b])
"Unsafe little endian bytes->int16-seq works as expected.")
(assert-equal test
&[(Maybe.Just (Uint16.from-long 776l)) (Maybe.Just (Uint16.from-long 2051l)) (Maybe.Nothing)]
&(bytes->int16-seq (Binary.Order.BigEndian) &[3b 8b 8b 3b 2b])
"Unsafe big endian bytes->int16-seq works as expected.")
(assert-equal test
&[3b 8b]
&(int16->bytes (Binary.Order.LittleEndian) (Uint16.from-long 2051l))
"Little endian int16->bytes works as expected.")
(assert-equal test
&[[3b 8b] [8b 3b]]
&(int16-seq->bytes (Binary.Order.LittleEndian) &[(Uint16.from-long 2051l) (Uint16.from-long 776l)])
"Little endian int16->bytes works as expected.")
;; int32 tests
(assert-equal test
(Uint32.from-long 67305985l)
(unsafe-bytes->int32 (Binary.Order.LittleEndian) &[1b 2b 3b 4b])
"Unsafe little endian unsafe-bytes->int32 works as expected.")
(assert-equal test
(Uint32.from-long 16909060l)
(unsafe-bytes->int32 (Binary.Order.BigEndian) &[1b 2b 3b 4b])
"Unsafe big endian unsafe-bytes->int32 works as expected.")
(assert-equal test
&(Maybe.Just (Uint32.from-long 67305985l))
&(bytes->int32 (Binary.Order.LittleEndian) &[1b 2b 3b 4b])
"Unsafe little endian bytes->int32 works as expected.")
(assert-equal test
&(Maybe.Just (Uint32.from-long 16909060l))
&(bytes->int32 (Binary.Order.BigEndian) &[1b 2b 3b 4b])
"Unsafe big endian bytes->int32 works as expected.")
(assert-equal test
&(Maybe.Nothing)
&(bytes->int32 (Binary.Order.LittleEndian) &[3b])
"bytes->int32 returns Nothing on insufficient data.")
(assert-equal test
&[(Uint32.from-long 67305985l) (Uint32.from-long 16909060l)]
&(unsafe-bytes->int32-seq (Binary.Order.LittleEndian) &[1b 2b 3b 4b 4b 3b 2b 1b])
"Unsafe little endian bytes->int32-seq works as expected.")
(assert-equal test
&[(Maybe.Just (Uint32.from-long 16909060l)) (Maybe.Just (Uint32.from-long 67305985l)) (Maybe.Nothing)]
&(bytes->int32-seq (Binary.Order.BigEndian) &[1b 2b 3b 4b 4b 3b 2b 1b 5b])
"Unsafe big endian bytes->int32-seq works as expected.")
(assert-equal test
&[1b 2b 3b 4b]
&(int32->bytes (Binary.Order.LittleEndian) (Uint32.from-long 67305985l))
"Little endian int32->bytes works as expected.")
(assert-equal test
&[[1b 2b 3b 4b] [4b 3b 2b 1b]]
&(int32-seq->bytes (Binary.Order.LittleEndian) &[(Uint32.from-long 67305985l) (Uint32.from-long 16909060l)])
"Little endian int32-seq->bytes works as expected.")
;; int64 tests
;; We only go up to 6b in the 6th position--going higher seems to cause precision loss (at least on my system).
(assert-equal test
(Uint64.from-long 6618611909121l)
(unsafe-bytes->int64 (Binary.Order.LittleEndian) &[1b 2b 3b 4b 5b 6b 0b 0b])
"Unsafe little endian unsafe-bytes->int64 works as expected.")
(assert-equal test
(Uint64.from-long 72623859790381056l)
(unsafe-bytes->int64 (Binary.Order.BigEndian) &[1b 2b 3b 4b 5b 6b 0b 0b])
"Unsafe big endian unsafe-bytes->int64 works as expected.")
(assert-equal test
&(Maybe.Just (Uint64.from-long 6618611909121l))
&(bytes->int64 (Binary.Order.LittleEndian) &[1b 2b 3b 4b 5b 6b 0b 0b])
"Unsafe little endian bytes->int64 works as expected.")
(assert-equal test
&(Maybe.Just (Uint64.from-long 72623859790381056l))
&(bytes->int64 (Binary.Order.BigEndian) &[1b 2b 3b 4b 5b 6b 0b 0b])
"Unsafe big endian bytes->int64 works as expected.")
(assert-equal test
&(Maybe.Nothing)
&(bytes->int64 (Binary.Order.LittleEndian) &[3b])
"bytes->int64 returns Nothing on insufficient data.")
(assert-equal test
&[(Uint64.from-long 6618611909121l) (Uint64.from-long 72623859790381056l)]
&(unsafe-bytes->int64-seq (Binary.Order.LittleEndian) &[1b 2b 3b 4b 5b 6b 0b 0b 0b 0b 6b 5b 4b 3b 2b 1b])
"Unsafe little endian bytes->int64-seq works as expected.")
(assert-equal test
&[(Maybe.Just (Uint64.from-long 72623859790381056l)) (Maybe.Just (Uint64.from-long 6618611909121l)) (Maybe.Nothing)]
&(bytes->int64-seq (Binary.Order.BigEndian) &[1b 2b 3b 4b 5b 6b 0b 0b 0b 0b 6b 5b 4b 3b 2b 1b 5b])
"Unsafe big endian bytes->int64-seq works as expected.")
(assert-equal test
&[1b 2b 3b 4b 5b 6b 0b 0b]
&(int64->bytes (Binary.Order.LittleEndian) (Uint64.from-long 6618611909121l))
"Little endian int64->bytes works as expected.")
(assert-equal test
&[[1b 2b 3b 4b 5b 6b 0b 0b] [0b 0b 6b 5b 4b 3b 2b 1b]]
&(int64-seq->bytes (Binary.Order.LittleEndian) &[ (Uint64.from-long 6618611909121l) (Uint64.from-long 72623859790381056l)])
"Little endian int64-seq->bytes works as expected.")
)