Skip to content

Commit 134ffed

Browse files
Chilleesimonlindholm
authored andcommitted
Standardized fuzztests for number theory code (kth-competitive-programming#83)
1 parent dcd3d95 commit 134ffed

File tree

9 files changed

+25
-68
lines changed

9 files changed

+25
-68
lines changed

fuzz-tests/number-theory/Eratosthenes.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,9 @@ struct prime_sieve {
4949
isprime[ii] &= (uchar)~(1<<ss);
5050
} } } } };
5151

52-
// const int MAX_PR = 100000000;
53-
54-
#if 1
5552

5653
#include "../../content/number-theory/eratosthenes.h"
5754

58-
#else
59-
60-
bitset<MAX_PR> isprime;
61-
vi eratosthenes_sieve(int lim) {
62-
isprime.set(); isprime[0] = isprime[1] = 0;
63-
for (int i = 4; i < lim; i += 2) isprime[i] = 0;
64-
for (int i = 3; i*i < lim; i += 2) if (isprime[i])
65-
for (int j = i*i; j < lim; j += i*2) isprime[j] = 0;
66-
vi pr;
67-
rep(i,2,lim) if (isprime[i]) pr.push_back(i);
68-
return pr;
69-
}
70-
71-
#endif
72-
7355
int main(int argc, char** argv) {
7456
ll s = 0, s2 = 0;
7557
prime_sieve ps(MAX_PR);

fuzz-tests/number-theory/FracBinarySearch.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ int main() {
3434
assert(best.second == f.q);
3535
}
3636
}
37+
cout<<"Tests passed!"<<endl;
3738
return 0;
3839
}

fuzz-tests/number-theory/MillerRabin.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ int main() {
6464
n *= 1237618231ULL;
6565
if (oldIsPrime(n) != isPrime(n)) {
6666
cout << "differs from old for " << n << endl;
67+
assert(false);
6768
}
6869
}
70+
cout<<"Tests passed"<<endl;
6971
}

fuzz-tests/number-theory/ModInverse.cpp

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,16 @@ typedef long long ll;
99
typedef pair<int, int> pii;
1010
typedef vector<int> vi;
1111

12-
const ll M = 1e9+7, LIM = 200000;
13-
14-
bool isPrime(int x) {
15-
if (x <= 1) return false;
16-
for (int i = 2; i*i <= x; ++i) {
17-
if (x % i == 0) return false;
18-
}
19-
return true;
12+
ll modpow(ll a, ll e, ll mod) {
13+
if (e == 0) return 1;
14+
ll x = modpow(a * a % mod, e >> 1, mod);
15+
return e & 1 ? x * a % mod : x;
2016
}
2117

22-
int ar[5] = {1,2,3,4,5};
23-
2418
int main() {
25-
vector<ll> inv(LIM, 1);
26-
for (int M = 1; M <= 1000; ++M) {
27-
if (!isPrime(M)) continue;
28-
inv.assign(LIM, 123123);
29-
inv[0] = -1000;
30-
inv[1] = 1;
31-
rep(i,2,LIM)
32-
inv[i] = M - (M / i) * inv[M % i] % M;
33-
rep(i,0,M) {
34-
bool works = (i * inv[i] % M == 1);
35-
bool relp = (__gcd(i, M) == 1);
36-
assert(works == relp);
37-
}
38-
}
19+
#include "../../content/number-theory/ModInverse.h"
20+
for (int i=1; i<10000; i++)
21+
assert(inv[i] == modpow(i, mod-2, mod));
22+
cout<<"Tests pass!"<<endl;
3923
}
4024

41-
ll inv[LIM] = {-10000, 1};
42-
int main2() {
43-
rep(i,2,LIM) inv[i] = M - (M / i) * inv[M % i] % M;
44-
cout << inv[0] << ' ' << inv[1] << ' ' << inv[2] << ' ' << inv[3] << endl;
45-
return 0;
46-
}

fuzz-tests/number-theory/ModSqrt.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ int main() {
2929
}
3030
next:;
3131
}
32+
cout<<"Tests passed!"<<endl;
3233
}

fuzz-tests/number-theory/ModSum.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void compare() {
4444
ll b = modsum_naive(to, c, k, m);
4545
if (a != b) {
4646
cout << "differ! " << to << ' ' << c << ' ' << k << ' ' << m << ": " << a << " vs " << b << endl;
47-
return;
47+
assert(false);
4848
}
4949
}
5050
}
@@ -61,7 +61,7 @@ void compare2() {
6161
ll b = divsum_naive(to, c, k, m);
6262
if (a != b) {
6363
cout << "differ! " << to << ' ' << c << ' ' << k << ' ' << m << ": " << a << " vs " << b << endl;
64-
return;
64+
assert(false);
6565
}
6666
}
6767
}
@@ -71,18 +71,14 @@ void compare2() {
7171

7272
int main() {
7373
compare(); compare2();
74-
cout << modsum((ll)1e18, 1, 2, 3) << endl;
75-
cout << (ll)1e18 << endl;
74+
assert(modsum((ll)1e18, 1, 2, 3) == (ll)1e18);
7675
rep(i,0,50) {
7776
ll t = (ll)rand() << 3;
7877
ll c = (ll)rand() << 2;
7978
ll k = (ll)rand() << 2;
8079
ll m = (ll)rand() >> 2;
81-
cout << modsum(t, c, k, m) / (long double)(m/2 * t) << endl;
80+
assert(abs(modsum(t, c, k, m) / ((long double)m/2 * t) - 1)<1e-5);
8281
}
83-
cout << modsum(1000000000000000000LL, 11231, 102917231231LL, 1236712312LL) << endl;
84-
// rep(i,0,1000000) {
85-
// modsum(1000000000000000000LL, 11231, 102917231231LL, 1236712312LL);
86-
// }
82+
cout<<"Tests passed!"<<endl;
8783
return 0;
8884
}

fuzz-tests/number-theory/ModularArithmetic.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ int main() {
2020
assert((mc * mb).x == a);
2121
}
2222
Mod a = 2;
23-
rep(i,0,17) {
24-
cout << i << ": " << (a ^ i).x << endl;
23+
ll cur=1;
24+
rep(i, 0, 17) {
25+
assert((a ^ i).x == cur);
26+
cur = (cur * 2) % mod;
27+
// cout << i << ": " << (a ^ i).x << endl;
2528
}
29+
cout<<"Tests passed!"<<endl;
2630
}

fuzz-tests/number-theory/continuedfractions.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@ int main() {
3636
assert(best.second == pa.second);
3737
}
3838
}
39-
return 0;
40-
}
41-
42-
int main2() {
43-
double x;
44-
ll N = 1000000;
45-
cin >> x >> N;
46-
auto pa = approximate(x, N);
47-
cout << pa.first << '/' << pa.second << endl;
39+
cout<<"Tests passed!"<<endl;
4840
return 0;
4941
}

fuzz-tests/numerical/berlekampmassey.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ int main() {
6464
});
6565
});
6666
}
67+
cout<<"Tests passed!"<<endl;
6768
return 0;
6869
}
6970

0 commit comments

Comments
 (0)