From d4e971906ed143c02b42f87790278dcb3cbaebf8 Mon Sep 17 00:00:00 2001 From: Kitteh Date: Sat, 5 Jun 2021 07:11:27 +0100 Subject: [PATCH] Add test to QVariantMapToQVariantList. --- .../utils/QVariantMapToQVariantList.zig | 27 +++++++++++++++++-- src/qtshit/utils/free/freeQVariantMap.zig | 4 +-- src/qtshit/utils/tests.zig | 6 +++++ src/qtshit/write/writeQVariantMap.zig | 4 +-- src/tests.zig | 2 +- 5 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 src/qtshit/utils/tests.zig diff --git a/src/qtshit/utils/QVariantMapToQVariantList.zig b/src/qtshit/utils/QVariantMapToQVariantList.zig index 823532f..8cd8bea 100644 --- a/src/qtshit/utils/QVariantMapToQVariantList.zig +++ b/src/qtshit/utils/QVariantMapToQVariantList.zig @@ -1,6 +1,7 @@ const std = @import("std"); const QVariant = @import("../types/QVariant.zig").QVariant; const range = @import("./RangeIter.zig").range; +const freeQVariantList = @import("./free/freeQVariantList.zig").freeQVariantList; pub fn QVariantMapToQVariantList(varMap: std.StringHashMap(QVariant), allocator: *std.mem.Allocator) ![]QVariant { var variantList = try allocator.alloc(QVariant, @intCast(usize, varMap.count() * 2)); @@ -11,8 +12,30 @@ pub fn QVariantMapToQVariantList(varMap: std.StringHashMap(QVariant), allocator: while (iter.next()) |i| { if (i % 2 == 1) continue; var item = varMapIter.next().?; - variantList[i] = QVariant{ .String = item.key }; - variantList[i + 1] = item.value; + variantList[i] = QVariant{ .String = item.key_ptr.* }; + variantList[i + 1] = item.value_ptr.*; } return variantList; } + +test "QVariantMap -> QVariantList" { + var map = std.StringHashMap(QVariant).init(std.testing.allocator); + defer map.deinit(); + + // These will get free'd by freeQVariantList + var acabString = try std.testing.allocator.dupe(u8, "ACAB"); + var owoString = try std.testing.allocator.dupe(u8, "owo"); + + try map.put(acabString, .{ .UInt = 1312 }); + try map.put(owoString, .{ .Byte = 12 }); + + var list = try QVariantMapToQVariantList(map, std.testing.allocator); + defer freeQVariantList(list, std.testing.allocator); + + try std.testing.expect(list.len == 4); + try std.testing.expectEqualStrings("ACAB", list[0].String); + try std.testing.expect(list[1].UInt == 1312); + try std.testing.expectEqualStrings("owo", list[2].String); + try std.testing.expect(list[3].Byte == 12); +} + diff --git a/src/qtshit/utils/free/freeQVariantMap.zig b/src/qtshit/utils/free/freeQVariantMap.zig index 1d873a8..53701e2 100644 --- a/src/qtshit/utils/free/freeQVariantMap.zig +++ b/src/qtshit/utils/free/freeQVariantMap.zig @@ -6,8 +6,8 @@ const freeQVariant = @import("./freeQVariant.zig").freeQVariant; pub fn freeQVariantMap(qvariantmap: std.StringHashMap(QVariant), allocator: *std.mem.Allocator) void { var qMapIter = qvariantmap.iterator(); while (qMapIter.next()) |v| { - freeString(v.key, allocator); - freeQVariant(v.value, allocator); + freeString(v.key_ptr.*, allocator); + freeQVariant(v.value_ptr.*, allocator); } // Compiler bug maybe? doesnt want me to drop const so have to store it in a variable var qi = qvariantmap; diff --git a/src/qtshit/utils/tests.zig b/src/qtshit/utils/tests.zig new file mode 100644 index 0000000..d183c55 --- /dev/null +++ b/src/qtshit/utils/tests.zig @@ -0,0 +1,6 @@ +const unicode = @import("./unicode.zig"); +const QVariantMapToQVariantList = @import("./QVariantMapToQVariantList.zig"); + +comptime { + @import("std").testing.refAllDecls(@This()); +} diff --git a/src/qtshit/write/writeQVariantMap.zig b/src/qtshit/write/writeQVariantMap.zig index 0f2e8c2..ea60323 100644 --- a/src/qtshit/write/writeQVariantMap.zig +++ b/src/qtshit/write/writeQVariantMap.zig @@ -10,8 +10,8 @@ pub fn writeQVariantMap(writer: anytype, allocator: *std.mem.Allocator, map: std var writeIterator = map.iterator(); while (writeIterator.next()) |entry| { - try writeString(data.writer(), allocator, entry.key); - try writeQVariant(data.writer(), allocator, entry.value); + try writeString(data.writer(), allocator, entry.key_ptr.*); + try writeQVariant(data.writer(), allocator, entry.value_ptr.*); } try writeUInt(writer, map.count()); diff --git a/src/tests.zig b/src/tests.zig index 211dfde..8be80ba 100644 --- a/src/tests.zig +++ b/src/tests.zig @@ -1,6 +1,6 @@ const readTests = @import("qtshit/read/tests.zig"); const writeTests = @import("qtshit/write/tests.zig"); -const unicode = @import("qtshit/utils/unicode.zig"); +const utilsTests = @import("qtshit/utils/tests.zig"); comptime { @import("std").testing.refAllDecls(@This());