diff options
| author | Pavel Verigo <paul.verigo@gmail.com> | 2024-07-18 17:54:44 +0200 |
|---|---|---|
| committer | Pavel Verigo <paul.verigo@gmail.com> | 2024-07-18 17:54:44 +0200 |
| commit | dc3176d6287955d2ecdaaa26f46f5577d5316d36 (patch) | |
| tree | 3bc1d7db6f6cc4555f076e2d506a3165c2f19ee9 /test | |
| parent | d1bd9518f97abc9ab80795962b8e0dfd8e4d8768 (diff) | |
| download | zig-dc3176d6287955d2ecdaaa26f46f5577d5316d36.tar.gz zig-dc3176d6287955d2ecdaaa26f46f5577d5316d36.zip | |
stage2-wasm: enhance add/subWithOverflow
Added behavior tests to verify implementation
Diffstat (limited to 'test')
| -rw-r--r-- | test/behavior/math.zig | 196 |
1 files changed, 112 insertions, 84 deletions
diff --git a/test/behavior/math.zig b/test/behavior/math.zig index 73b9b2f62e..7472d2f990 100644 --- a/test/behavior/math.zig +++ b/test/behavior/math.zig @@ -828,56 +828,72 @@ test "128-bit multiplication" { } } +fn testAddWithOverflow(comptime T: type, a: T, b: T, add: T, bit: u1) !void { + const ov = @addWithOverflow(a, b); + try expect(ov[0] == add); + try expect(ov[1] == bit); +} + test "@addWithOverflow" { 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_riscv64) return error.SkipZigTest; - { - var a: u8 = 250; - _ = &a; - const ov = @addWithOverflow(a, 100); - try expect(ov[0] == 94); - try expect(ov[1] == 1); - } - { - var a: u8 = 100; - _ = &a; - const ov = @addWithOverflow(a, 150); - try expect(ov[0] == 250); - try expect(ov[1] == 0); - } - { - var a: u8 = 200; - _ = &a; - var b: u8 = 99; - var ov = @addWithOverflow(a, b); - try expect(ov[0] == 43); - try expect(ov[1] == 1); - b = 55; - ov = @addWithOverflow(a, b); - try expect(ov[0] == 255); - try expect(ov[1] == 0); - } + try testAddWithOverflow(u8, 250, 100, 94, 1); + try testAddWithOverflow(u8, 100, 150, 250, 0); - { - var a: usize = 6; - var b: usize = 6; - _ = .{ &a, &b }; - const ov = @addWithOverflow(a, b); - try expect(ov[0] == 12); - try expect(ov[1] == 0); - } + try testAddWithOverflow(u8, 200, 99, 43, 1); + try testAddWithOverflow(u8, 200, 55, 255, 0); - { - var a: isize = -6; - var b: isize = -6; - _ = .{ &a, &b }; - const ov = @addWithOverflow(a, b); - try expect(ov[0] == -12); - try expect(ov[1] == 0); - } + try testAddWithOverflow(usize, 6, 6, 12, 0); + try testAddWithOverflow(usize, maxInt(usize), 6, 5, 1); + + try testAddWithOverflow(isize, -6, -6, -12, 0); + try testAddWithOverflow(isize, minInt(isize), -6, maxInt(isize) - 5, 1); +} + +test "@addWithOverflow > 64 bits" { + 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_riscv64) return error.SkipZigTest; + + try testAddWithOverflow(u65, 4, 105, 109, 0); + try testAddWithOverflow(u65, 1000, 100, 1100, 0); + try testAddWithOverflow(u65, 100, maxInt(u65) - 99, 0, 1); + try testAddWithOverflow(u65, maxInt(u65), maxInt(u65), maxInt(u65) - 1, 1); + try testAddWithOverflow(u65, maxInt(u65) - 1, maxInt(u65), maxInt(u65) - 2, 1); + try testAddWithOverflow(u65, maxInt(u65), maxInt(u65) - 1, maxInt(u65) - 2, 1); + + try testAddWithOverflow(u128, 4, 105, 109, 0); + try testAddWithOverflow(u128, 1000, 100, 1100, 0); + try testAddWithOverflow(u128, 100, maxInt(u128) - 99, 0, 1); + try testAddWithOverflow(u128, maxInt(u128), maxInt(u128), maxInt(u128) - 1, 1); + try testAddWithOverflow(u128, maxInt(u128) - 1, maxInt(u128), maxInt(u128) - 2, 1); + try testAddWithOverflow(u128, maxInt(u128), maxInt(u128) - 1, maxInt(u128) - 2, 1); + + try testAddWithOverflow(i65, 4, -105, -101, 0); + try testAddWithOverflow(i65, 1000, 100, 1100, 0); + try testAddWithOverflow(i65, minInt(i65), 1, minInt(i65) + 1, 0); + try testAddWithOverflow(i65, maxInt(i65), minInt(i65), -1, 0); + try testAddWithOverflow(i65, minInt(i65), maxInt(i65), -1, 0); + try testAddWithOverflow(i65, maxInt(i65), -2, maxInt(i65) - 2, 0); + try testAddWithOverflow(i65, maxInt(i65), maxInt(i65), -2, 1); + try testAddWithOverflow(i65, minInt(i65), minInt(i65), 0, 1); + try testAddWithOverflow(i65, maxInt(i65) - 1, maxInt(i65), -3, 1); + try testAddWithOverflow(i65, maxInt(i65), maxInt(i65) - 1, -3, 1); + + try testAddWithOverflow(i128, 4, -105, -101, 0); + try testAddWithOverflow(i128, 1000, 100, 1100, 0); + try testAddWithOverflow(i128, minInt(i128), 1, minInt(i128) + 1, 0); + try testAddWithOverflow(i128, maxInt(i128), minInt(i128), -1, 0); + try testAddWithOverflow(i128, minInt(i128), maxInt(i128), -1, 0); + try testAddWithOverflow(i128, maxInt(i128), -2, maxInt(i128) - 2, 0); + try testAddWithOverflow(i128, maxInt(i128), maxInt(i128), -2, 1); + try testAddWithOverflow(i128, minInt(i128), minInt(i128), 0, 1); + try testAddWithOverflow(i128, maxInt(i128) - 1, maxInt(i128), -3, 1); + try testAddWithOverflow(i128, maxInt(i128), maxInt(i128) - 1, -3, 1); } test "small int addition" { @@ -1265,56 +1281,68 @@ test "@mulWithOverflow u256" { } } +fn testSubWithOverflow(comptime T: type, a: T, b: T, sub: T, bit: u1) !void { + const ov = @subWithOverflow(a, b); + try expect(ov[0] == sub); + try expect(ov[1] == bit); +} + test "@subWithOverflow" { 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 - { - var a: u8 = 1; - _ = &a; - const ov = @subWithOverflow(a, 2); - try expect(ov[0] == 255); - try expect(ov[1] == 1); - } - { - var a: u8 = 1; - _ = &a; - const ov = @subWithOverflow(a, 1); - try expect(ov[0] == 0); - try expect(ov[1] == 0); - } + try testSubWithOverflow(u8, 1, 2, 255, 1); + try testSubWithOverflow(u8, 1, 1, 0, 0); - { - var a: u8 = 1; - _ = &a; - var b: u8 = 2; - var ov = @subWithOverflow(a, b); - try expect(ov[0] == 255); - try expect(ov[1] == 1); - b = 1; - ov = @subWithOverflow(a, b); - try expect(ov[0] == 0); - try expect(ov[1] == 0); - } + try testSubWithOverflow(u16, 10000, 10002, 65534, 1); + try testSubWithOverflow(u16, 10000, 9999, 1, 0); - { - var a: usize = 6; - var b: usize = 6; - _ = .{ &a, &b }; - const ov = @subWithOverflow(a, b); - try expect(ov[0] == 0); - try expect(ov[1] == 0); - } + try testSubWithOverflow(usize, 6, 6, 0, 0); + try testSubWithOverflow(usize, 6, 7, maxInt(usize), 1); + try testSubWithOverflow(isize, -6, -6, 0, 0); + try testSubWithOverflow(isize, minInt(isize), 6, maxInt(isize) - 5, 1); +} - { - var a: isize = -6; - var b: isize = -6; - _ = .{ &a, &b }; - const ov = @subWithOverflow(a, b); - try expect(ov[0] == 0); - try expect(ov[1] == 0); - } +test "@subWithOverflow > 64 bits" { + 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_riscv64) return error.SkipZigTest; + + try testSubWithOverflow(u65, 4, 105, maxInt(u65) - 100, 1); + try testSubWithOverflow(u65, 1000, 100, 900, 0); + try testSubWithOverflow(u65, maxInt(u65), maxInt(u65), 0, 0); + try testSubWithOverflow(u65, maxInt(u65) - 1, maxInt(u65), maxInt(u65), 1); + try testSubWithOverflow(u65, maxInt(u65), maxInt(u65) - 1, 1, 0); + + try testSubWithOverflow(u128, 4, 105, maxInt(u128) - 100, 1); + try testSubWithOverflow(u128, 1000, 100, 900, 0); + try testSubWithOverflow(u128, maxInt(u128), maxInt(u128), 0, 0); + try testSubWithOverflow(u128, maxInt(u128) - 1, maxInt(u128), maxInt(u128), 1); + try testSubWithOverflow(u128, maxInt(u128), maxInt(u128) - 1, 1, 0); + + try testSubWithOverflow(i65, 4, 105, -101, 0); + try testSubWithOverflow(i65, 1000, 100, 900, 0); + try testSubWithOverflow(i65, maxInt(i65), maxInt(i65), 0, 0); + try testSubWithOverflow(i65, minInt(i65), minInt(i65), 0, 0); + try testSubWithOverflow(i65, maxInt(i65) - 1, maxInt(i65), -1, 0); + try testSubWithOverflow(i65, maxInt(i65), maxInt(i65) - 1, 1, 0); + try testSubWithOverflow(i65, minInt(i65), 1, maxInt(i65), 1); + try testSubWithOverflow(i65, maxInt(i65), minInt(i65), -1, 1); + try testSubWithOverflow(i65, minInt(i65), maxInt(i65), 1, 1); + try testSubWithOverflow(i65, maxInt(i65), -2, minInt(i65) + 1, 1); + + try testSubWithOverflow(i128, 4, 105, -101, 0); + try testSubWithOverflow(i128, 1000, 100, 900, 0); + try testSubWithOverflow(i128, maxInt(i128), maxInt(i128), 0, 0); + try testSubWithOverflow(i128, minInt(i128), minInt(i128), 0, 0); + try testSubWithOverflow(i128, maxInt(i128) - 1, maxInt(i128), -1, 0); + try testSubWithOverflow(i128, maxInt(i128), maxInt(i128) - 1, 1, 0); + try testSubWithOverflow(i128, minInt(i128), 1, maxInt(i128), 1); + try testSubWithOverflow(i128, maxInt(i128), minInt(i128), -1, 1); + try testSubWithOverflow(i128, minInt(i128), maxInt(i128), 1, 1); + try testSubWithOverflow(i128, maxInt(i128), -2, minInt(i128) + 1, 1); } test "@shlWithOverflow" { |
