diff options
| author | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2022-03-08 15:40:00 +0100 |
|---|---|---|
| committer | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2022-03-08 15:40:00 +0100 |
| commit | 6d7217f37a241c688e4734d7b8f6904299cadd2b (patch) | |
| tree | ba70a38a0c8a600145383c22fe8bc7964425db48 /tests/core/encoding | |
| parent | 29e660b16f18df2c3d6781fef5d1baa3faac0c94 (diff) | |
[varint] Add LEB128 decoding + tests
Also make tests in general less spammy: Don't print [PASS] for each successful test, only report failures and progress.
Diffstat (limited to 'tests/core/encoding')
| -rw-r--r-- | tests/core/encoding/json/test_core_json.odin (renamed from tests/core/encoding/test_core_json.odin) | 36 | ||||
| -rw-r--r-- | tests/core/encoding/varint/test_core_varint.odin | 81 |
2 files changed, 98 insertions, 19 deletions
diff --git a/tests/core/encoding/test_core_json.odin b/tests/core/encoding/json/test_core_json.odin index 702086ea2..60e71bf72 100644 --- a/tests/core/encoding/test_core_json.odin +++ b/tests/core/encoding/json/test_core_json.odin @@ -9,32 +9,30 @@ TEST_count := 0 TEST_fail := 0 when ODIN_TEST { - expect :: testing.expect - log :: testing.log + expect :: testing.expect + log :: testing.log } else { - expect :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) { - fmt.printf("[%v] ", loc) - TEST_count += 1 - if !condition { - TEST_fail += 1 - fmt.println(message) - return - } - fmt.println(" PASS") - } - log :: proc(t: ^testing.T, v: any, loc := #caller_location) { - fmt.printf("[%v] ", loc) - fmt.printf("log: %v\n", v) - } + expect :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) { + TEST_count += 1 + if !condition { + TEST_fail += 1 + fmt.printf("[%v] %v\n", loc, message) + return + } + } + log :: proc(t: ^testing.T, v: any, loc := #caller_location) { + fmt.printf("[%v] ", loc) + fmt.printf("log: %v\n", v) + } } main :: proc() { - t := testing.T{} - + t := testing.T{} + parse_json(&t) marshal_json(&t) - fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count) + fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count) if TEST_fail > 0 { os.exit(1) } diff --git a/tests/core/encoding/varint/test_core_varint.odin b/tests/core/encoding/varint/test_core_varint.odin new file mode 100644 index 000000000..5b8ccdc2a --- /dev/null +++ b/tests/core/encoding/varint/test_core_varint.odin @@ -0,0 +1,81 @@ +package test_core_varint + +import "core:encoding/varint" +import "core:testing" +import "core:fmt" +import "core:os" + +TEST_count := 0 +TEST_fail := 0 + +when ODIN_TEST { + expect :: testing.expect + log :: testing.log +} else { + expect :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) { + TEST_count += 1 + if !condition { + TEST_fail += 1 + fmt.printf("[%v] %v\n", loc, message) + return + } + } + log :: proc(t: ^testing.T, v: any, loc := #caller_location) { + fmt.printf("[%v] ", loc) + fmt.printf("log: %v\n", v) + } +} + +main :: proc() { + t := testing.T{} + + test_dwarf(&t) + + fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count) + if TEST_fail > 0 { + os.exit(1) + } +} + +@(test) +test_dwarf :: proc(t: ^testing.T) { + for vector in ULEB_Vectors { + val, size := varint.decode_uleb128(vector.encoded) + + msg := fmt.tprintf("Expected %02x to decode to %v consuming %v bytes, got %v and %v", vector.encoded, vector.value, vector.size, val, size) + expect(t, size == vector.size && val == vector.value, msg) + } + + for vector in ILEB_Vectors { + val, size := varint.decode_ileb128(vector.encoded) + + msg := fmt.tprintf("Expected %02x to decode to %v consuming %v bytes, got %v and %v", vector.encoded, vector.value, vector.size, val, size) + expect(t, size == vector.size && val == vector.value, msg) + } +} + +ULEB_Test_Vector :: struct { + encoded: []u8, + value: u128, + size: int, +} + +ULEB_Vectors :: []ULEB_Test_Vector{ + { []u8{0x00}, 0, 1 }, + { []u8{0x7f}, 127, 1 }, + { []u8{0xE5, 0x8E, 0x26}, 624485, 3 }, + { []u8{0x80}, 0, 0 }, + { []u8{}, 0, 0 }, +} + +ILEB_Test_Vector :: struct { + encoded: []u8, + value: i128, + size: int, +} + +ILEB_Vectors :: []ILEB_Test_Vector{ + { []u8{0x00}, 0, 1 }, + { []u8{0xC0, 0xBB, 0x78}, -123456, 3 }, + { []u8{}, 0, 0 }, +}
\ No newline at end of file |