118 lines
3.3 KiB
Zig
118 lines
3.3 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");
|
|
|
|
test {
|
|
@import("std").testing.refAllDecls(@This());
|
|
}
|
|
|
|
const Object = msgpack.Object;
|
|
const serialise = msgpack.serialise;
|
|
|
|
test "nil" {
|
|
const alloc = std.testing.allocator;
|
|
|
|
const obj: Object = .nil;
|
|
defer obj.deinit(alloc);
|
|
|
|
const bytes = try serialise(alloc, obj);
|
|
defer alloc.free(bytes);
|
|
|
|
try std.testing.expectEqualSlices(u8, &[_]u8{0xc0}, bytes);
|
|
}
|
|
|
|
test "bool" {
|
|
const alloc = std.testing.allocator;
|
|
|
|
{
|
|
const obj = Object{ .bool = false };
|
|
defer obj.deinit(alloc);
|
|
|
|
const bytes = try serialise(alloc, obj);
|
|
defer alloc.free(bytes);
|
|
|
|
try std.testing.expectEqualSlices(u8, &[_]u8{0xc2}, bytes);
|
|
}
|
|
|
|
{
|
|
const obj = Object{ .bool = true };
|
|
defer obj.deinit(alloc);
|
|
|
|
const bytes = try serialise(alloc, obj);
|
|
defer alloc.free(bytes);
|
|
|
|
try std.testing.expectEqualSlices(u8, &[_]u8{0xc3}, bytes);
|
|
}
|
|
}
|
|
|
|
test "f32" {
|
|
const alloc = std.testing.allocator;
|
|
|
|
{
|
|
const bytes = try serialise(alloc, .{ .float32 = 3.4028234e38 });
|
|
defer alloc.free(bytes);
|
|
|
|
// try std.testing.expectEqual(3.4028234e38, obj.float32);
|
|
try std.testing.expectEqualSlices(u8, &[_]u8{ 0xca, 0x7f, 0x7f, 0xff, 0xff }, bytes);
|
|
}
|
|
}
|
|
|
|
test "f64" {
|
|
const alloc = std.testing.allocator;
|
|
|
|
{
|
|
const bytes = try serialise(alloc, .{ .float64 = 0.0 });
|
|
defer alloc.free(bytes);
|
|
|
|
// try std.testing.expectEqual(3.4028234e38, obj.float32);
|
|
try std.testing.expectEqualSlices(u8, &[_]u8{ 0xcb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, bytes);
|
|
}
|
|
|
|
{
|
|
const bytes = try serialise(alloc, .{ .float64 = 42.0 });
|
|
defer alloc.free(bytes);
|
|
|
|
// try std.testing.expectEqual(3.4028234e38, obj.float32);
|
|
try std.testing.expectEqualSlices(u8, &[_]u8{ 0xcb, 0x40, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, bytes);
|
|
}
|
|
}
|
|
|
|
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);
|
|
|
|
const bytes = try serialise(alloc, .{ .array = &[_]Object{ .{ .string = "le" }, .{ .string = "shit" } } });
|
|
defer alloc.free(bytes);
|
|
|
|
try std.testing.expectEqualSlices(u8, &[_]u8{ 0x92, 0xa2, 0x6c, 0x65, 0xa4, 0x73, 0x68, 0x69, 0x74 }, bytes);
|
|
}
|
|
}
|
|
|
|
test "map" {
|
|
const alloc = std.testing.allocator;
|
|
|
|
const MapEntry = msgpack.MapEntry;
|
|
|
|
{
|
|
const bytes = try serialise(alloc, .{ .map = &[_]MapEntry{
|
|
.{ .key = .{ .uinteger = 0 }, .value = .{ .string = "le" } },
|
|
.{ .key = .{ .uinteger = 1 }, .value = .{ .string = "shit" } },
|
|
} });
|
|
defer alloc.free(bytes);
|
|
|
|
try std.testing.expectEqualSlices(u8, &[_]u8{ 0x82, 0x00, 0xa2, 0x6c, 0x65, 0x01, 0xa4, 0x73, 0x68, 0x69, 0x74 }, bytes);
|
|
}
|
|
}
|