diff options
| author | Loris Cro <kappaloris@gmail.com> | 2023-06-18 09:06:40 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-06-18 09:06:40 +0200 |
| commit | 216ef10dc471e4db60a30208be178d6c59efeaaf (patch) | |
| tree | 8c239dab283ae9cb3b7fe099bae240bcc53f894e /test/behavior/switch.zig | |
| parent | 0fc1d396495c1ab482197021dedac8bea3f9401c (diff) | |
| parent | 729a051e9e38674233190aea23c0ac8c134f2d67 (diff) | |
| download | zig-216ef10dc471e4db60a30208be178d6c59efeaaf.tar.gz zig-216ef10dc471e4db60a30208be178d6c59efeaaf.zip | |
Merge branch 'master' into autodoc-searchkey
Diffstat (limited to 'test/behavior/switch.zig')
| -rw-r--r-- | test/behavior/switch.zig | 127 |
1 files changed, 103 insertions, 24 deletions
diff --git a/test/behavior/switch.zig b/test/behavior/switch.zig index b69add6fdf..02e3ef4dce 100644 --- a/test/behavior/switch.zig +++ b/test/behavior/switch.zig @@ -1,5 +1,6 @@ const builtin = @import("builtin"); const std = @import("std"); +const assert = std.debug.assert; const expect = std.testing.expect; const expectError = std.testing.expectError; const expectEqual = std.testing.expectEqual; @@ -88,7 +89,6 @@ fn nonConstSwitch(foo: SwitchStatementFoo) !void { const SwitchStatementFoo = enum { A, B, C, D }; test "switch with multiple expressions" { - if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO const x = switch (returnsFive()) { @@ -118,9 +118,10 @@ fn trueIfBoolFalseOtherwise(comptime T: type) bool { test "switching on booleans" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; try testSwitchOnBools(); - comptime try testSwitchOnBools(); + try comptime testSwitchOnBools(); } fn testSwitchOnBools() !void { @@ -185,10 +186,10 @@ test "switch variable for range and multiple prongs" { fn doTheTest() !void { var u: u8 = 16; try doTheSwitch(u); - comptime try doTheSwitch(u); + try comptime doTheSwitch(u); var v: u8 = 42; try doTheSwitch(v); - comptime try doTheSwitch(v); + try comptime doTheSwitch(v); } fn doTheSwitch(q: u8) !void { switch (q) { @@ -231,6 +232,7 @@ test "switch prong with variable" { if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; try switchProngWithVarFn(SwitchProngWithVarEnum{ .One = 13 }); try switchProngWithVarFn(SwitchProngWithVarEnum{ .Two = 13.0 }); @@ -255,9 +257,10 @@ test "switch on enum using pointer capture" { if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; try testSwitchEnumPtrCapture(); - comptime try testSwitchEnumPtrCapture(); + try comptime testSwitchEnumPtrCapture(); } fn testSwitchEnumPtrCapture() !void { @@ -273,11 +276,10 @@ fn testSwitchEnumPtrCapture() !void { } test "switch handles all cases of number" { - if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO try testSwitchHandleAllCases(); - comptime try testSwitchHandleAllCases(); + try comptime testSwitchHandleAllCases(); } fn testSwitchHandleAllCases() !void { @@ -316,6 +318,7 @@ test "switch on union with some prongs capturing" { if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const X = union(enum) { a, @@ -352,6 +355,7 @@ test "switch on const enum with var" { test "anon enum literal used in switch on union enum" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const Foo = union(enum) { a: i32, @@ -369,7 +373,7 @@ test "switch all prongs unreachable" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO try testAllProngsUnreachable(); - comptime try testAllProngsUnreachable(); + try comptime testAllProngsUnreachable(); } fn testAllProngsUnreachable() !void { @@ -390,6 +394,7 @@ fn switchWithUnreachable(x: i32) i32 { test "capture value of switch with all unreachable prongs" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const x = return_a_number() catch |err| switch (err) { else => unreachable, @@ -403,6 +408,7 @@ fn return_a_number() anyerror!i32 { test "switch on integer with else capturing expr" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const S = struct { fn doTheTest() !void { @@ -415,13 +421,14 @@ test "switch on integer with else capturing expr" { } }; try S.doTheTest(); - comptime try S.doTheTest(); + try comptime S.doTheTest(); } test "else prong of switch on error set excludes other cases" { if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const S = struct { fn doTheTest() !void { @@ -449,14 +456,14 @@ test "else prong of switch on error set excludes other cases" { } }; try S.doTheTest(); - comptime try S.doTheTest(); + try comptime S.doTheTest(); } test "switch prongs with error set cases make a new error set type for capture value" { - if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const S = struct { fn doTheTest() !void { @@ -486,11 +493,12 @@ test "switch prongs with error set cases make a new error set type for capture v } }; try S.doTheTest(); - comptime try S.doTheTest(); + try comptime S.doTheTest(); } test "return result loc and then switch with range implicit casted to error union" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const S = struct { fn doTheTest() !void { @@ -504,7 +512,7 @@ test "return result loc and then switch with range implicit casted to error unio } }; try S.doTheTest(); - comptime try S.doTheTest(); + try comptime S.doTheTest(); } test "switch with null and T peer types and inferred result location type" { @@ -524,14 +532,14 @@ test "switch with null and T peer types and inferred result location type" { } }; try S.doTheTest(1); - comptime try S.doTheTest(1); + try comptime S.doTheTest(1); } test "switch prongs with cases with identical payload types" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const Union = union(enum) { A: usize, @@ -569,12 +577,13 @@ test "switch prongs with cases with identical payload types" { } }; try S.doTheTest(); - comptime try S.doTheTest(); + try comptime S.doTheTest(); } test "switch on pointer type" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const S = struct { const X = struct { @@ -597,9 +606,9 @@ test "switch on pointer type" { try expect(1 == S.doTheTest(S.P1)); try expect(2 == S.doTheTest(S.P2)); try expect(3 == S.doTheTest(S.P3)); - comptime try expect(1 == S.doTheTest(S.P1)); - comptime try expect(2 == S.doTheTest(S.P2)); - comptime try expect(3 == S.doTheTest(S.P3)); + try comptime expect(1 == S.doTheTest(S.P1)); + try comptime expect(2 == S.doTheTest(S.P2)); + try comptime expect(3 == S.doTheTest(S.P3)); } test "switch on error set with single else" { @@ -615,13 +624,14 @@ test "switch on error set with single else" { }; try S.doTheTest(); - comptime try S.doTheTest(); + try comptime S.doTheTest(); } test "switch capture copies its payload" { if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; const S = struct { fn doTheTest() !void { @@ -640,7 +650,7 @@ test "switch capture copies its payload" { } }; try S.doTheTest(); - comptime try S.doTheTest(); + try comptime S.doTheTest(); } test "capture of integer forwards the switch condition directly" { @@ -660,8 +670,8 @@ test "capture of integer forwards the switch condition directly" { }; try S.foo(42); try S.foo(100); - comptime try S.foo(42); - comptime try S.foo(100); + try comptime S.foo(42); + try comptime S.foo(100); } test "enum value without tag name used as switch item" { @@ -692,10 +702,12 @@ test "switch item sizeof" { } }; try S.doTheTest(); - comptime try S.doTheTest(); + try comptime S.doTheTest(); } test "comptime inline switch" { + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + const U = union(enum) { a: type, b: type }; const value = comptime blk: { var u: U = .{ .a = u32 }; @@ -706,3 +718,70 @@ test "comptime inline switch" { try expectEqual(u32, value); } + +test "switch capture peer type resolution" { + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + + const U = union(enum) { + a: u32, + b: u64, + fn innerVal(u: @This()) u64 { + switch (u) { + .a, .b => |x| return x, + } + } + }; + + try expectEqual(@as(u64, 100), U.innerVal(.{ .a = 100 })); + try expectEqual(@as(u64, 200), U.innerVal(.{ .b = 200 })); +} + +test "switch capture peer type resolution for in-memory coercible payloads" { + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + + const T1 = c_int; + const T2 = @Type(@typeInfo(T1)); + + comptime assert(T1 != T2); + + const U = union(enum) { + a: T1, + b: T2, + fn innerVal(u: @This()) c_int { + switch (u) { + .a, .b => |x| return x, + } + } + }; + + try expectEqual(@as(c_int, 100), U.innerVal(.{ .a = 100 })); + try expectEqual(@as(c_int, 200), U.innerVal(.{ .b = 200 })); +} + +test "switch pointer capture peer type resolution" { + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + + const T1 = c_int; + const T2 = @Type(@typeInfo(T1)); + + comptime assert(T1 != T2); + + const U = union(enum) { + a: T1, + b: T2, + fn innerVal(u: *@This()) *c_int { + switch (u.*) { + .a, .b => |*ptr| return ptr, + } + } + }; + + var ua: U = .{ .a = 100 }; + var ub: U = .{ .b = 200 }; + + ua.innerVal().* = 111; + ub.innerVal().* = 222; + + try expectEqual(U{ .a = 111 }, ua); + try expectEqual(U{ .b = 222 }, ub); +} |
