std.MultiArrayList: add test coverage for 0-bit structs

closes #10618
solved by #17172
This commit is contained in:
Andrew Kelley 2023-09-23 17:08:40 -07:00
parent df5f0517b3
commit cc69315f03
2 changed files with 78 additions and 0 deletions

View file

@ -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) { pub fn getHashPtrAddrFn(comptime K: type, comptime Context: type) (fn (Context, K) u32) {
return struct { return struct {
fn hash(ctx: Context, key: K) u32 { fn hash(ctx: Context, key: K) u32 {

View file

@ -901,3 +901,52 @@ test "sorting a span" {
c += 1; 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));
}