From 2ab6de8ee41aeb7c71f36f04a3a2941a711c6dbe Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Mon, 15 May 2023 20:40:47 +0200 Subject: [PATCH] fix hex.encode and add tests for the package --- core/encoding/hex/hex.odin | 8 +- tests/core/encoding/hex/test_core_hex.odin | 93 ++++++++++++++++++++++ 2 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 tests/core/encoding/hex/test_core_hex.odin diff --git a/core/encoding/hex/hex.odin b/core/encoding/hex/hex.odin index 8d0102c02..ef0bab1d0 100644 --- a/core/encoding/hex/hex.odin +++ b/core/encoding/hex/hex.odin @@ -4,11 +4,11 @@ import "core:strings" encode :: proc(src: []byte, allocator := context.allocator) -> []byte #no_bounds_check { dst := make([]byte, len(src) * 2, allocator) - for i := 0; i < len(src); i += 1 { + for i, j := 0, 0; i < len(src); i += 1 { v := src[i] - dst[i] = HEXTABLE[v>>4] - dst[i+1] = HEXTABLE[v&0x0f] - i += 2 + dst[j] = HEXTABLE[v>>4] + dst[j+1] = HEXTABLE[v&0x0f] + j += 2 } return dst diff --git a/tests/core/encoding/hex/test_core_hex.odin b/tests/core/encoding/hex/test_core_hex.odin new file mode 100644 index 000000000..a5daa206e --- /dev/null +++ b/tests/core/encoding/hex/test_core_hex.odin @@ -0,0 +1,93 @@ +package test_core_hex + +import "core:encoding/hex" +import "core:testing" +import "core:fmt" +import "core:os" +import "core:bytes" + +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{} + + hex_encode(&t) + hex_decode(&t) + hex_decode_sequence(&t) + + fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count) + if TEST_fail > 0 { + os.exit(1) + } +} + +CASES :: [][2]string{ + {"11", "3131"}, + {"g", "67"}, + {"Hello", "48656c6c6f"}, +} + +@(test) +hex_encode :: proc(t: ^testing.T) { + for test in CASES { + encoded := string(hex.encode(transmute([]byte)test[0])) + expect( + t, + encoded == test[1], + fmt.tprintf("encode: %q -> %q (should be: %q)", test[0], encoded, test[1]), + ) + } +} + +@(test) +hex_decode :: proc(t: ^testing.T) { + for test in CASES { + decoded, ok := hex.decode(transmute([]byte)test[1]) + expect(t, ok, fmt.tprintf("decode: %q not ok", test[1])) + expect( + t, + string(decoded) == test[0], + fmt.tprintf("decode: %q -> %q (should be: %q)", test[1], string(decoded), test[0]), + ) + } +} + +@(test) +hex_decode_sequence :: proc(t: ^testing.T) { + b, ok := hex.decode_sequence("0x23") + expect(t, ok, "decode_sequence: 0x23 not ok") + expect(t, b == '#', fmt.tprintf("decode_sequence: 0x23 -> %c (should be: %c)", b, '#')) + + b, ok = hex.decode_sequence("0X3F") + expect(t, ok, "decode_sequence: 0X3F not ok") + expect(t, b == '?', fmt.tprintf("decode_sequence: 0X3F -> %c (should be: %c)", b, '?')) + + b, ok = hex.decode_sequence("2a") + expect(t, ok, "decode_sequence: 2a not ok") + expect(t, b == '*', fmt.tprintf("decode_sequence: 2a -> %c (should be: %c)", b, '*')) + + _, ok = hex.decode_sequence("1") + expect(t, !ok, "decode_sequence: 1 should be too short") + + _, ok = hex.decode_sequence("123") + expect(t, !ok, "decode_sequence: 123 should be too long") +}