summaryrefslogtreecommitdiff
path: root/test/string/trie.cpp
blob: 1ea5b1a56a808220c23a8f835ea473c730ff013a (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "../util.h"
#include <string/trie.cpp>

void stress_test() {
	multiset<vector<int>> naive;
	ll queries = 0;
	ll deleted = 0;
	for (int tries = 0; tries < 100'000; tries++) {
		{
			int n = Random::integer<int>(1, 20);
			auto s = Random::integers<int>(n, 0, 2);
			insert(s);
			naive.insert(s);
		}
		{
			int n = Random::integer<int>(1, 20);
			auto s = Random::integers<int>(n, 0, 2);
			bool got = erase(s);
			auto it = naive.find(s);
			bool expected = it != naive.end();
			if (expected) naive.erase(it);
			if (got != expected) cerr << "error" << FAIL;
			queries++;
			if (got) deleted++;
		}
	}
	cerr << "tested random queries: " << queries << " (" << deleted << ")" << endl;
}

constexpr int N = 10'000;
void performance_test() {
	timer t;
	trie = {node()};
	hash_t hash = 0;
	for (int tries = 0; tries < N; tries++) {
		{
			int n = Random::integer<int>(1, 2000);
			auto s = Random::integers<int>(n, 0, 2);
			t.start();
			insert(s);
			t.stop();
		}
		{
			int n = Random::integer<int>(1, 2000);
			auto s = Random::integers<int>(n, 0, 2);
			t.start();
			hash += erase(s);
			t.stop();
		}
	}
	if (t.time > 500) cerr << "too slow: " << t.time << FAIL;
	cerr << "tested performance: " << t.time << "ms (hash: " << hash << ")" << endl;
}

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