Helios++
Helios software for LiDAR simulations
RandomTest.h
1 #pragma once
2 
3 #include <noise/RandomnessGenerator.h>
4 
5 #if defined(_WIN32) || defined(_WIN64)
6 #define exND1 0.300623, -1.42744, 0.0473341, -0.51204, -1.43744
7 #else
8 #define exND1 -1.42744, 0.300623, -0.51204, 0.0473341, 0.500384
9 #endif
10 
11 namespace HeliosTests {
12 
18 class RandomTest : public BaseTest {
19 public:
20  // *** CONSTRUCTION *** //
21  // ********************** //
25  RandomTest() : BaseTest("Randomness generation test"){}
26 
27  // *** R U N *** //
28  // *************** //
32  bool run() override;
33 };
34 
36  const double eps = 0.0001;
37  double diff;
38 
39  // Test random uniform real distribution generation with long seed
40  double expectedURD1[] = {0.997185, 0.932557, 0.128124, 0.999041, 0.236089};
42  rg1.computeUniformRealDistribution(0.0, 1.0);
43  for(size_t i = 0 ; i < 5 ; i++){
44  diff = rg1.uniformRealDistributionNext() - expectedURD1[i];
45  if(diff < -eps || diff > eps) return false;
46  }
47 
48  // Test random uniform real distribution generation with double seed
49  double expectedURD2[] = {
50  -0.629836, 0.863082, 0.895461, -0.0305018, -0.358927
51  };
53  rg2.computeUniformRealDistribution(-1.0, 1.0);
54  for(size_t i = 0 ; i < 5 ; i++){
55  diff = rg2.uniformRealDistributionNext() - expectedURD2[i];
56  if(diff < -eps || diff > eps) return false;
57  }
58 
59  // Test random uniform real distribution with automatically generation
61  diff = rg3.uniformRealDistributionNext();
62  for(size_t i = 0 ; i < 4 ; i++){
63  // This comparison may lead to false fails, but not too often
64  if(rg3.uniformRealDistributionNext()==diff) return false;
65  }
66 
67  // Test random uniform real distribution with parse long seed
68  double expectedURD4[] = {
69  0.834397,
70  0.529073,
71  0.497834,
72  0.685763,
73  0.97071
74  };
75  RandomnessGenerator<double> rg4("256");
76  for(size_t i = 0 ; i < 5 ; i++){
77  diff = rg4.uniformRealDistributionNext() - expectedURD4[i];
78  if(diff < -eps || diff > eps) return false;
79  }
80 
81  // Test random uniform real distribution with parse double seed
82  double expectedURD5[] = {
83  22.7339,
84  31.8972,
85  97.8223,
86  45.5585,
87  30.8013
88  };
89  RandomnessGenerator<double> rg5("7.9");
90  rg5.computeUniformRealDistribution(0.0, 100.0);
91  for(size_t i = 0 ; i < 5 ; i++){
92  diff = rg5.uniformRealDistributionNext() - expectedURD5[i];
93  if(diff < -eps || diff > eps) return false;
94  }
95 
96  // Test random uniform real distribution with parse string timestamp seed
97  double expectedURD6[] = {
98  19.3035,
99  -78.9788,
100  34.9037,
101  -90.7673,
102  -50.6285
103  };
104  RandomnessGenerator<double> rg6("2000-08-22 11:30:27");
105  rg6.computeUniformRealDistribution(-100.0, 50.0);
106  for(size_t i = 0 ; i < 5 ; i++){
107  diff = rg6.uniformRealDistributionNext() - expectedURD6[i];
108  if(diff < -eps || diff > eps) return false;
109  }
110 
111  // Test normal distribution
112  double expectedND1[] = {exND1};
113  RandomnessGenerator<double> rg7(1337);
114  for(size_t i = 0 ; i < 5 ; i++){
115  diff = rg7.normalDistributionNext() - expectedND1[i];
116  if(diff < -eps || diff > eps) return false;
117  }
118 
119  // Test swap, copy/move constructor and copy/move assignment
120  double expectedURD8[] = {
121  0.150989,
122  0.995869,
123  0.214838,
124  0.0899676,
125  0.18734,
126  0.0592914
127  };
129  diff = rg8.uniformRealDistributionNext() - expectedURD8[0];
130  if(diff < -eps || diff > eps) return false;
131  RandomnessGenerator<double> rg8c(rg8);
132  diff = rg8c.uniformRealDistributionNext() - expectedURD8[1];
133  if(diff < -eps || diff > eps) return false;
134  rg8 = rg8c;
135  diff = rg8.uniformRealDistributionNext() - expectedURD8[2];
136  if(diff < -eps || diff > eps) return false;
137  rg8c = std::move(rg8);
138  diff = rg8c.uniformRealDistributionNext() - expectedURD8[3];
139  if(diff < -eps || diff > eps) return false;
140  RandomnessGenerator<double> rg8m(std::move(rg8c));
141  diff = rg8m.uniformRealDistributionNext() - expectedURD8[4];
142  if(diff < -eps || diff > eps) return false;
143  std::swap(rg8, rg8m);
144  diff = rg8.uniformRealDistributionNext() - expectedURD8[5];
145  if(diff < -eps || diff > eps) return false;
146 
147  // Test DEFAULT_RG behavior
148  RandomnessGenerator<double> drgCopy1(*DEFAULT_RG);
149  RandomnessGenerator<double> drgCopy2(*DEFAULT_RG);
150  for(size_t i = 0 ; i < 5 ; i++){
151  if(drgCopy1.uniformRealDistributionNext() ==
152  drgCopy2.uniformRealDistributionNext()
153  ){
154  // It could lead to false fails, but not too often
155  return false;
156  }
157  if(drgCopy1.normalDistributionNext() ==
158  drgCopy2.normalDistributionNext()
159  ){
160  // It could lead to false fails, but not too often
161  return false;
162  }
163  }
164  DEFAULT_RG->computeUniformRealDistribution(-1.0, 1.0);
165  DEFAULT_RG->computeNormalDistribution(4.0, 2.0);
166  RandomnessGenerator<double> drgCopy3(*DEFAULT_RG);
167  RandomnessGenerator<double> drgCopy4(*DEFAULT_RG);
168  for(size_t i = 0 ; i < 5 ; i++){
169  if(drgCopy3.uniformRealDistributionNext() !=
170  drgCopy4.uniformRealDistributionNext()
171  ){
172  return false;
173  }
174  if(drgCopy3.normalDistributionNext() !=
175  drgCopy4.normalDistributionNext()
176  ){
177  return false;
178  }
179  }
180 
181 
182 
183  // Successfully reached end of test
184  return true;
185 }
186 
187 
188 }
RandomTest()
Random test constructor.
Definition: RandomTest.h:25
BaseTest class.
Definition: BaseTest.h:19
Definition: BaseTest.h:6
bool run() override
Definition: RandomTest.h:35
RealType uniformRealDistributionNext()
Obtain the next value in the computed uniform real distribution.
Definition: RandomnessGenerator.h:374
Test for randomness generation.
Definition: RandomTest.h:18
RealType normalDistributionNext()
Obtain the next value in the computed normal distribution.
Definition: RandomnessGenerator.h:412
void computeUniformRealDistribution(RealType lowerBound, RealType upperBound)
Compute a uniform real distribution using the specified real data type.
Definition: RandomnessGenerator.h:343