authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-19 14:23:12+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-19 14:24:03+01:00
log59df39e949907183e76ac78d7ace07e96f72e8a8
tree1606f01b012850701dbc87ef3d8b6d2f5063f0ca
parentda86839af085bb5921d7f0112fda5ad9536dda10

add integer division tests


2 files changed, 94 insertions(+), 0 deletions(-)

test/behavior.zig+1
......@@ -50,6 +50,7 @@ test {
5050 _ = @import("behavior/tuple.zig");
5151 _ = @import("behavior/type.zig");
5252 _ = @import("behavior/var_args.zig");
53 _ = @import("behavior/int_div.zig");
5354
5455 // tests that don't pass for stage1
5556 if (builtin.zig_backend != .stage1) {
test/behavior/int_div.zig created+93
......@@ -0,0 +1,93 @@
1const std = @import("std");
2const builtin = @import("builtin");
3const expect = std.testing.expect;
4
5test "integer division" {
6 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
7 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
8 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
9 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
10
11 try testDivision();
12 comptime try testDivision();
13}
14fn 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(divExact(u32, 55, 11) == 5);
20 try expect(divExact(i32, -55, 11) == -5);
21 try expect(divExact(i64, -55, 11) == -5);
22 try expect(divExact(i16, -55, 11) == -5);
23
24 try expect(divFloor(i8, 5, 3) == 1);
25 try expect(divFloor(i16, -5, 3) == -2);
26 try expect(divFloor(i64, -0x80000000, -2) == 0x40000000);
27 try expect(divFloor(i32, 0, -0x80000000) == 0);
28 try expect(divFloor(i64, -0x40000001, 0x40000000) == -2);
29 try expect(divFloor(i32, -0x80000000, 1) == -0x80000000);
30 try expect(divFloor(i32, 10, 12) == 0);
31 try expect(divFloor(i32, -14, 12) == -2);
32 try expect(divFloor(i32, -2, 12) == -1);
33
34 try expect(divTrunc(i32, 5, 3) == 1);
35 try expect(divTrunc(i32, -5, 3) == -1);
36 try expect(divTrunc(i32, 9, -10) == 0);
37 try expect(divTrunc(i32, -9, 10) == 0);
38 try expect(divTrunc(i32, 10, 12) == 0);
39 try expect(divTrunc(i32, -14, 12) == -1);
40 try expect(divTrunc(i32, -2, 12) == 0);
41
42 try expect(mod(u32, 10, 12) == 10);
43 try expect(mod(i32, 10, 12) == 10);
44 try expect(mod(i64, -14, 12) == 10);
45 try expect(mod(i16, -2, 12) == 10);
46 try expect(mod(i8, -2, 12) == 10);
47
48 try expect(rem(i32, 10, 12) == 10);
49 try expect(rem(i32, -14, 12) == -2);
50 try expect(rem(i32, -2, 12) == -2);
51
52 comptime {
53 try expect(
54 1194735857077236777412821811143690633098347576 % 508740759824825164163191790951174292733114988 == 177254337427586449086438229241342047632117600,
55 );
56 try expect(
57 @rem(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -177254337427586449086438229241342047632117600,
58 );
59 try expect(
60 1194735857077236777412821811143690633098347576 / 508740759824825164163191790951174292733114988 == 2,
61 );
62 try expect(
63 @divTrunc(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -2,
64 );
65 try expect(
66 @divTrunc(1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == -2,
67 );
68 try expect(
69 @divTrunc(-1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == 2,
70 );
71 try expect(
72 4126227191251978491697987544882340798050766755606969681711 % 10 == 1,
73 );
74 }
75}
76fn div(comptime T: type, a: T, b: T) T {
77 return a / b;
78}
79fn divExact(comptime T: type, a: T, b: T) T {
80 return @divExact(a, b);
81}
82fn divFloor(comptime T: type, a: T, b: T) T {
83 return @divFloor(a, b);
84}
85fn divTrunc(comptime T: type, a: T, b: T) T {
86 return @divTrunc(a, b);
87}
88fn mod(comptime T: type, a: T, b: T) T {
89 return @mod(a, b);
90}
91fn rem(comptime T: type, a: T, b: T) T {
92 return @rem(a, b);
93}