summaryrefslogtreecommitdiff
path: root/datastructures/test/fenwickTree.cpp
diff options
context:
space:
mode:
authorGloria Mundi <gloria@gloria-mundi.eu>2024-03-10 20:43:13 +0100
committerGloria Mundi <gloria@gloria-mundi.eu>2024-03-10 20:43:13 +0100
commit0cebc901e79c21168601071e29ed8e4f4b6f9505 (patch)
treed917fc4bb1ecedb1794f09ded08c478b0be301dd /datastructures/test/fenwickTree.cpp
parent0f29ac59c2bf0e5eafc2a6fa436e3070085e3a1d (diff)
add tests for Fenwick Tree
Diffstat (limited to 'datastructures/test/fenwickTree.cpp')
-rw-r--r--datastructures/test/fenwickTree.cpp26
1 files changed, 26 insertions, 0 deletions
diff --git a/datastructures/test/fenwickTree.cpp b/datastructures/test/fenwickTree.cpp
new file mode 100644
index 0000000..4bc812a
--- /dev/null
+++ b/datastructures/test/fenwickTree.cpp
@@ -0,0 +1,26 @@
+#include "../fenwickTree.cpp"
+
+void test(int n) {
+ vector<ll> naive(n);
+ init(n);
+
+ for (int i = 0; i < 1000; i++) {
+ int p = util::randint(n);
+ ll delta = util::randint();
+ update(p, delta);
+ naive[p] += delta;
+
+ int r = util::randint(n+1) - 1;
+
+ ll naive_result = 0;
+ for (int i = 0; i <= r; i++) naive_result += naive[i];
+ ll fenwick_result = prefix_sum(r);
+ assert(naive_result == fenwick_result);
+ }
+}
+
+int main() {
+ test(1);
+ test(100);
+ test(1000);
+}