83 lines
1.5 KiB
Zig
83 lines
1.5 KiB
Zig
|
|
const std = @import("std");
|
||
|
|
|
||
|
|
const msgpack = @import("msgpack");
|
||
|
|
|
||
|
|
const deserialise = msgpack.deserialise.deserialise;
|
||
|
|
|
||
|
|
fn test_binary(bytes: []const u8, expected: []const u8) !void {
|
||
|
|
const alloc = std.testing.allocator;
|
||
|
|
|
||
|
|
const obj = try deserialise(alloc, bytes);
|
||
|
|
defer obj.deinit(alloc);
|
||
|
|
|
||
|
|
try std.testing.expectEqualStrings(expected, obj.raw.binary);
|
||
|
|
}
|
||
|
|
|
||
|
|
test "binary 1" {
|
||
|
|
const bytes = [_]u8{ 0xc4, 0x03, 'A', 'B', 'C' };
|
||
|
|
|
||
|
|
try test_binary(&bytes, "ABC");
|
||
|
|
}
|
||
|
|
|
||
|
|
test "binary 2" {
|
||
|
|
const bytes = [_]u8{ 0xc4, 0x00 };
|
||
|
|
|
||
|
|
try test_binary(&bytes, "");
|
||
|
|
}
|
||
|
|
|
||
|
|
test "binary 3" {
|
||
|
|
var bytes: [2 + 255]u8 = undefined;
|
||
|
|
|
||
|
|
bytes[0] = 0xc4;
|
||
|
|
bytes[1] = 0xff;
|
||
|
|
|
||
|
|
for (bytes[2..]) |*c| {
|
||
|
|
c.* = 'A';
|
||
|
|
}
|
||
|
|
|
||
|
|
try test_binary(&bytes, "A" ** 255);
|
||
|
|
}
|
||
|
|
|
||
|
|
test "binary 4" {
|
||
|
|
var bytes: [3 + 256]u8 = undefined;
|
||
|
|
|
||
|
|
bytes[0] = 0xc5;
|
||
|
|
bytes[1] = 0x01;
|
||
|
|
bytes[2] = 0x00;
|
||
|
|
|
||
|
|
for (bytes[3..]) |*c| {
|
||
|
|
c.* = 'A';
|
||
|
|
}
|
||
|
|
|
||
|
|
try test_binary(&bytes, "A" ** 256);
|
||
|
|
}
|
||
|
|
|
||
|
|
test "binary 5" {
|
||
|
|
var bytes: [3 + 65535]u8 = undefined;
|
||
|
|
|
||
|
|
bytes[0] = 0xc5;
|
||
|
|
bytes[1] = 0xff;
|
||
|
|
bytes[2] = 0xff;
|
||
|
|
|
||
|
|
for (bytes[3..]) |*c| {
|
||
|
|
c.* = 'A';
|
||
|
|
}
|
||
|
|
|
||
|
|
try test_binary(&bytes, "A" ** 65535);
|
||
|
|
}
|
||
|
|
|
||
|
|
test "binary 6" {
|
||
|
|
var bytes: [5 + 65536]u8 = undefined;
|
||
|
|
|
||
|
|
bytes[0] = 0xc6;
|
||
|
|
bytes[1] = 0x00;
|
||
|
|
bytes[2] = 0x01;
|
||
|
|
bytes[3] = 0x00;
|
||
|
|
bytes[4] = 0x00;
|
||
|
|
|
||
|
|
for (bytes[5..]) |*c| {
|
||
|
|
c.* = 'A';
|
||
|
|
}
|
||
|
|
|
||
|
|
try test_binary(&bytes, "A" ** 65536);
|
||
|
|
}
|