1 | #include <gtest/gtest.h> |
---|
2 | #include "util/Math.h" |
---|
3 | #include <cmath> |
---|
4 | |
---|
5 | namespace orxonox |
---|
6 | { |
---|
7 | /////////// |
---|
8 | // sgn() // |
---|
9 | /////////// |
---|
10 | TEST(sgn, PositiveInt) |
---|
11 | { |
---|
12 | EXPECT_EQ(1, sgn( 1)); |
---|
13 | EXPECT_EQ(1, sgn(10)); |
---|
14 | } |
---|
15 | |
---|
16 | TEST(sgn, PositiveDouble) |
---|
17 | { |
---|
18 | EXPECT_DOUBLE_EQ(1.0, sgn( 0.1)); |
---|
19 | EXPECT_DOUBLE_EQ(1.0, sgn( 1.0)); |
---|
20 | EXPECT_DOUBLE_EQ(1.0, sgn(10.0)); |
---|
21 | } |
---|
22 | |
---|
23 | TEST(sgn, NegativeInt) |
---|
24 | { |
---|
25 | EXPECT_EQ(-1, sgn( -1)); |
---|
26 | EXPECT_EQ(-1, sgn(-10)); |
---|
27 | } |
---|
28 | |
---|
29 | TEST(sgn, NegativeDouble) |
---|
30 | { |
---|
31 | EXPECT_DOUBLE_EQ(-1.0, sgn( -0.1)); |
---|
32 | EXPECT_DOUBLE_EQ(-1.0, sgn( -1.0)); |
---|
33 | EXPECT_DOUBLE_EQ(-1.0, sgn(-10.0)); |
---|
34 | } |
---|
35 | |
---|
36 | TEST(sgn, ZeroInt) |
---|
37 | { |
---|
38 | EXPECT_EQ(1, sgn(0)); |
---|
39 | } |
---|
40 | |
---|
41 | TEST(sgn, ZeroDouble) |
---|
42 | { |
---|
43 | EXPECT_DOUBLE_EQ(1.0, sgn(0.0)); |
---|
44 | } |
---|
45 | |
---|
46 | ///////////// |
---|
47 | // clamp() // |
---|
48 | ///////////// |
---|
49 | TEST(clamp, InRange) |
---|
50 | { |
---|
51 | // positive |
---|
52 | EXPECT_EQ(10, clamp(10, 10, 20)); |
---|
53 | EXPECT_EQ(15, clamp(15, 10, 20)); |
---|
54 | EXPECT_EQ(20, clamp(20, 10, 20)); |
---|
55 | |
---|
56 | // netagive |
---|
57 | EXPECT_EQ(-10, clamp(-10, -20, -10)); |
---|
58 | EXPECT_EQ(-15, clamp(-15, -20, -10)); |
---|
59 | EXPECT_EQ(-20, clamp(-20, -20, -10)); |
---|
60 | |
---|
61 | // mixed |
---|
62 | EXPECT_EQ(-10, clamp(-10, -10, 10)); |
---|
63 | EXPECT_EQ( 0, clamp( 0, -10, 10)); |
---|
64 | EXPECT_EQ( 10, clamp( 10, -10, 10)); |
---|
65 | } |
---|
66 | |
---|
67 | TEST(clamp, OutOfRange) |
---|
68 | { |
---|
69 | // positive |
---|
70 | EXPECT_EQ(10, clamp( 5, 10, 20)); |
---|
71 | EXPECT_EQ(20, clamp(25, 10, 20)); |
---|
72 | |
---|
73 | // netagive |
---|
74 | EXPECT_EQ(-10, clamp( -5, -20, -10)); |
---|
75 | EXPECT_EQ(-20, clamp(-25, -20, -10)); |
---|
76 | |
---|
77 | // mixed |
---|
78 | EXPECT_EQ(-10, clamp(-15, -10, 10)); |
---|
79 | EXPECT_EQ( 10, clamp( 15, -10, 10)); |
---|
80 | } |
---|
81 | |
---|
82 | ////////////// |
---|
83 | // square() // |
---|
84 | ////////////// |
---|
85 | TEST(square, Int) |
---|
86 | { |
---|
87 | // zero |
---|
88 | EXPECT_EQ( 0, square(0)); |
---|
89 | |
---|
90 | // positive |
---|
91 | EXPECT_EQ(1, square(1)); |
---|
92 | EXPECT_EQ(4, square(2)); |
---|
93 | EXPECT_EQ(9, square(3)); |
---|
94 | |
---|
95 | // negative |
---|
96 | EXPECT_EQ(1, square(-1)); |
---|
97 | EXPECT_EQ(4, square(-2)); |
---|
98 | EXPECT_EQ(9, square(-3)); |
---|
99 | } |
---|
100 | |
---|
101 | TEST(square, Double) |
---|
102 | { |
---|
103 | // zero |
---|
104 | EXPECT_DOUBLE_EQ( 0.0, square(0.0)); |
---|
105 | |
---|
106 | // positive |
---|
107 | EXPECT_DOUBLE_EQ( 1.00, square(1.0)); |
---|
108 | EXPECT_DOUBLE_EQ( 2.25, square(1.5)); |
---|
109 | EXPECT_DOUBLE_EQ( 4.00, square(2.0)); |
---|
110 | |
---|
111 | // negative |
---|
112 | EXPECT_DOUBLE_EQ( 1.00, square(-1.0)); |
---|
113 | EXPECT_DOUBLE_EQ( 2.25, square(-1.5)); |
---|
114 | EXPECT_DOUBLE_EQ( 4.00, square(-2.0)); |
---|
115 | } |
---|
116 | |
---|
117 | //////////// |
---|
118 | // cube() // |
---|
119 | //////////// |
---|
120 | TEST(cube, Int) |
---|
121 | { |
---|
122 | // zero |
---|
123 | EXPECT_EQ( 0, cube(0)); |
---|
124 | |
---|
125 | // positive |
---|
126 | EXPECT_EQ( 1, cube(1)); |
---|
127 | EXPECT_EQ( 8, cube(2)); |
---|
128 | EXPECT_EQ(27, cube(3)); |
---|
129 | |
---|
130 | // negative |
---|
131 | EXPECT_EQ( -1, cube(-1)); |
---|
132 | EXPECT_EQ( -8, cube(-2)); |
---|
133 | EXPECT_EQ(-27, cube(-3)); |
---|
134 | } |
---|
135 | |
---|
136 | TEST(cube, Double) |
---|
137 | { |
---|
138 | // zero |
---|
139 | EXPECT_DOUBLE_EQ( 0.0, cube(0.0)); |
---|
140 | |
---|
141 | // positive |
---|
142 | EXPECT_DOUBLE_EQ( 1.000, cube(1.0)); |
---|
143 | EXPECT_DOUBLE_EQ( 3.375, cube(1.5)); |
---|
144 | EXPECT_DOUBLE_EQ( 8.000, cube(2.0)); |
---|
145 | |
---|
146 | // negative |
---|
147 | EXPECT_DOUBLE_EQ( -1.000, cube(-1.0)); |
---|
148 | EXPECT_DOUBLE_EQ( -3.375, cube(-1.5)); |
---|
149 | EXPECT_DOUBLE_EQ( -8.000, cube(-2.0)); |
---|
150 | } |
---|
151 | |
---|
152 | /////////// |
---|
153 | // mod() // |
---|
154 | /////////// |
---|
155 | TEST(mod, ModOperator) |
---|
156 | { |
---|
157 | // zero |
---|
158 | EXPECT_EQ(0, 0 % 10); |
---|
159 | |
---|
160 | // positive |
---|
161 | EXPECT_EQ(1, 1 % 10); |
---|
162 | EXPECT_EQ(5, 5 % 10); |
---|
163 | EXPECT_EQ(9, 9 % 10); |
---|
164 | EXPECT_EQ(0, 10 % 10); |
---|
165 | EXPECT_EQ(5, 15 % 10); |
---|
166 | |
---|
167 | // negative |
---|
168 | EXPECT_EQ(-1, -1 % 10); |
---|
169 | EXPECT_EQ(-5, -5 % 10); |
---|
170 | EXPECT_EQ(-9, -9 % 10); |
---|
171 | EXPECT_EQ( 0, -10 % 10); |
---|
172 | EXPECT_EQ(-5, -15 % 10); |
---|
173 | } |
---|
174 | |
---|
175 | TEST(mod, ModFunction) |
---|
176 | { |
---|
177 | // zero |
---|
178 | EXPECT_EQ(0, mod(0, 10)); |
---|
179 | |
---|
180 | // positive |
---|
181 | EXPECT_EQ(1, mod( 1, 10)); |
---|
182 | EXPECT_EQ(5, mod( 5, 10)); |
---|
183 | EXPECT_EQ(9, mod( 9, 10)); |
---|
184 | EXPECT_EQ(0, mod(10, 10)); |
---|
185 | EXPECT_EQ(5, mod(15, 10)); |
---|
186 | |
---|
187 | // negative |
---|
188 | EXPECT_EQ(9, mod( -1, 10)); |
---|
189 | EXPECT_EQ(5, mod( -5, 10)); |
---|
190 | EXPECT_EQ(1, mod( -9, 10)); |
---|
191 | EXPECT_EQ(0, mod(-10, 10)); |
---|
192 | EXPECT_EQ(5, mod(-15, 10)); |
---|
193 | } |
---|
194 | |
---|
195 | /////////////// |
---|
196 | // zeroise() // |
---|
197 | /////////////// |
---|
198 | TEST(zeroise, Test) |
---|
199 | { |
---|
200 | EXPECT_EQ(0, zeroise<int>()); |
---|
201 | EXPECT_EQ((unsigned int)0, zeroise<unsigned int>()); |
---|
202 | EXPECT_DOUBLE_EQ(0.0, zeroise<double>()); |
---|
203 | EXPECT_EQ("", zeroise<std::string>()); |
---|
204 | EXPECT_EQ(Vector3::ZERO, zeroise<Vector3>()); |
---|
205 | } |
---|
206 | |
---|
207 | /////////////////// |
---|
208 | // interpolate() // |
---|
209 | /////////////////// |
---|
210 | TEST(interpolate, Linear) |
---|
211 | { |
---|
212 | EXPECT_EQ( 0, interpolate(0.00f, 0, 20)); |
---|
213 | EXPECT_EQ( 5, interpolate(0.25f, 0, 20)); |
---|
214 | EXPECT_EQ(10, interpolate(0.50f, 0, 20)); |
---|
215 | EXPECT_EQ(15, interpolate(0.75f, 0, 20)); |
---|
216 | EXPECT_EQ(20, interpolate(1.00f, 0, 20)); |
---|
217 | } |
---|
218 | |
---|
219 | TEST(interpolate, Smooth) |
---|
220 | { |
---|
221 | EXPECT_EQ( 0, interpolateSmooth(0.00f, 0, 20)); |
---|
222 | EXPECT_GT( 5, interpolateSmooth(0.25f, 0, 20)); |
---|
223 | EXPECT_EQ(10, interpolateSmooth(0.50f, 0, 20)); |
---|
224 | EXPECT_LT(15, interpolateSmooth(0.75f, 0, 20)); |
---|
225 | EXPECT_EQ(20, interpolateSmooth(1.00f, 0, 20)); |
---|
226 | } |
---|
227 | |
---|
228 | /////////// |
---|
229 | // rnd() // |
---|
230 | /////////// |
---|
231 | TEST(rnd, Default) |
---|
232 | { |
---|
233 | // how to test randomness? |
---|
234 | std::set<float> numbers; |
---|
235 | |
---|
236 | const float max = 1.0; |
---|
237 | const float min = 0.0; |
---|
238 | const size_t iterations = 1000; |
---|
239 | |
---|
240 | for (size_t i = 0; i < iterations; ++i) |
---|
241 | numbers.insert(rnd()); |
---|
242 | |
---|
243 | // must have at most <iterations> numbers in the set |
---|
244 | EXPECT_GE(iterations, numbers.size()); |
---|
245 | |
---|
246 | // all numbers must satisfy 0 <= <number> < 1 |
---|
247 | for (float number : numbers) |
---|
248 | { |
---|
249 | EXPECT_LE(min, number); |
---|
250 | EXPECT_GT(max, number); |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | TEST(rnd, Max) |
---|
255 | { |
---|
256 | // how to test randomness? |
---|
257 | std::set<float> numbers; |
---|
258 | |
---|
259 | const float max = 10.0; |
---|
260 | const float min = 0.0; |
---|
261 | const size_t iterations = 1000; |
---|
262 | |
---|
263 | for (size_t i = 0; i < iterations; ++i) |
---|
264 | numbers.insert(rnd(max)); |
---|
265 | |
---|
266 | // must have at most <iterations> numbers in the set |
---|
267 | EXPECT_GE(iterations, numbers.size()); |
---|
268 | |
---|
269 | // all numbers must satisfy 0 <= <number> < <max> |
---|
270 | for (float number : numbers) |
---|
271 | { |
---|
272 | EXPECT_LE(min, number); |
---|
273 | EXPECT_GT(max, number); |
---|
274 | } |
---|
275 | } |
---|
276 | |
---|
277 | TEST(rnd, MinMax) |
---|
278 | { |
---|
279 | // how to test randomness? |
---|
280 | std::set<float> numbers; |
---|
281 | |
---|
282 | const float max = 10.0; |
---|
283 | const float min = 5.0; |
---|
284 | const size_t iterations = 1000; |
---|
285 | |
---|
286 | for (size_t i = 0; i < iterations; ++i) |
---|
287 | numbers.insert(rnd(min, max)); |
---|
288 | |
---|
289 | // must have at most <iterations> numbers in the set |
---|
290 | EXPECT_GE(iterations, numbers.size()); |
---|
291 | |
---|
292 | // all numbers must satisfy <min> <= <number> < <max> |
---|
293 | for (float number : numbers) |
---|
294 | { |
---|
295 | EXPECT_LE(min, number); |
---|
296 | EXPECT_GT(max, number); |
---|
297 | } |
---|
298 | } |
---|
299 | |
---|
300 | ////////////// |
---|
301 | // rndsgn() // |
---|
302 | ////////////// |
---|
303 | TEST(rndsgn, Test) |
---|
304 | { |
---|
305 | // how to test randomness? |
---|
306 | std::set<float> numbers; |
---|
307 | |
---|
308 | const size_t iterations = 100; |
---|
309 | |
---|
310 | for (size_t i = 0; i < iterations; ++i) |
---|
311 | numbers.insert(rndsgn()); |
---|
312 | |
---|
313 | // must have at most 2 numbers in the set |
---|
314 | EXPECT_GE((size_t)2, numbers.size()); |
---|
315 | |
---|
316 | // all numbers must be either 1 oder -1 |
---|
317 | for (float number : numbers) |
---|
318 | EXPECT_TRUE(number == 1 || number == -1); |
---|
319 | } |
---|
320 | |
---|
321 | /////////////////////// |
---|
322 | // getUniqueNumber() // |
---|
323 | /////////////////////// |
---|
324 | TEST(getUniqueNumber, Test) |
---|
325 | { |
---|
326 | std::set<unsigned long> numbers; |
---|
327 | |
---|
328 | const size_t iterations = 1000; |
---|
329 | |
---|
330 | for (size_t i = 0; i < iterations; ++i) |
---|
331 | numbers.insert(getUniqueNumber()); |
---|
332 | |
---|
333 | // must have exactly <iterations> numbers in the set, since all numbers should be unique |
---|
334 | EXPECT_EQ(iterations, numbers.size()); |
---|
335 | } |
---|
336 | /* |
---|
337 | getAngle |
---|
338 | get2DViewdirection |
---|
339 | get2DViewcoordinates |
---|
340 | getPredictedPosition |
---|
341 | */ |
---|
342 | } |
---|