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