From 7aac6e9ff32d189e87ce0d465aaebbc2fe361e1c Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 11 Apr 2026 04:42:14 +0000 Subject: [PATCH] test: add HashMap resize beyond initial capacity test Added a focused test in `echo-core-zig/src/data/hashmap.zig` to specifically verify the resize logic of HashMap when inserting more items than its initial capacity. This ensures no data is lost during the resize process and that the map correctly resizes and preserves existing probe chains. The test uses proper memory tracking to prevent leaks. Co-authored-by: ulac000000 <132948319+ulac000000@users.noreply.github.com> --- echo-core-zig/src/data/hashmap.zig | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/echo-core-zig/src/data/hashmap.zig b/echo-core-zig/src/data/hashmap.zig index b12e8db..54109d0 100644 --- a/echo-core-zig/src/data/hashmap.zig +++ b/echo-core-zig/src/data/hashmap.zig @@ -294,3 +294,42 @@ 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 beyond initial capacity" { + var map = try HashMap([]const u8, i32).init(std.testing.allocator, 8); + defer map.deinit(); + + // Map has initial capacity 8. At 3/4 load factor (6 items), it should resize to 16. + // Inserting 9 items guarantees it crosses both the resize threshold and the initial physical capacity. + + // Store keys to free them later, as HashMap doesn't take ownership of dynamically allocated keys. + var keys = try std.ArrayList([]const u8).initCapacity(std.testing.allocator, 9); + defer { + for (keys.items) |key| { + std.testing.allocator.free(key); + } + keys.deinit(); + } + + var i: i32 = 0; + while (i < 9) : (i += 1) { + const key = try std.fmt.allocPrint(std.testing.allocator, "test_key_{d}", .{i}); + try keys.append(key); + try map.put(key, i); + } + + // Verify all 9 items are present and values are correct + i = 0; + while (i < 9) : (i += 1) { + const key = keys.items[@as(usize, @intCast(i))]; + const val_ptr = map.get(key); + try std.testing.expect(val_ptr != null); + try std.testing.expectEqual(i, val_ptr.?.*); + } + + // Verify count is 9 + try std.testing.expectEqual(@as(usize, 9), map.count); + + // Initial size is 8. After inserting 6 items it resizes to 16. + try std.testing.expect(map.entries.len >= 16); +}