~alva/zig-bare

4b3e470a4a5db3c311939e21e7fcd9088c812915 — owl 6 months ago a052c90
add go-bare unmarshal array/slice tests
1 files changed, 43 insertions(+), 0 deletions(-)

M src/interop/go-bare/unmarshal_test.zig
M src/interop/go-bare/unmarshal_test.zig => src/interop/go-bare/unmarshal_test.zig +43 -0
@@ 103,3 103,46 @@ test "unmarshal struct" {
    try expectEqual(@as(usize, 2), coords.y);
    try expectEqual(@as(usize, 3), coords.z);
}

test "unmarshal omitted fields" {
    const Coordinates = struct {
        x: usize,
        y: usize,
        // TODO: we don't have omitted fields, just optional.
        //z: ?usize,
    };
    const payload = &[_]u8{ 0x01, 0x02 };
    var fbs = io.fixedBufferStream(payload);
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    const coords = try d.decode(Coordinates);
    try expectEqual(@as(usize, 1), coords.x);
    try expectEqual(@as(usize, 2), coords.y);
}

test "unmarshal array" {
    const payload = &[_]u8{ 0x11, 0x22, 0x33, 0x44 };
    var fbs = io.fixedBufferStream(payload);
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    const val = try d.decode([4]u8);
    try expectEqual(@as(usize, 0x11), val[0]);
    try expectEqual(@as(usize, 0x22), val[1]);
    try expectEqual(@as(usize, 0x33), val[2]);
    try expectEqual(@as(usize, 0x44), val[3]);
}

test "unmarshal slice" {
    const payload = &[_]u8{ 0x04, 0x11, 0x22, 0x33, 0x44 };
    var fbs = io.fixedBufferStream(payload);
    const reader = fbs.reader();
    var d = decoder(testing.allocator, reader);
    defer d.deinit();
    const val = try d.decode([]const u8);
    try expectEqual(@as(usize, 0x11), val[0]);
    try expectEqual(@as(usize, 0x22), val[1]);
    try expectEqual(@as(usize, 0x33), val[2]);
    try expectEqual(@as(usize, 0x44), val[3]);
}