Skip to content

Commit dc001fa

Browse files
committed
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).
1 parent 33e7019 commit dc001fa

File tree

6 files changed

+43
-2
lines changed

6 files changed

+43
-2
lines changed

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ set(INTERNAL_HEADERS
137137
${SOURCES_DIR}/internal/catch_string_manip.hpp
138138
${SOURCES_DIR}/internal/catch_stringref.hpp
139139
${SOURCES_DIR}/catch_tag_alias.hpp
140+
${SOURCES_DIR}/catch_get_random_seed.hpp
140141
${SOURCES_DIR}/catch_tag_alias_autoregistrar.hpp
141142
${SOURCES_DIR}/internal/catch_tag_alias_registry.hpp
142143
${SOURCES_DIR}/catch_test_case_info.hpp
@@ -230,6 +231,7 @@ set(IMPL_SOURCES
230231
${SOURCES_DIR}/matchers/catch_matchers_predicate.cpp
231232
${SOURCES_DIR}/matchers/internal/catch_matchers_impl.cpp
232233
${SOURCES_DIR}/catch_tag_alias_autoregistrar.cpp
234+
${SOURCES_DIR}/catch_get_random_seed.cpp
233235
${SOURCES_DIR}/internal/catch_decomposer.cpp
234236
${SOURCES_DIR}/internal/catch_errno_guard.cpp
235237
${SOURCES_DIR}/internal/catch_lazy_expr.cpp

src/catch2/catch_all.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <catch2/catch_assertion_info.hpp>
2828
#include <catch2/catch_assertion_result.hpp>
2929
#include <catch2/catch_config.hpp>
30+
#include <catch2/catch_get_random_seed.hpp>
3031
#include <catch2/catch_message.hpp>
3132
#include <catch2/catch_section_info.hpp>
3233
#include <catch2/catch_session.hpp>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
// Copyright Catch2 Authors
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// (See accompanying file LICENSE_1_0.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_get_random_seed.hpp>
10+
11+
#include <catch2/internal/catch_context.hpp>
12+
#include <catch2/catch_config.hpp>
13+
14+
namespace Catch {
15+
std::uint32_t getSeed() {
16+
return getCurrentContext().getConfig()->rngSeed();
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
// Copyright Catch2 Authors
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// (See accompanying file LICENSE_1_0.txt or copy at
5+
// https://www.boost.org/LICENSE_1_0.txt)
6+
7+
// SPDX-License-Identifier: BSL-1.0
8+
#ifndef CATCH_GET_RANDOM_SEED_HPP_INCLUDED
9+
#define CATCH_GET_RANDOM_SEED_HPP_INCLUDED
10+
11+
#include <cstdint>
12+
13+
namespace Catch {
14+
//! Returns Catch2's current RNG seed.
15+
std::uint32_t getSeed();
16+
}
17+
18+
#endif // CATCH_GET_RANDOM_SEED_HPP_INCLUDED

src/catch2/reporters/catch_reporter_compact.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// SPDX-License-Identifier: BSL-1.0
88
#include <catch2/reporters/catch_reporter_compact.hpp>
99

10+
#include <catch2/catch_get_random_seed.hpp>
1011
#include <catch2/catch_test_spec.hpp>
1112
#include <catch2/reporters/catch_reporter_helpers.hpp>
1213
#include <catch2/interfaces/catch_interfaces_config.hpp>
@@ -261,7 +262,7 @@ class AssertionPrinter {
261262
<< serializeFilters( m_config->getTestsOrTags() )
262263
<< '\n';
263264
}
264-
m_stream << "RNG seed: " << m_config->rngSeed() << '\n';
265+
m_stream << "RNG seed: " << getSeed() << '\n';
265266
}
266267

267268
void CompactReporter::assertionEnded( AssertionStats const& _assertionStats ) {

src/catch2/reporters/catch_reporter_console.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <catch2/internal/catch_console_width.hpp>
2020
#include <catch2/reporters/catch_reporter_helpers.hpp>
2121
#include <catch2/internal/catch_move_and_forward.hpp>
22+
#include <catch2/catch_get_random_seed.hpp>
2223

2324
#include <cstdio>
2425

@@ -500,7 +501,7 @@ void ConsoleReporter::testRunStarting(TestRunInfo const& _testInfo) {
500501
m_stream << m_colour->guardColour( Colour::BrightYellow ) << "Filters: "
501502
<< serializeFilters( m_config->getTestsOrTags() ) << '\n';
502503
}
503-
m_stream << "Randomness seeded to: " << m_config->rngSeed() << '\n';
504+
m_stream << "Randomness seeded to: " << getSeed() << '\n';
504505
}
505506

506507
void ConsoleReporter::lazyPrint() {

0 commit comments

Comments
 (0)