diff options
| author | Techcable <Techcable@techcable.net> | 2022-10-01 01:31:17 -0700 |
|---|---|---|
| committer | Techcable <Techcable@techcable.net> | 2022-10-01 15:22:26 -0700 |
| commit | a560af96568a668bf0c01b3c58ad4dda647ea1b4 (patch) | |
| tree | 6e4a49b52e31fd14def6d51d0d4c175654c2bafa | |
| parent | 5b689d389fa50070c08520cdb6296dda3ad78a62 (diff) | |
| download | zig-a560af96568a668bf0c01b3c58ad4dda647ea1b4.tar.gz zig-a560af96568a668bf0c01b3c58ad4dda647ea1b4.zip | |
translate-c: Add tests for packed unions
Was missing test coverage before this (although it was suppored)
| -rw-r--r-- | test/translate_c.zig | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/test/translate_c.zig b/test/translate_c.zig index 755f61435b..9854e783d4 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -1391,6 +1391,74 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\pub const Foo = union_Foo; }); + cases.add("packed union - simple", + \\union Foo { + \\ char x; + \\ double y; + \\} __attribute__((packed)); + , &[_][]const u8{ + \\pub const union_Foo = extern union { + \\ x: u8 align(1), + \\ y: f64 align(1), + \\}; + , + \\pub const Foo = union_Foo; + }); + + cases.add("packed union - nested unpacked", + \\union Foo{ + \\ char x; + \\ double y; + \\ struct { + \\ char a; + \\ int b; + \\ } z; + \\} __attribute__((packed)); + , &[_][]const u8{ + // NOTE: The nested struct is *not* packed/aligned, + // even though the parent struct is + // this is consistent with GCC docs + \\const struct_unnamed_1 = extern struct { + \\ a: u8, + \\ b: c_int, + \\}; + , + \\pub const union_Foo = extern union { + \\ x: u8 align(1), + \\ y: f64 align(1), + \\ z: struct_unnamed_1 align(1), + \\}; + , + \\pub const Foo = union_Foo; + }); + + cases.add("packed union - nested packed", + \\union Foo{ + \\ char x; + \\ double y; + \\ struct { + \\ char a; + \\ int b; + \\ } __attribute__((packed)) z; + \\} __attribute__((packed)); + , &[_][]const u8{ + // in order for the nested struct to be packed, it must + // have an independent packed declaration on + // the nested type (see GCC docs for details) + \\const struct_unnamed_1 = extern struct { + \\ a: u8 align(1), + \\ b: c_int align(1), + \\}; + , + \\pub const union_Foo = extern union { + \\ x: u8 align(1), + \\ y: f64 align(1), + \\ z: struct_unnamed_1 align(1), + \\}; + , + \\pub const Foo = union_Foo; + }); + cases.add("string literal", \\const char *foo(void) { \\ return "bar"; |
