authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-09-25 02:33:32-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-09-25 02:33:32-07:00
log28ac9f8b704951010a49e33f405c4265bf3077d2
tree78f95298d4f743845b8eff8c1b07d7aa35aa80ce
parent731fd217db4caa6809d153f792157c03179b9b9d
parent6bd54a1d3ebd8d997158c57057ba742824cf7e0c
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

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

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

5 files changed, 82 insertions(+), 3 deletions(-)

lib/std/array_hash_map.zig+29
...@@ -2336,6 +2336,35 @@ test "sort" {...@@ -2336,6 +2336,35 @@ test "sort" {
2336 }2336 }
2337}2337}
23382338
2339test "0 sized key" {
2340 var map = AutoArrayHashMap(u0, i32).init(std.testing.allocator);
2341 defer map.deinit();
2342
2343 try testing.expectEqual(map.get(0), null);
2344
2345 try map.put(0, 5);
2346 try testing.expectEqual(map.get(0), 5);
2347
2348 try map.put(0, 10);
2349 try testing.expectEqual(map.get(0), 10);
2350
2351 try testing.expectEqual(map.swapRemove(0), true);
2352 try testing.expectEqual(map.get(0), null);
2353}
2354
2355test "0 sized key and 0 sized value" {
2356 var map = AutoArrayHashMap(u0, u0).init(std.testing.allocator);
2357 defer map.deinit();
2358
2359 try testing.expectEqual(map.get(0), null);
2360
2361 try map.put(0, 0);
2362 try testing.expectEqual(map.get(0), 0);
2363
2364 try testing.expectEqual(map.swapRemove(0), true);
2365 try testing.expectEqual(map.get(0), null);
2366}
2367
2339pub fn getHashPtrAddrFn(comptime K: type, comptime Context: type) (fn (Context, K) u32) {2368pub fn getHashPtrAddrFn(comptime K: type, comptime Context: type) (fn (Context, K) u32) {
2340 return struct {2369 return struct {
2341 fn hash(ctx: Context, key: K) u32 {2370 fn hash(ctx: Context, key: K) u32 {
lib/std/multi_array_list.zig+49
...@@ -901,3 +901,52 @@ test "sorting a span" {...@@ -901,3 +901,52 @@ test "sorting a span" {
901 c += 1;901 c += 1;
902 }902 }
903}903}
904
905test "0 sized struct field" {
906 const ally = testing.allocator;
907
908 const Foo = struct {
909 a: u0,
910 b: f32,
911 };
912
913 var list = MultiArrayList(Foo){};
914 defer list.deinit(ally);
915
916 try testing.expectEqualSlices(u0, &[_]u0{}, list.items(.a));
917 try testing.expectEqualSlices(f32, &[_]f32{}, list.items(.b));
918
919 try list.append(ally, .{ .a = 0, .b = 42.0 });
920 try testing.expectEqualSlices(u0, &[_]u0{0}, list.items(.a));
921 try testing.expectEqualSlices(f32, &[_]f32{42.0}, list.items(.b));
922
923 try list.insert(ally, 0, .{ .a = 0, .b = -1.0 });
924 try testing.expectEqualSlices(u0, &[_]u0{ 0, 0 }, list.items(.a));
925 try testing.expectEqualSlices(f32, &[_]f32{ -1.0, 42.0 }, list.items(.b));
926
927 list.swapRemove(list.len - 1);
928 try testing.expectEqualSlices(u0, &[_]u0{0}, list.items(.a));
929 try testing.expectEqualSlices(f32, &[_]f32{-1.0}, list.items(.b));
930}
931
932test "0 sized struct" {
933 const ally = testing.allocator;
934
935 const Foo = struct {
936 a: u0,
937 };
938
939 var list = MultiArrayList(Foo){};
940 defer list.deinit(ally);
941
942 try testing.expectEqualSlices(u0, &[_]u0{}, list.items(.a));
943
944 try list.append(ally, .{ .a = 0 });
945 try testing.expectEqualSlices(u0, &[_]u0{0}, list.items(.a));
946
947 try list.insert(ally, 0, .{ .a = 0 });
948 try testing.expectEqualSlices(u0, &[_]u0{ 0, 0 }, list.items(.a));
949
950 list.swapRemove(list.len - 1);
951 try testing.expectEqualSlices(u0, &[_]u0{0}, list.items(.a));
952}
src/InternPool.zig+1-3
...@@ -54,9 +54,7 @@ string_table: std.HashMapUnmanaged(...@@ -54,9 +54,7 @@ string_table: std.HashMapUnmanaged(
54 std.hash_map.default_max_load_percentage,54 std.hash_map.default_max_load_percentage,
55) = .{},55) = .{},
5656
57/// TODO: after https://github.com/ziglang/zig/issues/10618 is solved,57const FieldMap = std.ArrayHashMapUnmanaged(void, void, std.array_hash_map.AutoContext(void), false);
58/// change store_hash to false.
59const FieldMap = std.ArrayHashMapUnmanaged(void, void, std.array_hash_map.AutoContext(void), true);
6058
61const builtin = @import("builtin");59const builtin = @import("builtin");
62const std = @import("std");60const std = @import("std");
stage1/zig.h+3
...@@ -190,10 +190,13 @@ typedef char bool;...@@ -190,10 +190,13 @@ typedef char bool;
190190
191#if zig_has_attribute(weak) || defined(zig_gnuc)191#if zig_has_attribute(weak) || defined(zig_gnuc)
192#define zig_weak_linkage __attribute__((weak))192#define zig_weak_linkage __attribute__((weak))
193#define zig_weak_linkage_fn __attribute__((weak))
193#elif _MSC_VER194#elif _MSC_VER
194#define zig_weak_linkage __declspec(selectany)195#define zig_weak_linkage __declspec(selectany)
196#define zig_weak_linkage_fn
195#else197#else
196#define zig_weak_linkage zig_weak_linkage_unavailable198#define zig_weak_linkage zig_weak_linkage_unavailable
199#define zig_weak_linkage_fn zig_weak_linkage_unavailable
197#endif200#endif
198201
199#if zig_has_builtin(trap)202#if zig_has_builtin(trap)
stage1/zig1.wasm
Binary files a/stage1/zig1.wasm and b/stage1/zig1.wasm differ