kmer/test/kmer_test.gleam

77 lines
2.1 KiB
Gleam
Raw Normal View History

2024-11-30 14:48:01 +00:00
import gleam/list
import gleeunit
import gleeunit/should
import kmer.{type DecodeResult, type DecodedValue, Integer}
pub fn main() {
gleeunit.main()
}
fn table_test_decode_next_value(test_case: BitArray, expected: DecodedValue) {
kmer.decode_next_value(test_case) |> is_value(expected)
}
fn is_value(val: DecodeResult, expected: DecodedValue) {
should.be_ok(val) |> should.equal(#(expected, <<>>))
}
pub fn decode_immediate_int_test() {
[
// Small integers under 24
#(<<0>>, Integer(0)),
#(<<10>>, Integer(10)),
#(<<23>>, Integer(23)),
]
|> table_fn(table_test_decode_next_value)
}
pub fn decode_int_test() {
[
// 1-byte length (Maj.arg 24)
#(<<24, 24>>, Integer(24)),
#(<<24, 100>>, Integer(100)),
// 2-byte length (Maj.arg 25)
#(<<25, 1234:16>>, Integer(1234)),
#(<<25, 12_345:16>>, Integer(12_345)),
// 3-byte length (Maj.arg 26)
#(<<26, 1_234_567:32>>, Integer(1_234_567)),
#(<<26, 999_999_999:32>>, Integer(999_999_999)),
// 4-byte length (Maj.arg 27)
#(<<27, 2_099_210_021_019_898_881:64>>, Integer(2_099_210_021_019_898_881)),
#(<<27, 3:64>>, Integer(3)),
]
|> table_fn(table_test_decode_next_value)
}
pub fn decode_negative_int_test() {
[
// Immediate negative (under 25)
#(<<1:3, 0:5>>, Integer(-1)),
#(<<1:3, 8:5>>, Integer(-9)),
#(<<1:3, 23:5>>, Integer(-24)),
// 1-byte length (Maj.arg 24)
#(<<1:3, 24:5, 24:8>>, Integer(-25)),
#(<<1:3, 24:5, 100:8>>, Integer(-101)),
// 2-byte length (Maj.arg 25)
#(<<1:3, 25:5, 1234:16>>, Integer(-1235)),
#(<<1:3, 25:5, 12_345:16>>, Integer(-12_346)),
// 3-byte length (Maj.arg 26)
#(<<1:3, 26:5, 1_234_567:32>>, Integer(-1_234_568)),
#(<<1:3, 26:5, 999_999_999:32>>, Integer(-1_000_000_000)),
// 4-byte length (Maj.arg 27)
#(
<<1:3, 27:5, 2_099_210_021_019_898_881:64>>,
Integer(-2_099_210_021_019_898_882),
),
#(<<1:3, 27:5, 3:64>>, Integer(-4)),
]
|> table_fn(table_test_decode_next_value)
}
fn table_fn(tests: List(#(a, b)), test_fn: fn(a, b) -> Nil) {
list.map(tests, fn(t) {
let #(test_case, expected) = t
test_fn(test_case, expected)
})
}