Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/unittest/src/utils/common/RandHelperTest.cpp
169684 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.
4
// This program and the accompanying materials are made available under the
5
// terms of the Eclipse Public License 2.0 which is available at
6
// https://www.eclipse.org/legal/epl-2.0/
7
// This Source Code may also be made available under the following Secondary
8
// Licenses when the conditions for such availability set forth in the Eclipse
9
// Public License 2.0 are satisfied: GNU General Public License, version 2
10
// or later which is available at
11
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
/****************************************************************************/
14
/// @file RandHelperTest.cpp
15
/// @author Michael Behrisch
16
/// @date Oct 2010
17
///
18
// Tests the class RandHelper
19
/****************************************************************************/
20
21
22
// ===========================================================================
23
// included modules
24
// ===========================================================================
25
#include <config.h>
26
27
#include <gtest/gtest.h>
28
#include <utils/common/StdDefs.h>
29
#include <utils/common/StopWatch.h>
30
#include <utils/common/RandHelper.h>
31
32
33
// ===========================================================================
34
// test definitions
35
// ===========================================================================
36
/* Test the method 'rand' without parameters.*/
37
TEST(RandHelper, test_rand_range) {
38
for (int i = 0; i < 1000; i++) {
39
const double rand = RandHelper::rand();
40
EXPECT_LT(rand, double(1));
41
EXPECT_LE(0., rand);
42
}
43
}
44
45
/* Test the method 'rand' with an upper float limit.*/
46
TEST(RandHelper, test_rand_range_float) {
47
for (int i = 0; i < 1000; i++) {
48
const double rand = RandHelper::rand(double(10));
49
EXPECT_LT(rand, double(10));
50
EXPECT_LE(0., rand);
51
}
52
}
53
54
/* Test the method 'rand' with an upper int limit.*/
55
TEST(RandHelper, test_rand_range_int) {
56
for (int i = 0; i < 1000; i++) {
57
const double rand = RandHelper::rand(100);
58
EXPECT_LT(rand, 100);
59
EXPECT_LE(0, rand);
60
}
61
}
62
63
/* Test the method 'rand' with two float limits.*/
64
TEST(RandHelper, test_rand_range_two_float) {
65
for (int i = 0; i < 1000; i++) {
66
const double rand = RandHelper::rand(double(0.1), double(0.5));
67
EXPECT_LT(rand, double(0.5));
68
EXPECT_LE(double(0.1), rand);
69
}
70
}
71
72
/* Test the method 'rand' with two int limits.*/
73
TEST(RandHelper, test_rand_range_two_int) {
74
for (int i = 0; i < 1000; i++) {
75
const double rand = RandHelper::rand(50, 100);
76
EXPECT_LT(rand, 100);
77
EXPECT_LE(50, rand);
78
}
79
}
80
81
/* Test whether the 'rand' distribution is more or less uniform.*/
82
TEST(RandHelper, test_uniform) {
83
int count[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
84
for (int i = 0; i < 1000; i++) {
85
const double rand = RandHelper::rand(0., double(10));
86
count[(int)rand]++;
87
}
88
for (int i = 0; i < 10; i++) {
89
EXPECT_LE(50, count[i]) << "Testing interval " << i;
90
EXPECT_LT(count[i], 150) << "Testing interval " << i;
91
}
92
}
93
94
/* Test whether the 'randNorm' distribution is more or less gaussian.*/
95
TEST(RandHelper, test_norm) {
96
int count[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
97
for (int i = 0; i < 1000; i++) {
98
const double rand = RandHelper::randNorm(double(5), double(2));
99
count[MIN2(MAX2((int)rand, 0), 9)]++;
100
}
101
EXPECT_LE(0, count[0]);
102
EXPECT_LT(count[0], 100);
103
EXPECT_LE(0, count[1]);
104
EXPECT_LT(count[1], 100);
105
EXPECT_LE(50, count[2]);
106
EXPECT_LT(count[2], 150);
107
EXPECT_LE(100, count[3]);
108
EXPECT_LT(count[3], 200);
109
EXPECT_LE(100, count[4]);
110
EXPECT_LT(count[4], 250);
111
EXPECT_LE(100, count[5]);
112
EXPECT_LT(count[5], 250);
113
EXPECT_LE(100, count[6]);
114
EXPECT_LT(count[6], 200);
115
EXPECT_LE(0, count[7]);
116
EXPECT_LT(count[7], 100);
117
EXPECT_LE(0, count[8]);
118
EXPECT_LT(count[8], 100);
119
EXPECT_LE(0, count[9]);
120
EXPECT_LT(count[9], 100);
121
}
122
123
/* Test whether the 'rand' sequence is always the same.*/
124
TEST(RandHelper, test_sequence) {
125
RandHelper::initRand();
126
int expect[] = { 46, 17, 19, 75, 11, 42, 28, 22, 77, 11 };
127
for (int i = 0; i < 10; i++) {
128
EXPECT_EQ(expect[i], RandHelper::rand(100));
129
}
130
}
131
132
/* Test the performance of the method 'rand' without parameters.*/
133
// TEST(RandHelper, test_perf) {
134
// StopWatch sw;
135
// sw.start();
136
// for (int i = 0; i < 100000000; i++) {
137
// const double rand = RandHelper::rand();
138
// }
139
// std::cout << sw.stop();
140
// }
141
142