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 | |
| 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')
| -rw-r--r-- | test/behavior/switch.zig | 43 | ||||
| -rw-r--r-- | test/behavior/switch_loop.zig | 7 |
2 files changed, 42 insertions, 8 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 diff --git a/test/behavior/switch_loop.zig b/test/behavior/switch_loop.zig index 6bc268e390..b0498d89ce 100644 --- a/test/behavior/switch_loop.zig +++ b/test/behavior/switch_loop.zig @@ -3,7 +3,6 @@ const std = @import("std"); const expect = std.testing.expect; test "simple switch loop" { - 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 @@ -28,7 +27,6 @@ test "simple switch loop" { } test "switch loop with ranges" { - 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 @@ -50,7 +48,6 @@ test "switch loop with ranges" { } test "switch loop on enum" { - 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 @@ -75,7 +72,6 @@ test "switch loop on enum" { } test "switch loop with error set" { - 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 @@ -100,7 +96,6 @@ test "switch loop with error set" { } test "switch loop on tagged union" { - 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 @@ -134,7 +129,6 @@ test "switch loop on tagged union" { } test "switch loop dispatching instructions" { - 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 @@ -185,7 +179,6 @@ test "switch loop dispatching instructions" { } test "switch loop with pointer capture" { - 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 |
