authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-12-04 02:12:13-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-12-04 02:12:13-05:00
log76f3bdfff8224144e7b57748eb8eda2001d0308d
tree46e05c057146f2a0d781b42c6b42b8a565237b34
parentdd3437d5ba30c2101719fa38a95914fae5d965dd

add test for casting union to tag type of union


1 files changed, 17 insertions(+), 0 deletions(-)

test/cases/union.zig+17
......@@ -173,3 +173,20 @@ const ZeroBits = union {
173173test "union with only 1 field which is void should be zero bits" {
174174 comptime assert(@sizeOf(ZeroBits) == 0);
175175}
176
177const TheTag = enum {A, B, C};
178const TheUnion = union(TheTag) { A: i32, B: i32, C: i32 };
179test "union field access gives the enum values" {
180 assert(TheUnion.A == TheTag.A);
181 assert(TheUnion.B == TheTag.B);
182 assert(TheUnion.C == TheTag.C);
183}
184
185test "cast union to tag type of union" {
186 testCastUnionToTagType(TheUnion {.B = 1234});
187 comptime testCastUnionToTagType(TheUnion {.B = 1234});
188}
189
190fn testCastUnionToTagType(x: &const TheUnion) {
191 assert(TheTag(*x) == TheTag.B);
192}