Skip to content

Commit 703fe0f

Browse files
committed
test: add a pair of cases from bug reports
1 parent 40528b8 commit 703fe0f

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

test/behavior/packed-struct.zig

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,3 +1036,32 @@ test "modify nested packed struct aligned field" {
10361036
try std.testing.expectEqual(@as(u8, 1), opts.pretty_print.indent);
10371037
try std.testing.expect(!opts.baz);
10381038
}
1039+
1040+
test "assigning packed struct inside another packed struct" {
1041+
// Originally reported at https://github.com/ziglang/zig/issues/9674
1042+
1043+
const S = struct {
1044+
const Inner = packed struct {
1045+
bits: u3,
1046+
more_bits: u6,
1047+
};
1048+
1049+
const Outer = packed struct {
1050+
padding: u5,
1051+
inner: Inner,
1052+
};
1053+
fn t(inner: Inner) void {
1054+
r.inner = inner;
1055+
}
1056+
1057+
var mem: Outer = undefined;
1058+
var r: *volatile Outer = &mem;
1059+
};
1060+
1061+
const val = S.Inner{ .bits = 1, .more_bits = 11 };
1062+
S.mem.padding = 0;
1063+
S.t(val);
1064+
1065+
try expectEqual(val, S.mem.inner);
1066+
try expect(S.mem.padding == 0);
1067+
}

test/behavior/packed-union.zig

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,31 @@ test "flags in packed union at offset" {
8585
try expectEqual(false, test_bits.adv_flags.adv.flags.enable_1);
8686
try expectEqual(false, test_bits.adv_flags.adv.flags.enable_2);
8787
}
88+
89+
test "packed union in packed struct" {
90+
// Originally reported at https://github.com/ziglang/zig/issues/16581
91+
92+
const ReadRequest = packed struct { key: i32 };
93+
const RequestType = enum {
94+
read,
95+
insert,
96+
};
97+
const RequestUnion = packed union {
98+
read: ReadRequest,
99+
};
100+
101+
const Request = packed struct {
102+
active_type: RequestType,
103+
request: RequestUnion,
104+
const Self = @This();
105+
106+
fn init(read: ReadRequest) Self {
107+
return .{
108+
.active_type = .read,
109+
.request = RequestUnion{ .read = read },
110+
};
111+
}
112+
};
113+
114+
try std.testing.expectEqual(RequestType.read, Request.init(.{ .key = 3 }).active_type);
115+
}

0 commit comments

Comments
 (0)