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
This commit is contained in:
Klaus Basan
2018-05-13 12:22:52 +02:00
parent b4835e650d
commit 4f143539f8
3 changed files with 20 additions and 5 deletions

View File

@@ -100,20 +100,25 @@ namespace BlackMisc
int CMathUtils::randomInteger(int low, int high)
{
static QThreadStorage<uint> 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<uint>(QTime::currentTime().msec());
const uint seed = static_cast<uint>(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;
}

View File

@@ -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;
}

View File

@@ -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;