1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
#if 0
cc=${CC:-g++}
bin="$(mktemp)"
${cc} -x c++ -std=c++20 -o "$bin" "$(realpath $0)"
"$bin"
exit 0
#endif
#include "zmodn.h"
#include <concepts>
#include <functional>
#include <iostream>
#include <optional>
template<typename S, typename T>
requires std::convertible_to<S, T> || std::convertible_to<T, S>
void assert_equal(S actual, T expected) {
if (actual != expected) {
std::cout << "Error!" << std::endl;
std::cout << "Expected: " << expected << std::endl;
std::cout << "But got: " << actual << std::endl;
exit(1);
}
}
class Test {
public:
std::string name;
std::function<void()> f;
} tests[] = {
{
.name = "Constructor 2 mod 3",
.f = []() {
Zmod<3> two = Zmod<3>(2);
assert_equal(two.toint(), INT64_C(2));
}
},
{
.name = "Constructor -7 mod 3",
.f = []() {
Zmod<3> z = -7;
assert_equal(z, Zmod<3>(2));
}
},
{
.name = "1+1 mod 2",
.f = []() {
auto oneplusone = Zmod<2>(1) + Zmod<2>(1);
assert_equal(oneplusone, Zmod<2>(0));
}
},
{
.name = "2 -= 5 (mod 4)",
.f = []() {
Zmod<4> z = 2;
auto diff = (z -= 5);
assert_equal(z, Zmod<4>(1));
assert_equal(diff, Zmod<4>(1));
}
},
{
.name = "Inverse of 0 mod 2",
.f = []() {
Zmod<2> z = 0;
auto inv = z.inverse();
assert_equal(inv.has_value(), false);
}
},
{
.name = "Inverse of 1 mod 2",
.f = []() {
Zmod<2> z = 1;
auto inv = z.inverse();
assert_equal(inv.has_value(), true);
assert_equal(inv.value(), Zmod<2>(1));
}
},
{
.name = "Inverse of 5 mod 7",
.f = []() {
Zmod<7> z = 5;
auto inv = z.inverse();
assert_equal(inv.has_value(), true);
assert_equal(inv.value(), Zmod<7>(3));
}
},
{
.name = "Inverse of 4 mod 12",
.f = []() {
Zmod<12> z = 4;
auto inv = z.inverse();
assert_equal(inv.has_value(), false);
}
},
{
.name = "4 / 7 (mod 12)",
.f = []() {
Zmod<12> n = 4;
Zmod<12> d = 7;
auto inv = n / d;
assert_equal(inv.has_value(), true);
assert_equal(inv.value(), Zmod<12>(4));
}
},
{
.name = "4 /= 7 (mod 12)",
.f = []() {
Zmod<12> n = 4;
Zmod<12> d = 7;
auto inv = (n /= d);
assert_equal(inv.has_value(), true);
assert_equal(inv.value(), Zmod<12>(4));
assert_equal(n, Zmod<12>(4));
}
},
{
.name = "Multiplication overflow",
.f = []() {
Zmod<10> n = 8;
Zmod<10> m = 9;
auto prod = m * n;
assert_equal(prod.toint(), 2);
}
},
{
.name = "Multiplication and assignment overflow",
.f = []() {
Zmod<10> n = 8;
Zmod<10> m = 9;
n *= m;
assert_equal(n.toint(), 2);
}
},
{
.name = "2^12 mod 154",
.f = []() {
Zmod<154> b = 2;
auto p = b ^ 12;
assert_equal(p, 92);
}
},
{
.name = "4 / 2 mod 6",
.f = []() {
Zmod<6> four = 4;
Zmod<6> two = 2;
auto d = four / two;
assert_equal(d.has_value(), true);
assert_equal(d.value(), 2);
}
},
};
int main() {
for (auto t : tests) {
std::cout << t.name << ": ";
t.f();
std::cout << "OK" << std::endl;
}
std::cout << "All tests passed" << std::endl;
return 0;
}
|