summaryrefslogtreecommitdiff
path: root/test/math/inversions.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'test/math/inversions.cpp')
-rw-r--r--test/math/inversions.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/test/math/inversions.cpp b/test/math/inversions.cpp
new file mode 100644
index 0000000..d2a54b7
--- /dev/null
+++ b/test/math/inversions.cpp
@@ -0,0 +1,43 @@
+#include "../util.h"
+#include <datastructures/pbds.cpp>
+#include <math/inversions.cpp>
+
+ll naive(const vector<ll>& v) {
+ ll res = 0;
+ for (ll i = 0; i < sz(v); i++) {
+ for (ll j = 0; j < i; j++) {
+ if (v[j] > v[i]) res++;
+ }
+ }
+ return res;
+}
+
+void stress_test() {
+ ll queries = 0;
+ for (ll i = 0; i < 100'000; i++) {
+ int n = Random::integer<int>(1, 100);
+ auto v = Random::integers<ll>(n, -50, 50);
+ ll got = inversions(v);
+ ll expected = naive(v);
+ if (got != expected) cerr << "got: " << got << ", expected: " << expected << FAIL;
+ queries += n;
+ }
+ cerr << "tested queries: " << queries << endl;
+}
+
+constexpr int N = 200'000;
+void performance_test() {
+ timer t;
+ auto v = Random::integers<ll>(N, -10'000, 10'000);
+ t.start();
+ hash_t hash = inversions(v);
+ t.stop();
+ 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();
+}
+