[varint] Add signed LEB128 encoding.

This commit is contained in:
Jeroen van Rijn
2022-03-08 18:07:16 +01:00
parent 6d7217f37a
commit e76a5d8e12
2 changed files with 119 additions and 24 deletions
@@ -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 },
}