RandomNumberGeneration.tests.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright Catch2 Authors
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // https://www.boost.org/LICENSE_1_0.txt)
  5. // SPDX-License-Identifier: BSL-1.0
  6. #include <catch2/catch_test_macros.hpp>
  7. #include <catch2/internal/catch_random_number_generator.hpp>
  8. #include <catch2/internal/catch_random_seed_generation.hpp>
  9. #include <catch2/generators/catch_generators.hpp>
  10. TEST_CASE("Our PCG implementation provides expected results for known seeds", "[rng]") {
  11. Catch::SimplePcg32 rng;
  12. SECTION("Default seeded") {
  13. REQUIRE(rng() == 0xfcdb943b);
  14. REQUIRE(rng() == 0x6f55b921);
  15. REQUIRE(rng() == 0x4c17a916);
  16. REQUIRE(rng() == 0x71eae25f);
  17. REQUIRE(rng() == 0x6ce7909c);
  18. }
  19. SECTION("Specific seed") {
  20. rng.seed(0xabcd1234);
  21. REQUIRE(rng() == 0x57c08495);
  22. REQUIRE(rng() == 0x33c956ac);
  23. REQUIRE(rng() == 0x2206fd76);
  24. REQUIRE(rng() == 0x3501a35b);
  25. REQUIRE(rng() == 0xfdffb30f);
  26. // Also check repeated output after reseeding
  27. rng.seed(0xabcd1234);
  28. REQUIRE(rng() == 0x57c08495);
  29. REQUIRE(rng() == 0x33c956ac);
  30. REQUIRE(rng() == 0x2206fd76);
  31. REQUIRE(rng() == 0x3501a35b);
  32. REQUIRE(rng() == 0xfdffb30f);
  33. }
  34. }
  35. TEST_CASE("Comparison ops", "[rng]") {
  36. using Catch::SimplePcg32;
  37. REQUIRE(SimplePcg32{} == SimplePcg32{});
  38. REQUIRE(SimplePcg32{ 0 } != SimplePcg32{});
  39. REQUIRE_FALSE(SimplePcg32{ 1 } == SimplePcg32{ 2 });
  40. REQUIRE_FALSE(SimplePcg32{ 1 } != SimplePcg32{ 1 });
  41. }
  42. TEST_CASE("Random seed generation reports unknown methods", "[rng][seed]") {
  43. REQUIRE_THROWS(Catch::generateRandomSeed(static_cast<Catch::GenerateFrom>(77)));
  44. }
  45. TEST_CASE("Random seed generation accepts known methods", "[rng][seed]") {
  46. using Catch::GenerateFrom;
  47. const auto method = GENERATE(
  48. GenerateFrom::Time,
  49. GenerateFrom::RandomDevice,
  50. GenerateFrom::Default
  51. );
  52. REQUIRE_NOTHROW(Catch::generateRandomSeed(method));
  53. }