summaryrefslogtreecommitdiff
path: root/test/datastructures
diff options
context:
space:
mode:
Diffstat (limited to 'test/datastructures')
-rw-r--r--test/datastructures/persistent.cpp35
-rw-r--r--test/datastructures/persistentArray.cpp48
2 files changed, 83 insertions, 0 deletions
diff --git a/test/datastructures/persistent.cpp b/test/datastructures/persistent.cpp
new file mode 100644
index 0000000..4e304fa
--- /dev/null
+++ b/test/datastructures/persistent.cpp
@@ -0,0 +1,35 @@
+#include "../util.h"
+#pragma GCC diagnostic ignored "-Wshadow"
+#include <datastructures/persistent.cpp>
+
+void stress_test() {
+ ll queries = 0;
+ for (int tries = 0; tries < 100'000; tries++) {
+ int n = Random::integer<int>(1, 30);
+
+ int timeRef = Random::integer<int>(1, 30);
+ persistent<double> got(timeRef);
+ map<int, double> expected;
+
+ for (int i = 0; i < n; i++) {
+ timeRef += Random::integer<int>(1, 30);
+ double x = Random::real<double>(-1'000, 1'000);
+ auto t = got.set(x);
+
+ if (!expected.empty() && t <= expected.rbegin()->first) cerr << "error: time travel" << FAIL;
+ expected[t] = x;
+ }
+
+ double tmp = 0;
+ for (int i = expected.begin()->first; i < expected.rbegin()->first + 10; i++) {
+ if (expected.find(i) != expected.end()) tmp = expected[i];
+ if (got.get(i) != tmp) cerr << "got: " << got.get(i) << ", " << "expected: " << tmp << FAIL;
+ }
+ queries += n;
+ }
+ cerr << "tested random queries: " << queries << endl;
+}
+
+int main() {
+ stress_test();
+}
diff --git a/test/datastructures/persistentArray.cpp b/test/datastructures/persistentArray.cpp
new file mode 100644
index 0000000..6712089
--- /dev/null
+++ b/test/datastructures/persistentArray.cpp
@@ -0,0 +1,48 @@
+#include "../util.h"
+#pragma GCC diagnostic ignored "-Wshadow"
+#include <datastructures/persistent.cpp>
+#include <datastructures/persistentArray.cpp>
+
+void stress_test() {
+ ll queries = 0;
+ for (int tries = 0; tries < 2'000; tries++) {
+ int n = Random::integer<int>(1, 30)*1000;
+ int m = Random::integer<int>(1, 30);
+
+ persistentArray<double> got(m);
+ vector<double> cur(m);
+ vector<pair<int, vector<double>>> expected;
+ for (int i = 0; i < n; i++) {
+ int op = Random::integer<int>(0, 20);
+ if (op <= 10) {
+ int j = Random::integer<int>(0, m);
+ double x = Random::real<double>(-1'000, 1'000);
+
+ auto t = got.set(j, x);
+ if (!expected.empty() && t <= expected.rbegin()->first) cerr << "error: time travel" << FAIL;
+
+ cur[j] = x;
+ expected.emplace_back(t, cur);
+ } else if (op <= 16) {
+ if (sz(expected) < 1) continue;
+ int j = Random::integer<int>(0, sz(expected));
+ for (int k = 0; k < m; k++) {
+ if (got.get(k, expected[j].first) != expected[j].second[k]) cerr << "got: " << got.get(k, expected[j].first) << ", expected: " << expected[j].second[k] << FAIL;
+ }
+ } else {
+ if (sz(expected) < 1) continue;
+ int j = Random::integer<int>(0, sz(expected));
+ got.reset(expected[j].first);
+ expected.resize(j + 1);
+ cur = expected.back().second;
+ }
+
+ }
+ queries += n;
+ }
+ cerr << "tested random queries: " << queries << endl;
+}
+
+int main() {
+ stress_test();
+}