Add write tests.

This commit is contained in:
Kitteh 2021-06-04 19:26:07 +01:00
parent 67c9353455
commit 4437e5b89b
17 changed files with 186 additions and 3 deletions

View file

@ -1,4 +1,5 @@
const std = @import("std");
pub fn readByte(reader: anytype) !u8 {
//std.debug.print("read: readByte\n", .{});
return try reader.readByte();

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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 {