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