Merge pull request #17253 from ziglang/MultiArrayList-0bit-struct

std.MultiArrayList: add test coverage for 0-bit structs
This commit is contained in:
Andrew Kelley 2023-09-25 02:33:32 -07:00 committed by GitHub
commit 28ac9f8b70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 3 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) {
return struct {
fn hash(ctx: Context, key: K) u32 {

View File

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

View File

@ -54,9 +54,7 @@ string_table: std.HashMapUnmanaged(
std.hash_map.default_max_load_percentage,
) = .{},
/// TODO: after https://github.com/ziglang/zig/issues/10618 is solved,
/// change store_hash to false.
const FieldMap = std.ArrayHashMapUnmanaged(void, void, std.array_hash_map.AutoContext(void), true);
const FieldMap = std.ArrayHashMapUnmanaged(void, void, std.array_hash_map.AutoContext(void), false);
const builtin = @import("builtin");
const std = @import("std");

View File

@ -190,10 +190,13 @@ typedef char bool;
#if zig_has_attribute(weak) || defined(zig_gnuc)
#define zig_weak_linkage __attribute__((weak))
#define zig_weak_linkage_fn __attribute__((weak))
#elif _MSC_VER
#define zig_weak_linkage __declspec(selectany)
#define zig_weak_linkage_fn
#else
#define zig_weak_linkage zig_weak_linkage_unavailable
#define zig_weak_linkage_fn zig_weak_linkage_unavailable
#endif
#if zig_has_builtin(trap)

Binary file not shown.