diff options
| author | Gloria Mundi <gloria@gloria-mundi.eu> | 2024-11-16 01:24:14 +0100 |
|---|---|---|
| committer | Gloria Mundi <gloria@gloria-mundi.eu> | 2024-11-16 01:24:14 +0100 |
| commit | 98567ec798aa8ca2cfbcb85c774dd470f30e30d4 (patch) | |
| tree | 5113d5cc24d1ad5f93810b6442ce584a36950dc8 /test/string/z.cpp | |
| parent | ad3856a6b766087df0036de0b556f4700a6498c9 (diff) | |
| parent | 8d11c6c8213f46f0fa19826917c255edd5d43cb1 (diff) | |
mzuenni tests
Diffstat (limited to 'test/string/z.cpp')
| -rw-r--r-- | test/string/z.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/test/string/z.cpp b/test/string/z.cpp new file mode 100644 index 0000000..f890a3e --- /dev/null +++ b/test/string/z.cpp @@ -0,0 +1,41 @@ +#include "../util.h" +#include <string/z.cpp> + +vector<int> naive(const string& s) { + vector<int> res(sz(s)); + for (int i = 1; i < sz(s); i++) { + while (i + res[i] < sz(s) && s[res[i]] == s[i + res[i]]) res[i]++; + } + return res; +} + +void stress_test() { + ll queries = 0; + for (int tries = 0; tries < 100'000; tries++) { + int n = Random::integer<int>(1, 15); + auto s = Random::string(n, "abc"); + auto got = Z(s); + auto expected = naive(s); + if (got != expected) cerr << "error" << FAIL; + queries += n; + } + cerr << "tested random queries: " << queries << endl; +} + +constexpr int N = 10'000'000; +void performance_test() { + timer t; + auto s = Random::string(N, "a") + Random::string(N, "ab") + Random::string(N, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$#"); + t.start(); + auto res = Z(s); + t.stop(); + hash_t hash = 0; + for (int x : res) hash += x; + 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(); +} |
