msgpack-zig/tests/deserialise/string.zig
2025-04-02 17:38:38 +02:00

87 lines
1.7 KiB
Zig

const std = @import("std");
const msgpack = @import("msgpack");
const deserialise = msgpack.deserialise;
fn test_string(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.string);
}
test "fixstr 1" {
const bytes = [_]u8{ 0b101_00000 | 0x03, 'A', 'B', 'C' };
try test_string(&bytes, "ABC");
}
test "str8 1" {
const bytes = [_]u8{ 0xd9, 0x03, 'A', 'B', 'C' };
try test_string(&bytes, "ABC");
}
test "empty string" {
const bytes = [_]u8{ 0xd9, 0x00 };
try test_string(&bytes, "");
}
test "str8 2" {
var bytes: [2 + 255]u8 = undefined;
bytes[0] = 0xd9;
bytes[1] = 0xff;
for (bytes[2..]) |*c| {
c.* = 'A';
}
try test_string(&bytes, "A" ** 255);
}
test "str16 1" {
var bytes: [3 + 256]u8 = undefined;
bytes[0] = 0xda;
bytes[1] = 0x01;
bytes[2] = 0x00;
for (bytes[3..]) |*c| {
c.* = 'A';
}
try test_string(&bytes, "A" ** 256);
}
test "str16 2" {
var bytes: [3 + 65535]u8 = undefined;
bytes[0] = 0xda;
bytes[1] = 0xff;
bytes[2] = 0xff;
for (bytes[3..]) |*c| {
c.* = 'A';
}
try test_string(&bytes, "A" ** 65535);
}
test "str32 1" {
var bytes: [5 + 65536]u8 = undefined;
bytes[0] = 0xdb;
bytes[1] = 0x00;
bytes[2] = 0x01;
bytes[3] = 0x00;
bytes[4] = 0x00;
for (bytes[5..]) |*c| {
c.* = 'A';
}
try test_string(&bytes, "A" ** 65536);
}
test "fixstr 2" {
try test_string(&[_]u8{ 0xaa, 0x6c, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65 }, "le message");
}