From 5b2cd802103a5474e0226e7ef02e19d1edf8f0ef Mon Sep 17 00:00:00 2001 From: Kitteh Date: Fri, 4 Jun 2021 15:13:02 +0100 Subject: [PATCH] Add some more tests. --- src/qtshit/read/readByte.zig | 9 +++++++ src/qtshit/read/readInt.zig | 8 +++++++ src/qtshit/read/readLong.zig | 8 +++++++ src/qtshit/read/readQByteArray.zig | 37 ++++++++++++++++++++--------- src/qtshit/read/readQDateTime.zig | 10 ++++++++ src/qtshit/read/readQStringList.zig | 13 ++++++++++ src/qtshit/read/tests.zig | 11 +++++++++ src/qtshit/read/usertypes/tests.zig | 6 +++++ src/tests.zig | 5 +--- 9 files changed, 92 insertions(+), 15 deletions(-) create mode 100644 src/qtshit/read/tests.zig create mode 100644 src/qtshit/read/usertypes/tests.zig diff --git a/src/qtshit/read/readByte.zig b/src/qtshit/read/readByte.zig index 26450af..b62af66 100644 --- a/src/qtshit/read/readByte.zig +++ b/src/qtshit/read/readByte.zig @@ -3,3 +3,12 @@ pub fn readByte(reader: anytype) !u8 { //std.debug.print("read: readByte\n", .{}); return try reader.readByte(); } + +test "deserialize byte" { + var bytes = &[_]u8{0x01}; + var fBS = std.io.fixedBufferStream(bytes); + + var byte = try readByte(fBS.reader()); + + try std.testing.expect(byte == 0x01); +} \ No newline at end of file diff --git a/src/qtshit/read/readInt.zig b/src/qtshit/read/readInt.zig index dc00647..a04685f 100644 --- a/src/qtshit/read/readInt.zig +++ b/src/qtshit/read/readInt.zig @@ -4,3 +4,11 @@ pub fn readInt(reader: anytype) !i32 { //std.debug.print("read: readInt\n", .{}); return try reader.readIntBig(i32); } + +test "deserialize int" { + var bytes = &[_]u8{ 0xFF, 0xFF, 0xFA, 0xE0 }; + var fBS = std.io.fixedBufferStream(bytes); + + var int = try readInt(fBS.reader()); + try std.testing.expect(int == -1312); +} diff --git a/src/qtshit/read/readLong.zig b/src/qtshit/read/readLong.zig index 81b93e6..f768dae 100644 --- a/src/qtshit/read/readLong.zig +++ b/src/qtshit/read/readLong.zig @@ -4,3 +4,11 @@ pub fn readLong(reader: anytype) !i64 { //std.debug.print("read: readLong\n", .{}); return try reader.readIntBig(i64); } + +test "deserialize long" { + var bytes = &[_]u8{ 0xFF, 0xFF, 0xFF, 0xE1, 0x73, 0xDE, 0xD7, 0xAD }; + var fBS = std.io.fixedBufferStream(bytes); + + var long = try readLong(fBS.reader()); + try std.testing.expect(long == -131200002131); +} \ No newline at end of file diff --git a/src/qtshit/read/readQByteArray.zig b/src/qtshit/read/readQByteArray.zig index 1ff40cd..d0d27ce 100644 --- a/src/qtshit/read/readQByteArray.zig +++ b/src/qtshit/read/readQByteArray.zig @@ -1,24 +1,39 @@ const std = @import("std"); const readUInt = @import("./readUInt.zig").readUInt; +const range = @import("../utils/RangeIter.zig").range; +const freeQByteArray = @import("../utils/free/freeQByteArray.zig").freeQByteArray; pub fn readQByteArray(reader: anytype, allocator: *std.mem.Allocator) ![]u8 { var length = try readUInt(reader); - if (length == 4294967295) { + if (length == std.math.maxInt(@TypeOf(length))) { return ""; } - - //std.debug.print("read: readQByteArray length={d}\n", .{length}); var byteList = try allocator.alloc(u8, @intCast(usize, length)); - var index: usize = 0; - while (true) { - //std.debug.print("read: readQByteArray length={d} index={d} \n", .{length, index}); - //std.debug.print("read: readQByteArray \"{s}\" \n", .{byteList}); - if (index == length) return byteList; - + var iter = range(u32, 0, length); + while (iter.next()) |i| { const byte = try reader.readByte(); - byteList[index] = byte; - index += 1; + byteList[i] = byte; } + //std.debug.print("read: readQByteArray length={d} content={any} \n", .{length, byteList}); + return byteList; } + +test "deserialize QByteArray" { + var bytes = &[_]u8{ 00, 00, 00, 04, 11, 22, 33, 44 }; + var fBS = std.io.fixedBufferStream(bytes); + + var qbytearray = try readQByteArray(fBS.reader(), std.testing.allocator); + defer freeQByteArray(qbytearray, std.testing.allocator); + try std.testing.expect(std.mem.eql(u8, qbytearray, &[_]u8{ 11, 22, 33, 44 })); +} + +test "deserialize QByteArray max length" { + var bytes = &[_]u8{ 0xff, 0xff, 0xff, 0xff }; + var fBS = std.io.fixedBufferStream(bytes); + + var qbytearray = try readQByteArray(fBS.reader(), std.testing.allocator); + defer freeQByteArray(qbytearray, std.testing.allocator); + try std.testing.expect(std.mem.eql(u8, qbytearray, "")); +} \ No newline at end of file diff --git a/src/qtshit/read/readQDateTime.zig b/src/qtshit/read/readQDateTime.zig index a6b4a2e..47af277 100644 --- a/src/qtshit/read/readQDateTime.zig +++ b/src/qtshit/read/readQDateTime.zig @@ -11,3 +11,13 @@ pub fn readQDateTime(reader: anytype) !QDateTime { .Zone = try readByte(reader), }; } + +test "deserialize QDateTime" { + var bytes = &[_]u8{ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x02 }; + var fBS = std.io.fixedBufferStream(bytes); + + var qdatetime = try readQDateTime(fBS.reader()); + try std.testing.expect(qdatetime.JulianDay == 0x01); + try std.testing.expect(qdatetime.MillisOfDay == 0x01); + try std.testing.expect(qdatetime.Zone == 0x02); +} \ No newline at end of file diff --git a/src/qtshit/read/readQStringList.zig b/src/qtshit/read/readQStringList.zig index ff16575..2ae25b4 100644 --- a/src/qtshit/read/readQStringList.zig +++ b/src/qtshit/read/readQStringList.zig @@ -3,6 +3,7 @@ const range = @import("../utils/RangeIter.zig").range; const readUInt = @import("./readUInt.zig").readUInt; const readString = @import("./readString.zig").readString; +const freeQStringList = @import("../utils/free/freeQStringList.zig").freeQStringList; pub fn readQStringList(reader: anytype, allocator: *std.mem.Allocator) ![][]const u8 { var length = try readUInt(reader); @@ -17,3 +18,15 @@ pub fn readQStringList(reader: anytype, allocator: *std.mem.Allocator) ![][]cons return stringList; } + +test "deserialize QStringList" { + var bytes = &[_]u8{ 0, 0, 0, 2, 0, 0, 0, 6, 0, 117, 0, 119, 0, 117, 0, 0, 0, 6, 0, 111, 0, 119, 0, 111 }; + var fBS = std.io.fixedBufferStream(bytes); + + var qstringlist = try readQStringList(fBS.reader(), std.testing.allocator); + defer freeQStringList(qstringlist, std.testing.allocator); + + try std.testing.expect(qstringlist.len == 2); + try std.testing.expectEqualStrings(qstringlist[0], "uwu"); + try std.testing.expectEqualStrings(qstringlist[1], "owo"); +} diff --git a/src/qtshit/read/tests.zig b/src/qtshit/read/tests.zig new file mode 100644 index 0000000..1135ac7 --- /dev/null +++ b/src/qtshit/read/tests.zig @@ -0,0 +1,11 @@ +const usertypeTests = @import("./usertypes/tests.zig"); +const readByte = @import("./readByte.zig"); +const readInt = @import("./readInt.zig"); +const readLong = @import("./readLong.zig"); +const readQByteArray = @import("./readQByteArray.zig"); +const readQDateTime = @import("./readQDateTime.zig"); +const readQStringList = @import("./readQStringList.zig"); + +comptime { + @import("std").testing.refAllDecls(@This()); +} \ No newline at end of file diff --git a/src/qtshit/read/usertypes/tests.zig b/src/qtshit/read/usertypes/tests.zig new file mode 100644 index 0000000..585a628 --- /dev/null +++ b/src/qtshit/read/usertypes/tests.zig @@ -0,0 +1,6 @@ +const readBufferInfo = @import("./readBufferInfo.zig"); +const readMessage = @import("./readMessage.zig"); + +comptime { + @import("std").testing.refAllDecls(@This()); +} \ No newline at end of file diff --git a/src/tests.zig b/src/tests.zig index a609b2e..d91cc31 100644 --- a/src/tests.zig +++ b/src/tests.zig @@ -1,9 +1,6 @@ -const readBufferInfo = @import("qtshit/read/usertypes/readBufferInfo.zig"); -const readMessage = @import("qtshit/read/usertypes/readMessage.zig"); +const readTests = @import("qtshit/read/tests.zig"); const unicode = @import("qtshit/utils/unicode.zig"); -// import anything with tests. - comptime { @import("std").testing.refAllDecls(@This()); } \ No newline at end of file