From 8d1805f81c97a0b773772e86aa39f26c894b7985 Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Sat, 29 Jul 2023 05:51:33 -0400 Subject: [PATCH] behavior: add coverage for no longer reproducing issue Closes #14305 --- test/behavior.zig | 1 + test/behavior/packed-union.zig | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 test/behavior/packed-union.zig diff --git a/test/behavior.zig b/test/behavior.zig index 249d3191e6..fe2856649a 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -193,6 +193,7 @@ test { _ = @import("behavior/optional.zig"); _ = @import("behavior/packed-struct.zig"); _ = @import("behavior/packed_struct_explicit_backing_int.zig"); + _ = @import("behavior/packed-union.zig"); _ = @import("behavior/pointers.zig"); _ = @import("behavior/popcount.zig"); _ = @import("behavior/prefetch.zig"); diff --git a/test/behavior/packed-union.zig b/test/behavior/packed-union.zig new file mode 100644 index 0000000000..ceb22e1ba9 --- /dev/null +++ b/test/behavior/packed-union.zig @@ -0,0 +1,40 @@ +const std = @import("std"); +const builtin = @import("builtin"); +const expectEqual = std.testing.expectEqual; + +test "flags in packed union" { + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + + const FlagBits = packed struct(u8) { + enable_1: bool = false, + enable_2: bool = false, + enable_3: bool = false, + enable_4: bool = false, + other_flags: packed union { + flags: packed struct(u4) { + enable_1: bool = true, + enable_2: bool = false, + enable_3: bool = false, + enable_4: bool = false, + }, + bits: u4, + } = .{ .flags = .{} }, + }; + var test_bits: FlagBits = .{}; + + try expectEqual(false, test_bits.enable_1); + try expectEqual(true, test_bits.other_flags.flags.enable_1); + + test_bits.enable_1 = true; + + try expectEqual(true, test_bits.enable_1); + try expectEqual(true, test_bits.other_flags.flags.enable_1); + + test_bits.other_flags.flags.enable_1 = false; + + try expectEqual(true, test_bits.enable_1); + try expectEqual(false, test_bits.other_flags.flags.enable_1); +}