Debian Codegraph @

src/external/catch2/tests/SelfTest/IntrospectiveTests/RandomNumberGeneration.tests.cpp

raw
1
2 // Copyright Catch2 Authors
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE.txt or copy at
5 // https://www.boost.org/LICENSE_1_0.txt)
6
7 // SPDX-License-Identifier: BSL-1.0
8
9 #include <catch2/catch_test_macros.hpp>
10 #include <catch2/catch_template_test_macros.hpp>
11 #include <catch2/internal/catch_floating_point_helpers.hpp>
12 #include <catch2/internal/catch_random_integer_helpers.hpp>
13 #include <catch2/internal/catch_random_number_generator.hpp>
14 #include <catch2/internal/catch_random_seed_generation.hpp>
15 #include <catch2/internal/catch_uniform_floating_point_distribution.hpp>
16 #include <catch2/internal/catch_uniform_integer_distribution.hpp>
17 #include <catch2/generators/catch_generators.hpp>
18 #include <catch2/matchers/catch_matchers_range_equals.hpp>
19
20 #include <random>
21
22 TEST_CASE("Our PCG implementation provides expected results for known seeds", "[rng]") {
23 Catch::SimplePcg32 rng;
24 SECTION("Default seeded") {
25 REQUIRE(rng() == 0xfcdb943b);
26 REQUIRE(rng() == 0x6f55b921);
27 REQUIRE(rng() == 0x4c17a916);
28 REQUIRE(rng() == 0x71eae25f);
29 REQUIRE(rng() == 0x6ce7909c);
30 }
31 SECTION("Specific seed") {
32 rng.seed(0xabcd1234);
33 REQUIRE(rng() == 0x57c08495);
34 REQUIRE(rng() == 0x33c956ac);
35 REQUIRE(rng() == 0x2206fd76);
36 REQUIRE(rng() == 0x3501a35b);
37 REQUIRE(rng() == 0xfdffb30f);
38
39 // Also check repeated output after reseeding
40 rng.seed(0xabcd1234);
41 REQUIRE(rng() == 0x57c08495);
42 REQUIRE(rng() == 0x33c956ac);
43 REQUIRE(rng() == 0x2206fd76);
44 REQUIRE(rng() == 0x3501a35b);
45 REQUIRE(rng() == 0xfdffb30f);
46 }
47 }
48
49 TEST_CASE("Comparison ops", "[rng]") {
50 using Catch::SimplePcg32;
51 REQUIRE(SimplePcg32{} == SimplePcg32{});
52 REQUIRE(SimplePcg32{ 0 } != SimplePcg32{});
53 REQUIRE_FALSE(SimplePcg32{ 1 } == SimplePcg32{ 2 });
54 REQUIRE_FALSE(SimplePcg32{ 1 } != SimplePcg32{ 1 });
55 }
56
57 TEST_CASE("Random seed generation reports unknown methods", "[rng][seed]") {
58 REQUIRE_THROWS(Catch::generateRandomSeed(static_cast<Catch::GenerateFrom>(77)));
59 }
60
61 TEST_CASE("Random seed generation accepts known methods", "[rng][seed]") {
62 using Catch::GenerateFrom;
63 const auto method = GENERATE(
64 GenerateFrom::Time,
65 GenerateFrom::RandomDevice,
66 GenerateFrom::Default
67 );
68
69 REQUIRE_NOTHROW(Catch::generateRandomSeed(method));
70 }
71
72 TEMPLATE_TEST_CASE("uniform_floating_point_distribution never returns infs from finite range",
73 "[rng][distribution][floating-point][approvals]", float, double) {
74 std::random_device rd{};
75 Catch::SimplePcg32 pcg( rd() );
76 Catch::uniform_floating_point_distribution<TestType> dist(
77 -std::numeric_limits<TestType>::max(),
78 std::numeric_limits<TestType>::max() );
79
80 for (size_t i = 0; i < 10'000; ++i) {
81 auto ret = dist( pcg );
82 REQUIRE_FALSE( std::isinf( ret ) );
83 REQUIRE_FALSE( std::isnan( ret ) );
84 }
85 }
86
87 TEST_CASE( "fillBitsFrom - shortening and stretching", "[rng][approvals]" ) {
88 using Catch::Detail::fillBitsFrom;
89
90 // The seed is not important, but the numbers below have to be repeatable.
91 // They should also exhibit the same general pattern of being prefixes
92 Catch::SimplePcg32 pcg( 0xaabb'ccdd );
93
94 SECTION( "Shorten to 8 bits" ) {
95 // We cast the result to avoid dealing with char-like type in uint8_t
96 auto shortened = static_cast<uint32_t>( fillBitsFrom<uint8_t>( pcg ) );
97 REQUIRE( shortened == 0xcc );
98 }
99 SECTION( "Shorten to 16 bits" ) {
100 auto shortened = fillBitsFrom<uint16_t>( pcg );
101 REQUIRE( shortened == 0xccbe );
102 }
103 SECTION( "Keep at 32 bits" ) {
104 auto n = fillBitsFrom<uint32_t>( pcg );
105 REQUIRE( n == 0xccbe'5f04 );
106 }
107 SECTION( "Stretch to 64 bits" ) {
108 auto stretched = fillBitsFrom<uint64_t>( pcg );
109 REQUIRE( stretched == 0xccbe'5f04'a424'a486 );
110 }
111 }
112
113 TEST_CASE("uniform_integer_distribution can return the bounds", "[rng][distribution]") {
114 Catch::uniform_integer_distribution<int32_t> dist( -10, 10 );
115 REQUIRE( dist.a() == -10 );
116 REQUIRE( dist.b() == 10 );
117 }
118
119 namespace {
120 template <typename T>
121 static void CheckReturnValue(Catch::uniform_integer_distribution<T>& dist,
122 Catch::SimplePcg32& rng,
123 T target) {
124 REQUIRE( dist.a() == dist.b() );
125 for (int i = 0; i < 1'000; ++i) {
126 REQUIRE( dist( rng ) == target );
127 }
128 }
129 }
130
131 TEMPLATE_TEST_CASE( "uniform_integer_distribution can handle unit ranges",
132 "[rng][distribution][approvals]",
133 unsigned char,
134 signed char,
135 char,
136 uint8_t,
137 int8_t,
138 uint16_t,
139 int16_t,
140 uint32_t,
141 int32_t,
142 uint64_t,
143 int64_t,
144 size_t,
145 ptrdiff_t) {
146 // We want random seed to sample different parts of the rng state,
147 // the output is predetermined anyway
148 std::random_device rd;
149 auto seed = rd();
150 CAPTURE( seed );
151 Catch::SimplePcg32 pcg( seed );
152
153 // We check unitary ranges of 3 different values, min for type, max for type,
154 // some value in between just to make sure
155 SECTION("lowest value") {
156 constexpr auto lowest = std::numeric_limits<TestType>::min();
157 Catch::uniform_integer_distribution<TestType> dist( lowest, lowest );
158 CheckReturnValue( dist, pcg, lowest );
159 }
160 SECTION( "highest value" ) {
161 constexpr auto highest = std::numeric_limits<TestType>::max();
162 Catch::uniform_integer_distribution<TestType> dist( highest, highest );
163 CheckReturnValue( dist, pcg, highest );
164 }
165 SECTION( "some value" ) {
166 constexpr auto some = TestType( 42 );
167 Catch::uniform_integer_distribution<TestType> dist( some, some );
168 CheckReturnValue( dist, pcg, some );
169 }
170 }
171
172 // Bool needs its own test because it doesn't have a valid "third" value
173 TEST_CASE( "uniform_integer_distribution can handle boolean unit ranges",
174 "[rng][distribution][approvals]" ) {
175 // We want random seed to sample different parts of the rng state,
176 // the output is predetermined anyway
177 std::random_device rd;
178 auto seed = rd();
179 CAPTURE( seed );
180 Catch::SimplePcg32 pcg( seed );
181
182 // We check unitary ranges of 3 different values, min for type, max for
183 // type, some value in between just to make sure
184 SECTION( "lowest value" ) {
185 Catch::uniform_integer_distribution<bool> dist( false, false );
186 CheckReturnValue( dist, pcg, false );
187 }
188 SECTION( "highest value" ) {
189 Catch::uniform_integer_distribution<bool> dist( true, true );
190 CheckReturnValue( dist, pcg, true );
191 }
192 }
193
194 TEMPLATE_TEST_CASE( "uniform_integer_distribution can handle full width ranges",
195 "[rng][distribution][approvals]",
196 unsigned char,
197 signed char,
198 char,
199 uint8_t,
200 int8_t,
201 uint16_t,
202 int16_t,
203 uint32_t,
204 int32_t,
205 uint64_t,
206 int64_t ) {
207 // We want random seed to sample different parts of the rng state,
208 // the output is predetermined anyway
209 std::random_device rd;
210 auto seed = rd();
211 CAPTURE( seed );
212 Catch::SimplePcg32 pcg( seed );
213
214 constexpr auto lowest = std::numeric_limits<TestType>::min();
215 constexpr auto highest = std::numeric_limits<TestType>::max();
216 Catch::uniform_integer_distribution<TestType> dist( lowest, highest );
217 STATIC_REQUIRE( std::is_same<TestType, decltype( dist( pcg ) )>::value );
218
219 // We need to do bit operations on the results, so we will have to
220 // cast them to unsigned type.
221 using BitType = std::make_unsigned_t<TestType>;
222 BitType ORs = 0;
223 BitType ANDs = BitType(-1);
224 for (int i = 0; i < 100; ++i) {
225 auto bits = static_cast<BitType>( dist( pcg ) );
226 ORs |= bits;
227 ANDs &= bits;
228 }
229 // Assuming both our RNG and distribution are unbiased, asking for
230 // the full range should essentially give us random bit generator.
231 // Over long run, OR of all the generated values should have all
232 // bits set to 1, while AND should have all bits set to 0.
233 // The chance of this test failing for unbiased pipeline is
234 // 1 / 2**iters, which for 100 iterations is astronomical.
235 REQUIRE( ORs == BitType( -1 ) );
236 REQUIRE( ANDs == 0 );
237 }
238
239 namespace {
240 template <typename T>
241 struct uniform_integer_test_params;
242
243 template <>
244 struct uniform_integer_test_params<bool> {
245 static constexpr bool lowest = false;
246 static constexpr bool highest = true;
247 // This seems weird, but it is an artifact of the specific seed
248 static constexpr bool expected[] = { true,
249 true,
250 true,
251 true,
252 true,
253 true,
254 false,
255 true,
256 true,
257 true,
258 true,
259 true,
260 false,
261 true,
262 true };
263 };
264
265 template <>
266 struct uniform_integer_test_params<char> {
267 static constexpr char lowest = 32;
268 static constexpr char highest = 126;
269 static constexpr char expected[] = { 'k',
270 '\\',
271 'Z',
272 'X',
273 '`',
274 'Q',
275 ';',
276 'o',
277 ']',
278 'T',
279 'v',
280 'p',
281 ':',
282 'S',
283 't' };
284 };
285
286 template <>
287 struct uniform_integer_test_params<uint8_t> {
288 static constexpr uint8_t lowest = 3;
289 static constexpr uint8_t highest = 123;
290 static constexpr uint8_t expected[] = { 'c',
291 'P',
292 'M',
293 'J',
294 'U',
295 'A',
296 '%',
297 'h',
298 'Q',
299 'F',
300 'q',
301 'i',
302 '$',
303 'E',
304 'o' };
305 };
306
307 template <>
308 struct uniform_integer_test_params<int8_t> {
309 static constexpr int8_t lowest = -27;
310 static constexpr int8_t highest = 73;
311 static constexpr int8_t expected[] = { '5',
312 '%',
313 '#',
314 ' ',
315 '*',
316 25,
317 2,
318 '9',
319 '&',
320 29,
321 'A',
322 ':',
323 1,
324 28,
325 '?' };
326 };
327
328 template <>
329 struct uniform_integer_test_params<uint16_t> {
330 static constexpr uint16_t lowest = 123;
331 static constexpr uint16_t highest = 33333;
332 static constexpr uint16_t expected[] = { 26684,
333 21417,
334 20658,
335 19791,
336 22896,
337 17433,
338 9806,
339 27948,
340 21767,
341 18588,
342 30556,
343 28244,
344 9439,
345 18293,
346 29949 };
347 };
348
349 template <>
350 struct uniform_integer_test_params<int16_t> {
351 static constexpr int16_t lowest = -17222;
352 static constexpr int16_t highest = 17222;
353 static constexpr int16_t expected[] = { 10326,
354 4863,
355 4076,
356 3177,
357 6397,
358 731,
359 -7179,
360 11637,
361 5226,
362 1929,
363 14342,
364 11944,
365 -7560,
366 1623,
367 13712 };
368 };
369
370 template <>
371 struct uniform_integer_test_params<uint32_t> {
372 static constexpr uint32_t lowest = 17222;
373 static constexpr uint32_t highest = 234234;
374 static constexpr uint32_t expected[] = { 190784,
375 156367,
376 151409,
377 145743,
378 166032,
379 130337,
380 80501,
381 199046,
382 158654,
383 137883,
384 216091,
385 200981,
386 78099,
387 135954,
388 212120 };
389 };
390
391 template <>
392 struct uniform_integer_test_params<int32_t> {
393 static constexpr int32_t lowest = -237272;
394 static constexpr int32_t highest = 234234;
395 static constexpr int32_t expected[] = { 139829,
396 65050,
397 54278,
398 41969,
399 86051,
400 8494,
401 -99785,
402 157781,
403 70021,
404 24890,
405 194815,
406 161985,
407 -105004,
408 20699,
409 186186 };
410 };
411
412 template <>
413 struct uniform_integer_test_params<uint64_t> {
414 static constexpr uint64_t lowest = 1234;
415 static constexpr uint64_t highest = 1234567890;
416 static constexpr uint64_t expected[] = { 987382749,
417 763380386,
418 846572137,
419 359990258,
420 804599765,
421 1131353566,
422 346324913,
423 1108760730,
424 1141693933,
425 856999148,
426 879390623,
427 1149485521,
428 900556586,
429 952385958,
430 807916408 };
431 };
432
433 template <>
434 struct uniform_integer_test_params<int64_t> {
435 static constexpr int64_t lowest = -1234567890;
436 static constexpr int64_t highest = 1234567890;
437 static constexpr int64_t expected[] = { 740197113,
438 292191940,
439 458575608,
440 -514589122,
441 374630781,
442 1028139036,
443 -541919840,
444 982953318,
445 1048819790,
446 479429651,
447 524212647,
448 1064402981,
449 566544615,
450 670203462,
451 381264073 };
452 };
453
454 // We need these definitions for C++14 and earlier, but
455 // GCC will complain about them in newer C++ standards
456 #if __cplusplus <= 201402L
457 constexpr bool uniform_integer_test_params<bool>::expected[];
458 constexpr char uniform_integer_test_params<char>::expected[];
459 constexpr uint8_t uniform_integer_test_params<uint8_t>::expected[];
460 constexpr int8_t uniform_integer_test_params<int8_t>::expected[];
461 constexpr uint16_t uniform_integer_test_params<uint16_t>::expected[];
462 constexpr int16_t uniform_integer_test_params<int16_t>::expected[];
463 constexpr uint32_t uniform_integer_test_params<uint32_t>::expected[];
464 constexpr int32_t uniform_integer_test_params<int32_t>::expected[];
465 constexpr uint64_t uniform_integer_test_params<uint64_t>::expected[];
466 constexpr int64_t uniform_integer_test_params<int64_t>::expected[];
467 #endif
468
469 }
470
471 TEMPLATE_TEST_CASE( "uniform_integer_distribution is reproducible",
472 "[rng][distribution][approvals]",
473 bool,
474 char,
475 uint8_t,
476 int8_t,
477 uint16_t,
478 int16_t,
479 uint32_t,
480 int32_t,
481 uint64_t,
482 int64_t) {
483 Catch::SimplePcg32 pcg( 0xaabb'ccdd );
484
485 constexpr auto lowest = uniform_integer_test_params<TestType>::lowest;
486 constexpr auto highest = uniform_integer_test_params<TestType>::highest;
487 Catch::uniform_integer_distribution<TestType> dist(lowest, highest);
488
489 constexpr auto iters = 15;
490 std::array<TestType, iters> generated;
491 for (int i = 0; i < iters; ++i) {
492 generated[i] = dist( pcg );
493 }
494
495 REQUIRE_THAT(generated, Catch::Matchers::RangeEquals(uniform_integer_test_params<TestType>::expected));
496 }
497
498 // The reproducibility tests assume that operations on `float`/`double`
499 // happen in the same precision as the operated-upon type. This is
500 // generally true, unless the code is compiled for 32 bit targets without
501 // SSE2 enabled, in which case the operations are done in the x87 FPU,
502 // which usually implies doing math in 80 bit floats, and then rounding
503 // into smaller type when the type is saved into memory. This obviously
504 // leads to a different answer, than doing the math in the correct precision.
505 #if ( defined( _MSC_VER ) && _M_IX86_FP < 2 ) || \
506 ( defined( __GNUC__ ) && \
507 ( ( defined( __i386__ ) || defined( __x86_64__ ) ) ) && \
508 !defined( __SSE2_MATH__ ) )
509 # define CATCH_TEST_CONFIG_DISABLE_FLOAT_REPRODUCIBILITY_TESTS
510 #endif
511
512 #if !defined( CATCH_TEST_CONFIG_DISABLE_FLOAT_REPRODUCIBILITY_TESTS )
513
514 namespace {
515 template <typename T>
516 struct uniform_fp_test_params;
517
518 template<>
519 struct uniform_fp_test_params<float> {
520 // These are exactly representable
521 static constexpr float lowest = -256.125f;
522 static constexpr float highest = 385.125f;
523 // These are just round-trip formatted
524 static constexpr float expected[] = { 92.56961f,
525 -23.170044f,
526 310.81833f,
527 -53.023132f,
528 105.03287f,
529 198.77591f,
530 -172.72931f,
531 51.805176f,
532 -241.10156f,
533 64.66101f,
534 212.12509f,
535 -49.24292f,
536 -177.1399f,
537 245.23679f,
538 173.22421f };
539 };
540 template <>
541 struct uniform_fp_test_params<double> {
542 // These are exactly representable
543 static constexpr double lowest = -234582.9921875;
544 static constexpr double highest = 261238.015625;
545 // These are just round-trip formatted
546 static constexpr double expected[] = { 35031.207052832615,
547 203783.3401838024,
548 44667.940405848756,
549 -170100.5877224467,
550 -222966.7418051684,
551 127472.72630072923,
552 -173510.88209096913,
553 97394.16172239158,
554 119123.6921592663,
555 22595.741022785165,
556 8988.68409120926,
557 136906.86520606978,
558 33369.19104222473,
559 60912.7615841752,
560 -149060.05936760217 };
561 };
562
563 // We need these definitions for C++14 and earlier, but
564 // GCC will complain about them in newer C++ standards
565 #if __cplusplus <= 201402L
566 constexpr float uniform_fp_test_params<float>::expected[];
567 constexpr double uniform_fp_test_params<double>::expected[];
568 #endif
569 } // namespace
570
571 TEMPLATE_TEST_CASE( "uniform_floating_point_distribution is reproducible",
572 "[rng][distribution][floating-point][approvals]",
573 float,
574 double ) {
575 Catch::SimplePcg32 pcg( 0xaabb'aabb );
576
577 const auto lowest = uniform_fp_test_params<TestType>::lowest;
578 const auto highest = uniform_fp_test_params<TestType>::highest;
579 Catch::uniform_floating_point_distribution<TestType> dist( lowest, highest );
580
581 constexpr auto iters = 15;
582 std::array<TestType, iters> generated;
583 for ( int i = 0; i < iters; ++i ) {
584 generated[i] = dist( pcg );
585 }
586
587 REQUIRE_THAT( generated, Catch::Matchers::RangeEquals( uniform_fp_test_params<TestType>::expected ) );
588 }
589
590 #endif // ^^ float reproducibility tests are enabled
591
592 TEMPLATE_TEST_CASE( "uniform_floating_point_distribution can handle unitary ranges",
593 "[rng][distribution][floating-point][approvals]",
594 float,
595 double ) {
596 std::random_device rd;
597 auto seed = rd();
598 CAPTURE( seed );
599 Catch::SimplePcg32 pcg( seed );
600
601 const auto highest = TestType(385.125);
602 Catch::uniform_floating_point_distribution<TestType> dist( highest,
603 highest );
604
605 constexpr auto iters = 20;
606 for (int i = 0; i < iters; ++i) {
607 REQUIRE( Catch::Detail::directCompare( dist( pcg ), highest ) );
608 }
609 }