From 703fe0f121df9df44721250528e508762e161780 Mon Sep 17 00:00:00 2001 From: Xavier Bouchoux Date: Sun, 8 Oct 2023 11:35:33 +0200 Subject: [PATCH] test: add a pair of cases from bug reports --- test/behavior/packed-struct.zig | 29 +++++++++++++++++++++++++++++ test/behavior/packed-union.zig | 28 ++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/test/behavior/packed-struct.zig b/test/behavior/packed-struct.zig index 6240814df7..de98b3e27d 100644 --- a/test/behavior/packed-struct.zig +++ b/test/behavior/packed-struct.zig @@ -1036,3 +1036,32 @@ test "modify nested packed struct aligned field" { try std.testing.expectEqual(@as(u8, 1), opts.pretty_print.indent); try std.testing.expect(!opts.baz); } + +test "assigning packed struct inside another packed struct" { + // Originally reported at https://github.com/ziglang/zig/issues/9674 + + const S = struct { + const Inner = packed struct { + bits: u3, + more_bits: u6, + }; + + const Outer = packed struct { + padding: u5, + inner: Inner, + }; + fn t(inner: Inner) void { + r.inner = inner; + } + + var mem: Outer = undefined; + var r: *volatile Outer = &mem; + }; + + const val = S.Inner{ .bits = 1, .more_bits = 11 }; + S.mem.padding = 0; + S.t(val); + + try expectEqual(val, S.mem.inner); + try expect(S.mem.padding == 0); +} diff --git a/test/behavior/packed-union.zig b/test/behavior/packed-union.zig index da62acdd04..ab14bf38e7 100644 --- a/test/behavior/packed-union.zig +++ b/test/behavior/packed-union.zig @@ -85,3 +85,31 @@ test "flags in packed union at offset" { try expectEqual(false, test_bits.adv_flags.adv.flags.enable_1); try expectEqual(false, test_bits.adv_flags.adv.flags.enable_2); } + +test "packed union in packed struct" { + // Originally reported at https://github.com/ziglang/zig/issues/16581 + + const ReadRequest = packed struct { key: i32 }; + const RequestType = enum { + read, + insert, + }; + const RequestUnion = packed union { + read: ReadRequest, + }; + + const Request = packed struct { + active_type: RequestType, + request: RequestUnion, + const Self = @This(); + + fn init(read: ReadRequest) Self { + return .{ + .active_type = .read, + .request = RequestUnion{ .read = read }, + }; + } + }; + + try std.testing.expectEqual(RequestType.read, Request.init(.{ .key = 3 }).active_type); +}