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/reroot.cpp | |
| parent | 9e9c033aa41f786f494cadea43563f83f3e951a3 (diff) | |
add remaining tests
Diffstat (limited to 'test/graph/reroot.cpp')
| -rw-r--r-- | test/graph/reroot.cpp | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/test/graph/reroot.cpp b/test/graph/reroot.cpp new file mode 100644 index 0000000..d5043b4 --- /dev/null +++ b/test/graph/reroot.cpp @@ -0,0 +1,59 @@ +#include "../util.h" +#include <graph/reroot.cpp> + +void stress_test() { + for (int tries = 0; tries < 50'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].emplace_back(b, Random::integer(0, 10)); + adj[b].emplace_back(a, Random::integer(0, 10)); + }); + + Reroot re; + auto ans = re.solve(); + + auto dfs = [&](auto& self, int u, int p) -> ll { + ll val = 0; + for (auto [v, w] : adj[u]) if (v != p) { + val ^= ((v ^ u) + self(self, v, u)) * w % MOD; + } + return u ^ val; + }; + for (int i=0; i<n; i++) { + if (ans[i] != dfs(dfs, i, -1)) { + cerr << "WA expected " << dfs(dfs, i, -1) << " got " << ans[i] << FAIL; + } + } + } + cerr << "tested random 50'000 tests" << 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].emplace_back(b, Random::integer(0, (int)1e9)); + adj[b].emplace_back(a, Random::integer(0, (int)1e9)); + }); + + ll hash = 0; + t.start(); + Reroot re; + auto ans = re.solve(); + hash = accumulate(all(ans), 0LL); + 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(); +} |
