Skip to content

Commit

Permalink
Allow easy retrieval of RNG seed by the users
Browse files Browse the repository at this point in the history
This makes it so that they don't need parallel RNG seed passing
infrastructure for randomized data generation (e.g. inputs for
benchmarks).
  • Loading branch information
horenmar committed Aug 18, 2022
1 parent 33e7019 commit dc001fa
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/catch2/catch_all.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <catch2/catch_assertion_info.hpp>
#include <catch2/catch_assertion_result.hpp>
#include <catch2/catch_config.hpp>
#include <catch2/catch_get_random_seed.hpp>
#include <catch2/catch_message.hpp>
#include <catch2/catch_section_info.hpp>
#include <catch2/catch_session.hpp>
Expand Down
18 changes: 18 additions & 0 deletions src/catch2/catch_get_random_seed.cpp
Original file line number Diff line number Diff line change
@@ -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 <catch2/catch_get_random_seed.hpp>

#include <catch2/internal/catch_context.hpp>
#include <catch2/catch_config.hpp>

namespace Catch {
std::uint32_t getSeed() {
return getCurrentContext().getConfig()->rngSeed();
}
}
18 changes: 18 additions & 0 deletions src/catch2/catch_get_random_seed.hpp
Original file line number Diff line number Diff line change
@@ -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 <cstdint>

namespace Catch {
//! Returns Catch2's current RNG seed.
std::uint32_t getSeed();
}

#endif // CATCH_GET_RANDOM_SEED_HPP_INCLUDED
3 changes: 2 additions & 1 deletion src/catch2/reporters/catch_reporter_compact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// SPDX-License-Identifier: BSL-1.0
#include <catch2/reporters/catch_reporter_compact.hpp>

#include <catch2/catch_get_random_seed.hpp>
#include <catch2/catch_test_spec.hpp>
#include <catch2/reporters/catch_reporter_helpers.hpp>
#include <catch2/interfaces/catch_interfaces_config.hpp>
Expand Down Expand Up @@ -261,7 +262,7 @@ class AssertionPrinter {
<< 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 ) {
Expand Down
3 changes: 2 additions & 1 deletion src/catch2/reporters/catch_reporter_console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <catch2/internal/catch_console_width.hpp>
#include <catch2/reporters/catch_reporter_helpers.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>
#include <catch2/catch_get_random_seed.hpp>

#include <cstdio>

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit dc001fa

Please sign in to comment.