From cc69315f032239d15496326f79ceec488f188ea9 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 23 Sep 2023 17:08:40 -0700 Subject: [PATCH] std.MultiArrayList: add test coverage for 0-bit structs closes #10618 solved by #17172 --- lib/std/array_hash_map.zig | 29 +++++++++++++++++++++ lib/std/multi_array_list.zig | 49 ++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/lib/std/array_hash_map.zig b/lib/std/array_hash_map.zig index e170815151..1bb85a255d 100644 --- a/lib/std/array_hash_map.zig +++ b/lib/std/array_hash_map.zig @@ -2336,6 +2336,35 @@ test "sort" { } } +test "0 sized key" { + var map = AutoArrayHashMap(u0, i32).init(std.testing.allocator); + defer map.deinit(); + + try testing.expectEqual(map.get(0), null); + + try map.put(0, 5); + try testing.expectEqual(map.get(0), 5); + + try map.put(0, 10); + try testing.expectEqual(map.get(0), 10); + + try testing.expectEqual(map.swapRemove(0), true); + try testing.expectEqual(map.get(0), null); +} + +test "0 sized key and 0 sized value" { + var map = AutoArrayHashMap(u0, u0).init(std.testing.allocator); + defer map.deinit(); + + try testing.expectEqual(map.get(0), null); + + try map.put(0, 0); + try testing.expectEqual(map.get(0), 0); + + try testing.expectEqual(map.swapRemove(0), true); + try testing.expectEqual(map.get(0), null); +} + pub fn getHashPtrAddrFn(comptime K: type, comptime Context: type) (fn (Context, K) u32) { return struct { fn hash(ctx: Context, key: K) u32 { diff --git a/lib/std/multi_array_list.zig b/lib/std/multi_array_list.zig index 8b5df4a2e4..c18d2c2a5a 100644 --- a/lib/std/multi_array_list.zig +++ b/lib/std/multi_array_list.zig @@ -901,3 +901,52 @@ test "sorting a span" { c += 1; } } + +test "0 sized struct field" { + const ally = testing.allocator; + + const Foo = struct { + a: u0, + b: f32, + }; + + var list = MultiArrayList(Foo){}; + defer list.deinit(ally); + + try testing.expectEqualSlices(u0, &[_]u0{}, list.items(.a)); + try testing.expectEqualSlices(f32, &[_]f32{}, list.items(.b)); + + try list.append(ally, .{ .a = 0, .b = 42.0 }); + try testing.expectEqualSlices(u0, &[_]u0{0}, list.items(.a)); + try testing.expectEqualSlices(f32, &[_]f32{42.0}, list.items(.b)); + + try list.insert(ally, 0, .{ .a = 0, .b = -1.0 }); + try testing.expectEqualSlices(u0, &[_]u0{ 0, 0 }, list.items(.a)); + try testing.expectEqualSlices(f32, &[_]f32{ -1.0, 42.0 }, list.items(.b)); + + list.swapRemove(list.len - 1); + try testing.expectEqualSlices(u0, &[_]u0{0}, list.items(.a)); + try testing.expectEqualSlices(f32, &[_]f32{-1.0}, list.items(.b)); +} + +test "0 sized struct" { + const ally = testing.allocator; + + const Foo = struct { + a: u0, + }; + + var list = MultiArrayList(Foo){}; + defer list.deinit(ally); + + try testing.expectEqualSlices(u0, &[_]u0{}, list.items(.a)); + + try list.append(ally, .{ .a = 0 }); + try testing.expectEqualSlices(u0, &[_]u0{0}, list.items(.a)); + + try list.insert(ally, 0, .{ .a = 0 }); + try testing.expectEqualSlices(u0, &[_]u0{ 0, 0 }, list.items(.a)); + + list.swapRemove(list.len - 1); + try testing.expectEqualSlices(u0, &[_]u0{0}, list.items(.a)); +}