From 4437e5b89ba180185c81309ffcdbe7819aa8842f Mon Sep 17 00:00:00 2001 From: Kitteh Date: Fri, 4 Jun 2021 19:26:07 +0100 Subject: [PATCH] Add write tests. --- src/qtshit/read/readByte.zig | 1 + src/qtshit/read/readUInt.zig | 2 +- src/qtshit/read/tests.zig | 2 ++ src/qtshit/write/tests.zig | 17 +++++++++++++++++ src/qtshit/write/writeByte.zig | 11 +++++++++++ src/qtshit/write/writeInt.zig | 11 +++++++++++ src/qtshit/write/writeQByteArray.zig | 12 +++++++++++- src/qtshit/write/writeQStringList.zig | 15 +++++++++++++++ src/qtshit/write/writeQVariant.zig | 18 ++++++++++++++++++ src/qtshit/write/writeQVariantHeader.zig | 10 ++++++++++ src/qtshit/write/writeQVariantList.zig | 19 +++++++++++++++++++ src/qtshit/write/writeQVariantMap.zig | 15 +++++++++++++++ src/qtshit/write/writeShort.zig | 11 +++++++++++ src/qtshit/write/writeSignedByte.zig | 13 ++++++++++++- src/qtshit/write/writeString.zig | 20 ++++++++++++++++++++ src/qtshit/write/writeUInt.zig | 11 +++++++++++ src/tests.zig | 1 + 17 files changed, 186 insertions(+), 3 deletions(-) create mode 100644 src/qtshit/write/tests.zig diff --git a/src/qtshit/read/readByte.zig b/src/qtshit/read/readByte.zig index 4946ec4..19094fb 100644 --- a/src/qtshit/read/readByte.zig +++ b/src/qtshit/read/readByte.zig @@ -1,4 +1,5 @@ const std = @import("std"); + pub fn readByte(reader: anytype) !u8 { //std.debug.print("read: readByte\n", .{}); return try reader.readByte(); diff --git a/src/qtshit/read/readUInt.zig b/src/qtshit/read/readUInt.zig index ea03edf..a76d3d8 100644 --- a/src/qtshit/read/readUInt.zig +++ b/src/qtshit/read/readUInt.zig @@ -5,7 +5,7 @@ pub fn readUInt(reader: anytype) !u32 { return try reader.readIntBig(u32); } -test "deserialize String" { +test "deserialize UInt" { var bytes = &[_]u8{ 0x00, 0x7A, 0x31, 0xC7 }; var fBS = std.io.fixedBufferStream(bytes); diff --git a/src/qtshit/read/tests.zig b/src/qtshit/read/tests.zig index bdd9e77..903fc97 100644 --- a/src/qtshit/read/tests.zig +++ b/src/qtshit/read/tests.zig @@ -10,6 +10,8 @@ const readQVariantList = @import("./readQVariantList.zig"); const readQVariantMap = @import("./readQVariantMap.zig"); const readShort = @import("./readShort.zig"); const readSignedByte = @import("./readSignedByte.zig"); +const readString = @import("./readString.zig"); +const readUInt = @import("./readUInt.zig"); comptime { @import("std").testing.refAllDecls(@This()); diff --git a/src/qtshit/write/tests.zig b/src/qtshit/write/tests.zig new file mode 100644 index 0000000..37645d5 --- /dev/null +++ b/src/qtshit/write/tests.zig @@ -0,0 +1,17 @@ +const writeByte = @import("./writeByte.zig"); +const writeInt = @import("./writeInt.zig"); +//const writeLong = @import("./writeLong.zig"); +const writeQByteArray = @import("./writeQByteArray.zig"); +//const writeQDateTime = @import("./writeQDateTime.zig"); +const writeQStringList = @import("./writeQStringList.zig"); +const writeQVariant = @import("./writeQVariant.zig"); +const writeQVariantList = @import("./writeQVariantList.zig"); +const writeQVariantMap = @import("./writeQVariantMap.zig"); +const writeShort = @import("./writeShort.zig"); +const writeSignedByte = @import("./writeSignedByte.zig"); +const writeString = @import("./writeString.zig"); +const writeUInt = @import("./writeUInt.zig"); + +comptime { + @import("std").testing.refAllDecls(@This()); +} diff --git a/src/qtshit/write/writeByte.zig b/src/qtshit/write/writeByte.zig index 740008e..043063d 100644 --- a/src/qtshit/write/writeByte.zig +++ b/src/qtshit/write/writeByte.zig @@ -1,3 +1,14 @@ +const std = @import("std"); + pub fn writeByte(writer: anytype, byte: u8) !void { try writer.writeByte(byte); } + +test "serialize byte" { + var byteList = std.ArrayList(u8).init(std.testing.allocator); + defer byteList.deinit(); + + try writeByte(byteList.writer(), 0x01); + + try std.testing.expect(std.mem.eql(u8, byteList.items, &[_]u8{0x01})); +} \ No newline at end of file diff --git a/src/qtshit/write/writeInt.zig b/src/qtshit/write/writeInt.zig index 0dad30b..d9c2358 100644 --- a/src/qtshit/write/writeInt.zig +++ b/src/qtshit/write/writeInt.zig @@ -1,3 +1,14 @@ +const std = @import("std"); + pub fn writeInt(writer: anytype, number: i32) !void { try writer.writeIntBig(i32, number); } + +test "serialize int" { + var byteList = std.ArrayList(u8).init(std.testing.allocator); + defer byteList.deinit(); + + try writeInt(byteList.writer(), -1312); + + try std.testing.expect(std.mem.eql(u8, byteList.items, &[_]u8{ 0xFF, 0xFF, 0xFA, 0xE0 })); +} diff --git a/src/qtshit/write/writeQByteArray.zig b/src/qtshit/write/writeQByteArray.zig index a75764f..494b4f6 100644 --- a/src/qtshit/write/writeQByteArray.zig +++ b/src/qtshit/write/writeQByteArray.zig @@ -1,6 +1,16 @@ +const std = @import("std"); const writeUInt = @import("./writeUInt.zig").writeUInt; -pub fn writeQByteArray(writer: anytype, array: []u8) !void { +pub fn writeQByteArray(writer: anytype, array: []const u8) !void { try writeUInt(writer, @intCast(u32, array.len)); try writer.writeAll(array); } + +test "serialize QByteArray" { + var byteList = std.ArrayList(u8).init(std.testing.allocator); + defer byteList.deinit(); + + try writeQByteArray(byteList.writer(), &[_]u8{ 11, 22, 33, 44 }); + + try std.testing.expect(std.mem.eql(u8, byteList.items, &[_]u8{ 00, 00, 00, 04, 11, 22, 33, 44 })); +} \ No newline at end of file diff --git a/src/qtshit/write/writeQStringList.zig b/src/qtshit/write/writeQStringList.zig index b28ebcc..57aa449 100644 --- a/src/qtshit/write/writeQStringList.zig +++ b/src/qtshit/write/writeQStringList.zig @@ -8,3 +8,18 @@ pub fn writeQStringList(writer: anytype, allocator: *std.mem.Allocator, strList: try writeString(writer, allocator, string); } } + +test "serialize QStringList" { + var byteList = std.ArrayList(u8).init(std.testing.allocator); + defer byteList.deinit(); + + var stringList = std.ArrayList([]const u8).init(std.testing.allocator); + defer stringList.deinit(); + + try stringList.append("uwu"); + try stringList.append("owo"); + + try writeQStringList(byteList.writer(), std.testing.allocator, stringList.items); + + try std.testing.expect(std.mem.eql(u8, byteList.items, &[_]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 })); +} \ No newline at end of file diff --git a/src/qtshit/write/writeQVariant.zig b/src/qtshit/write/writeQVariant.zig index cf7154b..77a627d 100644 --- a/src/qtshit/write/writeQVariant.zig +++ b/src/qtshit/write/writeQVariant.zig @@ -55,3 +55,21 @@ pub fn writeQVariant(writer: anytype, allocator: *std.mem.Allocator, variant: QV //}, } } + +test "serialize QVariant Int" { + var byteList = std.ArrayList(u8).init(std.testing.allocator); + defer byteList.deinit(); + + try writeQVariant(byteList.writer(), std.testing.allocator, .{ .Int = 1337 }); + + try std.testing.expect(std.mem.eql(u8, byteList.items, &[_]u8{ 0, 0, 0, 2, 0, 0, 0, 5, 57 })); +} + +test "serialize QVariant String" { + var byteList = std.ArrayList(u8).init(std.testing.allocator); + defer byteList.deinit(); + + try writeQVariant(byteList.writer(), std.testing.allocator, .{ .String = "ACAB" }); + + try std.testing.expect(std.mem.eql(u8, byteList.items, &[_]u8{ 0, 0, 0, 10, 0, 0, 0, 0, 8, 0, 65, 0, 67, 0, 65, 0, 66 })); +} \ No newline at end of file diff --git a/src/qtshit/write/writeQVariantHeader.zig b/src/qtshit/write/writeQVariantHeader.zig index 1856a28..d58229b 100644 --- a/src/qtshit/write/writeQVariantHeader.zig +++ b/src/qtshit/write/writeQVariantHeader.zig @@ -1,3 +1,5 @@ +const std = @import("std"); + const writeUInt = @import("./writeUInt.zig").writeUInt; const writeByte = @import("./writeByte.zig").writeByte; @@ -5,3 +7,11 @@ pub fn writeQVariantHeader(writer: anytype, type_id: u32) !void { try writeUInt(writer, type_id); try writeByte(writer, 0); } +test "serialize QVariantHeader" { + var byteList = std.ArrayList(u8).init(std.testing.allocator); + defer byteList.deinit(); + + try writeQVariantHeader(byteList.writer(), 1); + + try std.testing.expect(std.mem.eql(u8, byteList.items, &[_]u8{ 0, 0, 0, 1, 0 })); +} diff --git a/src/qtshit/write/writeQVariantList.zig b/src/qtshit/write/writeQVariantList.zig index 8f24340..6571c9c 100644 --- a/src/qtshit/write/writeQVariantList.zig +++ b/src/qtshit/write/writeQVariantList.zig @@ -9,3 +9,22 @@ pub fn writeQVariantList(writer: anytype, allocator: *std.mem.Allocator, varList try writeQVariant(writer, allocator, v); } } + +test "serialize QVariantList" { + var byteList = std.ArrayList(u8).init(std.testing.allocator); + defer byteList.deinit(); + + var variantList = std.ArrayList(QVariantType).init(std.testing.allocator); + defer variantList.deinit(); + + try variantList.append(.{ + .UInt = 1312, + }); + try variantList.append(.{ + .String = "Awoo!~", + }); + + try writeQVariantList(byteList.writer(), std.testing.allocator, variantList.items); + + try std.testing.expect(std.mem.eql(u8, byteList.items, &[_]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 })); +} diff --git a/src/qtshit/write/writeQVariantMap.zig b/src/qtshit/write/writeQVariantMap.zig index a04f6c0..0f2e8c2 100644 --- a/src/qtshit/write/writeQVariantMap.zig +++ b/src/qtshit/write/writeQVariantMap.zig @@ -19,3 +19,18 @@ pub fn writeQVariantMap(writer: anytype, allocator: *std.mem.Allocator, map: std return; } + +test "serialize QVariantMap" { + var byteList = std.ArrayList(u8).init(std.testing.allocator); + defer byteList.deinit(); + + var map = std.StringHashMap(QVariantType).init(std.testing.allocator); + defer map.deinit(); + + try map.put("ACAB", .{ .UInt = 1312 }); + try map.put("owo", .{ .Byte = 12 }); + + try writeQVariantMap(byteList.writer(), std.testing.allocator, map); + + try std.testing.expect(std.mem.eql(u8, byteList.items, &[_]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 })); +} diff --git a/src/qtshit/write/writeShort.zig b/src/qtshit/write/writeShort.zig index c360a2f..7e8dbf2 100644 --- a/src/qtshit/write/writeShort.zig +++ b/src/qtshit/write/writeShort.zig @@ -1,3 +1,14 @@ +const std = @import("std"); + pub fn writeShort(writer: anytype, number: u16) !void { try writer.writeIntBig(u16, number); } + +test "serialize Short" { + var byteList = std.ArrayList(u8).init(std.testing.allocator); + defer byteList.deinit(); + + try writeShort(byteList.writer(), 0x1312); + + try std.testing.expect(std.mem.eql(u8, byteList.items, &[_]u8{ 0x13, 0x12 })); +} diff --git a/src/qtshit/write/writeSignedByte.zig b/src/qtshit/write/writeSignedByte.zig index bbd1cef..cfb5df6 100644 --- a/src/qtshit/write/writeSignedByte.zig +++ b/src/qtshit/write/writeSignedByte.zig @@ -1,3 +1,14 @@ +const std = @import("std"); + pub fn writeSignedByte(writer: anytype, byte: i8) !void { - try writer.writeIntBig(i8, number); + try writer.writeIntBig(i8, byte); +} + +test "serialize Signed Byte" { + var byteList = std.ArrayList(u8).init(std.testing.allocator); + defer byteList.deinit(); + + try writeSignedByte(byteList.writer(), -69); + + try std.testing.expect(std.mem.eql(u8, byteList.items, &[_]u8{ 0xBB })); } diff --git a/src/qtshit/write/writeString.zig b/src/qtshit/write/writeString.zig index 84875ad..301b949 100644 --- a/src/qtshit/write/writeString.zig +++ b/src/qtshit/write/writeString.zig @@ -12,3 +12,23 @@ pub fn writeString(writer: anytype, allocator: *std.mem.Allocator, str: []const try writeInt(writer, @intCast(i32, str_utf16BE.len * 2)); try writer.writeAll(std.mem.sliceAsBytes(str_utf16BE)); } + +test "serialize String" { + var byteList = std.ArrayList(u8).init(std.testing.allocator); + defer byteList.deinit(); + + try writeString(byteList.writer(), std.testing.allocator, "owo"); + + try std.testing.expect(std.mem.eql(u8, byteList.items, &[_]u8{ + 0, + 0, + 0, + 6, + 0, + 111, + 0, + 119, + 0, + 111, + })); +} diff --git a/src/qtshit/write/writeUInt.zig b/src/qtshit/write/writeUInt.zig index 6d40257..acd411c 100644 --- a/src/qtshit/write/writeUInt.zig +++ b/src/qtshit/write/writeUInt.zig @@ -1,3 +1,14 @@ +const std = @import("std"); + pub fn writeUInt(writer: anytype, number: u32) !void { try writer.writeIntBig(u32, number); } + +test "serialize UInt" { + var byteList = std.ArrayList(u8).init(std.testing.allocator); + defer byteList.deinit(); + + try writeUInt(byteList.writer(), 8008135); + + try std.testing.expect(std.mem.eql(u8, byteList.items, &[_]u8{ 0x00, 0x7A, 0x31, 0xC7 })); +} diff --git a/src/tests.zig b/src/tests.zig index d585522..211dfde 100644 --- a/src/tests.zig +++ b/src/tests.zig @@ -1,4 +1,5 @@ const readTests = @import("qtshit/read/tests.zig"); +const writeTests = @import("qtshit/write/tests.zig"); const unicode = @import("qtshit/utils/unicode.zig"); comptime {