2025-03-31 00:02:39 +02:00
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
|
|
const msgpack = @import("msgpack");
|
|
|
|
|
|
2025-04-02 02:44:26 +02:00
|
|
|
const deserialise = msgpack.deserialise;
|
2025-03-31 00:02:39 +02:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2025-04-02 02:44:26 +02:00
|
|
|
try std.testing.expectEqualStrings(expected, obj.string);
|
2025-03-31 00:02:39 +02:00
|
|
|
}
|
|
|
|
|
|
2025-04-02 02:44:26 +02:00
|
|
|
test "fixstr 1" {
|
2025-03-31 00:02:39 +02:00
|
|
|
const bytes = [_]u8{ 0b101_00000 | 0x03, 'A', 'B', 'C' };
|
|
|
|
|
|
|
|
|
|
try test_string(&bytes, "ABC");
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-02 02:44:26 +02:00
|
|
|
test "str8 1" {
|
2025-03-31 00:02:39 +02:00
|
|
|
const bytes = [_]u8{ 0xd9, 0x03, 'A', 'B', 'C' };
|
|
|
|
|
|
|
|
|
|
try test_string(&bytes, "ABC");
|
|
|
|
|
}
|
2025-04-02 02:44:26 +02:00
|
|
|
test "empty string" {
|
2025-03-31 00:02:39 +02:00
|
|
|
const bytes = [_]u8{ 0xd9, 0x00 };
|
|
|
|
|
|
|
|
|
|
try test_string(&bytes, "");
|
|
|
|
|
}
|
2025-04-02 02:44:26 +02:00
|
|
|
test "str8 2" {
|
2025-03-31 00:02:39 +02:00
|
|
|
var bytes: [2 + 255]u8 = undefined;
|
|
|
|
|
|
|
|
|
|
bytes[0] = 0xd9;
|
|
|
|
|
bytes[1] = 0xff;
|
|
|
|
|
|
|
|
|
|
for (bytes[2..]) |*c| {
|
|
|
|
|
c.* = 'A';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try test_string(&bytes, "A" ** 255);
|
|
|
|
|
}
|
2025-04-02 02:44:26 +02:00
|
|
|
test "str16 1" {
|
2025-03-31 00:02:39 +02:00
|
|
|
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);
|
|
|
|
|
}
|
2025-04-02 02:44:26 +02:00
|
|
|
test "str16 2" {
|
2025-03-31 00:02:39 +02:00
|
|
|
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);
|
|
|
|
|
}
|
2025-04-02 02:44:26 +02:00
|
|
|
test "str32 1" {
|
2025-03-31 00:02:39 +02:00
|
|
|
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);
|
|
|
|
|
}
|
2025-04-02 02:44:26 +02:00
|
|
|
test "fixstr 2" {
|
|
|
|
|
try test_string(&[_]u8{ 0xaa, 0x6c, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65 }, "le message");
|
|
|
|
|
}
|