diff options
| author | Pavel Verigo <paul.verigo@gmail.com> | 2024-09-18 16:55:50 +0200 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2025-02-22 18:34:00 -0500 |
| commit | b25d93e7d95418ea92b388ff8b58a04673c04539 (patch) | |
| tree | 81a486b469fdffd0078d6289b1fe5390e1ab614a /test/behavior/switch.zig | |
| parent | 61b69a418db2f525c4be4e19029487f61fb3234e (diff) | |
| download | zig-b25d93e7d95418ea92b388ff8b58a04673c04539.tar.gz zig-b25d93e7d95418ea92b388ff8b58a04673c04539.zip | |
stage2-wasm: implement switch_dispatch + handle > 32 bit integers in switches
Updated solution is future proof for arbitary size integer handling for both strategies .br_table lowering if switch case is dense, .br_if base jump table if values are too sparse.
Diffstat (limited to 'test/behavior/switch.zig')
| -rw-r--r-- | test/behavior/switch.zig | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/test/behavior/switch.zig b/test/behavior/switch.zig index 8ffef186d1..ad744ef76a 100644 --- a/test/behavior/switch.zig +++ b/test/behavior/switch.zig @@ -4,6 +4,8 @@ const assert = std.debug.assert; const expect = std.testing.expect; const expectError = std.testing.expectError; const expectEqual = std.testing.expectEqual; +const minInt = std.math.minInt; +const maxInt = std.math.maxInt; test "switch with numbers" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO @@ -41,6 +43,46 @@ fn testSwitchWithAllRanges(x: u32, y: u32) u32 { }; } +test "switch arbitrary int size" { + 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 + if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; // TODO + + if (builtin.zig_backend == .stage2_c and builtin.os.tag == .windows) return error.SkipZigTest; // TODO + + try expect(testSwitchArbInt(u64, 0) == 0); + try expect(testSwitchArbInt(u64, 12) == 1); + try expect(testSwitchArbInt(u64, maxInt(u64)) == 2); + try expect(testSwitchArbInt(u64, 5555) == 3); + + try expect(testSwitchArbInt(i64, minInt(i64)) == 0); + try expect(testSwitchArbInt(i64, 12) == 1); + try expect(testSwitchArbInt(i64, maxInt(i64)) == 2); + try expect(testSwitchArbInt(i64, -1000) == 3); + + try expect(testSwitchArbInt(u128, 0) == 0); + try expect(testSwitchArbInt(u128, 12) == 1); + try expect(testSwitchArbInt(u128, maxInt(u128)) == 2); + try expect(testSwitchArbInt(u128, 5555) == 3); + + try expect(testSwitchArbInt(i128, minInt(i128)) == 0); + try expect(testSwitchArbInt(i128, 12) == 1); + try expect(testSwitchArbInt(i128, maxInt(i128)) == 2); + try expect(testSwitchArbInt(i128, -1000) == 3); +} + +fn testSwitchArbInt(comptime T: type, x: T) u32 { + return switch (x) { + minInt(T) => 0, + 10...15 => 1, + maxInt(T) => 2, + else => 3, + }; +} + test "implicit comptime switch" { const x = 3 + 4; const result = switch (x) { @@ -1003,7 +1045,6 @@ test "labeled switch with break" { } test "unlabeled break ignores switch" { - if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO 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_sparc64) return error.SkipZigTest; // TODO |
