summaryrefslogtreecommitdiff
path: root/test/other
diff options
context:
space:
mode:
Diffstat (limited to 'test/other')
-rw-r--r--test/other/bitOps.cpp59
-rw-r--r--test/other/bitOps.cpp.awk9
-rw-r--r--test/other/josephus2.cpp2
-rw-r--r--test/other/recover.cpp44
4 files changed, 113 insertions, 1 deletions
diff --git a/test/other/bitOps.cpp b/test/other/bitOps.cpp
new file mode 100644
index 0000000..44f6297
--- /dev/null
+++ b/test/other/bitOps.cpp
@@ -0,0 +1,59 @@
+#include "../util.h"
+#include <other/bitOps.cpp>
+
+void test_subsets() {
+ int queries = 0;
+ for (int i = 0; i < 1000'000; i++) {
+ int mask = 0;
+ int limBits = Random::integer<int>(1, 15);
+ for (int j = 0; j < limBits; j++) {
+ mask |= 1 << Random::integer<int>(0, 31);
+ }
+
+ int expeced = 1 << __builtin_popcount(mask);
+ int last = -1;
+ int seen = 1;
+ subsets(mask, [&](int active){
+ if (active <= 0) cerr << "error active: " << active << FAIL;
+ if (last >= 0 && active >= last) cerr << "error active: " << active << ", last: " << last << FAIL;
+ last = active;
+ seen++;
+ });
+ if (expeced != seen) cerr << "got: " << seen << ", expeced: " << expeced << FAIL;
+ queries++;
+ }
+ cerr << "tested subsets queries: " << queries << endl;
+}
+
+ll naive(ll x) {
+ vector<ll> bits;
+ for (ll i = 0; i < 64; i++) {
+ bits.push_back(x & 1);
+ x >>= 1;
+ }
+ reverse(all(bits));
+ next_permutation(all(bits));
+ reverse(all(bits));
+ x = 0;
+ for (ll i = 0, j = 1; i < 64; i++, j <<= 1) {
+ if (bits[i] != 0) x |= j;
+ }
+ return x;
+}
+
+void test_nextPerm() {
+ int queries = 0;
+ for (int i = 0; i < 1000'000; i++) {
+ ll x = 4;//Random::integer<ll>(1, LL::INF);
+ ll got = nextPerm(x);
+ ll expeced = naive(x);
+ if (got != expeced) cerr << x << ": got: " << got << ", expeced: " << expeced << FAIL;
+ queries++;
+ }
+ cerr << "tested nextPerm queries: " << queries << endl;
+}
+
+int main() {
+ test_subsets();
+ test_nextPerm();
+} \ No newline at end of file
diff --git a/test/other/bitOps.cpp.awk b/test/other/bitOps.cpp.awk
new file mode 100644
index 0000000..f1abcfb
--- /dev/null
+++ b/test/other/bitOps.cpp.awk
@@ -0,0 +1,9 @@
+/for \(int subset/ {
+ print "template<typename F>"
+ print "void subsets(int bitmask, F&& f) {"
+}
+/\/\/ Nächste Permutation/ {
+ print " {f(subset);}"
+ print "}"
+}
+{ print }
diff --git a/test/other/josephus2.cpp b/test/other/josephus2.cpp
index d28fe0d..85a9d28 100644
--- a/test/other/josephus2.cpp
+++ b/test/other/josephus2.cpp
@@ -31,7 +31,7 @@ void performance_test() {
hash += rotateLeft(1'000'000'000'000'000'000ll + i);
}
t.stop();
- if (t.time > 500) cerr << "too slow: " << t.time << FAIL;
+ if (t.time > 750) cerr << "too slow: " << t.time << FAIL;
cerr << "tested performance: " << t.time << "ms (hash: " << hash << ")" << endl;
}
diff --git a/test/other/recover.cpp b/test/other/recover.cpp
new file mode 100644
index 0000000..72853e5
--- /dev/null
+++ b/test/other/recover.cpp
@@ -0,0 +1,44 @@
+#include "../util.h"
+#include <other/recover.cpp>
+#include <math/shortModInv.cpp>
+
+void stress_test() {
+ ll queries = 0;
+ timer t;
+ for (int i = 0; i < 500; i++) {
+ ll p = Random::prime<ll>(10000);
+ for (ll j = 0; 2*j*j < p; j++) {
+ for (ll b = 1; 2*b*b < p; b++) {
+ if (gcd(j, b) != 1) continue;
+ for (ll a : {-j, j}) {
+ ll c = a * multInv(b, p);
+
+ t.start();
+ auto [x, y] = recover(c, p);
+ t.stop();
+
+ if (a != x || b != y) cerr << "got: " << x << "/" << y << ", expected: " << a << "/" << b << FAIL;
+ queries++;
+ }
+ }
+ }
+ for (ll c = 0; c < p; c++) {
+ t.start();
+ auto [x, y] = recover(c, p);
+ t.stop();
+
+ if (y < 0) continue;
+ if (y == 0) cerr << "error: y=0" << FAIL;
+ ll got = (((x * multInv(y, p)) % p) + p) % p;
+ if (got != c) cerr << "got: " << got << ", expected: " << c << FAIL;
+ queries++;
+ }
+ }
+ cerr << "tested random queries: " << queries << endl;
+ if (t.time > 500) cerr << "too slow: " << t.time << FAIL;
+ cerr << "tested performance: " << t.time << "ms" << endl;
+}
+
+int main() {
+ stress_test();
+}