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

102 lines
2.7 KiB
Zig

const std = @import("std");
pub const msgpack = @import("msgpack");
pub const binary = @import("binary.zig");
pub const string = @import("string.zig");
pub const int = @import("int.zig");
pub const uint = @import("uint.zig");
const deserialise = msgpack.deserialise;
test {
@import("std").testing.refAllDecls(@This());
}
test "nil" {
const alloc = std.testing.allocator;
const obj = try deserialise(alloc, &[_]u8{0xc0});
defer obj.deinit(alloc);
try std.testing.expectEqualDeep(msgpack.Object.nil, obj);
}
test "bool" {
const alloc = std.testing.allocator;
{
const obj = try deserialise(alloc, &[_]u8{0xc2});
defer obj.deinit(alloc);
try std.testing.expectEqualDeep(msgpack.Object{ .bool = false }, obj);
}
{
const obj = try deserialise(alloc, &[_]u8{0xc3});
defer obj.deinit(alloc);
try std.testing.expectEqualDeep(msgpack.Object{ .bool = true }, obj);
}
}
test "f32" {
const alloc = std.testing.allocator;
{
const obj = try deserialise(alloc, &[_]u8{ 0xca, 0x7f, 0x7f, 0xff, 0xff });
defer obj.deinit(alloc);
// try std.testing.expectEqualDeep(msgpack.Object{ .float = 3.4028234e38 }, obj);
try std.testing.expectEqual(3.4028234e38, obj.float32);
}
}
test "f64" {
const alloc = std.testing.allocator;
{
const obj = try deserialise(alloc, &[_]u8{ 0xcb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });
defer obj.deinit(alloc);
try std.testing.expectEqualDeep(msgpack.Object{ .float64 = 0.0 }, obj);
}
{
const obj = try deserialise(alloc, &[_]u8{ 0xcb, 0x40, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });
defer obj.deinit(alloc);
try std.testing.expectEqualDeep(msgpack.Object{ .float64 = 42.0 }, obj);
}
}
test "array" {
const alloc = std.testing.allocator;
{
const obj = try deserialise(alloc, &[_]u8{ 0x92, 0xa2, 0x6c, 0x65, 0xa4, 0x73, 0x68, 0x69, 0x74 });
defer obj.deinit(alloc);
try std.testing.expectEqual(2, obj.array.len);
try std.testing.expectEqualStrings("le", obj.array[0].string);
try std.testing.expectEqualStrings("shit", obj.array[1].string);
}
}
test "map" {
const alloc = std.testing.allocator;
{
const obj = try deserialise(alloc, &[_]u8{ 0x82, 0x00, 0xa2, 0x6c, 0x65, 0x01, 0xa4, 0x73, 0x68, 0x69, 0x74 });
defer obj.deinit(alloc);
try std.testing.expectEqual(2, obj.map.len);
try std.testing.expectEqual(0, obj.map[0].key.uinteger);
try std.testing.expectEqualStrings("le", obj.map[0].value.string);
try std.testing.expectEqual(1, obj.map[1].key.uinteger);
try std.testing.expectEqualStrings("shit", obj.map[1].value.string);
}
}