| ... | @@ -4,70 +4,102 @@ const expect = std.testing.expect; | ... | @@ -4,70 +4,102 @@ const expect = std.testing.expect; |
| 4 | const expectEqual = std.testing.expectEqual; | 4 | const expectEqual = std.testing.expectEqual; |
| 5 | const Tag = std.meta.Tag; | 5 | const Tag = std.meta.Tag; |
| 6 | | 6 | |
| 7 | const Foo = union { | 7 | const FooWithFloats = union { |
| 8 | float: f64, | 8 | float: f64, |
| 9 | int: i32, | 9 | int: i32, |
| 10 | }; | 10 | }; |
| 11 | | 11 | |
| 12 | test "basic unions" { | 12 | test "basic unions with floats" { |
| 13 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; | 13 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; |
| 14 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; | 14 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; |
| 15 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; | 15 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 16 | | 16 | |
| 17 | var foo = Foo{ .int = 1 }; | 17 | var foo = FooWithFloats{ .int = 1 }; |
| 18 | try expect(foo.int == 1); | 18 | try expect(foo.int == 1); |
| 19 | foo = Foo{ .float = 12.34 }; | 19 | foo = FooWithFloats{ .float = 12.34 }; |
| 20 | try expect(foo.float == 12.34); | 20 | try expect(foo.float == 12.34); |
| 21 | } | 21 | } |
| 22 | | 22 | |
| 23 | test "init union with runtime value" { | 23 | fn setFloat(foo: *FooWithFloats, x: f64) void { |
| | 24 | foo.* = FooWithFloats{ .float = x }; |
| | 25 | } |
| | 26 | |
| | 27 | test "init union with runtime value - floats" { |
| 24 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; | 28 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; |
| 25 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; | 29 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; |
| 26 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; | 30 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 27 | | 31 | |
| 28 | var foo: Foo = undefined; | 32 | var foo: FooWithFloats = undefined; |
| 29 | | 33 | |
| 30 | setFloat(&foo, 12.34); | 34 | setFloat(&foo, 12.34); |
| 31 | try expect(foo.float == 12.34); | 35 | try expect(foo.float == 12.34); |
| | 36 | } |
| | 37 | |
| | 38 | test "basic unions" { |
| | 39 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; |
| | 40 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| | 41 | |
| | 42 | var foo = Foo{ .int = 1 }; |
| | 43 | try expect(foo.int == 1); |
| | 44 | foo = Foo{ .str = .{ .slice = "Hello!" } }; |
| | 45 | try expect(std.mem.eql(u8, foo.str.slice, "Hello!")); |
| | 46 | } |
| | 47 | |
| | 48 | const Foo = union { |
| | 49 | int: i32, |
| | 50 | str: struct { |
| | 51 | slice: []const u8, |
| | 52 | }, |
| | 53 | }; |
| | 54 | |
| | 55 | test "init union with runtime value" { |
| | 56 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; |
| | 57 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| | 58 | |
| | 59 | var foo: Foo = undefined; |
| 32 | | 60 | |
| 33 | setInt(&foo, 42); | 61 | setInt(&foo, 42); |
| 34 | try expect(foo.int == 42); | 62 | try expect(foo.int == 42); |
| 35 | } | | |
| 36 | | 63 | |
| 37 | fn setFloat(foo: *Foo, x: f64) void { | 64 | setStr(&foo, "Hello!"); |
| 38 | foo.* = Foo{ .float = x }; | 65 | try expect(std.mem.eql(u8, foo.str.slice, "Hello!")); |
| 39 | } | 66 | } |
| 40 | | 67 | |
| 41 | fn setInt(foo: *Foo, x: i32) void { | 68 | fn setInt(foo: *Foo, x: i32) void { |
| 42 | foo.* = Foo{ .int = x }; | 69 | foo.* = Foo{ .int = x }; |
| 43 | } | 70 | } |
| 44 | | 71 | |
| | 72 | fn setStr(foo: *Foo, slice: []const u8) void { |
| | 73 | foo.* = Foo{ .str = .{ .slice = slice } }; |
| | 74 | } |
| | 75 | |
| 45 | test "comptime union field access" { | 76 | test "comptime union field access" { |
| 46 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; | 77 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; |
| 47 | | 78 | |
| 48 | comptime { | 79 | comptime { |
| 49 | var foo = Foo{ .int = 0 }; | 80 | var foo = FooWithFloats{ .int = 0 }; |
| 50 | try expect(foo.int == 0); | 81 | try expect(foo.int == 0); |
| 51 | | 82 | |
| 52 | foo = Foo{ .float = 42.42 }; | 83 | foo = FooWithFloats{ .float = 12.34 }; |
| 53 | try expect(foo.float == 42.42); | 84 | try expect(foo.float == 12.34); |
| 54 | } | 85 | } |
| 55 | } | 86 | } |
| 56 | | 87 | |
| 57 | const FooExtern = extern union { | 88 | const FooExtern = extern union { |
| 58 | float: f64, | | |
| 59 | int: i32, | 89 | int: i32, |
| | 90 | str: struct { |
| | 91 | slice: []const u8, |
| | 92 | }, |
| 60 | }; | 93 | }; |
| 61 | | 94 | |
| 62 | test "basic extern unions" { | 95 | test "basic extern unions" { |
| 63 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; | | |
| 64 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; | 96 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; |
| 65 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; | 97 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 66 | | 98 | |
| 67 | var foo = FooExtern{ .int = 1 }; | 99 | var foo = FooExtern{ .int = 1 }; |
| 68 | try expect(foo.int == 1); | 100 | try expect(foo.int == 1); |
| 69 | foo.float = 12.34; | 101 | foo.str.slice = "Well"; |
| 70 | try expect(foo.float == 12.34); | 102 | try expect(std.mem.eql(u8, foo.str.slice, "Well")); |
| 71 | } | 103 | } |
| 72 | | 104 | |
| 73 | const ExternPtrOrInt = extern union { | 105 | const ExternPtrOrInt = extern union { |