Replace randomization code, fixes #973

This commit is contained in:
Dale Glass 2024-06-01 19:46:34 +02:00
parent 09e5a60c8b
commit bbc7c0a146

View file

@ -18,6 +18,7 @@
#include <QtCore/QQueue> #include <QtCore/QQueue>
#include <test-utils/GLMTestUtils.h> #include <test-utils/GLMTestUtils.h>
#include <test-utils/QTestExtensions.h> #include <test-utils/QTestExtensions.h>
#include <QRandomGenerator64>
QTEST_MAIN(MovingPercentileTests) QTEST_MAIN(MovingPercentileTests)
@ -41,70 +42,67 @@ void MovingPercentileTests::testRunningMedian() {
int64_t MovingPercentileTests::random() { int64_t MovingPercentileTests::random() {
return ((int64_t) rand() << 48) ^ return QRandomGenerator64::global()->generate();
((int64_t) rand() << 32) ^
((int64_t) rand() << 16) ^
((int64_t) rand());
} }
void MovingPercentileTests::testRunningMinForN (int n) { void MovingPercentileTests::testRunningMinForN (int n) {
// Stores the last n samples // Stores the last n samples
QQueue<int64_t> samples; QQueue<int64_t> samples;
MovingPercentile movingMin (n, 0.0f); MovingPercentile movingMin (n, 0.0f);
for (int s = 0; s < 3 * n; ++s) { for (int s = 0; s < 3 * n; ++s) {
int64_t sample = random(); int64_t sample = random();
samples.push_back(sample); samples.push_back(sample);
if (samples.size() > n) if (samples.size() > n)
samples.pop_front(); samples.pop_front();
if (samples.size() == 0) { if (samples.size() == 0) {
QFAIL_WITH_MESSAGE("\n\n\n\tWTF\n\tsamples.size() = " << samples.size() << ", n = " << n); QFAIL_WITH_MESSAGE("\n\n\n\tWTF\n\tsamples.size() = " << samples.size() << ", n = " << n);
} }
movingMin.updatePercentile(sample); movingMin.updatePercentile(sample);
// Calculate the minimum of the moving samples // Calculate the minimum of the moving samples
int64_t expectedMin = std::numeric_limits<int64_t>::max(); int64_t expectedMin = std::numeric_limits<int64_t>::max();
int prevSize = samples.size(); int prevSize = samples.size();
for (auto val : samples) { for (auto val : samples) {
expectedMin = std::min(val, expectedMin); expectedMin = std::min(val, expectedMin);
} }
QCOMPARE(samples.size(), prevSize); QCOMPARE(samples.size(), prevSize);
QVERIFY(movingMin.getValueAtPercentile() - expectedMin == 0L); QVERIFY(movingMin.getValueAtPercentile() - expectedMin == 0L);
} }
} }
void MovingPercentileTests::testRunningMaxForN (int n) { void MovingPercentileTests::testRunningMaxForN (int n) {
// Stores the last n samples // Stores the last n samples
QQueue<int64_t> samples; QQueue<int64_t> samples;
MovingPercentile movingMax (n, 1.0f); MovingPercentile movingMax (n, 1.0f);
for (int s = 0; s < 10000; ++s) { for (int s = 0; s < 10000; ++s) {
int64_t sample = random(); int64_t sample = random();
samples.push_back(sample); samples.push_back(sample);
if (samples.size() > n) { if (samples.size() > n) {
samples.pop_front(); samples.pop_front();
} }
if (samples.size() == 0) { if (samples.size() == 0) {
QFAIL_WITH_MESSAGE("\n\n\n\tWTF\n\tsamples.size() = " << samples.size() << ", n = " << n); QFAIL_WITH_MESSAGE("\n\n\n\tWTF\n\tsamples.size() = " << samples.size() << ", n = " << n);
} }
movingMax.updatePercentile(sample); movingMax.updatePercentile(sample);
// Calculate the maximum of the moving samples // Calculate the maximum of the moving samples
int64_t expectedMax = std::numeric_limits<int64_t>::min(); int64_t expectedMax = std::numeric_limits<int64_t>::min();
for (auto val : samples) for (auto val : samples)
expectedMax = std::max(val, expectedMax); expectedMax = std::max(val, expectedMax);
QVERIFY(movingMax.getValueAtPercentile() - expectedMax == 0L); QVERIFY(movingMax.getValueAtPercentile() - expectedMax == 0L);
} }
} }
@ -112,34 +110,34 @@ void MovingPercentileTests::testRunningMaxForN (int n) {
void MovingPercentileTests::testRunningMedianForN (int n) { void MovingPercentileTests::testRunningMedianForN (int n) {
// Stores the last n samples // Stores the last n samples
QQueue<int64_t> samples; QQueue<int64_t> samples;
MovingPercentile movingMedian (n, 0.5f); MovingPercentile movingMedian (n, 0.5f);
for (int s = 0; s < 10000; ++s) { for (int s = 0; s < 10000; ++s) {
int64_t sample = random(); int64_t sample = random();
samples.push_back(sample); samples.push_back(sample);
if (samples.size() > n) if (samples.size() > n)
samples.pop_front(); samples.pop_front();
if (samples.size() == 0) { if (samples.size() == 0) {
QFAIL_WITH_MESSAGE("\n\n\n\tWTF\n\tsamples.size() = " << samples.size() << ", n = " << n); QFAIL_WITH_MESSAGE("\n\n\n\tWTF\n\tsamples.size() = " << samples.size() << ", n = " << n);
} }
movingMedian.updatePercentile(sample); movingMedian.updatePercentile(sample);
auto median = movingMedian.getValueAtPercentile(); auto median = movingMedian.getValueAtPercentile();
// Check the number of samples that are > or < median // Check the number of samples that are > or < median
int samplesGreaterThan = 0; int samplesGreaterThan = 0;
int samplesLessThan = 0; int samplesLessThan = 0;
for (auto value : samples) { for (auto value : samples) {
if (value < median) if (value < median)
++samplesGreaterThan; ++samplesGreaterThan;
else if (value > median) else if (value > median)
++samplesLessThan; ++samplesLessThan;
} }
QCOMPARE_WITH_LAMBDA(samplesLessThan, n / 2, [=]() { QCOMPARE_WITH_LAMBDA(samplesLessThan, n / 2, [=]() {
return samplesLessThan <= n / 2; return samplesLessThan <= n / 2;
}); });