summaryrefslogtreecommitdiff
path: root/test/string/trie.cpp
diff options
context:
space:
mode:
authorGloria Mundi <gloria@gloria-mundi.eu>2024-11-16 01:24:14 +0100
committerGloria Mundi <gloria@gloria-mundi.eu>2024-11-16 01:24:14 +0100
commit98567ec798aa8ca2cfbcb85c774dd470f30e30d4 (patch)
tree5113d5cc24d1ad5f93810b6442ce584a36950dc8 /test/string/trie.cpp
parentad3856a6b766087df0036de0b556f4700a6498c9 (diff)
parent8d11c6c8213f46f0fa19826917c255edd5d43cb1 (diff)
mzuenni tests
Diffstat (limited to 'test/string/trie.cpp')
-rw-r--r--test/string/trie.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/test/string/trie.cpp b/test/string/trie.cpp
new file mode 100644
index 0000000..45d89cf
--- /dev/null
+++ b/test/string/trie.cpp
@@ -0,0 +1,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();
+ performance_test();
+}