Skip to content

Commit

Permalink
Merge pull request #991 from daleglass-overte/fix-tests
Browse files Browse the repository at this point in the history
Fix tests
  • Loading branch information
daleglass authored Jun 1, 2024
2 parents 09e5a60 + 30d5d64 commit 44d61e4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
4 changes: 3 additions & 1 deletion tests/shared/src/BitVectorHelperTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static void readWriteHelper(const std::vector<bool>& src) {
int numBits = (int)src.size();
int numBytes = calcBitVectorSize(numBits);
uint8_t* bytes = new uint8_t[numBytes];
memset(bytes, numBytes, sizeof(uint8_t));
memset(bytes, 0, numBytes);
int numBytesWritten = writeBitVector(bytes, numBits, [&](int i) {
return src[i];
});
Expand All @@ -53,6 +53,8 @@ static void readWriteHelper(const std::vector<bool>& src) {
bool b = dst[i];
QCOMPARE(a, b);
}

delete[] bytes;
}

void BitVectorHelperTests::readWriteTest() {
Expand Down
5 changes: 5 additions & 0 deletions tests/shared/src/FileCacheTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ void FileCacheTests::testFreeSpacePreservation() {
cache->setMinFreeSize(targetFreeSpace);
QCOMPARE(cache->getNumCachedFiles(), (size_t)5);
QCOMPARE(cache->getNumTotalFiles(), (size_t)5);
qDebug() << "Free space: " << getFreeSpace();
qDebug() << "Target : " << targetFreeSpace;

qInfo() << "The following test may fail if free disk space was changed by another program during the test's runtime";

QVERIFY(getFreeSpace() >= targetFreeSpace);
for (int i = 0; i < 95; ++i) {
std::string key = getFileKey(i);
Expand Down
54 changes: 26 additions & 28 deletions tests/shared/src/MovingPercentileTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <QtCore/QQueue>
#include <test-utils/GLMTestUtils.h>
#include <test-utils/QTestExtensions.h>
#include <QRandomGenerator64>

QTEST_MAIN(MovingPercentileTests)

Expand All @@ -41,105 +42,102 @@ void MovingPercentileTests::testRunningMedian() {


int64_t MovingPercentileTests::random() {
return ((int64_t) rand() << 48) ^
((int64_t) rand() << 32) ^
((int64_t) rand() << 16) ^
((int64_t) rand());
return QRandomGenerator64::global()->generate();
}

void MovingPercentileTests::testRunningMinForN (int n) {
// Stores the last n samples
QQueue<int64_t> samples;

MovingPercentile movingMin (n, 0.0f);

for (int s = 0; s < 3 * n; ++s) {
int64_t sample = random();

samples.push_back(sample);
if (samples.size() > n)
samples.pop_front();

if (samples.size() == 0) {
QFAIL_WITH_MESSAGE("\n\n\n\tWTF\n\tsamples.size() = " << samples.size() << ", n = " << n);
}

movingMin.updatePercentile(sample);

// Calculate the minimum of the moving samples
int64_t expectedMin = std::numeric_limits<int64_t>::max();

int prevSize = samples.size();
for (auto val : samples) {
expectedMin = std::min(val, expectedMin);
}
QCOMPARE(samples.size(), prevSize);

QVERIFY(movingMin.getValueAtPercentile() - expectedMin == 0L);
}
}

void MovingPercentileTests::testRunningMaxForN (int n) {

// Stores the last n samples
QQueue<int64_t> samples;

MovingPercentile movingMax (n, 1.0f);

for (int s = 0; s < 10000; ++s) {
int64_t sample = random();

samples.push_back(sample);
if (samples.size() > n) {
samples.pop_front();
}

if (samples.size() == 0) {
QFAIL_WITH_MESSAGE("\n\n\n\tWTF\n\tsamples.size() = " << samples.size() << ", n = " << n);
}

movingMax.updatePercentile(sample);

// Calculate the maximum of the moving samples
int64_t expectedMax = std::numeric_limits<int64_t>::min();
for (auto val : samples)
expectedMax = std::max(val, expectedMax);

QVERIFY(movingMax.getValueAtPercentile() - expectedMax == 0L);
}
}

void MovingPercentileTests::testRunningMedianForN (int n) {
// Stores the last n samples
QQueue<int64_t> samples;

MovingPercentile movingMedian (n, 0.5f);

for (int s = 0; s < 10000; ++s) {
int64_t sample = random();

samples.push_back(sample);
if (samples.size() > n)
samples.pop_front();

if (samples.size() == 0) {
QFAIL_WITH_MESSAGE("\n\n\n\tWTF\n\tsamples.size() = " << samples.size() << ", n = " << n);
}

movingMedian.updatePercentile(sample);
auto median = movingMedian.getValueAtPercentile();

// Check the number of samples that are > or < median
int samplesGreaterThan = 0;
int samplesLessThan = 0;

for (auto value : samples) {
if (value < median)
++samplesGreaterThan;
else if (value > median)
++samplesLessThan;
}

QCOMPARE_WITH_LAMBDA(samplesLessThan, n / 2, [=]() {
return samplesLessThan <= n / 2;
});
Expand Down

0 comments on commit 44d61e4

Please sign in to comment.