diff options
| author | Yidi <noob999noob999@gmail.com> | 2024-09-08 11:14:12 +0200 |
|---|---|---|
| committer | Yidi <noob999noob999@gmail.com> | 2024-09-08 11:14:12 +0200 |
| commit | de2c4df728e62c5761edf7d58a30f3c59d7d4d20 (patch) | |
| tree | 46801ee99532d9427010c2a9dac3ea236e76b2db /test/graph/hld.cpp | |
| parent | 9e9c033aa41f786f494cadea43563f83f3e951a3 (diff) | |
add remaining tests
Diffstat (limited to 'test/graph/hld.cpp')
| -rw-r--r-- | test/graph/hld.cpp | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/test/graph/hld.cpp b/test/graph/hld.cpp new file mode 100644 index 0000000..bcd9c8c --- /dev/null +++ b/test/graph/hld.cpp @@ -0,0 +1,83 @@ +#include "../util.h" +#include <graph/hld.cpp> + +struct Seg { // very real segment tree + vector<int> a; + + Seg(int n) : a(n) {} + + void inc(int l, int r) { + for (int i=l; i<r; i++) a[i]++; + } +}; + +void stress_test() { + for (int tries = 0; tries < 100'000; tries++) { + int n = Random::integer<int>(1, 50); + Graph<NoData> g(n); + g.tree(); + + adj.assign(n, {}); + g.forEdges([&](int a, int b){ + adj[a].push_back(b); + adj[b].push_back(a); + }); + + init(Random::integer(0, n)); + + vector<int> a(n); + auto dfs = [&](auto& self, int u, int p, int t) { + if (u == t) { + a[u]++; + return true; + } + for (int v : adj[u]) if (v != p) { + if (self(self, v, u, t)) { + a[u]++; + return true; + } + } + return false; + }; + + Seg seg(n); + + int Q = Random::integer(1, 50); + for (int q=0; q<Q; q++) { + int u = Random::integer(0, n), v = Random::integer(0, n); + for_intervals(u, v, [&](int l, int r) { seg.inc(l, r); }); + dfs(dfs, u, -1, v); + } + for (int i=0; i<n; i++) { + if (seg.a[in[i]] != a[i]) cerr << "WA: expected " << a[i] << " got " << seg.a[in[i]] << FAIL; + } + } + cerr << "tested random tests: " << 100'000 << endl; +} + +constexpr int N = 1'000'000; +void performance_test() { + timer t; + Graph<NoData> g(N); + g.tree(); + adj.assign(N, {}); + g.forEdges([&](int a, int b){ + adj[a].push_back(b); + adj[b].push_back(a); + }); + vector<pair<int, int>> qu(N); + for (auto& [x, y] : qu) x = Random::integer(0, N), y = Random::integer(0, N); + + ll hash = 0; + t.start(); + init(0); + for (auto [x, y] : qu) for_intervals(x, y, [&](int l, int r){ hash += r-l; }); + t.stop(); + if (t.time > 1000) cerr << "too slow: " << t.time << FAIL; + cerr << "tested performance: " << t.time << "ms (hash: " << hash << ")" << endl; +} + +int main() { + stress_test(); + performance_test(); +} |
