Add test to QVariantMapToQVariantList.

This commit is contained in:
Kitteh 2021-06-05 07:11:27 +01:00
parent a6a1e6ba71
commit d4e971906e
5 changed files with 36 additions and 7 deletions

View file

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

View file

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

View file

@ -0,0 +1,6 @@
const unicode = @import("./unicode.zig");
const QVariantMapToQVariantList = @import("./QVariantMapToQVariantList.zig");
comptime {
@import("std").testing.refAllDecls(@This());
}

View file

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

View file

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