diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2022-02-28 17:42:59 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-28 17:42:59 +0100 |
| commit | 331cc810ded2058f2e2767b0485eb18d888a45e5 (patch) | |
| tree | 9e5ba37e5b06eaccf5ff36976955a32cda5ddfc4 /test/behavior | |
| parent | 90059a12e0ffe433132450f9f43221a198a22106 (diff) | |
| parent | 16f9774d2d6f358c97637e35609dfe0fc14cb501 (diff) | |
| download | zig-331cc810ded2058f2e2767b0485eb18d888a45e5.tar.gz zig-331cc810ded2058f2e2767b0485eb18d888a45e5.zip | |
Merge pull request #11012 from ziglang/x64-union-tag
stage2,x64: basic (un)tagged unions
Diffstat (limited to 'test/behavior')
| -rw-r--r-- | test/behavior/union.zig | 75 |
1 files changed, 48 insertions, 27 deletions
diff --git a/test/behavior/union.zig b/test/behavior/union.zig index 6044bf983c..391de2e901 100644 --- a/test/behavior/union.zig +++ b/test/behavior/union.zig @@ -4,68 +4,100 @@ const expect = std.testing.expect; const expectEqual = std.testing.expectEqual; const Tag = std.meta.Tag; -const Foo = union { +const FooWithFloats = union { float: f64, int: i32, }; -test "basic unions" { +test "basic unions with floats" { if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; - var foo = Foo{ .int = 1 }; + var foo = FooWithFloats{ .int = 1 }; try expect(foo.int == 1); - foo = Foo{ .float = 12.34 }; + foo = FooWithFloats{ .float = 12.34 }; try expect(foo.float == 12.34); } -test "init union with runtime value" { +fn setFloat(foo: *FooWithFloats, x: f64) void { + foo.* = FooWithFloats{ .float = x }; +} + +test "init union with runtime value - floats" { if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; - var foo: Foo = undefined; + var foo: FooWithFloats = undefined; setFloat(&foo, 12.34); try expect(foo.float == 12.34); +} + +test "basic unions" { + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; + + var foo = Foo{ .int = 1 }; + try expect(foo.int == 1); + foo = Foo{ .str = .{ .slice = "Hello!" } }; + try expect(std.mem.eql(u8, foo.str.slice, "Hello!")); +} + +const Foo = union { + int: i32, + str: struct { + slice: []const u8, + }, +}; + +test "init union with runtime value" { + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; + + var foo: Foo = undefined; setInt(&foo, 42); try expect(foo.int == 42); -} -fn setFloat(foo: *Foo, x: f64) void { - foo.* = Foo{ .float = x }; + setStr(&foo, "Hello!"); + try expect(std.mem.eql(u8, foo.str.slice, "Hello!")); } fn setInt(foo: *Foo, x: i32) void { foo.* = Foo{ .int = x }; } +fn setStr(foo: *Foo, slice: []const u8) void { + foo.* = Foo{ .str = .{ .slice = slice } }; +} + test "comptime union field access" { comptime { - var foo = Foo{ .int = 0 }; + var foo = FooWithFloats{ .int = 0 }; try expect(foo.int == 0); - foo = Foo{ .float = 42.42 }; - try expect(foo.float == 42.42); + foo = FooWithFloats{ .float = 12.34 }; + try expect(foo.float == 12.34); } } const FooExtern = extern union { - float: f64, int: i32, + str: struct { + slice: []const u8, + }, }; test "basic extern unions" { - if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; var foo = FooExtern{ .int = 1 }; try expect(foo.int == 1); - foo.float = 12.34; - try expect(foo.float == 12.34); + foo.str.slice = "Well"; + try expect(std.mem.eql(u8, foo.str.slice, "Well")); } const ExternPtrOrInt = extern union { @@ -129,7 +161,6 @@ test "access a member of tagged union with conflicting enum tag name" { } test "constant tagged union with payload" { - if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; @@ -227,7 +258,6 @@ fn testComparison() !void { } test "comparison between union and enum literal" { - if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; @@ -297,7 +327,6 @@ pub const PackThis = union(enum) { }; test "constant packed union" { - if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; @@ -457,7 +486,6 @@ test "update the tag value for zero-sized unions" { } test "union initializer generates padding only if needed" { - if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; @@ -470,7 +498,6 @@ test "union initializer generates padding only if needed" { } test "runtime tag name with single field" { - if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; @@ -483,7 +510,6 @@ test "runtime tag name with single field" { } test "method call on an empty union" { - if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; @@ -547,7 +573,6 @@ test "tagged union type" { } test "tagged union as return value" { - if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; @@ -628,7 +653,6 @@ fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) !void { } test "switch on union with only 1 field" { - if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO @@ -658,7 +682,6 @@ const PartialInstWithPayload = union(enum) { }; test "union with only 1 field casted to its enum type which has enum value specified" { - if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; @@ -796,7 +819,6 @@ test "@unionInit stored to a const" { } test "@unionInit can modify a union type" { - if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO @@ -839,7 +861,6 @@ test "@unionInit can modify a pointer value" { } test "union no tag with struct member" { - if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
