1
0
Fork 0
QuasselClient/src/qtshit/test.zig
2021-06-03 12:31:40 +01:00

148 lines
4.3 KiB
Zig

const std = @import("std");
const expect = std.testing.expect;
const global_allocator = std.testing.allocator;
const read = @import("./read.zig");
const write = @import("./write.zig");
const QVariant = @import("./types/QVariant.zig").QVariant;
const freeQVariant = @import("./utils/freeQVariant.zig").freeQVariant;
test "UInt serialization/deserialization" {
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
try write.writeUInt(byteList.writer(), 4242);
var fBS = std.io.fixedBufferStream(byteList.items);
var val = try read.readUInt(fBS.reader());
try expect(val == 4242);
}
test "Short serialization/deserialization" {
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
try write.writeShort(byteList.writer(), 6969);
var fBS = std.io.fixedBufferStream(byteList.items);
var val = try read.readShort(fBS.reader());
try expect(val == 6969);
}
test "Byte serialization/deserialization" {
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
try write.writeByte(byteList.writer(), 'a');
var fBS = std.io.fixedBufferStream(byteList.items);
var val = try read.readByte(fBS.reader());
try expect(val == 'a');
}
test "String serialization/deserialization" {
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
var arr = "Hello World!".*;
try write.writeString(byteList.writer(), global_allocator, &arr);
var fBS = std.io.fixedBufferStream(byteList.items);
var val = try read.readString(fBS.reader(), global_allocator);
defer global_allocator.free(val);
try std.testing.expectEqualStrings("Hello World!", val);
}
test "QByteArray serialization/deserialization" {
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
var arr = std.ArrayList(u8).init(global_allocator);
defer arr.deinit();
try arr.append(13);
try arr.append(12);
try write.writeQByteArray(byteList.writer(), arr.items);
var fBS = std.io.fixedBufferStream(byteList.items);
var val = try read.readQByteArray(fBS.reader(), global_allocator);
defer global_allocator.free(val);
try expect(arr.items.len == val.len);
for (arr.items) |item, index| {
try std.testing.expect(item == val[index]);
}
}
test "QStringList serialization/deserialization" {
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
var arr = std.ArrayList([]const u8).init(global_allocator);
defer arr.deinit();
try arr.append("Hewwo");
try arr.append("World");
try write.writeQStringList(byteList.writer(), global_allocator, arr.items);
var fBS = std.io.fixedBufferStream(byteList.items);
var val = try read.readQStringList(fBS.reader(), global_allocator);
defer {
for (val) |str| {
global_allocator.free(str);
}
global_allocator.free(val);
}
try expect(arr.items.len == val.len);
for (arr.items) |str, index| {
try std.testing.expectEqualStrings(str, val[index]);
}
}
test "QVariant UInt serialization/deserialization" {
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
// ACAB
try write.writeQVariant(byteList.writer(), global_allocator, .{ .UInt = 1312 });
var fBS = std.io.fixedBufferStream(byteList.items);
var variant = try read.readQVariant(fBS.reader(), global_allocator);
try expect(variant.UInt == 1312);
}
test "QVariant QVariantMap serialization/deserialization" {
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
var map = std.StringHashMap(QVariant).init(global_allocator);
defer map.deinit();
try map.put("testkey", .{ .UInt = 1337 });
try write.writeQVariant(byteList.writer(), global_allocator, .{
.QVariantMap = map,
});
var fBS = std.io.fixedBufferStream(byteList.items);
var varient = try read.readQVariant(fBS.reader(), global_allocator);
defer freeQVariant(varient, global_allocator);
var qVariantMap = varient.QVariantMap;
var testKey = qVariantMap.get("testkey");
if (testKey) |key| {
try expect(key.UInt == 1337);
} else {
try expect(!true);
}
}