Add some more tests.

This commit is contained in:
Kitteh 2021-06-04 15:13:02 +01:00
parent b3a83eff9c
commit 5b2cd80210
9 changed files with 92 additions and 15 deletions

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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, ""));
}

View file

@ -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);
}

View file

@ -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");
}

11
src/qtshit/read/tests.zig Normal file
View file

@ -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());
}

View file

@ -0,0 +1,6 @@
const readBufferInfo = @import("./readBufferInfo.zig");
const readMessage = @import("./readMessage.zig");
comptime {
@import("std").testing.refAllDecls(@This());
}

View file

@ -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());
}