mirror of
https://github.com/abseil/abseil-cpp.git
synced 2026-06-04 12:07:05 +08:00
Use absl::InsecureBitGen in place of std::random_device in Abseil tests.
PiperOrigin-RevId: 750721356 Change-Id: I84e8a1c2a80335983e557270ef78b8b0b4f1e452
This commit is contained in:
committed by
Copybara-Service
parent
46ce0ecdef
commit
3870e283fb
@@ -16,6 +16,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <initializer_list>
|
||||
#include <iterator>
|
||||
@@ -35,6 +36,7 @@
|
||||
#include "absl/base/config.h"
|
||||
#include "absl/base/macros.h"
|
||||
#include "absl/memory/memory.h"
|
||||
#include "absl/random/random.h"
|
||||
#include "absl/types/span.h"
|
||||
|
||||
namespace {
|
||||
@@ -986,25 +988,16 @@ TEST(MutatingTest, RotateCopy) {
|
||||
EXPECT_THAT(actual, ElementsAre(3, 4, 1, 2, 5));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T RandomlySeededPrng() {
|
||||
std::random_device rdev;
|
||||
std::seed_seq::result_type data[T::state_size];
|
||||
std::generate_n(data, T::state_size, std::ref(rdev));
|
||||
std::seed_seq prng_seed(data, data + T::state_size);
|
||||
return T(prng_seed);
|
||||
}
|
||||
|
||||
TEST(MutatingTest, Shuffle) {
|
||||
std::vector<int> actual = {1, 2, 3, 4, 5};
|
||||
absl::c_shuffle(actual, RandomlySeededPrng<std::mt19937_64>());
|
||||
absl::c_shuffle(actual, absl::InsecureBitGen());
|
||||
EXPECT_THAT(actual, UnorderedElementsAre(1, 2, 3, 4, 5));
|
||||
}
|
||||
|
||||
TEST(MutatingTest, Sample) {
|
||||
std::vector<int> actual;
|
||||
absl::c_sample(std::vector<int>{1, 2, 3, 4, 5}, std::back_inserter(actual), 3,
|
||||
RandomlySeededPrng<std::mt19937_64>());
|
||||
absl::InsecureBitGen());
|
||||
EXPECT_THAT(actual, IsSubsetOf({1, 2, 3, 4, 5}));
|
||||
EXPECT_THAT(actual, SizeIs(3));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user