Add some more read tests.

This commit is contained in:
Kitteh 2021-06-04 17:22:11 +01:00
parent 5b2cd80210
commit 1ec3cfb28f
8 changed files with 95 additions and 5 deletions

View file

@ -2,8 +2,9 @@ const std = @import("std");
const readUInt = @import("./readUInt.zig").readUInt; const readUInt = @import("./readUInt.zig").readUInt;
const readByte = @import("./readByte.zig").readByte; const readByte = @import("./readByte.zig").readByte;
const readQVariantT = @import("./readQVariantT.zig").readQVariantT; 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 { pub fn readQVariant(reader: anytype, allocator: *std.mem.Allocator) !QVariant {
var type_id = try readUInt(reader); 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}); //std.debug.print("read: readQVariant type_id={d} mysteryByte={d} \n", .{type_id, mysteryByte});
return try readQVariantT(reader, type_id, allocator); 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");
}

View file

@ -3,11 +3,11 @@ const range = @import("../utils/RangeIter.zig").range;
const readUInt = @import("./readUInt.zig").readUInt; const readUInt = @import("./readUInt.zig").readUInt;
const readQVariant = @import("./readQVariant.zig").readQVariant; const readQVariant = @import("./readQVariant.zig").readQVariant;
const freeQVariantList = @import("../utils/free/freeQVariantList.zig").freeQVariantList;
const QVariant = @import("../types/QVariant.zig").QVariant; 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); var length = try readUInt(reader);
//std.debug.print("read: readQVariantList length={d}\n", .{length}); //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; 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!~");
}

View file

@ -4,16 +4,15 @@ const range = @import("../utils/RangeIter.zig").range;
const readUInt = @import("./readUInt.zig").readUInt; const readUInt = @import("./readUInt.zig").readUInt;
const readString = @import("./readString.zig").readString; const readString = @import("./readString.zig").readString;
const readQVariant = @import("./readQVariant.zig").readQVariant; const readQVariant = @import("./readQVariant.zig").readQVariant;
const freeQVariantMap = @import("../utils/free/freeQVariantMap.zig").freeQVariantMap;
const QVariant = @import("../types/QVariant.zig").QVariant; const QVariant = @import("../types/QVariant.zig").QVariant;
pub fn readQVariantMap(reader: anytype, allocator: *std.mem.Allocator) !std.StringHashMap(QVariant) { pub fn readQVariantMap(reader: anytype, allocator: *std.mem.Allocator) !std.StringHashMap(QVariant) {
var map = std.StringHashMap(QVariant).init(allocator); var map = std.StringHashMap(QVariant).init(allocator);
var numItems = try readUInt(reader); var numItems = try readUInt(reader);
//std.debug.print("read: readQVariantMap length={d} \n", .{numItems}); //std.debug.print("read: readQVariantMap length={d} \n", .{numItems});
var iter = range(u32, 0, numItems); var iter = range(u32, 0, numItems);
while (iter.next()) |i| { while (iter.next()) |i| {
var key = try readString(reader, allocator); var key = try readString(reader, allocator);
@ -23,3 +22,13 @@ pub fn readQVariantMap(reader: anytype, allocator: *std.mem.Allocator) !std.Stri
return map; 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);
}

View file

@ -4,3 +4,12 @@ pub fn readShort(reader: anytype) !u16 {
//std.debug.print("read: readShort\n", .{}); //std.debug.print("read: readShort\n", .{});
return try reader.readIntBig(u16); 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);
}

View file

@ -4,3 +4,11 @@ pub fn readSignedByte(reader: anytype) !i8 {
//std.debug.print("read: readSignedByte\n", .{}); //std.debug.print("read: readSignedByte\n", .{});
return try reader.readIntBig(i8); 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);
}

View file

@ -2,6 +2,7 @@ const std = @import("std");
const readInt = @import("./readInt.zig").readInt; const readInt = @import("./readInt.zig").readInt;
const range = @import("../utils/RangeIter.zig").range; const range = @import("../utils/RangeIter.zig").range;
const unicode = @import("../utils/unicode.zig"); 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}); 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; 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);
}

View file

@ -4,3 +4,11 @@ pub fn readUInt(reader: anytype) !u32 {
//std.debug.print("read: readUInt\n", .{}); //std.debug.print("read: readUInt\n", .{});
return try reader.readIntBig(u32); 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);
}

View file

@ -5,6 +5,11 @@ const readLong = @import("./readLong.zig");
const readQByteArray = @import("./readQByteArray.zig"); const readQByteArray = @import("./readQByteArray.zig");
const readQDateTime = @import("./readQDateTime.zig"); const readQDateTime = @import("./readQDateTime.zig");
const readQStringList = @import("./readQStringList.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 { comptime {
@import("std").testing.refAllDecls(@This()); @import("std").testing.refAllDecls(@This());