Skip to content

Commit 9a107ff

Browse files
committed
Add CRT test
1 parent 12af084 commit 9a107ff

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

fuzz-tests/number-theory/CRT.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
#define rep(i, a, b) for(int i = a; i < int(b); ++i)
5+
#define trav(a, v) for(auto& a : v)
6+
#define all(x) x.begin(), x.end()
7+
#define sz(x) (int)(x).size()
8+
9+
typedef long long ll;
10+
typedef pair<int, int> pii;
11+
typedef vector<int> vi;
12+
13+
#include "../../content/number-theory/CRT.h"
14+
15+
ll rmod(ll a, ll b) { return (a % b + b) % b; }
16+
17+
std::mt19937_64 rng(2);
18+
19+
ll randExp() {
20+
int e = rand() % 63; // 64
21+
return uniform_int_distribution<ll>(0, (ll)((1ULL << e) - 1))(rng);
22+
}
23+
24+
int main(int argc, char** argv) {
25+
rep(it,0,10000000) {
26+
ll a = randExp() * (rand() % 2 ? 1 : -1);
27+
ll b = randExp() * (rand() % 2 ? 1 : -1);
28+
ll m = randExp() + 1;
29+
ll n = randExp() + 1;
30+
ll g = __gcd(m, n);
31+
if (n * (__int128_t)m > LLONG_MAX) continue;
32+
if (n * (__int128_t)m > (1LL << 62) && (abs(a) > m || abs(b) > n)) continue;
33+
if ((a - b) % g == 0) {
34+
ll r = crt(a, m, b, n);
35+
if (rmod(r, m) != rmod(a, m) || rmod(r, n) != rmod(b, n)) {
36+
cout << a << endl;
37+
cout << b << endl;
38+
cout << m << endl;
39+
cout << n << endl;
40+
}
41+
assert(rmod(r, m) == rmod(a, m));
42+
assert(rmod(r, n) == rmod(b, n));
43+
if (-m < a && a < m && -n < b && b < n) {
44+
assert(0 <= r);
45+
assert(r < m*n/g);
46+
}
47+
}
48+
}
49+
cout<<"Tests passed!"<<endl;
50+
}

0 commit comments

Comments
 (0)