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