From d24f813f3f2a05c112e803e4256b53535895fc98 Mon Sep 17 00:00:00 2001 From: Jedidiah Barber Date: Wed, 14 Jul 2021 11:49:10 +1200 Subject: Initial mirror commit --- .../unit/RandomGenerator/RandomGenerator_test.cpp | 79 ++++++++++++++++++++++ tests/unit/RandomGenerator/rndGen.pri | 6 ++ 2 files changed, 85 insertions(+) create mode 100644 tests/unit/RandomGenerator/RandomGenerator_test.cpp create mode 100644 tests/unit/RandomGenerator/rndGen.pri (limited to 'tests/unit/RandomGenerator') diff --git a/tests/unit/RandomGenerator/RandomGenerator_test.cpp b/tests/unit/RandomGenerator/RandomGenerator_test.cpp new file mode 100644 index 0000000..d334c9b --- /dev/null +++ b/tests/unit/RandomGenerator/RandomGenerator_test.cpp @@ -0,0 +1,79 @@ +#include +#include +#include "../../../src/utils/RandomGenerator.h" + +class RandomGeneratorTest: public testing::Test +{ +protected: + void generateRandoms(); + void checkStats(); + +private: + void checkMinMeanMax(double min, double mean, double max); + void minMeanMax(double& min, double& mean, double& max); + double stddev(double mean); + +protected: + static const int Num = 10000; + +protected: + RandomGenerator random; + double x[Num]; +}; + +TEST_F(RandomGeneratorTest, Stats) +{ + generateRandoms(); + checkStats(); +} + +TEST_F(RandomGeneratorTest, getArray) +{ + QByteArray ba = random.getArray(); +} + +void RandomGeneratorTest::generateRandoms() +{ + for(int i = 0; i < Num; i++) + x[i] = random.getInRange_11(); +} + +void RandomGeneratorTest::checkStats() +{ + double min; + double mean; + double max; + minMeanMax(min, mean, max); + + checkMinMeanMax(min, mean, max); + ASSERT_GT(stddev(mean), 0.55); +} + +void RandomGeneratorTest::checkMinMeanMax(double min, double mean, double max) +{ + ASSERT_NEAR(-1, min, 0.01); + ASSERT_NEAR(0, mean, 0.2); + ASSERT_NEAR(1, max, 0.01); +} + +void RandomGeneratorTest::minMeanMax(double& min, double& mean, double& max) +{ + double sum = 0; + min = 1000; + max = -1000; + for(int i = 0; i < Num; i++) + { + min = fmin(x[i], min); + max = fmax(x[i], max); + sum += x[i]; + } + mean = sum / Num; +} + +double RandomGeneratorTest::stddev(double mean) +{ + double varSum = 0; + for(int i = 0; i < Num; i++) + varSum += pow((mean - x[i]), 2); + return sqrt(varSum / Num); +} diff --git a/tests/unit/RandomGenerator/rndGen.pri b/tests/unit/RandomGenerator/rndGen.pri new file mode 100644 index 0000000..dbc9300 --- /dev/null +++ b/tests/unit/RandomGenerator/rndGen.pri @@ -0,0 +1,6 @@ +HEADERS += \ + $$SRC/utils/RandomGenerator.h + +SOURCES += \ + $$PWD/RandomGenerator_test.cpp \ + $$SRC/utils/RandomGenerator.cpp -- cgit