aboutsummaryrefslogtreecommitdiff
path: root/tests/core/encoding
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2022-03-08 18:07:16 +0100
committerJeroen van Rijn <Kelimion@users.noreply.github.com>2022-03-08 18:07:16 +0100
commite76a5d8e12ae3d6e8e75e8df53a30bfbcc66b829 (patch)
treebffca0059ce13d0d150a20f740cbff85e868065b /tests/core/encoding
parent6d7217f37a241c688e4734d7b8f6904299cadd2b (diff)
[varint] Add signed LEB128 encoding.
Diffstat (limited to 'tests/core/encoding')
-rw-r--r--tests/core/encoding/varint/test_core_varint.odin47
1 files changed, 37 insertions, 10 deletions
diff --git a/tests/core/encoding/varint/test_core_varint.odin b/tests/core/encoding/varint/test_core_varint.odin
index 5b8ccdc2a..63a9d1d72 100644
--- a/tests/core/encoding/varint/test_core_varint.odin
+++ b/tests/core/encoding/varint/test_core_varint.odin
@@ -4,6 +4,7 @@ import "core:encoding/varint"
import "core:testing"
import "core:fmt"
import "core:os"
+import "core:slice"
TEST_count := 0
TEST_fail := 0
@@ -39,18 +40,40 @@ main :: proc() {
@(test)
test_dwarf :: proc(t: ^testing.T) {
+ buf: [varint.LEB128_MAX_BYTES]u8
+
for vector in ULEB_Vectors {
- val, size := varint.decode_uleb128(vector.encoded)
+ val, size, err := 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)
+
+ msg = fmt.tprintf("Expected decoder to return error %v, got %v", vector.error, err)
+ expect(t, err == vector.error, msg)
+
+ if err == .None { // Try to roundtrip
+ size, err = varint.encode_uleb128(buf[:], vector.value)
+
+ msg = fmt.tprintf("Expected %v to encode to %02x, got %02x", vector.value, vector.encoded, buf[:size])
+ expect(t, size == vector.size && slice.simple_equal(vector.encoded, buf[:size]), msg)
+ }
}
for vector in ILEB_Vectors {
- val, size := varint.decode_ileb128(vector.encoded)
+ val, size, err := 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)
+
+ msg = fmt.tprintf("Expected decoder to return error %v, got %v", vector.error, err)
+ expect(t, err == vector.error, msg)
+
+ if err == .None { // Try to roundtrip
+ size, err = varint.encode_ileb128(buf[:], vector.value)
+
+ msg = fmt.tprintf("Expected %v to encode to %02x, got %02x", vector.value, vector.encoded, buf[:size])
+ expect(t, size == vector.size && slice.simple_equal(vector.encoded, buf[:size]), msg)
+ }
}
}
@@ -58,24 +81,28 @@ ULEB_Test_Vector :: struct {
encoded: []u8,
value: u128,
size: int,
+ error: varint.Error,
}
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 },
+ { []u8{0x00}, 0, 1, .None },
+ { []u8{0x7f}, 127, 1, .None },
+ { []u8{0xE5, 0x8E, 0x26}, 624485, 3, .None },
+ { []u8{0x80}, 0, 0, .Buffer_Too_Small },
+ { []u8{}, 0, 0, .Buffer_Too_Small },
}
ILEB_Test_Vector :: struct {
encoded: []u8,
value: i128,
size: int,
+ error: varint.Error,
}
ILEB_Vectors :: []ILEB_Test_Vector{
- { []u8{0x00}, 0, 1 },
- { []u8{0xC0, 0xBB, 0x78}, -123456, 3 },
- { []u8{}, 0, 0 },
+ { []u8{0x00}, 0, 1, .None },
+ { []u8{0x3f}, 63, 1, .None },
+ { []u8{0x40}, -64, 1, .None },
+ { []u8{0xC0, 0xBB, 0x78}, -123456, 3, .None },
+ { []u8{}, 0, 0, .Buffer_Too_Small },
} \ No newline at end of file