| ... | ... | @@ -0,0 +1,54 @@ |
| 1 | const std = @import("std"); |
| 2 | const builtin = @import("builtin"); |
| 3 | const expect = std.testing.expect; |
| 4 | |
| 5 | test "wasm integer division" { |
| 6 | // This test is copied from int_div.zig, with additional test cases for @divFloor on floats. |
| 7 | // TODO: Remove this test once the division tests in math.zig and int_div.zig pass with the |
| 8 | // stage2 wasm backend. |
| 9 | if (builtin.zig_backend != .stage2_wasm) return error.SkipZigTest; |
| 10 | |
| 11 | try testDivision(); |
| 12 | try comptime testDivision(); |
| 13 | } |
| 14 | fn testDivision() !void { |
| 15 | try expect(div(u32, 13, 3) == 4); |
| 16 | try expect(div(u64, 13, 3) == 4); |
| 17 | try expect(div(u8, 13, 3) == 4); |
| 18 | |
| 19 | try expect(divFloor(i8, 5, 3) == 1); |
| 20 | try expect(divFloor(i16, -5, 3) == -2); |
| 21 | try expect(divFloor(i64, -0x80000000, -2) == 0x40000000); |
| 22 | try expect(divFloor(i32, 0, -0x80000000) == 0); |
| 23 | try expect(divFloor(i64, -0x40000001, 0x40000000) == -2); |
| 24 | try expect(divFloor(i32, -0x80000000, 1) == -0x80000000); |
| 25 | try expect(divFloor(i32, 10, 12) == 0); |
| 26 | try expect(divFloor(i32, -14, 12) == -2); |
| 27 | try expect(divFloor(i32, -2, 12) == -1); |
| 28 | try expect(divFloor(f32, 56.0, 9.0) == 6.0); |
| 29 | try expect(divFloor(f32, 1053.0, -41.0) == -26.0); |
| 30 | try expect(divFloor(f16, -43.0, 12.0) == -4.0); |
| 31 | try expect(divFloor(f64, -90.0, -9.0) == 10.0); |
| 32 | |
| 33 | try expect(mod(u32, 10, 12) == 10); |
| 34 | try expect(mod(i32, 10, 12) == 10); |
| 35 | try expect(mod(i64, -14, 12) == 10); |
| 36 | try expect(mod(i16, -2, 12) == 10); |
| 37 | try expect(mod(i8, -2, 12) == 10); |
| 38 | |
| 39 | try expect(rem(i32, 10, 12) == 10); |
| 40 | try expect(rem(i32, -14, 12) == -2); |
| 41 | try expect(rem(i32, -2, 12) == -2); |
| 42 | } |
| 43 | fn div(comptime T: type, a: T, b: T) T { |
| 44 | return a / b; |
| 45 | } |
| 46 | fn divFloor(comptime T: type, a: T, b: T) T { |
| 47 | return @divFloor(a, b); |
| 48 | } |
| 49 | fn mod(comptime T: type, a: T, b: T) T { |
| 50 | return @mod(a, b); |
| 51 | } |
| 52 | fn rem(comptime T: type, a: T, b: T) T { |
| 53 | return @rem(a, b); |
| 54 | } |