diff --git a/src/qtshit/read/readQVariant.zig b/src/qtshit/read/readQVariant.zig index fca7604..7868827 100644 --- a/src/qtshit/read/readQVariant.zig +++ b/src/qtshit/read/readQVariant.zig @@ -2,8 +2,9 @@ const std = @import("std"); const readUInt = @import("./readUInt.zig").readUInt; const readByte = @import("./readByte.zig").readByte; const readQVariantT = @import("./readQVariantT.zig").readQVariantT; -const QVariant = @import("../types/QVariant.zig").QVariant; +const freeQVariant = @import("../utils/free/freeQVariant.zig").freeQVariant; +const QVariant = @import("../types/QVariant.zig").QVariant; pub fn readQVariant(reader: anytype, allocator: *std.mem.Allocator) !QVariant { var type_id = try readUInt(reader); @@ -11,3 +12,29 @@ pub fn readQVariant(reader: anytype, allocator: *std.mem.Allocator) !QVariant { //std.debug.print("read: readQVariant type_id={d} mysteryByte={d} \n", .{type_id, mysteryByte}); return try readQVariantT(reader, type_id, allocator); } +test "deserialize QVariant Byte" { + var bytes = &[_]u8{ 0, 0, 0, 1, 0, 255 }; + var fBS = std.io.fixedBufferStream(bytes); + + var qvariant = try readQVariant(fBS.reader(), std.testing.allocator); + defer freeQVariant(qvariant, std.testing.allocator); + try std.testing.expect(qvariant.Byte == 255); +} + +test "deserialize QVariant Int" { + var bytes = &[_]u8{ 0, 0, 0, 2, 0, 0, 0, 5, 57 }; + var fBS = std.io.fixedBufferStream(bytes); + + var qvariant = try readQVariant(fBS.reader(), std.testing.allocator); + defer freeQVariant(qvariant, std.testing.allocator); + try std.testing.expect(qvariant.Int == 1337); +} + +test "deserialize QVariant String" { + var bytes = &[_]u8{ 0, 0, 0, 10, 0, 0, 0, 0, 8, 0, 65, 0, 67, 0, 65, 0, 66 }; + var fBS = std.io.fixedBufferStream(bytes); + + var qvariant = try readQVariant(fBS.reader(), std.testing.allocator); + defer freeQVariant(qvariant, std.testing.allocator); + try std.testing.expectEqualStrings(qvariant.String, "ACAB"); +} \ No newline at end of file diff --git a/src/qtshit/read/readQVariantList.zig b/src/qtshit/read/readQVariantList.zig index 57d6880..d85449d 100644 --- a/src/qtshit/read/readQVariantList.zig +++ b/src/qtshit/read/readQVariantList.zig @@ -3,11 +3,11 @@ const range = @import("../utils/RangeIter.zig").range; const readUInt = @import("./readUInt.zig").readUInt; const readQVariant = @import("./readQVariant.zig").readQVariant; +const freeQVariantList = @import("../utils/free/freeQVariantList.zig").freeQVariantList; const QVariant = @import("../types/QVariant.zig").QVariant; - -pub fn readQVariantList(reader: anytype, allocator: *std.mem.Allocator) ![]QVariant { +pub fn readQVariantList(reader: anytype, allocator: *std.mem.Allocator) ![]QVariant { var length = try readUInt(reader); //std.debug.print("read: readQVariantList length={d}\n", .{length}); @@ -20,3 +20,15 @@ pub fn readQVariantList(reader: anytype, allocator: *std.mem.Allocator) ![]QVari return variantList; } + +test "deserialize QVariantList" { + var bytes = &[_]u8{ 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 5, 32, 0, 0, 0, 10, 0, 0, 0, 0, 12, 0, 65, 0, 119, 0, 111, 0, 111, 0, 33, 0, 126 }; + var fBS = std.io.fixedBufferStream(bytes); + + var qvariantlist = try readQVariantList(fBS.reader(), std.testing.allocator); + defer freeQVariantList(qvariantlist, std.testing.allocator); + + try std.testing.expect(qvariantlist.len == 2); + try std.testing.expect(qvariantlist[0].UInt == 1312); + try std.testing.expectEqualStrings(qvariantlist[1].String, "Awoo!~"); +} diff --git a/src/qtshit/read/readQVariantMap.zig b/src/qtshit/read/readQVariantMap.zig index 70011fa..7d7dc00 100644 --- a/src/qtshit/read/readQVariantMap.zig +++ b/src/qtshit/read/readQVariantMap.zig @@ -4,16 +4,15 @@ const range = @import("../utils/RangeIter.zig").range; const readUInt = @import("./readUInt.zig").readUInt; const readString = @import("./readString.zig").readString; const readQVariant = @import("./readQVariant.zig").readQVariant; +const freeQVariantMap = @import("../utils/free/freeQVariantMap.zig").freeQVariantMap; const QVariant = @import("../types/QVariant.zig").QVariant; - pub fn readQVariantMap(reader: anytype, allocator: *std.mem.Allocator) !std.StringHashMap(QVariant) { var map = std.StringHashMap(QVariant).init(allocator); var numItems = try readUInt(reader); //std.debug.print("read: readQVariantMap length={d} \n", .{numItems}); - var iter = range(u32, 0, numItems); while (iter.next()) |i| { var key = try readString(reader, allocator); @@ -23,3 +22,13 @@ pub fn readQVariantMap(reader: anytype, allocator: *std.mem.Allocator) !std.Stri return map; } + +test "deserialize QVariantMap" { + var bytes = &[_]u8{ 0, 0, 0, 2, 0, 0, 0, 8, 0, 65, 0, 67, 0, 65, 0, 66, 0, 0, 0, 3, 0, 0, 0, 5, 32, 0, 0, 0, 6, 0, 111, 0, 119, 0, 111, 0, 0, 0, 1, 0, 12 }; + var fBS = std.io.fixedBufferStream(bytes); + var qvariantmap = try readQVariantMap(fBS.reader(), std.testing.allocator); + defer freeQVariantMap(qvariantmap, std.testing.allocator); + + try std.testing.expect(qvariantmap.get("ACAB").?.UInt == 1312); + try std.testing.expect(qvariantmap.get("owo").?.Byte == 12); +} diff --git a/src/qtshit/read/readShort.zig b/src/qtshit/read/readShort.zig index b078b85..beff4a3 100644 --- a/src/qtshit/read/readShort.zig +++ b/src/qtshit/read/readShort.zig @@ -4,3 +4,12 @@ pub fn readShort(reader: anytype) !u16 { //std.debug.print("read: readShort\n", .{}); return try reader.readIntBig(u16); } + +test "deserialize short" { + var bytes = &[_]u8{0x13, 0x12}; + var fBS = std.io.fixedBufferStream(bytes); + + var short = try readShort(fBS.reader()); + + try std.testing.expect(short == 0x1312); +} \ No newline at end of file diff --git a/src/qtshit/read/readSignedByte.zig b/src/qtshit/read/readSignedByte.zig index 02e6faa..628b67b 100644 --- a/src/qtshit/read/readSignedByte.zig +++ b/src/qtshit/read/readSignedByte.zig @@ -4,3 +4,11 @@ pub fn readSignedByte(reader: anytype) !i8 { //std.debug.print("read: readSignedByte\n", .{}); return try reader.readIntBig(i8); } + +test "deserialize signed byte" { + var bytes = &[_]u8{0xBB}; + var fBS = std.io.fixedBufferStream(bytes); + + var signedByte = try readSignedByte(fBS.reader()); + try std.testing.expect(signedByte == -69); +} \ No newline at end of file diff --git a/src/qtshit/read/readString.zig b/src/qtshit/read/readString.zig index 83a2c50..f312f64 100644 --- a/src/qtshit/read/readString.zig +++ b/src/qtshit/read/readString.zig @@ -2,6 +2,7 @@ const std = @import("std"); const readInt = @import("./readInt.zig").readInt; const range = @import("../utils/RangeIter.zig").range; const unicode = @import("../utils/unicode.zig"); +const freeString = @import("../utils/free/freeString.zig").freeString; pub const ReadStringErrors = (error{ExpectedSecondSurrogateHalf} || error{UnexpectedSecondSurrogateHalf} || error{DanglingSurrogateHalf} || error{OutOfMemory}); @@ -23,3 +24,14 @@ pub fn readString(reader: anytype, allocator: *std.mem.Allocator) (@TypeOf(reade return utf8; } + +test "deserialize String" { + var bytes = &[_]u8{ 0, 0, 0, 6, 0, 111, 0, 119, 0, 111, }; + var fBS = std.io.fixedBufferStream(bytes); + + var str = try readString(fBS.reader(), std.testing.allocator); + defer freeString(str, std.testing.allocator); + + try std.testing.expectEqualStrings("owo", str); +} + diff --git a/src/qtshit/read/readUInt.zig b/src/qtshit/read/readUInt.zig index 8327518..ea03edf 100644 --- a/src/qtshit/read/readUInt.zig +++ b/src/qtshit/read/readUInt.zig @@ -4,3 +4,11 @@ pub fn readUInt(reader: anytype) !u32 { //std.debug.print("read: readUInt\n", .{}); return try reader.readIntBig(u32); } + +test "deserialize String" { + var bytes = &[_]u8{ 0x00, 0x7A, 0x31, 0xC7 }; + var fBS = std.io.fixedBufferStream(bytes); + + var uint = try readUInt(fBS.reader()); + try std.testing.expect(uint == 8008135); +} diff --git a/src/qtshit/read/tests.zig b/src/qtshit/read/tests.zig index 1135ac7..c76ea7f 100644 --- a/src/qtshit/read/tests.zig +++ b/src/qtshit/read/tests.zig @@ -5,6 +5,11 @@ const readLong = @import("./readLong.zig"); const readQByteArray = @import("./readQByteArray.zig"); const readQDateTime = @import("./readQDateTime.zig"); const readQStringList = @import("./readQStringList.zig"); +const readQVariant = @import("./readQVariant.zig"); +const readQVariantList = @import("./readQVariantList.zig"); +const readQVariantMap = @import("./readQVariantMap.zig"); +const readShort = @import("./readShort.zig"); +const readSignedByte = @import("./readSignedByte.zig"); comptime { @import("std").testing.refAllDecls(@This());