Skip to content

Commit

Permalink
tests: Add stress_test for MovQuantile
Browse files Browse the repository at this point in the history
  • Loading branch information
gavv authored and jeshwanthreddy13 committed Aug 6, 2024
1 parent 72c9798 commit 9ce7af7
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/tests/roc_core/test_mov_quantile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <CppUTest/TestHarness.h>

#include "roc_core/fast_random.h"
#include "roc_core/heap_arena.h"
#include "roc_core/mov_quantile.h"

Expand Down Expand Up @@ -132,5 +133,38 @@ TEST(mov_quantile, test_maximum) {
LONGS_EQUAL((int64_t)102, quant.mov_quantile()); // test complete window
}

TEST(mov_quantile, stress_test) {
enum { NumIterations = 10, NumElems = 1000, MinWindow = 1, MaxWindow = 100 };

for (size_t i = 0; i < NumIterations; i++) {
const size_t q_win_sz = fast_random_range(MinWindow, MaxWindow);
const double q = (double)fast_random() / (double)UINT32_MAX;

MovQuantile<double> quant(arena, q_win_sz, q);
CHECK(quant.is_valid());

double elems[NumElems] = {};

for (size_t n = 0; n < NumElems; n++) {
elems[n] = (double)fast_random() / (double)UINT32_MAX;
quant.add(elems[n]);

const size_t n_elems = n + 1;

// slow and simple quantile calculation
double cur_win[NumElems] = {};
const size_t cur_win_sz = std::min(q_win_sz, n_elems);
memcpy(cur_win, elems + n_elems - cur_win_sz, cur_win_sz * sizeof(double));
std::sort(cur_win, cur_win + cur_win_sz);
const size_t cur_win_mid = (size_t)std::floor((cur_win_sz - 1) * q);

const double expected = cur_win[cur_win_mid];
const double actual = quant.mov_quantile();

DOUBLES_EQUAL(expected, actual, 0.00001);
}
}
}

} // namespace core
} // namespace roc

0 comments on commit 9ce7af7

Please sign in to comment.