gem5 v24.1.0.1
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
random.test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 CNRS
3 * All rights reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <gtest/gtest-spi.h>
30#include <gtest/gtest.h>
31
32#include <unordered_map>
33#include <utility>
34#include <set>
35#include <algorithm>
36
37#include "base/gtest/logging.hh"
38#include "base/random.hh"
39
40namespace gem5
41{
42
47class RandomTest : public ::testing::Test
48{
49 public:
51 {
52 return Random::instances;
53 }
54
55 static uint64_t getGlobalSeed()
56 {
57 return Random::globalSeed;
58 }
59};
60
61}
62
63using namespace gem5;
65
70TEST(RandomCtor, UInt64DefaultConstruct)
71{
72 ASSERT_EQ(RandomTest::getGlobalSeed(), std::mt19937_64::default_seed);
73 // Init seed was default seed 5489
75 ASSERT_EQ(RandomTest::getGlobalSeed(), std::mt19937_64::default_seed);
76 // First uint64_t corresponds to the default seed
77 ASSERT_EQ(dut->random<uint64_t>(), 14514284786278117030llu);
78}
79
80TEST(RandomCtor, DoubleDefaultConstruct)
81{
82 // Init seed was default seed 5489
84 // First double corresponds to the default seed
85 // 14514284786278117030llu / std::numeric_limits<uint64_t>::max()
86 ASSERT_EQ(dut->random<double>(),
87 0.7868209548678020137657540544751100242137908935546875d);
88}
89
90TEST(RandomCtor, FloatDefaultConstruct)
91{
92 // Init seed was default seed 5489
94 // First float corresponds to the default seed
95 // 14514284786278117030llu / std::numeric_limits<uint64_t>::max()
96 ASSERT_EQ(dut->random<float>(), 0.786820948123931884765625f);
97}
98
103TEST(RandomCtor, ConstructUserSpecifiedSeed)
104{
106 ASSERT_EQ(dut->random<uint64_t>(), 13930160852258120406llu);
107}
108
113TEST(RandomCtor, ConstructThenReseed)
114{
116 ASSERT_EQ(dut->random<uint64_t>(), 14514284786278117030llu);
117 dut->init(42);
118 ASSERT_EQ(dut->random<uint64_t>(), 13930160852258120406llu);
119}
120
125TEST(RandomRange, MinEqualsMax)
126{
128
129 for (int i = 0; i < 10; i++) {
130 ASSERT_EQ(dut->random<int>(0, 0), 0);
131 ASSERT_EQ(dut->random<uint64_t>(1, 1), 1);
132 ASSERT_EQ(dut->random<int>(-1, -1), -1);
133 }
134}
135
136constexpr int loopCount = 30000;
137
146{
147 double freq = ((double) count / loopCount);
148 return freq >= 0.32 && freq <= 0.34;
149}
150
156TEST(RandomRange, Coverage)
157{
159
160 // Count occurences if we want to check
161 // frequencies in the future
162 std::unordered_map<int, int> values;
163
164 for (int i = 0; i < loopCount; i++) {
165 values[dut->random<int>(4,6)]++;
166 }
167
168 ASSERT_EQ(values.count(4), 1);
169 ASSERT_EQ(values.count(5), 1);
170 ASSERT_EQ(values.count(6), 1);
171 ASSERT_EQ(values.size(), 3);
172 ASSERT_EQ(withinFreqRange(values[4]), true);
173 ASSERT_EQ(withinFreqRange(values[5]), true);
174 ASSERT_EQ(withinFreqRange(values[6]), true);
175 values.clear();
176
177 for (int i = 0; i < loopCount; i++) {
178 values[dut->random<int>(-1,1)]++;
179 }
180
181 ASSERT_EQ(values.count(-1), 1);
182 ASSERT_EQ(values.count(0), 1);
183 ASSERT_EQ(values.count(1), 1);
184 ASSERT_EQ(values.size(), 3);
185 ASSERT_EQ(withinFreqRange(values[-1]), true);
186 ASSERT_EQ(withinFreqRange(values[0]), true);
187 ASSERT_EQ(withinFreqRange(values[1]), true);
188
189 values.clear();
190
191 for (int i = 0; i < loopCount; i++) {
192 values[dut->random<int>(-6,-4)]++;
193 }
194
195 ASSERT_EQ(values.count(-6), 1);
196 ASSERT_EQ(values.count(-5), 1);
197 ASSERT_EQ(values.count(-4), 1);
198 ASSERT_EQ(values.size(), 3);
199 ASSERT_EQ(withinFreqRange(values[-6]), true);
200 ASSERT_EQ(withinFreqRange(values[-5]), true);
201 ASSERT_EQ(withinFreqRange(values[-4]), true);
202}
203
204
205
210TEST(RandomConstruct, LiveInstancesAdd)
211{
212 RandomPtr base_rng = Random::genRandom();
213 RandomPtr my_rng = Random::genRandom();
214
215 // All pointers have been added
216 ASSERT_EQ(RandomTest::getInstances()->size(), 2);
217
218 std::set<Random*> ptrs = { base_rng.get(), my_rng.get() };
219
220 // The correct pointers have been added
221 ASSERT_EQ(std::count_if(
222 RandomTest::getInstances()->begin(),
224 [&](const auto & rng) { return ptrs.count(rng.lock().get()) != 0;}), 2);
225}
226
231TEST(RandomConstruct, LiveInstancesRemove)
232{
233 {
234 RandomPtr base_rng = Random::genRandom();
235 {
236 RandomPtr my_rng = Random::genRandom(42);
237 ASSERT_EQ(RandomTest::getInstances()->size(), 2);
238 }
239 ASSERT_EQ(RandomTest::getInstances()->size(), 1);
240 }
241 ASSERT_EQ(RandomTest::getInstances(), nullptr);
242}
243
248TEST(RandomReseed, ConstructThenReseed)
249{
251 ASSERT_EQ(rng->random<uint64_t>(), 14514284786278117030llu);
252 rng->init(42);
253 ASSERT_EQ(rng->random<uint64_t>(), 13930160852258120406llu);
254 ASSERT_EQ(RandomTest::getGlobalSeed(), std::mt19937_64::default_seed);
255}
256
261TEST(RandomReseed, GlobalReseedLive)
262{
263 RandomPtr base_rng = Random::genRandom();
264 RandomPtr my_rng = Random::genRandom(1337);
265
266 ASSERT_EQ(base_rng->random<uint64_t>(), 14514284786278117030llu);
267 ASSERT_EQ(my_rng->random<uint64_t>(), 12913197394697896830llu);
268
270
271 ASSERT_EQ(base_rng->random<uint64_t>(), 13930160852258120406llu);
272 ASSERT_EQ(my_rng->random<uint64_t>(), 13930160852258120406llu);
273}
274
279TEST(RandomReseed, GlobalReseedFuture)
280{
282 RandomPtr base_rng = Random::genRandom();
283 ASSERT_EQ(base_rng->random<uint64_t>(), 13930160852258120406llu);
284}
285
289TEST(RandomDeathTest, InvalidRange)
290{
291#ifdef NDEBUG
292 GTEST_SKIP() << "Skipping as assertions are "
293 "stripped out of fast builds";
294#endif
296 ASSERT_DEATH(dut->random<int>(4, 2), "");
297}
Helper class to access private members of Random.
static Random::Instances * getInstances()
static uint64_t getGlobalSeed()
static uint64_t globalSeed
Definition random.hh:88
std::shared_ptr< Random > RandomPtr
Definition random.hh:65
static void reseedAll(uint64_t seed)
Facility to reseed all live instances and ensure future default constructed instances also use the ne...
Definition random.hh:136
static RandomPtr genRandom()
Definition random.hh:68
static Instances * instances
Collection of all live instances of Random to enable global reseeding.
Definition random.hh:110
STL vector class.
Definition stl.hh:37
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 22 > u
Bitfield< 9 > d
Definition misc_types.hh:64
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
TEST(RandomCtor, UInt64DefaultConstruct)
Checking that default construction uses the default seed as specified by the standard.
bool withinFreqRange(int count)
Helper function for subsequent range tests, returns true if a random number was produced 32% <= freq ...
constexpr int loopCount
gem5::Random::RandomPtr RandomPtr

Generated on Mon Jan 13 2025 04:28:30 for gem5 by doxygen 1.9.8