| 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/internal/catch_random_number_generator.hpp> |
| 11 | #include <catch2/internal/catch_random_seed_generation.hpp> |
| 12 | #include <catch2/generators/catch_generators.hpp> |
| 13 | |
| 14 | TEST_CASE("Our PCG implementation provides expected results for known seeds", "[rng]") { |
| 15 | Catch::SimplePcg32 rng; |
| 16 | SECTION("Default seeded") { |
| 17 | REQUIRE(rng() == 0xfcdb943b); |
| 18 | REQUIRE(rng() == 0x6f55b921); |
| 19 | REQUIRE(rng() == 0x4c17a916); |
| 20 | REQUIRE(rng() == 0x71eae25f); |
| 21 | REQUIRE(rng() == 0x6ce7909c); |
| 22 | } |
| 23 | SECTION("Specific seed") { |
| 24 | rng.seed(0xabcd1234); |
| 25 | REQUIRE(rng() == 0x57c08495); |
| 26 | REQUIRE(rng() == 0x33c956ac); |
| 27 | REQUIRE(rng() == 0x2206fd76); |
| 28 | REQUIRE(rng() == 0x3501a35b); |
| 29 | REQUIRE(rng() == 0xfdffb30f); |
| 30 | |
| 31 | // Also check repeated output after reseeding |
| 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 | } |
| 40 | |
| 41 | TEST_CASE("Comparison ops", "[rng]") { |
| 42 | using Catch::SimplePcg32; |
| 43 | REQUIRE(SimplePcg32{} == SimplePcg32{}); |
| 44 | REQUIRE(SimplePcg32{ 0 } != SimplePcg32{}); |
| 45 | REQUIRE_FALSE(SimplePcg32{ 1 } == SimplePcg32{ 2 }); |
| 46 | REQUIRE_FALSE(SimplePcg32{ 1 } != SimplePcg32{ 1 }); |
| 47 | } |
| 48 | |
| 49 | TEST_CASE("Random seed generation reports unknown methods", "[rng][seed]") { |
| 50 | REQUIRE_THROWS(Catch::generateRandomSeed(static_cast<Catch::GenerateFrom>(77))); |
| 51 | } |
| 52 | |
| 53 | TEST_CASE("Random seed generation accepts known methods", "[rng][seed]") { |
| 54 | using Catch::GenerateFrom; |
| 55 | const auto method = GENERATE( |
| 56 | GenerateFrom::Time, |
| 57 | GenerateFrom::RandomDevice, |
| 58 | GenerateFrom::Default |
| 59 | ); |
| 60 | |
| 61 | REQUIRE_NOTHROW(Catch::generateRandomSeed(method)); |
| 62 | } |