serialiser
This commit is contained in:
parent
113f49f6a4
commit
e1f1084a39
14 changed files with 698 additions and 51 deletions
|
|
@ -2,7 +2,7 @@ const std = @import("std");
|
|||
|
||||
const msgpack = @import("msgpack");
|
||||
|
||||
const deserialise = msgpack.deserialise.deserialise;
|
||||
const deserialise = msgpack.deserialise;
|
||||
|
||||
fn test_binary(bytes: []const u8, expected: []const u8) !void {
|
||||
const alloc = std.testing.allocator;
|
||||
|
|
@ -10,22 +10,22 @@ fn test_binary(bytes: []const u8, expected: []const u8) !void {
|
|||
const obj = try deserialise(alloc, bytes);
|
||||
defer obj.deinit(alloc);
|
||||
|
||||
try std.testing.expectEqualStrings(expected, obj.raw.binary);
|
||||
try std.testing.expectEqualStrings(expected, obj.binary);
|
||||
}
|
||||
|
||||
test "binary 1" {
|
||||
test "fixbin" {
|
||||
const bytes = [_]u8{ 0xc4, 0x03, 'A', 'B', 'C' };
|
||||
|
||||
try test_binary(&bytes, "ABC");
|
||||
}
|
||||
|
||||
test "binary 2" {
|
||||
test "empty bin" {
|
||||
const bytes = [_]u8{ 0xc4, 0x00 };
|
||||
|
||||
try test_binary(&bytes, "");
|
||||
}
|
||||
|
||||
test "binary 3" {
|
||||
test "bin8" {
|
||||
var bytes: [2 + 255]u8 = undefined;
|
||||
|
||||
bytes[0] = 0xc4;
|
||||
|
|
@ -38,7 +38,7 @@ test "binary 3" {
|
|||
try test_binary(&bytes, "A" ** 255);
|
||||
}
|
||||
|
||||
test "binary 4" {
|
||||
test "bin16 1" {
|
||||
var bytes: [3 + 256]u8 = undefined;
|
||||
|
||||
bytes[0] = 0xc5;
|
||||
|
|
@ -52,7 +52,7 @@ test "binary 4" {
|
|||
try test_binary(&bytes, "A" ** 256);
|
||||
}
|
||||
|
||||
test "binary 5" {
|
||||
test "bin16 2" {
|
||||
var bytes: [3 + 65535]u8 = undefined;
|
||||
|
||||
bytes[0] = 0xc5;
|
||||
|
|
@ -66,7 +66,7 @@ test "binary 5" {
|
|||
try test_binary(&bytes, "A" ** 65535);
|
||||
}
|
||||
|
||||
test "binary 6" {
|
||||
test "bin32" {
|
||||
var bytes: [5 + 65536]u8 = undefined;
|
||||
|
||||
bytes[0] = 0xc6;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ const std = @import("std");
|
|||
|
||||
pub const msgpack = @import("msgpack");
|
||||
|
||||
const deserialise = msgpack.deserialise.deserialise;
|
||||
const deserialise = msgpack.deserialise;
|
||||
|
||||
fn test_int(bytes: []const u8, expected: i64) !void {
|
||||
const alloc = std.testing.allocator;
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
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");
|
||||
|
||||
pub const msgpack = @import("msgpack");
|
||||
|
||||
const deserialise = msgpack.deserialise.deserialise;
|
||||
const deserialise = msgpack.deserialise;
|
||||
|
||||
test {
|
||||
@import("std").testing.refAllDecls(@This());
|
||||
|
|
@ -39,3 +39,64 @@ test "bool" {
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ const std = @import("std");
|
|||
|
||||
const msgpack = @import("msgpack");
|
||||
|
||||
const deserialise = msgpack.deserialise.deserialise;
|
||||
const deserialise = msgpack.deserialise;
|
||||
|
||||
fn test_string(bytes: []const u8, expected: []const u8) !void {
|
||||
const alloc = std.testing.allocator;
|
||||
|
|
@ -10,26 +10,26 @@ fn test_string(bytes: []const u8, expected: []const u8) !void {
|
|||
const obj = try deserialise(alloc, bytes);
|
||||
defer obj.deinit(alloc);
|
||||
|
||||
try std.testing.expectEqualStrings(expected, obj.raw.string);
|
||||
try std.testing.expectEqualStrings(expected, obj.string);
|
||||
}
|
||||
|
||||
test "string 1" {
|
||||
test "fixstr 1" {
|
||||
const bytes = [_]u8{ 0b101_00000 | 0x03, 'A', 'B', 'C' };
|
||||
|
||||
try test_string(&bytes, "ABC");
|
||||
}
|
||||
|
||||
test "string 2" {
|
||||
test "str8 1" {
|
||||
const bytes = [_]u8{ 0xd9, 0x03, 'A', 'B', 'C' };
|
||||
|
||||
try test_string(&bytes, "ABC");
|
||||
}
|
||||
test "string 3" {
|
||||
test "empty string" {
|
||||
const bytes = [_]u8{ 0xd9, 0x00 };
|
||||
|
||||
try test_string(&bytes, "");
|
||||
}
|
||||
test "string 4" {
|
||||
test "str8 2" {
|
||||
var bytes: [2 + 255]u8 = undefined;
|
||||
|
||||
bytes[0] = 0xd9;
|
||||
|
|
@ -41,7 +41,7 @@ test "string 4" {
|
|||
|
||||
try test_string(&bytes, "A" ** 255);
|
||||
}
|
||||
test "string 5" {
|
||||
test "str16 1" {
|
||||
var bytes: [3 + 256]u8 = undefined;
|
||||
|
||||
bytes[0] = 0xda;
|
||||
|
|
@ -54,7 +54,7 @@ test "string 5" {
|
|||
|
||||
try test_string(&bytes, "A" ** 256);
|
||||
}
|
||||
test "string 6" {
|
||||
test "str16 2" {
|
||||
var bytes: [3 + 65535]u8 = undefined;
|
||||
|
||||
bytes[0] = 0xda;
|
||||
|
|
@ -67,7 +67,7 @@ test "string 6" {
|
|||
|
||||
try test_string(&bytes, "A" ** 65535);
|
||||
}
|
||||
test "string 7" {
|
||||
test "str32 1" {
|
||||
var bytes: [5 + 65536]u8 = undefined;
|
||||
|
||||
bytes[0] = 0xdb;
|
||||
|
|
@ -82,3 +82,6 @@ test "string 7" {
|
|||
|
||||
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");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ const std = @import("std");
|
|||
|
||||
pub const msgpack = @import("msgpack");
|
||||
|
||||
const deserialise = msgpack.deserialise.deserialise;
|
||||
const deserialise = msgpack.deserialise;
|
||||
|
||||
fn test_uint(bytes: []const u8, expected: u64) !void {
|
||||
const alloc = std.testing.allocator;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
pub const deserialise = @import("deserialise/root.zig");
|
||||
pub const serialise = @import("serialise/root.zig");
|
||||
|
||||
test {
|
||||
@import("std").testing.refAllDecls(@This());
|
||||
|
|
|
|||
82
tests/serialise/binary.zig
Normal file
82
tests/serialise/binary.zig
Normal 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);
|
||||
}
|
||||
43
tests/serialise/int.zig
Normal file
43
tests/serialise/int.zig
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
const std = @import("std");
|
||||
|
||||
const msgpack = @import("msgpack");
|
||||
|
||||
const Object = msgpack.Object;
|
||||
const serialise = msgpack.serialise;
|
||||
|
||||
fn test_int(expected: []const u8, i: i64) !void {
|
||||
const alloc = std.testing.allocator;
|
||||
|
||||
const bytes = try serialise(alloc, .{ .integer = i });
|
||||
defer alloc.free(bytes);
|
||||
|
||||
try std.testing.expectEqualSlices(u8, expected, bytes);
|
||||
}
|
||||
|
||||
test "neg fixint" {
|
||||
try test_int(&[_]u8{0xFF}, -1);
|
||||
}
|
||||
|
||||
test "int i8" {
|
||||
try test_int(&[_]u8{ 0xd0, 0x80 }, std.math.minInt(i8));
|
||||
|
||||
try test_int(&[_]u8{ 0xd0, 0x7F }, std.math.maxInt(i8));
|
||||
}
|
||||
|
||||
test "int i16" {
|
||||
try test_int(&[_]u8{ 0xd1, 0x80, 0x00 }, std.math.minInt(i16));
|
||||
|
||||
try test_int(&[_]u8{ 0xd1, 0x7f, 0xff }, std.math.maxInt(i16));
|
||||
}
|
||||
|
||||
test "int i32" {
|
||||
try test_int(&[_]u8{ 0xd2, 0x80, 0x00, 0x00, 0x00 }, std.math.minInt(i32));
|
||||
|
||||
try test_int(&[_]u8{ 0xd2, 0x7f, 0xff, 0xff, 0xff }, std.math.maxInt(i32));
|
||||
}
|
||||
|
||||
test "int i64" {
|
||||
try test_int(&[_]u8{ 0xd3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, std.math.minInt(i64));
|
||||
|
||||
try test_int(&[_]u8{ 0xd3, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, std.math.maxInt(i64));
|
||||
}
|
||||
83
tests/serialise/root.zig
Normal file
83
tests/serialise/root.zig
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
84
tests/serialise/string.zig
Normal file
84
tests/serialise/string.zig
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
const std = @import("std");
|
||||
|
||||
const msgpack = @import("msgpack");
|
||||
|
||||
const serialise = msgpack.serialise;
|
||||
|
||||
fn test_string(expected: []const u8, s: []const u8) !void {
|
||||
const alloc = std.testing.allocator;
|
||||
|
||||
const bytes = try serialise(alloc, .{ .string = s });
|
||||
defer alloc.free(bytes);
|
||||
|
||||
try std.testing.expectEqualSlices(u8, expected, bytes);
|
||||
}
|
||||
|
||||
test "fixstr" {
|
||||
const bytes = [_]u8{ 0b101_00000 | 0x03, 'A', 'B', 'C' };
|
||||
|
||||
try test_string(&bytes, "ABC");
|
||||
}
|
||||
|
||||
test "empty str" {
|
||||
const bytes = [_]u8{0b101_00000};
|
||||
|
||||
try test_string(&bytes, "");
|
||||
}
|
||||
|
||||
test "string 4" {
|
||||
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 "string 5" {
|
||||
// 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 "string 6" {
|
||||
// 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 "string 7" {
|
||||
// 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 "string 8" {
|
||||
// try test_string(&[_]u8{ 0xaa, 0x6c, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65 }, "le message");
|
||||
// }
|
||||
39
tests/serialise/uint.zig
Normal file
39
tests/serialise/uint.zig
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
const std = @import("std");
|
||||
|
||||
const msgpack = @import("msgpack");
|
||||
|
||||
const Object = msgpack.Object;
|
||||
const serialise = msgpack.serialise;
|
||||
|
||||
fn test_uint(expected: []const u8, u: u64) !void {
|
||||
const alloc = std.testing.allocator;
|
||||
|
||||
const bytes = try serialise(alloc, .{ .uinteger = u });
|
||||
defer alloc.free(bytes);
|
||||
|
||||
try std.testing.expectEqualSlices(u8, expected, bytes);
|
||||
}
|
||||
|
||||
test "pos fixint" {
|
||||
try test_uint(&[_]u8{0x00}, 0);
|
||||
|
||||
try test_uint(&[_]u8{0x07}, 0x07);
|
||||
|
||||
try test_uint(&[_]u8{0x7F}, 0x7F);
|
||||
}
|
||||
|
||||
test "int u8" {
|
||||
try test_uint(&[_]u8{ 0xcc, 0xff }, std.math.maxInt(u8));
|
||||
}
|
||||
|
||||
test "int u16" {
|
||||
try test_uint(&[_]u8{ 0xcd, 0xff, 0xff }, std.math.maxInt(u16));
|
||||
}
|
||||
|
||||
test "int u32" {
|
||||
try test_uint(&[_]u8{ 0xce, 0xff, 0xff, 0xff, 0xff }, std.math.maxInt(u32));
|
||||
}
|
||||
|
||||
test "int u64" {
|
||||
try test_uint(&[_]u8{ 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, std.math.maxInt(u64));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue