Work on mem leaks in tests.

This commit is contained in:
Kitteh 2021-06-01 13:00:29 +01:00
parent 7d84fda25d
commit 7fca728668
4 changed files with 72 additions and 17 deletions

View file

@ -1,9 +1,19 @@
const std = @import("std");
const QVariant = union {
pub const QVariant = union {
int: u32,
short: u16,
byte: u8,
string: []u8
stringlist: std.ArrayList([]const u8)
string: []u8,
stringlist: std.ArrayList([]const u8),
empty: bool,
};
pub const QVariantTypes = enum(u32) {
byte = 1,
// IDK why there is 2???
int_1 = 2,
int_2 = 3,
string = 10,
};

View file

@ -1,5 +1,5 @@
const std = @import("std");
const QVariant = @import("./qvariant.zig").QVariant;
const qvar = @import("./qvariant.zig");
pub fn get_int(reader: anytype) !u32 {
return try reader.readIntBig(u32);
@ -41,17 +41,26 @@ pub fn get_qvariant(reader: anytype) !u32 {
return type_id;
}
pub fn get_variant_t(reader: anytype, type_id: u32) !QVariant {
pub fn get_variant_t(reader: anytype, type_id: u32, allocator: *std.mem.Allocator) !qvar.QVariant {
switch (type_id) {
1 => {
@enumToInt(qvar.QVariantTypes.byte) => {
var byte = try get_byte(reader);
return QVariant{ .byte = byte };
return qvar.QVariant{ .byte = byte };
},
@enumToInt(qvar.QVariantTypes.int_1), @enumToInt(qvar.QVariantTypes.int_2) => {
var int = try get_int(reader);
return qvar.QVariant{ .int = int };
},
@enumToInt(qvar.QVariantTypes.string) => {
var str = try get_string(reader, allocator);
return qvar.QVariant{ .string = str };
},
else => {},
}
return qvar.QVariant{ .empty = true };
}
pub fn get_variant(reader: anytype) !QVariant {
pub fn get_variant(reader: anytype, allocator: *std.mem.Allocator) !qvar.QVariant {
var type_id = try get_qvariant(reader);
return try get_variant_t(reader, type_id);
return try get_variant_t(reader, type_id, allocator);
}

View file

@ -1,11 +1,13 @@
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 qvar = @import("./qvariant.zig");
test "read/write int" {
var byteList = std.ArrayList(u8).init(std.heap.page_allocator);
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
try write.add_int(byteList.writer(), 4242);
@ -17,7 +19,7 @@ test "read/write int" {
}
test "read/write short" {
var byteList = std.ArrayList(u8).init(std.heap.page_allocator);
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
try write.add_short(byteList.writer(), 6969);
@ -29,7 +31,7 @@ test "read/write short" {
}
test "read/write byte" {
var byteList = std.ArrayList(u8).init(std.heap.page_allocator);
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
try write.add_byte(byteList.writer(), 'a');
@ -41,7 +43,7 @@ test "read/write byte" {
}
test "read/write string" {
var byteList = std.ArrayList(u8).init(std.heap.page_allocator);
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
var arr = "Hello World!".*;
@ -49,16 +51,17 @@ test "read/write string" {
try write.add_string(byteList.writer(), &arr);
var fBS = std.io.fixedBufferStream(byteList.items);
var val = try read.get_string(fBS.reader(), std.heap.page_allocator);
var val = try read.get_string(fBS.reader(), global_allocator);
defer global_allocator.free(val);
try expect(std.mem.eql(u8, val, "Hello World!"));
}
test "read/write stringlist" {
var byteList = std.ArrayList(u8).init(std.heap.page_allocator);
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
var arr = std.ArrayList([]const u8).init(std.heap.page_allocator);
var arr = std.ArrayList([]const u8).init(global_allocator);
defer arr.deinit();
try arr.append("Hewwo");
try arr.append("World");
@ -66,10 +69,36 @@ test "read/write stringlist" {
try write.add_stringlist(byteList.writer(), arr);
var fBS = std.io.fixedBufferStream(byteList.items);
var val = try read.get_stringlist(fBS.reader(), std.heap.page_allocator);
var val = try read.get_stringlist(fBS.reader(), global_allocator);
try expect(arr.items.len == val.items.len);
for (arr.items) |str, index| {
try std.testing.expectEqualStrings(str, val.items[index]);
}
}
test "read/write byte variant" {
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
try write.add_qvariant(byteList.writer(), @enumToInt(qvar.QVariantTypes.byte));
try write.add_byte(byteList.writer(), 'a');
var fBS = std.io.fixedBufferStream(byteList.items);
var val = try read.get_variant(fBS.reader(), global_allocator);
try expect(val.byte == 'a');
}
test "read/write int variant" {
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
try write.add_qvariant(byteList.writer(), @enumToInt(qvar.QVariantTypes.int_2));
try write.add_int(byteList.writer(), 1312);
var fBS = std.io.fixedBufferStream(byteList.items);
var val = try read.get_variant(fBS.reader(), global_allocator);
try expect(val.int == 1312);
}

View file

@ -23,3 +23,10 @@ pub fn add_stringlist(writer: anytype, strList: std.ArrayList([]const u8)) !void
try add_string(writer, string);
}
}
// Call this and Then write your type
pub fn add_qvariant(writer: anytype, type_id: u32) !void {
try add_int(writer, type_id);
try add_byte(writer, 0);
}