Line data Source code
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 1 : TEST(RandHelper, test_rand_range) {
38 1001 : for (int i = 0; i < 1000; i++) {
39 1000 : const double rand = RandHelper::rand();
40 1000 : EXPECT_LT(rand, double(1));
41 1000 : EXPECT_LE(0., rand);
42 : }
43 1 : }
44 :
45 : /* Test the method 'rand' with an upper float limit.*/
46 1 : TEST(RandHelper, test_rand_range_float) {
47 1001 : for (int i = 0; i < 1000; i++) {
48 1000 : const double rand = RandHelper::rand(double(10));
49 1000 : EXPECT_LT(rand, double(10));
50 1000 : EXPECT_LE(0., rand);
51 : }
52 1 : }
53 :
54 : /* Test the method 'rand' with an upper int limit.*/
55 1 : TEST(RandHelper, test_rand_range_int) {
56 1001 : for (int i = 0; i < 1000; i++) {
57 1000 : const double rand = RandHelper::rand(100);
58 1000 : EXPECT_LT(rand, 100);
59 1000 : EXPECT_LE(0, rand);
60 : }
61 1 : }
62 :
63 : /* Test the method 'rand' with two float limits.*/
64 1 : TEST(RandHelper, test_rand_range_two_float) {
65 1001 : for (int i = 0; i < 1000; i++) {
66 1000 : const double rand = RandHelper::rand(double(0.1), double(0.5));
67 1000 : EXPECT_LT(rand, double(0.5));
68 1000 : EXPECT_LE(double(0.1), rand);
69 : }
70 1 : }
71 :
72 : /* Test the method 'rand' with two int limits.*/
73 1 : TEST(RandHelper, test_rand_range_two_int) {
74 1001 : for (int i = 0; i < 1000; i++) {
75 1000 : const double rand = RandHelper::rand(50, 100);
76 1000 : EXPECT_LT(rand, 100);
77 1000 : EXPECT_LE(50, rand);
78 : }
79 1 : }
80 :
81 : /* Test whether the 'rand' distribution is more or less uniform.*/
82 1 : TEST(RandHelper, test_uniform) {
83 1 : int count[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
84 1001 : for (int i = 0; i < 1000; i++) {
85 : const double rand = RandHelper::rand(0., double(10));
86 1000 : count[(int)rand]++;
87 : }
88 11 : for (int i = 0; i < 10; i++) {
89 10 : EXPECT_LE(50, count[i]) << "Testing interval " << i;
90 10 : EXPECT_LT(count[i], 150) << "Testing interval " << i;
91 : }
92 1 : }
93 :
94 : /* Test whether the 'randNorm' distribution is more or less gaussian.*/
95 1 : TEST(RandHelper, test_norm) {
96 1 : int count[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
97 1001 : for (int i = 0; i < 1000; i++) {
98 1000 : const double rand = RandHelper::randNorm(double(5), double(2));
99 1000 : count[MIN2(MAX2((int)rand, 0), 9)]++;
100 : }
101 1 : EXPECT_LE(0, count[0]);
102 1 : EXPECT_LT(count[0], 100);
103 1 : EXPECT_LE(0, count[1]);
104 1 : EXPECT_LT(count[1], 100);
105 1 : EXPECT_LE(50, count[2]);
106 1 : EXPECT_LT(count[2], 150);
107 1 : EXPECT_LE(100, count[3]);
108 1 : EXPECT_LT(count[3], 200);
109 1 : EXPECT_LE(100, count[4]);
110 1 : EXPECT_LT(count[4], 250);
111 1 : EXPECT_LE(100, count[5]);
112 1 : EXPECT_LT(count[5], 250);
113 1 : EXPECT_LE(100, count[6]);
114 1 : EXPECT_LT(count[6], 200);
115 1 : EXPECT_LE(0, count[7]);
116 1 : EXPECT_LT(count[7], 100);
117 1 : EXPECT_LE(0, count[8]);
118 1 : EXPECT_LT(count[8], 100);
119 1 : EXPECT_LE(0, count[9]);
120 1 : EXPECT_LT(count[9], 100);
121 1 : }
122 :
123 : /* Test whether the 'rand' sequence is always the same.*/
124 1 : TEST(RandHelper, test_sequence) {
125 1 : RandHelper::initRand();
126 1 : int expect[] = { 46, 17, 19, 75, 11, 42, 28, 22, 77, 11 };
127 11 : for (int i = 0; i < 10; i++) {
128 10 : EXPECT_EQ(expect[i], RandHelper::rand(100));
129 : }
130 1 : }
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 : // }
|