summaryrefslogtreecommitdiff
path: root/tests/unit/RandomGenerator/RandomGenerator_test.cpp
blob: d334c9bbb793eb72c12b00de3cf1d91400b710e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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);
}