summaryrefslogtreecommitdiff
path: root/test/math/piLehmer.cpp
blob: bfc714fc202e1497458d2f01b656667000370f97 (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
#include "../util.h"
#include <math/primeSieve.cpp>
namespace legendre {
	#include <math/piLegendre.cpp>
}
namespace lehmer {
	#include <math/piLehmer.cpp>
}

void stress_test() {
	int queries = 0;
	for (int i = 0; i < 1'000; i++) {
		ll x = Random::integer<ll>(0, 1'000'000'000);
		auto got = lehmer::pi(x);
		auto expected = legendre::pi(x);
		if (got != expected) cerr << "got: " << got << ", expected: " << expected << FAIL;
		queries++;
	}
	cerr << "tested random queries: " << queries << endl;
}

void performance_test() {
	timer t;
	hash_t hash = 0;
	t.start();
	lehmer::init();
	t.stop();
	for (int i = 0; i < 1; i++)  {
		ll x = Random::integer<ll>(0, 1000'000'000'000);
		t.start();
		hash += lehmer::pi(x);
		t.stop();
	}
	if (t.time > 1500) cerr << "too slow: " << t.time << FAIL;
	cerr << "tested performance: " << t.time << "ms (hash: " << hash << ")" << endl;
}

int main() {
	if (!sanitize) performance_test();
	if (sanitize) lehmer::init();
	stress_test();
}