Skip to content

Commit

Permalink
Use compatible RNG with shuffle (#1688)
Browse files Browse the repository at this point in the history
Fixes #1687
  • Loading branch information
kevinushey committed Sep 21, 2022
1 parent 8b4c265 commit 81dfbed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# testthat (development version)

* Fixed an issue that could prevent compilation of Catch unit tests with
LLVM 15. In the interim, packages needing a local workaround can set
`PKG_CPPFLAGS = -DCATCH_CONFIG_CPP11_NO_SHUFFLE` in their `src/Makevars`.
(@kevinushey, #1687)

* `local_reproducible_output` will no longer attempt to set the local language
when an R version is used that was not compiled with natural language support
(NLS), which would previously emit non-test-related warnings during testing
Expand Down
20 changes: 8 additions & 12 deletions inst/include/testthat/vendor/catch.h
Original file line number Diff line number Diff line change
Expand Up @@ -7165,27 +7165,23 @@ namespace Catch {
// #included from: catch_test_case_registry_impl.hpp
#define TWOBLUECUBES_CATCH_TEST_CASE_REGISTRY_IMPL_HPP_INCLUDED

#include <vector>
#include <algorithm>
#include <set>
#include <sstream>
#include <algorithm>
#include <vector>

#ifdef CATCH_CONFIG_CPP11_SHUFFLE
#include <random>
#endif

namespace Catch {

struct RandomNumberGenerator {
typedef std::ptrdiff_t result_type;

result_type operator()( result_type n ) const { return rand() % n; }

#ifdef CATCH_CONFIG_CPP11_SHUFFLE
static constexpr result_type min() { return 0; }
static constexpr result_type max() { return 1000000; }
result_type operator()() const { return rand() % max(); }
#endif
template<typename V>
static void shuffle( V& vector ) {
RandomNumberGenerator rng;
#ifdef CATCH_CONFIG_CPP11_SHUFFLE
std::random_device device;
std::mt19937 rng( device() );
std::shuffle( vector.begin(), vector.end(), rng );
#else
random_shuffle( vector.begin(), vector.end(), rng );
Expand Down

0 comments on commit 81dfbed

Please sign in to comment.