~alva/zig-bare

220e1c35d20cce74ce3243f6bc82422fe5f10343 — owl 5 months ago a4f465e
add go-bare marshal array/slice/map tests
1 files changed, 113 insertions(+), 0 deletions(-)

M src/interop/go-bare/marshal_test.zig
M src/interop/go-bare/marshal_test.zig => src/interop/go-bare/marshal_test.zig +113 -0
@@ 97,3 97,116 @@ test "marshal struct" {
    try e.encode(coords);
    try testing.expectEqualSlices(u8, &reference, fbs.getWritten());
}

test "marshal array" {
    var buf: [0x1_000]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);
    const writer = fbs.writer();
    var e = encoder(writer);

    const val = [4]u8{ 0x11, 0x22, 0x33, 0x44 };
    const reference = &[_]u8{ 0x11, 0x22, 0x33, 0x44 };
    try e.encode(val);
    try testing.expectEqualSlices(u8, reference, fbs.getWritten());
}

test "marshal slice" {
    var buf: [0x1_000]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);
    const writer = fbs.writer();
    var e = encoder(writer);

    const val: []const u8 = &[_]u8{ 0x11, 0x22, 0x33, 0x44 };
    const reference = &[_]u8{ 0x04, 0x11, 0x22, 0x33, 0x44 };
    try e.encode(val);
    try testing.expectEqualSlices(u8, reference, fbs.getWritten());
}

test "marshal map" {
    var buf: [0x1_000]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);
    const writer = fbs.writer();
    var e = encoder(writer);

    var map = std.AutoHashMap(u8, u8).init(testing.allocator);
    defer map.deinit();
    try map.put(0x01, 0x11);
    try map.put(0x01, 0x11);
    try map.put(0x02, 0x22);
    try map.put(0x03, 0x33);

    try e.encode(map);

    const references = &[_][]const u8{
        &[_]u8{
            0x03,
            0x01,
            0x11,
            0x02,
            0x22,
            0x03,
            0x33,
        },
        &[_]u8{
            0x03,
            0x01,
            0x11,
            0x03,
            0x33,
            0x02,
            0x22,
        },
        &[_]u8{
            0x03,
            0x03,
            0x33,
            0x02,
            0x22,
            0x01,
            0x11,
        },
        &[_]u8{
            0x03,
            0x01,
            0x11,
            0x03,
            0x33,
            0x02,
            0x22,
        },
        &[_]u8{
            0x03,
            0x03,
            0x33,
            0x01,
            0x11,
            0x02,
            0x22,
        },
        &[_]u8{
            0x03,
            0x02,
            0x22,
            0x03,
            0x33,
            0x01,
            0x11,
        },
        &[_]u8{
            0x03,
            0x02,
            0x22,
            0x01,
            0x11,
            0x03,
            0x33,
        },
    };

    for (references) |ref| {
        if (std.mem.eql(u8, ref, fbs.getWritten()))
            return;
    }

    unreachable;
}