Commit 703fe0f121

Xavier Bouchoux <xavierb@gmail.com>
2023-10-08 11:35:33
test: add a pair of cases from bug reports
1 parent 40528b8
Changed files (2)
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);
+}
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);
+}