summaryrefslogtreecommitdiff
path: root/tests/unit/RandomGenerator
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit/RandomGenerator')
-rw-r--r--tests/unit/RandomGenerator/RandomGenerator_test.cpp79
-rw-r--r--tests/unit/RandomGenerator/rndGen.pri6
2 files changed, 85 insertions, 0 deletions
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 <gtest/gtest.h>
+#include <cmath>
+#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