~alva/zig-bare

9ac4c797fd6be7999c450eac3748a374e4ceacda — owl 6 months ago 5b6f6a1
port go-bare marshal tests
3 files changed, 65 insertions(+), 2 deletions(-)

M src/bare.zig
A src/interop/go-bare/marshal_test.zig
M src/test.zig
M src/bare.zig => src/bare.zig +2 -2
@@ 267,8 267,8 @@ pub fn Encoder(comptime WriterType: type) type {

        fn encodeInt(self: *Self, comptime T: type, value: T) !void {
            return switch (T) {
                usize, c_uint => self.encodeVarUint(T),
                isize, c_int => self.encodeVarInt(T),
                usize, c_uint => self.encodeVarUint(value),
                isize, c_int => self.encodeVarInt(value),
                i8,
                u8,
                i16,

A src/interop/go-bare/marshal_test.zig => src/interop/go-bare/marshal_test.zig +62 -0
@@ 0,0 1,62 @@
const std = @import("std");
const io = std.io;
const math = std.math;
const mem = std.mem;
const testing = std.testing;

const bare = @import("bare");
const decoder = bare.decoder;
const encoder = bare.encoder;

const expect = testing.expect;
const expectEqual = testing.expectEqual;
const expectEqualSlices = testing.expectEqualSlices;
const expectError = testing.expectError;

const payloads1 = [_][]const u8{
    &[_]u8{0x42},
    &[_]u8{ 0xFE, 0xCA },
    &[_]u8{ 0xEF, 0xBE, 0xAD, 0xDE },
    &[_]u8{ 0xEF, 0xBE, 0xAD, 0xDE, 0xBE, 0xBA, 0xFE, 0xCA },
    &[_]u8{ 0xEF, 0xFD, 0xB6, 0xF5, 0x0D },
    &[_]u8{0xD6},
    &[_]u8{ 0x2E, 0xFB },
    &[_]u8{ 0xB2, 0x9E, 0x43, 0xFF },
    &[_]u8{ 0x4F, 0x0B, 0x6E, 0x9D, 0xAB, 0x23, 0xD4, 0xFF },
    &[_]u8{ 0x9B, 0x85, 0xE3, 0x0B },
    &[_]u8{ 0x71, 0x2D, 0xA7, 0x44 },
    &[_]u8{ 0x9B, 0x6C, 0xC9, 0x20, 0xF0, 0x21, 0x3F, 0x42 },
    &[_]u8{0x01},
    &[_]u8{0x00},
    &[_]u8{ 0x1B, 0xE3, 0x81, 0x93, 0xE3, 0x82, 0x93, 0xE3, 0x81, 0xAB, 0xE3, 0x81, 0xA1, 0xE3, 0x81, 0xAF, 0xE3, 0x80, 0x81, 0xE4, 0xB8, 0x96, 0xE7, 0x95, 0x8C, 0xEF, 0xBC, 0x81 },
};

// tests ported from go-bare
test "marshal value" {
    inline for (.{
        .{ payloads1[0], u8, 0x42 },
        .{ payloads1[1], u16, 0xCAFE },
        .{ payloads1[2], u32, 0xDEADBEEF },
        .{ payloads1[3], u64, 0xCAFEBABEDEADBEEF },
        .{ payloads1[4], usize, 0xDEADBEEF },
        .{ payloads1[5], i8, -42 },
        .{ payloads1[6], i16, -1234 },
        .{ payloads1[7], i32, -12345678 },
        .{ payloads1[8], i64, -12345678987654321 },
        .{ payloads1[9], isize, -12345678 },
        .{ payloads1[10], f32, 1337.42 },
        .{ payloads1[11], f64, 133713371337.42424242 },
        .{ payloads1[12], bool, true },
        .{ payloads1[13], bool, false },
        .{ payloads1[14], []const u8, "こんにちは、世界!" },
    }) |tuple| {
        var buf: [0x1_000]u8 = undefined;
        var fbs = io.fixedBufferStream(&buf);
        const writer = fbs.writer();
        var e = encoder(writer);
        // should return without error
        const val: tuple[1] = tuple[2];
        try e.encode(val);
        try expectEqualSlices(u8, tuple[0], fbs.getWritten());
    }
}

M src/test.zig => src/test.zig +1 -0
@@ 16,6 16,7 @@ const expectError = testing.expectError;
test "refAllDecls" {
    std.testing.refAllDecls(bare);
    std.testing.refAllDecls(@import("interop/go-bare/unmarshal_test.zig"));
    std.testing.refAllDecls(@import("interop/go-bare/marshal_test.zig"));
}

test "read u8" {