1 | // |
2 | // RandomTest.cpp |
3 | // |
4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "RandomTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/Random.h" |
15 | #include <vector> |
16 | #include <cmath> |
17 | |
18 | |
19 | using Poco::UInt32; |
20 | |
21 | |
22 | RandomTest::RandomTest(const std::string& rName): CppUnit::TestCase(rName) |
23 | { |
24 | } |
25 | |
26 | |
27 | RandomTest::~RandomTest() |
28 | { |
29 | } |
30 | |
31 | |
32 | void RandomTest::testSequence1() |
33 | { |
34 | Poco::Random rnd1; |
35 | Poco::Random rnd2; |
36 | rnd1.seed(12345); |
37 | rnd2.seed(12345); |
38 | for (int i = 0; i < 100; ++i) |
39 | { |
40 | assertTrue (rnd1.next() == rnd2.next()); |
41 | } |
42 | } |
43 | |
44 | |
45 | void RandomTest::testSequence2() |
46 | { |
47 | Poco::Random rnd1; |
48 | Poco::Random rnd2; |
49 | rnd1.seed(12345); |
50 | rnd2.seed(54321); |
51 | |
52 | bool equals = true; |
53 | for (int i = 0; i < 20; ++i) |
54 | { |
55 | if (rnd1.next() != rnd2.next()) |
56 | { |
57 | equals = false; |
58 | break; |
59 | } |
60 | } |
61 | assertTrue (!equals); |
62 | } |
63 | |
64 | |
65 | void RandomTest::testDistribution1() |
66 | { |
67 | Poco::Random rnd; |
68 | rnd.seed(123456); |
69 | const int n = 11; |
70 | int d[n] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
71 | for (int i = 0; i < 100; ++i) |
72 | { |
73 | d[rnd.next() % n] = 1; |
74 | } |
75 | int sum = 0; |
76 | for (int k = 0; k < n; ++k) sum += d[k]; |
77 | |
78 | assertTrue (sum == n); |
79 | } |
80 | |
81 | |
82 | void RandomTest::testDistribution2() |
83 | { |
84 | Poco::Random rnd; |
85 | rnd.seed(); |
86 | const int n = 101; |
87 | std::vector<int> d(n, 0); |
88 | for (int i = 0; i < 10000; ++i) |
89 | { |
90 | d[rnd.next(n)]++; |
91 | } |
92 | int sum = 0; |
93 | for (int k = 0; k < n; ++k) sum += d[k]; |
94 | int avg = sum/n; |
95 | int var = 0; |
96 | for (int k = 0; k < n; ++k) var += (d[k] - avg)*(d[k] - avg); |
97 | var /= n; |
98 | int sd = int(std::sqrt((double) var)); |
99 | |
100 | assertTrue (95 < avg && avg < 105); |
101 | assertTrue (sd < 15); |
102 | } |
103 | |
104 | |
105 | void RandomTest::testDistribution3() |
106 | { |
107 | Poco::Random rnd; |
108 | rnd.seed(); |
109 | const int n = 101; |
110 | std::vector<int> d(n, 0); |
111 | for (int i = 0; i < 10000; ++i) |
112 | { |
113 | d[int(rnd.nextFloat()*n)]++; |
114 | } |
115 | int sum = 0; |
116 | for (int k = 0; k < n; ++k) sum += d[k]; |
117 | int avg = sum/n; |
118 | int var = 0; |
119 | for (int k = 0; k < n; ++k) var += (d[k] - avg)*(d[k] - avg); |
120 | var /= n; |
121 | int sd = int(std::sqrt((double) var)); |
122 | |
123 | assertTrue (95 < avg && avg < 105); |
124 | assertTrue (sd < 15); |
125 | } |
126 | |
127 | |
128 | void RandomTest::setUp() |
129 | { |
130 | } |
131 | |
132 | |
133 | void RandomTest::tearDown() |
134 | { |
135 | } |
136 | |
137 | |
138 | CppUnit::Test* RandomTest::suite() |
139 | { |
140 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("RandomTest" ); |
141 | |
142 | CppUnit_addTest(pSuite, RandomTest, testSequence1); |
143 | CppUnit_addTest(pSuite, RandomTest, testSequence2); |
144 | CppUnit_addTest(pSuite, RandomTest, testDistribution1); |
145 | CppUnit_addTest(pSuite, RandomTest, testDistribution2); |
146 | CppUnit_addTest(pSuite, RandomTest, testDistribution3); |
147 | |
148 | return pSuite; |
149 | } |
150 | |