~alva/zig-bare

7c1b963c6d58e6196bf63a12edf637afdd92583a — Alva 4 months ago dba916a
Test tagged union with active void member
1 files changed, 29 insertions(+), 0 deletions(-)

M src/test.zig
M src/test.zig => src/test.zig +29 -0
@@ 409,6 409,18 @@ test "write tagged union" {
    expectEqualSlices(u8, fbs.getWritten(), expected[0..]);
}

test "write tagged union with void member active" {
    var buf: [10]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);

    const Foo = union(enum) { a: i64, b: void, c: u8 };
    const foo = Foo{ .b = {} };

    try Writer.init().write(foo, fbs.outStream());
    const expected = "\x01";
    expectEqualSlices(u8, fbs.getWritten(), expected[0..]);
}

test "write void" {
     var buf: [10]u8 = undefined;
     var fbs = io.fixedBufferStream(&buf);


@@ 530,6 542,23 @@ test "round trip tagged union" {
    expectEqual(foo, res);
}

test "round trip tagged union with void member active" {
    const Foo = union(enum) {
        a: i64,
        b: void,
        c: bool,
    };
    const foo = Foo{ .b = {} };
    var buf: [0x100]u8 = undefined;
    var fbs = io.fixedBufferStream(&buf);

    try Writer.init().write(foo, fbs.outStream());
    const res = try Reader.init(testing.allocator)
        .read(Foo, io.fixedBufferStream(fbs.getWritten()).inStream());

    expectEqual(foo, res);
}

test "bare.Reader frees its memory" {
    var r = Reader.init(testing.allocator);
    _ = try r.read(std.AutoHashMap(u8, u8), io.fixedBufferStream("\x02\x01\x02\x03\x04").inStream());