From dc001fa935d71b4b77f263fce405c9dbdfcbfe28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Thu, 18 Aug 2022 21:22:28 +0200 Subject: [PATCH] Allow easy retrieval of RNG seed by the users This makes it so that they don't need parallel RNG seed passing infrastructure for randomized data generation (e.g. inputs for benchmarks). --- src/CMakeLists.txt | 2 ++ src/catch2/catch_all.hpp | 1 + src/catch2/catch_get_random_seed.cpp | 18 ++++++++++++++++++ src/catch2/catch_get_random_seed.hpp | 18 ++++++++++++++++++ .../reporters/catch_reporter_compact.cpp | 3 ++- .../reporters/catch_reporter_console.cpp | 3 ++- 6 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 src/catch2/catch_get_random_seed.cpp create mode 100644 src/catch2/catch_get_random_seed.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bd0b6d97..c76a96d5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -137,6 +137,7 @@ set(INTERNAL_HEADERS ${SOURCES_DIR}/internal/catch_string_manip.hpp ${SOURCES_DIR}/internal/catch_stringref.hpp ${SOURCES_DIR}/catch_tag_alias.hpp + ${SOURCES_DIR}/catch_get_random_seed.hpp ${SOURCES_DIR}/catch_tag_alias_autoregistrar.hpp ${SOURCES_DIR}/internal/catch_tag_alias_registry.hpp ${SOURCES_DIR}/catch_test_case_info.hpp @@ -230,6 +231,7 @@ set(IMPL_SOURCES ${SOURCES_DIR}/matchers/catch_matchers_predicate.cpp ${SOURCES_DIR}/matchers/internal/catch_matchers_impl.cpp ${SOURCES_DIR}/catch_tag_alias_autoregistrar.cpp + ${SOURCES_DIR}/catch_get_random_seed.cpp ${SOURCES_DIR}/internal/catch_decomposer.cpp ${SOURCES_DIR}/internal/catch_errno_guard.cpp ${SOURCES_DIR}/internal/catch_lazy_expr.cpp diff --git a/src/catch2/catch_all.hpp b/src/catch2/catch_all.hpp index 5c715e35..94f55223 100644 --- a/src/catch2/catch_all.hpp +++ b/src/catch2/catch_all.hpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include diff --git a/src/catch2/catch_get_random_seed.cpp b/src/catch2/catch_get_random_seed.cpp new file mode 100644 index 00000000..12a5b6d8 --- /dev/null +++ b/src/catch2/catch_get_random_seed.cpp @@ -0,0 +1,18 @@ + +// Copyright Catch2 Authors +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// SPDX-License-Identifier: BSL-1.0 + +#include + +#include +#include + +namespace Catch { + std::uint32_t getSeed() { + return getCurrentContext().getConfig()->rngSeed(); + } +} diff --git a/src/catch2/catch_get_random_seed.hpp b/src/catch2/catch_get_random_seed.hpp new file mode 100644 index 00000000..6df3c3a0 --- /dev/null +++ b/src/catch2/catch_get_random_seed.hpp @@ -0,0 +1,18 @@ + +// Copyright Catch2 Authors +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// SPDX-License-Identifier: BSL-1.0 +#ifndef CATCH_GET_RANDOM_SEED_HPP_INCLUDED +#define CATCH_GET_RANDOM_SEED_HPP_INCLUDED + +#include + +namespace Catch { + //! Returns Catch2's current RNG seed. + std::uint32_t getSeed(); +} + +#endif // CATCH_GET_RANDOM_SEED_HPP_INCLUDED diff --git a/src/catch2/reporters/catch_reporter_compact.cpp b/src/catch2/reporters/catch_reporter_compact.cpp index 3f036ec5..a09f0483 100644 --- a/src/catch2/reporters/catch_reporter_compact.cpp +++ b/src/catch2/reporters/catch_reporter_compact.cpp @@ -7,6 +7,7 @@ // SPDX-License-Identifier: BSL-1.0 #include +#include #include #include #include @@ -261,7 +262,7 @@ private: << serializeFilters( m_config->getTestsOrTags() ) << '\n'; } - m_stream << "RNG seed: " << m_config->rngSeed() << '\n'; + m_stream << "RNG seed: " << getSeed() << '\n'; } void CompactReporter::assertionEnded( AssertionStats const& _assertionStats ) { diff --git a/src/catch2/reporters/catch_reporter_console.cpp b/src/catch2/reporters/catch_reporter_console.cpp index bb755ed7..88d43b53 100644 --- a/src/catch2/reporters/catch_reporter_console.cpp +++ b/src/catch2/reporters/catch_reporter_console.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include @@ -500,7 +501,7 @@ void ConsoleReporter::testRunStarting(TestRunInfo const& _testInfo) { m_stream << m_colour->guardColour( Colour::BrightYellow ) << "Filters: " << serializeFilters( m_config->getTestsOrTags() ) << '\n'; } - m_stream << "Randomness seeded to: " << m_config->rngSeed() << '\n'; + m_stream << "Randomness seeded to: " << getSeed() << '\n'; } void ConsoleReporter::lazyPrint() {