From c721fa6d5dc10e1f93da31cb17ccaeb83df3d105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Wed, 16 Mar 2022 07:08:57 +0200 Subject: [PATCH] split bdz int and str tests --- src/sections.zig | 51 ++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/src/sections.zig b/src/sections.zig index 3cd4988..97fb6c3 100644 --- a/src/sections.zig +++ b/src/sections.zig @@ -693,29 +693,30 @@ test "users compare function" { try testing.expect(!cmpUser({}, bb, b)); } -test "bdzIdx" { - const allocator = testing.allocator; - const k_u32 = [_]u32{ 42, 1, 2, 3 }; - const k_str = [_][]const u8{ "42", "1", "2", "3" }; - assert(k_u32.len == k_str.len); - const mphf_str = try cmph.packStr(allocator, k_str[0..]); - const mphf_u32 = try cmph.packU32(allocator, k_u32[0..]); - defer allocator.free(mphf_str); - defer allocator.free(mphf_u32); - - { - var result = try bdzIdx(u32, allocator, mphf_u32, k_u32[0..]); - defer allocator.free(result); - var used = [_]bool{false} ** k_u32.len; - for (result) |elem| used[result[elem]] = true; - for (used) |item| try testing.expect(item); - } - - { - var result = try bdzIdx([]const u8, allocator, mphf_str, k_str[0..]); - defer allocator.free(result); - var used = [_]bool{false} ** k_u32.len; - for (result) |elem| used[result[elem]] = true; - for (used) |item| try testing.expect(item); - } +fn expectUsedHashes(allocator: Allocator, arr: []const u32) !void { + var used = try allocator.alloc(bool, arr.len); + defer allocator.free(used); + mem.set(bool, used, false); + for (arr) |elem| + used[arr[elem]] = true; + for (used) |item| + try testing.expect(item); +} + +test "bdzIdx on u32" { + const keys = [_]u32{ 42, 1, 2, 3 }; + const mphf = try cmph.packU32(testing.allocator, keys[0..]); + defer testing.allocator.free(mphf); + var result = try bdzIdx(u32, testing.allocator, mphf, keys[0..]); + defer testing.allocator.free(result); + try expectUsedHashes(testing.allocator, result); +} + +test "bdzIdx on str" { + const keys = [_][]const u8{ "42", "1", "2", "3" }; + const mphf = try cmph.packStr(testing.allocator, keys[0..]); + defer testing.allocator.free(mphf); + var result = try bdzIdx([]const u8, testing.allocator, mphf, keys[0..]); + defer testing.allocator.free(result); + try expectUsedHashes(testing.allocator, result); }