master
1const std = @import("std");
2const builtin = @import("builtin");
3const minInt = std.math.minInt;
4const maxInt = std.math.maxInt;
5const expect = std.testing.expect;
6
7test "wrapping add" {
8 if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
9 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
10
11 const S = struct {
12 fn doTheTest() !void {
13 try testWrapAdd(i8, -3, 10, 7);
14 try testWrapAdd(i8, -128, -128, 0);
15 try testWrapAdd(i2, 1, 1, -2);
16 try testWrapAdd(i64, maxInt(i64), 1, minInt(i64));
17 try testWrapAdd(i128, maxInt(i128), -maxInt(i128), 0);
18 try testWrapAdd(i128, minInt(i128), maxInt(i128), -1);
19 try testWrapAdd(i8, 127, 127, -2);
20 try testWrapAdd(u8, 3, 10, 13);
21 try testWrapAdd(u8, 255, 255, 254);
22 try testWrapAdd(u2, 3, 2, 1);
23 try testWrapAdd(u3, 7, 1, 0);
24 try testWrapAdd(u128, maxInt(u128), 1, minInt(u128));
25 }
26
27 fn testWrapAdd(comptime T: type, lhs: T, rhs: T, expected: T) !void {
28 try expect((lhs +% rhs) == expected);
29
30 var x = lhs;
31 x +%= rhs;
32 try expect(x == expected);
33 }
34 };
35
36 try S.doTheTest();
37 try comptime S.doTheTest();
38
39 try comptime S.testWrapAdd(comptime_int, 0, 0, 0);
40 try comptime S.testWrapAdd(comptime_int, 3, 2, 5);
41 try comptime S.testWrapAdd(comptime_int, 651075816498665588400716961808225370057, 468229432685078038144554201546849378455, 1119305249183743626545271163355074748512);
42 try comptime S.testWrapAdd(comptime_int, 7, -593423721213448152027139550640105366508, -593423721213448152027139550640105366501);
43}
44
45test "wrapping subtraction" {
46 if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
47 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
48
49 const S = struct {
50 fn doTheTest() !void {
51 try testWrapSub(i8, -3, 10, -13);
52 try testWrapSub(i8, -128, -128, 0);
53 try testWrapSub(i8, -1, 127, -128);
54 try testWrapSub(i64, minInt(i64), 1, maxInt(i64));
55 try testWrapSub(i128, maxInt(i128), -1, minInt(i128));
56 try testWrapSub(i128, minInt(i128), -maxInt(i128), -1);
57 try testWrapSub(u8, 10, 3, 7);
58 try testWrapSub(u8, 0, 255, 1);
59 try testWrapSub(u5, 0, 31, 1);
60 try testWrapSub(u128, 0, maxInt(u128), 1);
61 }
62
63 fn testWrapSub(comptime T: type, lhs: T, rhs: T, expected: T) !void {
64 try expect((lhs -% rhs) == expected);
65
66 var x = lhs;
67 x -%= rhs;
68 try expect(x == expected);
69 }
70 };
71
72 try S.doTheTest();
73 try comptime S.doTheTest();
74
75 try comptime S.testWrapSub(comptime_int, 0, 0, 0);
76 try comptime S.testWrapSub(comptime_int, 3, 2, 1);
77 try comptime S.testWrapSub(comptime_int, 651075816498665588400716961808225370057, 468229432685078038144554201546849378455, 182846383813587550256162760261375991602);
78 try comptime S.testWrapSub(comptime_int, 7, -593423721213448152027139550640105366508, 593423721213448152027139550640105366515);
79}
80
81test "wrapping multiplication" {
82 if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
83 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
84
85 // TODO: once #9660 has been solved, remove this line
86 if (builtin.cpu.arch.isWasm()) return error.SkipZigTest;
87
88 const S = struct {
89 fn doTheTest() !void {
90 try testWrapMul(i8, -3, 10, -30);
91 try testWrapMul(i4, 2, 4, -8);
92 try testWrapMul(i8, 2, 127, -2);
93 try testWrapMul(i8, -128, -128, 0);
94 try testWrapMul(i8, maxInt(i8), maxInt(i8), 1);
95 try testWrapMul(i16, maxInt(i16), -1, minInt(i16) + 1);
96 try testWrapMul(i128, maxInt(i128), -1, minInt(i128) + 1);
97 try testWrapMul(i128, minInt(i128), -1, minInt(i128));
98 try testWrapMul(u8, 10, 3, 30);
99 try testWrapMul(u8, 2, 255, 254);
100 try testWrapMul(u128, maxInt(u128), maxInt(u128), 1);
101 }
102
103 fn testWrapMul(comptime T: type, lhs: T, rhs: T, expected: T) !void {
104 try expect((lhs *% rhs) == expected);
105
106 var x = lhs;
107 x *%= rhs;
108 try expect(x == expected);
109 }
110 };
111
112 try S.doTheTest();
113 try comptime S.doTheTest();
114
115 try comptime S.testWrapMul(comptime_int, 0, 0, 0);
116 try comptime S.testWrapMul(comptime_int, 3, 2, 6);
117 try comptime S.testWrapMul(comptime_int, 651075816498665588400716961808225370057, 468229432685078038144554201546849378455, 304852860194144160265083087140337419215516305999637969803722975979232817921935);
118 try comptime S.testWrapMul(comptime_int, 7, -593423721213448152027139550640105366508, -4153966048494137064189976854480737565556);
119}