From 4f143539f8c0264ef4175356110c05e346a847f2 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sun, 13 May 2018 12:22:52 +0200 Subject: [PATCH] Fixed unit tests * Random number generation was faulty, as we have to use RAND_MAX and not INT_MAX * fixed CTestAircraftSituation::testSetRotateUpPitch, we need to use a defined front function otherwise it can be randomly outside range * adjusted performance unit test a bit, as the value vary a lot Ref T261 --- src/blackmisc/math/mathutils.cpp | 13 +++++++++---- tests/blackmisc/testaircraftsituation.cpp | 7 +++++++ tests/blackmisc/testinterpolatorlinear.cpp | 5 ++++- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/blackmisc/math/mathutils.cpp b/src/blackmisc/math/mathutils.cpp index efe0e6070..ddc9dba89 100644 --- a/src/blackmisc/math/mathutils.cpp +++ b/src/blackmisc/math/mathutils.cpp @@ -100,20 +100,25 @@ namespace BlackMisc int CMathUtils::randomInteger(int low, int high) { static QThreadStorage seeds; + Q_ASSERT_X(high < INT_MAX, Q_FUNC_INFO, "Cannot add 1"); + Q_ASSERT_X(low >= 0 && high >= 0, Q_FUNC_INFO, "Only valid for positive values"); + Q_ASSERT_X(high <= RAND_MAX, Q_FUNC_INFO, "RAND_MAX exceeded"); if (!seeds.hasLocalData()) { // seed is per thread! - uint seed = static_cast(QTime::currentTime().msec()); + const uint seed = static_cast(QTime::currentTime().msec()); qsrand(seed); seeds.setLocalData(seed); } - int r(qrand()); - return r % ((high + 1) - low) + low; + const int r(qrand()); + const int mod = (high + 1) - low; + return (r % mod) + low; } double CMathUtils::randomDouble(double max) { - static const int MAX(INT_MAX); + // on Win system, RAND_MAX is only 16bit, on other systems higher + static const int MAX(RAND_MAX < INT_MAX ? RAND_MAX : INT_MAX - 1); const double r = randomInteger(0, MAX); return (r / MAX) * max; } diff --git a/tests/blackmisc/testaircraftsituation.cpp b/tests/blackmisc/testaircraftsituation.cpp index 069451777..7addb3ad4 100644 --- a/tests/blackmisc/testaircraftsituation.cpp +++ b/tests/blackmisc/testaircraftsituation.cpp @@ -228,13 +228,20 @@ namespace BlackMiscTest CAircraftSituationList CTestAircraftSituation::testSetRotateUpPitch(const CAircraftSituationList &situations) { CAircraftSituationList newSituations; + double average = 0; for (const CAircraftSituation &situation : situations) { CAircraftSituation s(situation); const double pitch = CMathUtils::randomDouble(1.5); + average += pitch; s.setPitch(CAngle(pitch, CAngleUnit::deg())); newSituations.push_back(s); } + average = average / newSituations.size(); + CAircraftSituation avg = newSituations.front(); + avg.setPitch(CAngle(average, CAngleUnit::deg())); + avg.addMsecs(1000); // make this the latest situation + newSituations.push_front(avg); // first value is average pitch, so it will NOT be detected return newSituations; } diff --git a/tests/blackmisc/testinterpolatorlinear.cpp b/tests/blackmisc/testinterpolatorlinear.cpp index 96a767427..9d787a5d7 100644 --- a/tests/blackmisc/testinterpolatorlinear.cpp +++ b/tests/blackmisc/testinterpolatorlinear.cpp @@ -134,8 +134,11 @@ namespace BlackMiscTest interpolationNo++; } } + + // check on time just to learn if interpolation suddenly gets very slow + // this is a risky test as in some situations the values can be exceeded int timeMs = timer.elapsed(); - QVERIFY2(timeMs < interpolationNo, "Interpolation > 1ms"); + QVERIFY2(timeMs < interpolationNo * 1.5, "Interpolation > 1.5ms"); qDebug() << timeMs << "ms" << "for" << interpolationNo << "interpolations"; int fetchedParts = 0;