From faaa98c50f969c18d50dd47221cf18146a6834b2 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 7 Apr 2026 05:00:39 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20test:=20Add=20test=20for=20HashM?= =?UTF-8?q?ap=20resize=20load=20factor=20boundary?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds a specific test for the `HashMap` resize logic checking the exact behavior at the load factor limit (count >= entries.len * 3 / 4) and ensuring data persistence. Co-authored-by: ulac000000 <132948319+ulac000000@users.noreply.github.com> --- echo-core-zig/src/data/hashmap.zig | 37 ++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/echo-core-zig/src/data/hashmap.zig b/echo-core-zig/src/data/hashmap.zig index b12e8db..dd972b1 100644 --- a/echo-core-zig/src/data/hashmap.zig +++ b/echo-core-zig/src/data/hashmap.zig @@ -294,3 +294,40 @@ test "HashMap multiple resizes and collisions" { // 1000 / 0.75 = 1333.33 -> next power of 2 is 2048 try std.testing.expect(map.entries.len >= 2048); } + +test "HashMap resize load factor boundary" { + // Initial capacity is 8 + var map = try HashMap([]const u8, i32).init(std.testing.allocator, 8); + defer map.deinit(); + + // The load factor limit is entries.len * 3 / 4. + // 8 * 3 / 4 = 6. So on the 6th insertion, resize() should be triggered. + + // Insert 5 items + try map.put("k1", 1); + try map.put("k2", 2); + try map.put("k3", 3); + try map.put("k4", 4); + try map.put("k5", 5); + + try std.testing.expectEqual(@as(usize, 8), map.entries.len); + try std.testing.expectEqual(@as(usize, 5), map.count); + + // Insert 6th item. The load factor is exactly reached (count is now 6, capacity limit is 6). + try map.put("k6", 6); + try std.testing.expectEqual(@as(usize, 8), map.entries.len); + + // Insert 7th item, should trigger resize to 16 because count (6) is >= 8 * 3 / 4. + try map.put("k7", 7); + try std.testing.expectEqual(@as(usize, 16), map.entries.len); + try std.testing.expectEqual(@as(usize, 7), map.count); + + // Verify all keys are still present + try std.testing.expectEqual(@as(i32, 1), map.get("k1").?.*); + try std.testing.expectEqual(@as(i32, 2), map.get("k2").?.*); + try std.testing.expectEqual(@as(i32, 3), map.get("k3").?.*); + try std.testing.expectEqual(@as(i32, 4), map.get("k4").?.*); + try std.testing.expectEqual(@as(i32, 5), map.get("k5").?.*); + try std.testing.expectEqual(@as(i32, 6), map.get("k6").?.*); + try std.testing.expectEqual(@as(i32, 7), map.get("k7").?.*); +}