From b3a83eff9ce66f1a23e496960b63b4862fac1b71 Mon Sep 17 00:00:00 2001 From: Kitteh Date: Fri, 4 Jun 2021 14:14:19 +0100 Subject: [PATCH] Add some tests. --- src/client.zig | 3 +- src/qtshit/read/usertypes/readBufferInfo.zig | 16 +++++ src/qtshit/read/usertypes/readMessage.zig | 31 +++++++++- src/qtshit/types/UserType.zig | 2 +- src/qtshit/utils/free/freeBufferInfo.zig | 7 +++ src/qtshit/utils/free/freeMessage.zig | 13 ++++ src/qtshit/utils/free/freeQByteArray.zig | 5 ++ src/qtshit/utils/free/freeQStringList.zig | 9 +++ src/qtshit/utils/free/freeQVariant.zig | 37 +++++++++++ src/qtshit/utils/free/freeQVariantList.zig | 10 +++ src/qtshit/utils/free/freeQVariantMap.zig | 15 +++++ src/qtshit/utils/free/freeString.zig | 5 ++ src/qtshit/utils/free/freeUserType.zig | 22 +++++++ src/qtshit/utils/freeQVariant.zig | 64 -------------------- src/tests.zig | 9 +++ 15 files changed, 181 insertions(+), 67 deletions(-) create mode 100644 src/qtshit/utils/free/freeBufferInfo.zig create mode 100644 src/qtshit/utils/free/freeMessage.zig create mode 100644 src/qtshit/utils/free/freeQByteArray.zig create mode 100644 src/qtshit/utils/free/freeQStringList.zig create mode 100644 src/qtshit/utils/free/freeQVariant.zig create mode 100644 src/qtshit/utils/free/freeQVariantList.zig create mode 100644 src/qtshit/utils/free/freeQVariantMap.zig create mode 100644 src/qtshit/utils/free/freeString.zig create mode 100644 src/qtshit/utils/free/freeUserType.zig delete mode 100644 src/qtshit/utils/freeQVariant.zig create mode 100644 src/tests.zig diff --git a/src/client.zig b/src/client.zig index 2b0860a..da66b40 100644 --- a/src/client.zig +++ b/src/client.zig @@ -3,7 +3,7 @@ const read = @import("./qtshit/read.zig"); const write = @import("./qtshit/write.zig"); const QVariantType = @import("./qtshit/types/QVariant.zig").QVariant; const prettyPrintQVariant = @import("./qtshit/utils/prettyPrintQVariant.zig").prettyPrintQVariant; -const freeQVariant = @import("./qtshit/utils/freeQVariant.zig").freeQVariant; +const freeQVariant = @import("./qtshit/utils/free/freeQVariant.zig").freeQVariant; const QVariantMapToQVariantList = @import("./qtshit/utils/QVariantMapToQVariantList.zig").QVariantMapToQVariantList; @@ -52,6 +52,7 @@ pub const Client = struct { var featureList = std.ArrayList([]const u8).init(s.allocator); defer featureList.deinit(); try featureList.append("LongTime"); + try featureList.append("LongMessageID"); try featureList.append("SenderPrefixes"); try featureList.append("RichMessages"); diff --git a/src/qtshit/read/usertypes/readBufferInfo.zig b/src/qtshit/read/usertypes/readBufferInfo.zig index 1f5ac09..ab2c3b7 100644 --- a/src/qtshit/read/usertypes/readBufferInfo.zig +++ b/src/qtshit/read/usertypes/readBufferInfo.zig @@ -4,6 +4,7 @@ const readInt = @import("../readInt.zig").readInt; const readShort = @import("../readShort.zig").readShort; const readByte = @import("../readByte.zig").readByte; const readQByteArray = @import("../readQByteArray.zig").readQByteArray; +const freeBufferInfo = @import("../../utils/free/freeBufferInfo.zig").freeBufferInfo; pub fn readBufferInfo(reader: anytype, allocator: *std.mem.Allocator) !BufferInfo { var id = try readInt(reader); @@ -23,3 +24,18 @@ pub fn readBufferInfo(reader: anytype, allocator: *std.mem.Allocator) !BufferInf .Name = name, }; } + +test "deserialize BufferInfo" { + var bytes = &[_]u8{ + 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 5, 35, 116, 101, 115, 116, + }; + var fBS = std.io.fixedBufferStream(bytes); + + var bufferInfo = try readBufferInfo(fBS.reader(), std.testing.allocator); + defer freeBufferInfo(bufferInfo, std.testing.allocator); + + try std.testing.expect(bufferInfo.ID == 1); + try std.testing.expect(bufferInfo.NetworkID == 1); + try std.testing.expect(bufferInfo.Type == 2); + try std.testing.expectEqualStrings("#test", bufferInfo.Name); +} \ No newline at end of file diff --git a/src/qtshit/read/usertypes/readMessage.zig b/src/qtshit/read/usertypes/readMessage.zig index 5083a6d..9caeb19 100644 --- a/src/qtshit/read/usertypes/readMessage.zig +++ b/src/qtshit/read/usertypes/readMessage.zig @@ -5,10 +5,11 @@ const readLong = @import("../readLong.zig").readLong; const readByte = @import("../readByte.zig").readByte; const readBufferInfo = @import("./readBufferInfo.zig").readBufferInfo; const readQByteArray = @import("../readQByteArray.zig").readQByteArray; +const freeMessage = @import("../../utils/free/freeMessage.zig").freeMessage; pub fn readMessage(reader: anytype, allocator: *std.mem.Allocator) !Message { return Message{ - .MessageID = try readInt(reader), + .MessageID = try readLong(reader), .Timestamp = try readLong(reader), .Type = try readInt(reader), .Flags = try readByte(reader), @@ -20,3 +21,31 @@ pub fn readMessage(reader: anytype, allocator: *std.mem.Allocator) !Message { .Content = try readQByteArray(reader, allocator), }; } + +test "deserialize Message" { + var bytes = &[_]u8{ + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 95, 244, 79, 69, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, + 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 5, 35, 116, 101, 115, 116, 0, 0, 0, 4, 116, 101, 115, 116, 0, 0, + 0, 6, 98, 108, 97, 98, 108, 97, 0, 0, 0, 9, 116, 101, 115, 116, 32, 117, 115, 101, 114, 0, 0, 0, 28, 104, 116, + 116, 112, 115, 58, 47, 47, 106, 102, 107, 97, 108, 115, 100, 107, 106, 102, 106, 46, 99, 111, 109, 47, 107, 106, 107, 106, 0, + 0, 0, 22, 116, 104, 105, 115, 32, 105, 115, 32, 97, 32, 116, 101, 115, 116, 32, 109, 101, 115, 115, 97, 103, 101, + }; + var fBS = std.io.fixedBufferStream(bytes); + + var message = try readMessage(fBS.reader(), std.testing.allocator); + defer freeMessage(message, std.testing.allocator); + + try std.testing.expect(message.MessageID == 1); + try std.testing.expect(message.Timestamp == 1609846597); + try std.testing.expect(message.Type == 1); + try std.testing.expect(message.Flags == 0); + try std.testing.expect(message.Buffer.ID == 1); + try std.testing.expect(message.Buffer.NetworkID == 1); + try std.testing.expect(message.Buffer.Type == 2); + try std.testing.expectEqualStrings("#test", message.Buffer.Name); + try std.testing.expectEqualStrings("test", message.Sender); + try std.testing.expectEqualStrings("this is a test message", message.Content); + try std.testing.expectEqualStrings("blabla", message.SenderPrefixes); + try std.testing.expectEqualStrings("test user", message.RealName); + try std.testing.expectEqualStrings("https://jfkalsdkjfj.com/kjkj", message.AvatarUrl); +} diff --git a/src/qtshit/types/UserType.zig b/src/qtshit/types/UserType.zig index e9b54d2..2754f65 100644 --- a/src/qtshit/types/UserType.zig +++ b/src/qtshit/types/UserType.zig @@ -24,7 +24,7 @@ pub const BufferInfo = struct { }; pub const Message = struct { - MessageID: i32, + MessageID: i64, Timestamp: i64, Type: i32, Flags: u8, diff --git a/src/qtshit/utils/free/freeBufferInfo.zig b/src/qtshit/utils/free/freeBufferInfo.zig new file mode 100644 index 0000000..83348d1 --- /dev/null +++ b/src/qtshit/utils/free/freeBufferInfo.zig @@ -0,0 +1,7 @@ +const std = @import("std"); +const BufferInfo = @import("../../types/UserType.zig").BufferInfo; +const freeString = @import("./freeString.zig").freeString; + +pub fn freeBufferInfo(bufferInfo: BufferInfo, allocator: *std.mem.Allocator) void { + freeString(bufferInfo.Name, allocator); +} diff --git a/src/qtshit/utils/free/freeMessage.zig b/src/qtshit/utils/free/freeMessage.zig new file mode 100644 index 0000000..e7f5573 --- /dev/null +++ b/src/qtshit/utils/free/freeMessage.zig @@ -0,0 +1,13 @@ +const std = @import("std"); +const Message = @import("../../types/UserType.zig").Message; +const freeQByteArray = @import("./freeQByteArray.zig").freeQByteArray; +const freeBufferInfo = @import("./freeBufferInfo.zig").freeBufferInfo; + +pub fn freeMessage(message: Message, allocator: *std.mem.Allocator) void { + freeBufferInfo(message.Buffer, allocator); + freeQByteArray(message.SenderPrefixes, allocator); + freeQByteArray(message.Sender, allocator); + freeQByteArray(message.RealName, allocator); + freeQByteArray(message.AvatarUrl, allocator); + freeQByteArray(message.Content, allocator); +} diff --git a/src/qtshit/utils/free/freeQByteArray.zig b/src/qtshit/utils/free/freeQByteArray.zig new file mode 100644 index 0000000..b596878 --- /dev/null +++ b/src/qtshit/utils/free/freeQByteArray.zig @@ -0,0 +1,5 @@ +const std = @import("std"); + +pub fn freeQByteArray(qbytearray: []u8, allocator: *std.mem.Allocator) void { + allocator.free(qbytearray); +} diff --git a/src/qtshit/utils/free/freeQStringList.zig b/src/qtshit/utils/free/freeQStringList.zig new file mode 100644 index 0000000..235b722 --- /dev/null +++ b/src/qtshit/utils/free/freeQStringList.zig @@ -0,0 +1,9 @@ +const std = @import("std"); +const freeString = @import("./freeString.zig").freeString; + +pub fn freeQStringList(qstringlist: [][]const u8, allocator: *std.mem.Allocator) void { + for (qstringlist) |string| { + freeString(string, allocator); + } + allocator.free(qstringlist); +} diff --git a/src/qtshit/utils/free/freeQVariant.zig b/src/qtshit/utils/free/freeQVariant.zig new file mode 100644 index 0000000..51f6a35 --- /dev/null +++ b/src/qtshit/utils/free/freeQVariant.zig @@ -0,0 +1,37 @@ +const std = @import("std"); +const QVariant = @import("../../types/QVariant.zig").QVariant; +const freeQVariantMap = @import("./freeQVariantMap.zig").freeQVariantMap; +const freeQVariantList = @import("./freeQVariantList.zig").freeQVariantList; +const freeQStringList = @import("./freeQStringList.zig").freeQStringList; +const freeQByteArray = @import("./freeQByteArray.zig").freeQByteArray; +const freeString = @import("./freeString.zig").freeString; +const freeUserType = @import("./freeUserType.zig").freeUserType; + + +pub fn freeQVariant(variant: QVariant, allocator: *std.mem.Allocator) void { + switch (variant) { + .Byte, .Int, .UInt, .Short, .QDateTime => { + // Static + }, + + .QVariantMap => |value| { + freeQVariantMap(value, allocator); + }, + + .QVariantList => |value| { + freeQVariantList(value, allocator); + }, + .String => |value| { + freeString(value, allocator); + }, + .QStringList => |value| { + freeQStringList(value, allocator); + }, + .QByteArray => |value| { + freeQByteArray(value, allocator); + }, + .UserType => |value| { + freeUserType(value, allocator); + }, + } +} diff --git a/src/qtshit/utils/free/freeQVariantList.zig b/src/qtshit/utils/free/freeQVariantList.zig new file mode 100644 index 0000000..23ece4b --- /dev/null +++ b/src/qtshit/utils/free/freeQVariantList.zig @@ -0,0 +1,10 @@ +const std = @import("std"); +const QVariant = @import("../../types/QVariant.zig").QVariant; +const freeQVariant = @import("./freeQVariant.zig").freeQVariant; + +pub fn freeQVariantList(qvariantlist: []QVariant, allocator: *std.mem.Allocator) void { + for (qvariantlist) |varient, i| { + freeQVariant(varient, allocator); + } + allocator.free(qvariantlist); +} diff --git a/src/qtshit/utils/free/freeQVariantMap.zig b/src/qtshit/utils/free/freeQVariantMap.zig new file mode 100644 index 0000000..e27e122 --- /dev/null +++ b/src/qtshit/utils/free/freeQVariantMap.zig @@ -0,0 +1,15 @@ +const std = @import("std"); +const QVariant = @import("../../types/QVariant.zig").QVariant; +const freeString = @import("./freeString.zig").freeString; +const freeQVariant = @import("./freeQVariant.zig").freeQVariant; + +pub fn freeQVariantMap(qvariantmap: std.StringHashMap(QVariant), allocator: *std.mem.Allocator) void { + var qMapIter = qvariantmap.iterator(); + while (qMapIter.next()) |v| { + freeString(v.key, allocator); + freeQVariant(v.value, allocator); + } + // Compiler bug maybe? doesnt want me to drop const so have to store it in a variable + var qi = qvariantmap; + qi.deinit(); +} \ No newline at end of file diff --git a/src/qtshit/utils/free/freeString.zig b/src/qtshit/utils/free/freeString.zig new file mode 100644 index 0000000..9be6998 --- /dev/null +++ b/src/qtshit/utils/free/freeString.zig @@ -0,0 +1,5 @@ +const std = @import("std"); + +pub fn freeString(string: []const u8, allocator: *std.mem.Allocator) void { + allocator.free(string); +} diff --git a/src/qtshit/utils/free/freeUserType.zig b/src/qtshit/utils/free/freeUserType.zig new file mode 100644 index 0000000..dadce8b --- /dev/null +++ b/src/qtshit/utils/free/freeUserType.zig @@ -0,0 +1,22 @@ +const std = @import("std"); +const UserType = @import("../../types/UserType.zig").UserType; +const freeQVariantMap = @import("./freeQVariantMap.zig").freeQVariantMap; +const freeMessage = @import("./freeMessage.zig").freeMessage; +const freeBufferInfo = @import("./freeBufferInfo.zig").freeBufferInfo; + +pub fn freeUserType(usertype: UserType, allocator: *std.mem.Allocator) void { + switch (usertype) { + .BufferId, .IdentityId, .NetworkId, .MsgId, .PeerPtr => { + // Static + }, + .IrcUser, .IrcChannel, .Identity, .NetworkInfo, .NetworkServer => |value| { + freeQVariantMap(value, allocator); + }, + .BufferInfo => |value| { + freeBufferInfo(value, allocator); + }, + .Message => |value| { + freeMessage(value, allocator); + }, + } +} diff --git a/src/qtshit/utils/freeQVariant.zig b/src/qtshit/utils/freeQVariant.zig deleted file mode 100644 index 5be73b2..0000000 --- a/src/qtshit/utils/freeQVariant.zig +++ /dev/null @@ -1,64 +0,0 @@ -const std = @import("std"); -const QVariant = @import("../types/QVariant.zig").QVariant; - -pub fn freeQVariantMap(qvariantmap: std.StringHashMap(QVariant), allocator: *std.mem.Allocator) void { - var qMapIter = qvariantmap.iterator(); - while (qMapIter.next()) |v| { - allocator.free(v.key); - freeQVariant(v.value, allocator); - } - // Compiler bug maybe? doesnt want me to drop const so have to store it in a variable - var qi = qvariantmap; - qi.deinit(); -} - -pub fn freeQVariant(variant: QVariant, allocator: *std.mem.Allocator) void { - switch (variant) { - .Byte, .Int, .UInt, .Short, .QDateTime => { - // Static - }, - - .QVariantMap => |q| { - freeQVariantMap(q, allocator); - }, - - .QVariantList => |l| { - for (l) |varient, i| { - freeQVariant(varient, allocator); - } - allocator.free(l); - }, - .String => |s| { - allocator.free(s); - }, - .QStringList => |l| { - for (l) |varient, i| { - allocator.free(varient); - } - allocator.free(l); - }, - .QByteArray => |ba| { - allocator.free(ba); - }, - .UserType => |usertype| { - switch (usertype) { - .BufferId, .IdentityId, .NetworkId, .MsgId, .PeerPtr => { - // Statid - }, - .IrcUser, .IrcChannel, .Identity, .NetworkInfo, .NetworkServer => |value| { - freeQVariantMap(value, allocator); - }, - .BufferInfo => |value| { - allocator.free(value.Name); - }, - .Message => |value| { - allocator.free(value.Buffer.Name); - allocator.free(value.SenderPrefixes); - allocator.free(value.RealName); - allocator.free(value.AvatarUrl); - allocator.free(value.Content); - } - } - }, - } -} diff --git a/src/tests.zig b/src/tests.zig new file mode 100644 index 0000000..a609b2e --- /dev/null +++ b/src/tests.zig @@ -0,0 +1,9 @@ +const readBufferInfo = @import("qtshit/read/usertypes/readBufferInfo.zig"); +const readMessage = @import("qtshit/read/usertypes/readMessage.zig"); +const unicode = @import("qtshit/utils/unicode.zig"); + +// import anything with tests. + +comptime { + @import("std").testing.refAllDecls(@This()); +} \ No newline at end of file