summaryrefslogtreecommitdiff
path: root/test/math/recover.cpp
blob: 72853e5b61bd27510fef04779903ca39945ccddc (plain)
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
#include "../util.h"
#include <other/recover.cpp>
#include <math/shortModInv.cpp>

void stress_test() {
	ll queries = 0;
	timer t;
	for (int i = 0; i < 500; i++) {
		ll p = Random::prime<ll>(10000);
		for (ll j = 0; 2*j*j < p; j++) {
			for (ll b = 1; 2*b*b < p; b++) {
				if (gcd(j, b) != 1) continue;
				for (ll a : {-j, j}) {
					ll c = a * multInv(b, p);
					
					t.start();
					auto [x, y] = recover(c, p);
					t.stop();

					if (a != x || b != y) cerr << "got: " << x << "/" << y << ", expected: " << a << "/" << b << FAIL;
					queries++;
				}
			}
		}
		for (ll c = 0; c < p; c++) {
			t.start();
			auto [x, y] = recover(c, p);
			t.stop();

			if (y < 0) continue;
			if (y == 0) cerr << "error: y=0" << FAIL;
			ll got = (((x * multInv(y, p)) % p) + p) % p;
			if (got != c) cerr << "got: " << got << ", expected: " << c << FAIL;
			queries++;
		}
	}
	cerr << "tested random queries: " << queries << endl;
	if (t.time > 500) cerr << "too slow: " << t.time << FAIL;
	cerr << "tested performance: " << t.time << "ms" << endl;
}

int main() {
	stress_test();
}