serialiser

This commit is contained in:
Moritz Gmeiner 2025-04-02 02:44:26 +02:00
commit e1f1084a39
14 changed files with 698 additions and 51 deletions

View file

@ -0,0 +1,82 @@
test "fixbin" {
const bytes = [_]u8{ 0xc4, 0x03, 'A', 'B', 'C' };
try test_binary(&bytes, "ABC");
}
test "empty bin" {
const bytes = [_]u8{ 0xc4, 0x00 };
try test_binary(&bytes, "");
}
test "bin8" {
var bytes: [2 + 255]u8 = undefined;
bytes[0] = 0xc4;
bytes[1] = 0xff;
for (bytes[2..]) |*c| {
c.* = 'A';
}
try test_binary(&bytes, "A" ** 255);
}
const std = @import("std");
const msgpack = @import("msgpack");
const serialise = msgpack.serialise;
fn test_binary(expected: []const u8, s: []const u8) !void {
const alloc = std.testing.allocator;
const bytes = try serialise(alloc, .{ .binary = s });
defer alloc.free(bytes);
try std.testing.expectEqualSlices(u8, expected, bytes);
}
test "bin16 1" {
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 "bin16 2" {
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 "bin32" {
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);
}