forked from numenta/nupic.core-legacy
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathRandomTest.cpp
More file actions
278 lines (222 loc) · 6.96 KB
/
RandomTest.cpp
File metadata and controls
278 lines (222 loc) · 6.96 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* ---------------------------------------------------------------------
* HTM Community Edition of NuPIC
* Copyright (C) 2013, Numenta, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero Public License for more details.
*
* You should have received a copy of the GNU Affero Public License
* along with this program. If not, see http://www.gnu.org/licenses.
* --------------------------------------------------------------------- */
/**
* @file
*/
#include <gtest/gtest.h>
#include <htm/utils/Random.hpp>
#include <htm/os/Env.hpp>
#include <htm/utils/Log.hpp>
#include <htm/os/Timer.hpp>
#include <fstream>
#include <sstream>
#include <vector>
namespace testing {
using namespace htm;
using namespace std;
TEST(RandomTest, Seeding) {
{
Random r;
ASSERT_TRUE(r.getSeed() != 0) << "Should initialize with randomized seed";
auto x = r.getUInt32();
ASSERT_NE(x, 0u);
}
// test getSeed
{
Random r(98765);
ASSERT_EQ(98765U, r.getSeed());
}
{ // seed & equals
Random r1(1);
Random r2(2);
ASSERT_NE(r1.getUInt32(), r2.getUInt32());
ASSERT_NE(r1, r2) << "Randoms with different seed must be different";
Random r1b(1);
ASSERT_NE(r1, r1b) << "different steps";
auto x = r1b.getUInt32();
ASSERT_EQ(r1, r1b) << "Randoms with same seed must be the same." << x;
}
{ //MAX_INT seed
Random r(-1);
ASSERT_EQ(r(), 419326371u);
}
for(int i=0; i< 10; i++) {
ASSERT_NE(Random(0).getSeed(), Random(0).getSeed()) << "Randomly seeded generators should not be identical!";
}
}
TEST(RandomTest, CopyConstructor) {
// test copy constructor.
Random r1(289436);
for (int i = 0; i < 100; i++)
r1.getUInt32();
Random r2(r1); //copy
UInt32 v1, v2;
for (int i = 0; i < 100; i++) {
v1 = r1.getUInt32();
v2 = r2.getUInt32();
ASSERT_EQ(v1, v2) << "copy constructor";
}
}
TEST(RandomTest, OperatorEquals) {
// test operator=
Random r1(289436);
for (int i = 0; i < 100; i++)
r1.getUInt32();
Random r2(86726008);
for (int i = 0; i < 100; i++)
r2.getUInt32();
r2 = r1;
NTA_ASSERT(r1 == r2);
UInt32 v1, v2;
for (int i = 0; i < 100; i++) {
v1 = r1.getUInt32();
v2 = r2.getUInt32();
ASSERT_EQ(v1, v2) << "operator=";
}
}
TEST(RandomTest, testSerialization) { // test serialization/deserialization using Cereal
const UInt SEED = 862973u;
Random r1(SEED);
ASSERT_EQ(r1.getSeed(), SEED) << "RNG seed not set properly";
//burn-in
for (int i = 0; i < 100; i++) r1.getUInt32();
EXPECT_EQ(r1.getUInt32(), 2276275187u) << "Before serialization must be same";
// serialize
std::stringstream ss;
r1.save(ss);
// deserialize into r2
Random r2;
r2.load(ss);
// r1 and r2 should be identical
EXPECT_EQ(r1, r2) << "load from serialization";
EXPECT_EQ(r2.getUInt32(), 3537119063u) << "Deserialized is not deterministic";
EXPECT_EQ(r1.getUInt32(), 3537119063u); //move the same number of steps
EXPECT_EQ(r1.getSeed(), r2.getSeed());
EXPECT_EQ(r2.getSeed(), SEED) << "Rng deserialized seed is not the same!";
for (int i = 0; i < 100; i++) {
EXPECT_EQ(r1.getUInt32(), r2.getUInt32()) << "serialization";
}
}
TEST(RandomTest, ReturnInCorrectRange) {
// make sure that we are returning values in the correct range
// @todo perform statistical tests
Random r;
UInt64 seed = r.getSeed();
ASSERT_TRUE(seed != 0) << "seed not zero";
int i;
UInt32 max32 = 10000000;
for (i = 0; i < 2000; i++) {
UInt32 i32 = r.getUInt32(max32);
ASSERT_TRUE(i32 < max32) << "UInt32";
Real64 r64 = r.getReal64();
ASSERT_TRUE(r64 >= 0.0 && r64 < 1.0) << "Real64";
}
}
TEST(RandomTest, getUInt32) {
// tests for getUInt32
Random r1(1);
EXPECT_EQ(1791095845u, r1.getUInt32())
<< "check getUInt64, seed 1, first call";
EXPECT_EQ(4282876139u, r1.getUInt32())
<< "check getUInt64, seed 1, second call";
Random r2(2);
EXPECT_EQ(1872583848u, r2.getUInt32())
<< "check getUInt64, seed 2, first call";
EXPECT_EQ(794921487u, r2.getUInt32())
<< "check getUInt64, seed 2, second call";
Random r3(7464235991977222558);
EXPECT_EQ(1606095383u, r3.getUInt32())
<< "check getUInt64, big seed, first call";
EXPECT_EQ(59943411u, r3.getUInt32())
<< "check getUInt64, big seed, second call";
}
TEST(RandomTest, getReal64) {
// tests for getReal64
Random r1(1);
EXPECT_DOUBLE_EQ(0.41702199853421701, r1.getReal64());
EXPECT_DOUBLE_EQ(0.99718480836534518, r1.getReal64());
Random r2(2);
EXPECT_DOUBLE_EQ(0.43599490272719293, r2.getReal64());
EXPECT_DOUBLE_EQ(0.18508208151559394, r2.getReal64());
Random r3(7464235991977222558);
EXPECT_DOUBLE_EQ(0.37394822188046489, r3.getReal64());
EXPECT_DOUBLE_EQ(0.013956662969187988, r3.getReal64());
}
TEST(RandomTest, Sampling) {
// tests for sampling
const vector<UInt> population = {1u, 2u, 3u, 4u};
Random r(17);
{
// choose some elements
auto choices = r.sample<UInt>(population, 2);
EXPECT_EQ(3u, choices[0]) << "check sample 0";
EXPECT_EQ(2u, choices[1]) << "check sample 1";
}
{
// choose all elements
vector<UInt> choices = r.sample<UInt>(population, 4);
EXPECT_EQ(1u, choices[0]) << "check sample 0";
EXPECT_EQ(2u, choices[1]) << "check sample 1";
EXPECT_EQ(4u, choices[2]) << "check sample 2";
EXPECT_EQ(3u, choices[3]) << "check sample 3";
}
//check population list remained unmodified
ASSERT_EQ(1u, population[0]) << "check sample p 0";
ASSERT_EQ(2u, population[1]) << "check sample p 1";
ASSERT_EQ(3u, population[2]) << "check sample p 2";
ASSERT_EQ(4u, population[3]) << "check sample p 3";
{
// nChoices > nPopulation
EXPECT_THROW(r.sample<UInt>(population, 5), Exception) << "checking for exception from population too small";
}
}
TEST(RandomTest, Shuffling) {
// tests for shuffling
Random r(1);
UInt32 arr[] = {1u, 2u, 3u, 4u};
const UInt32 exp[] = {4u, 1u, 3u, 2u};
ASSERT_NO_THROW(r.shuffle(std::begin(arr), std::end(arr)));
EXPECT_EQ(exp[0], arr[0]) << "check shuffle 0";
EXPECT_EQ(exp[1], arr[1]) << "check shuffle 1";
EXPECT_EQ(exp[2], arr[2]) << "check shuffle 2";
EXPECT_EQ(exp[3], arr[3]) << "check shuffle 3";
}
/**
* Test operator '=='
*/
TEST(RandomTest, testEqualsOperator) {
Random r1(42), r2(42), r3(3);
ASSERT_EQ(r1, r2);
ASSERT_NE(r1, r3);
ASSERT_NE(r2, r3);
UInt32 v1, v2;
v1 = r1.getUInt32();
ASSERT_NE(r1, r2) << "one step diff";
v2 = r2.getUInt32();
ASSERT_EQ(r1, r2) << "synchronized steps";
ASSERT_EQ(v1, v2);
}
TEST(RandomTest, testGetUIntSpeed) {
Random r1(42);
UInt32 rnd;
const int RUNS = 10000000;
for(int i=0; i<RUNS; i++) {
rnd = r1.getUInt32(10000); //get random int [0..1M)
}
EXPECT_EQ(rnd, 9278u);
}
} //-ns