added deserialiser tests, fixed some bugs in deserialised

This commit is contained in:
Moritz Gmeiner 2025-03-31 00:02:39 +02:00
commit 0275d67669
8 changed files with 267 additions and 93 deletions

View file

@ -0,0 +1,83 @@
const std = @import("std");
const msgpack = @import("msgpack");
const deserialise = msgpack.deserialise.deserialise;
fn test_binary(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.raw.binary);
}
test "binary 1" {
const bytes = [_]u8{ 0xc4, 0x03, 'A', 'B', 'C' };
try test_binary(&bytes, "ABC");
}
test "binary 2" {
const bytes = [_]u8{ 0xc4, 0x00 };
try test_binary(&bytes, "");
}
test "binary 3" {
var bytes: [2 + 255]u8 = undefined;
bytes[0] = 0xc4;
bytes[1] = 0xff;
for (bytes[2..]) |*c| {
c.* = 'A';
}
try test_binary(&bytes, "A" ** 255);
}
test "binary 4" {
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 "binary 5" {
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 "binary 6" {
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);
}

38
tests/deserialise/int.zig Normal file
View file

@ -0,0 +1,38 @@
const std = @import("std");
pub const msgpack = @import("msgpack");
const deserialise = msgpack.deserialise.deserialise;
fn test_int(bytes: []const u8, expected: i64) !void {
const alloc = std.testing.allocator;
const obj = try deserialise(alloc, bytes);
defer obj.deinit(alloc);
try std.testing.expectEqual(expected, obj.integer);
}
test "int i8" {
try test_int(&[_]u8{ 0xd0, 0x80 }, std.math.minInt(i8));
try test_int(&[_]u8{0x7f}, std.math.maxInt(i8));
}
test "int i16" {
try test_int(&[_]u8{ 0xd1, 0x80, 0x00 }, std.math.minInt(i16));
try test_int(&[_]u8{ 0xcd, 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{ 0xce, 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{ 0xcf, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, std.math.maxInt(i64));
}

View file

@ -0,0 +1,40 @@
const std = @import("std");
pub const binary = @import("binary.zig");
pub const string = @import("string.zig");
pub const int = @import("int.zig");
pub const msgpack = @import("msgpack");
const deserialise = msgpack.deserialise.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);
}
}

View file

@ -0,0 +1,84 @@
const std = @import("std");
const msgpack = @import("msgpack");
const deserialise = msgpack.deserialise.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.raw.string);
}
test "string 1" {
const bytes = [_]u8{ 0b101_00000 | 0x03, 'A', 'B', 'C' };
try test_string(&bytes, "ABC");
}
test "string 2" {
const bytes = [_]u8{ 0xd9, 0x03, 'A', 'B', 'C' };
try test_string(&bytes, "ABC");
}
test "string 3" {
const bytes = [_]u8{ 0xd9, 0x00 };
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);
}

5
tests/root.zig Normal file
View file

@ -0,0 +1,5 @@
pub const deserialise = @import("deserialise/root.zig");
test {
@import("std").testing.refAllDecls(@This());
}