summaryrefslogtreecommitdiff
path: root/test/math
diff options
context:
space:
mode:
authorLucas Schwebler <lucas.schwebler@gmail.com>2024-09-07 22:02:34 +0200
committerLucas Schwebler <lucas.schwebler@gmail.com>2024-09-07 22:02:34 +0200
commit6ae1d00fca78145c73f826c5490452726b9bf161 (patch)
tree591921b79e3c3747906dee44a5067ae9a3ca5701 /test/math
parente1b35ad44b9168cfd1a6d4ca4ed7658b81e93663 (diff)
add divSum (sum of floor linear)
Diffstat (limited to 'test/math')
-rw-r--r--test/math/divSum.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/test/math/divSum.cpp b/test/math/divSum.cpp
new file mode 100644
index 0000000..1f82387
--- /dev/null
+++ b/test/math/divSum.cpp
@@ -0,0 +1,48 @@
+#include "../util.h"
+#include <math/divSum.cpp>
+
+ll naive(ll n, ll m, ll a, ll b){
+ ll ans = 0;
+ for(ll i = 0; i < n; i++){
+ ans += (a*i+b)/m;
+ }
+ return ans;
+}
+
+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);
+ int a = Random::integer<int>(0, 100);
+ int b = Random::integer<int>(0, 100);
+ ll expected = naive(n, m, a, b);
+ ll got = divSum(n, m, a, b);
+ if (got != expected) cerr << "got: " << got << ", expected: " << expected << FAIL;
+ queries++;
+ }
+ cerr << "tested queries: " << queries << endl;
+}
+
+constexpr int N = 1'000'000;
+void performance_test() {
+ timer t;
+ hash_t hash = 0;
+ for (int operations = 0; operations < N; operations++) {
+ ll n = Random::integer<ll>(1, 1'000'000'000);
+ ll m = Random::integer<ll>(1, 1'000'000'000);
+ ll a = Random::integer<ll>(0, 1'000'000'000);
+ ll b = Random::integer<ll>(0, 1'000'000'000);
+ t.start();
+ hash += divSum(n, m, a, b);
+ t.stop();
+ }
+ if (t.time > 750) cerr << "too slow: " << t.time << FAIL;
+ cerr << "tested performance: " << t.time << "ms (hash: " << hash << ")" << endl;
+}
+
+int main() {
+ stress_test();
+ performance_test();
+}
+