summaryrefslogtreecommitdiff
path: root/test/math/transforms/multiplyNTT.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/math/transforms/multiplyNTT.cpp
parentad3856a6b766087df0036de0b556f4700a6498c9 (diff)
parent8d11c6c8213f46f0fa19826917c255edd5d43cb1 (diff)
mzuenni tests
Diffstat (limited to 'test/math/transforms/multiplyNTT.cpp')
-rw-r--r--test/math/transforms/multiplyNTT.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/test/math/transforms/multiplyNTT.cpp b/test/math/transforms/multiplyNTT.cpp
new file mode 100644
index 0000000..70fc137
--- /dev/null
+++ b/test/math/transforms/multiplyNTT.cpp
@@ -0,0 +1,56 @@
+#include "../../util.h"
+#include <math/modPowIterativ.cpp>
+#include <math/transforms/ntt.cpp>
+#include <math/transforms/multiplyNTT.cpp>
+
+vector<ll> naive(const vector<ll>& a, const vector<ll>& b) {
+ vector<ll> res;
+ for (ll i = 1;; i *= 2) {
+ if (sz(a) + sz(b) <= i) {
+ res.resize(i, 0);
+ break;
+ }
+ }
+ for (int i = 0; i < sz(a); i++) {
+ for (int j = 0; j < sz(b); j++) {
+ res[i+j] += a[i] * b[j];
+ res[i+j] %= mod;
+ }
+ }
+ return res;
+}
+
+void stress_test() {
+ ll queries = 0;
+ for (ll i = 0; i < 10'000; i++) {
+ int n = Random::integer<int>(1, 100);
+ int m = Random::integer<int>(1, 100);
+ auto a = Random::integers<ll>(n, 0, mod);
+ auto b = Random::integers<ll>(m, 0, mod);
+ auto expected = naive(a, b);
+ auto got = mul(a, b);
+ if (got != expected) cerr << "error" << FAIL;
+ queries += n;
+ }
+ cerr << "tested queries: " << queries << endl;
+}
+
+constexpr int N = 1ll << 20;
+void performance_test() {
+ timer t;
+ vector<ll> a = Random::integers<ll>(N, 0, mod);
+ vector<ll> b = Random::integers<ll>(N, 0, mod);
+ t.start();
+ auto got = mul(a, b);
+ t.stop();
+ hash_t hash = 0;
+ for (ll i = 0; i < N; i++) hash += i * got[i];
+ 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();
+}
+