summaryrefslogtreecommitdiffstats
path: root/src/test/common/test_counter.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/common/test_counter.cc')
-rw-r--r--src/test/common/test_counter.cc40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/test/common/test_counter.cc b/src/test/common/test_counter.cc
new file mode 100644
index 000000000..f9a7d6e6c
--- /dev/null
+++ b/src/test/common/test_counter.cc
@@ -0,0 +1,40 @@
+#include "common/DecayCounter.h"
+
+#include <gtest/gtest.h>
+
+#include <list>
+#include <cmath>
+
+TEST(DecayCounter, steady)
+{
+ static const double duration = 2.0;
+ static const double max = 2048.0;
+ static const double rate = 3.5;
+
+ DecayCounter d{DecayRate{rate}};
+ d.hit(max);
+ const auto start = DecayCounter::clock::now();
+ double total = 0.0;
+ while (1) {
+ const auto now = DecayCounter::clock::now();
+ auto el = std::chrono::duration<double>(now-start);
+ if (el.count() > duration) {
+ break;
+ }
+
+ double v = d.get();
+ double diff = max-v;
+ if (diff > 0.0) {
+ d.hit(diff);
+ total += diff;
+ }
+ }
+
+ /* Decay function: dN/dt = -λM where λ = ln(0.5)/rate
+ * (where M is the maximum value of the counter, not varying with time.)
+ * Integrating over t: N = -λMt (+c)
+ */
+ double expected = -1*std::log(0.5)/rate*max*duration;
+ std::cerr << "t " << total << " e " << expected << std::endl;
+ ASSERT_LT(std::abs(total-expected)/expected, 0.05);
+}