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