Skip to content

Commit 3870e28

Browse files
laramielcopybara-github
authored andcommitted
Use absl::InsecureBitGen in place of std::random_device in Abseil tests.
PiperOrigin-RevId: 750721356 Change-Id: I84e8a1c2a80335983e557270ef78b8b0b4f1e452
1 parent 46ce0ec commit 3870e28

27 files changed

+110
-136
lines changed

absl/algorithm/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ cc_test(
8383
"//absl/base:config",
8484
"//absl/base:core_headers",
8585
"//absl/memory",
86+
"//absl/random",
8687
"//absl/types:span",
8788
"@googletest//:gtest",
8889
"@googletest//:gtest_main",

absl/algorithm/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ absl_cc_test(
6868
absl::config
6969
absl::core_headers
7070
absl::memory
71+
absl::random_random
7172
absl::span
7273
GTest::gmock_main
7374
)

absl/algorithm/container_test.cc

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <algorithm>
1818
#include <array>
19+
#include <cstddef>
1920
#include <functional>
2021
#include <initializer_list>
2122
#include <iterator>
@@ -35,6 +36,7 @@
3536
#include "absl/base/config.h"
3637
#include "absl/base/macros.h"
3738
#include "absl/memory/memory.h"
39+
#include "absl/random/random.h"
3840
#include "absl/types/span.h"
3941

4042
namespace {
@@ -986,25 +988,16 @@ TEST(MutatingTest, RotateCopy) {
986988
EXPECT_THAT(actual, ElementsAre(3, 4, 1, 2, 5));
987989
}
988990

989-
template <typename T>
990-
T RandomlySeededPrng() {
991-
std::random_device rdev;
992-
std::seed_seq::result_type data[T::state_size];
993-
std::generate_n(data, T::state_size, std::ref(rdev));
994-
std::seed_seq prng_seed(data, data + T::state_size);
995-
return T(prng_seed);
996-
}
997-
998991
TEST(MutatingTest, Shuffle) {
999992
std::vector<int> actual = {1, 2, 3, 4, 5};
1000-
absl::c_shuffle(actual, RandomlySeededPrng<std::mt19937_64>());
993+
absl::c_shuffle(actual, absl::InsecureBitGen());
1001994
EXPECT_THAT(actual, UnorderedElementsAre(1, 2, 3, 4, 5));
1002995
}
1003996

1004997
TEST(MutatingTest, Sample) {
1005998
std::vector<int> actual;
1006999
absl::c_sample(std::vector<int>{1, 2, 3, 4, 5}, std::back_inserter(actual), 3,
1007-
RandomlySeededPrng<std::mt19937_64>());
1000+
absl::InsecureBitGen());
10081001
EXPECT_THAT(actual, IsSubsetOf({1, 2, 3, 4, 5}));
10091002
EXPECT_THAT(actual, SizeIs(3));
10101003
}

absl/container/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ cc_library(
494494
"//absl/base:no_destructor",
495495
"//absl/memory",
496496
"//absl/meta:type_traits",
497+
"//absl/random",
497498
"//absl/strings",
498499
],
499500
)
@@ -625,6 +626,7 @@ cc_test(
625626
"//absl/base:config",
626627
"//absl/base:core_headers",
627628
"//absl/profiling:sample_recorder",
629+
"//absl/random",
628630
"//absl/synchronization",
629631
"//absl/synchronization:thread_pool",
630632
"//absl/time",
@@ -785,6 +787,7 @@ cc_test(
785787
"//absl/memory",
786788
"//absl/meta:type_traits",
787789
"//absl/numeric:int128",
790+
"//absl/random",
788791
"//absl/strings",
789792
"//absl/types:optional",
790793
"@googletest//:gtest",

absl/container/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ absl_cc_library(
543543
absl::memory
544544
absl::meta
545545
absl::no_destructor
546+
absl::random_random
546547
absl::strings
547548
TESTONLY
548549
)
@@ -658,6 +659,7 @@ absl_cc_test(
658659
DEPS
659660
absl::config
660661
absl::hashtablez_sampler
662+
absl::random_random
661663
GTest::gmock_main
662664
)
663665

@@ -824,6 +826,7 @@ absl_cc_test(
824826
absl::node_hash_set
825827
absl::optional
826828
absl::prefetch
829+
absl::random_random
827830
absl::raw_hash_set
828831
absl::strings
829832
absl::test_allocator

absl/container/internal/hash_generator_testing.cc

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,61 +14,39 @@
1414

1515
#include "absl/container/internal/hash_generator_testing.h"
1616

17+
#include <algorithm>
1718
#include <deque>
19+
#include <random>
20+
#include <string>
1821

1922
#include "absl/base/no_destructor.h"
23+
#include "absl/random/random.h"
24+
#include "absl/strings/string_view.h"
2025

2126
namespace absl {
2227
ABSL_NAMESPACE_BEGIN
2328
namespace container_internal {
2429
namespace hash_internal {
25-
namespace {
26-
27-
class RandomDeviceSeedSeq {
28-
public:
29-
using result_type = typename std::random_device::result_type;
30-
31-
template <class Iterator>
32-
void generate(Iterator start, Iterator end) {
33-
while (start != end) {
34-
*start = gen_();
35-
++start;
36-
}
37-
}
38-
39-
private:
40-
std::random_device gen_;
41-
};
42-
43-
} // namespace
44-
45-
std::mt19937_64* GetSharedRng() {
46-
static absl::NoDestructor<std::mt19937_64> rng([] {
47-
RandomDeviceSeedSeq seed_seq;
48-
return std::mt19937_64(seed_seq);
49-
}());
50-
return rng.get();
51-
}
5230

5331
std::string Generator<std::string>::operator()() const {
32+
absl::InsecureBitGen gen;
5433
// NOLINTNEXTLINE(runtime/int)
5534
std::uniform_int_distribution<short> chars(0x20, 0x7E);
5635
std::string res;
5736
res.resize(32);
58-
std::generate(res.begin(), res.end(),
59-
[&]() { return chars(*GetSharedRng()); });
37+
std::generate(res.begin(), res.end(), [&]() { return chars(gen); });
6038
return res;
6139
}
6240

6341
absl::string_view Generator<absl::string_view>::operator()() const {
6442
static absl::NoDestructor<std::deque<std::string>> arena;
43+
absl::InsecureBitGen gen;
6544
// NOLINTNEXTLINE(runtime/int)
6645
std::uniform_int_distribution<short> chars(0x20, 0x7E);
6746
arena->emplace_back();
6847
auto& res = arena->back();
6948
res.resize(32);
70-
std::generate(res.begin(), res.end(),
71-
[&]() { return chars(*GetSharedRng()); });
49+
std::generate(res.begin(), res.end(), [&]() { return chars(gen); });
7250
return res;
7351
}
7452

absl/container/internal/hash_generator_testing.h

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
#include <algorithm>
2424
#include <cassert>
2525
#include <iosfwd>
26+
#include <memory>
2627
#include <random>
28+
#include <string>
2729
#include <tuple>
2830
#include <type_traits>
2931
#include <utility>
@@ -32,6 +34,7 @@
3234
#include "absl/container/internal/hash_policy_testing.h"
3335
#include "absl/memory/memory.h"
3436
#include "absl/meta/type_traits.h"
37+
#include "absl/random/random.h"
3538
#include "absl/strings/string_view.h"
3639

3740
namespace absl {
@@ -48,8 +51,6 @@ struct IsMap<Map, absl::void_t<typename Map::mapped_type>> : std::true_type {};
4851

4952
} // namespace generator_internal
5053

51-
std::mt19937_64* GetSharedRng();
52-
5354
enum Enum : uint64_t {
5455
kEnumEmpty,
5556
kEnumDeleted,
@@ -69,29 +70,27 @@ struct Generator;
6970

7071
template <class T>
7172
struct Generator<T, typename std::enable_if<std::is_integral<T>::value>::type> {
72-
T operator()() const {
73-
std::uniform_int_distribution<T> dist;
74-
return dist(*GetSharedRng());
75-
}
73+
T operator()() const { return dist(gen); }
74+
mutable absl::InsecureBitGen gen;
75+
mutable std::uniform_int_distribution<T> dist;
7676
};
7777

7878
template <>
7979
struct Generator<Enum> {
80-
Enum operator()() const {
81-
std::uniform_int_distribution<typename std::underlying_type<Enum>::type>
82-
dist;
83-
return static_cast<Enum>(dist(*GetSharedRng()));
84-
}
80+
Enum operator()() const { return static_cast<Enum>(dist(gen)); }
81+
mutable absl::InsecureBitGen gen;
82+
mutable std::uniform_int_distribution<
83+
typename std::underlying_type<Enum>::type>
84+
dist;
8585
};
8686

8787
template <>
8888
struct Generator<EnumClass> {
89-
EnumClass operator()() const {
90-
std::uniform_int_distribution<
91-
typename std::underlying_type<EnumClass>::type>
92-
dist;
93-
return static_cast<EnumClass>(dist(*GetSharedRng()));
94-
}
89+
EnumClass operator()() const { return static_cast<EnumClass>(dist(gen)); }
90+
mutable absl::InsecureBitGen gen;
91+
mutable std::uniform_int_distribution<
92+
typename std::underlying_type<EnumClass>::type>
93+
dist;
9594
};
9695

9796
template <>
@@ -135,17 +134,17 @@ struct Generator<std::unique_ptr<T>> {
135134

136135
template <class U>
137136
struct Generator<U, absl::void_t<decltype(std::declval<U&>().key()),
138-
decltype(std::declval<U&>().value())>>
137+
decltype(std::declval<U&>().value())>>
139138
: Generator<std::pair<
140139
typename std::decay<decltype(std::declval<U&>().key())>::type,
141140
typename std::decay<decltype(std::declval<U&>().value())>::type>> {};
142141

143142
template <class Container>
144-
using GeneratedType = decltype(
145-
std::declval<const Generator<
146-
typename std::conditional<generator_internal::IsMap<Container>::value,
147-
typename Container::value_type,
148-
typename Container::key_type>::type>&>()());
143+
using GeneratedType =
144+
decltype(std::declval<const Generator<typename std::conditional<
145+
generator_internal::IsMap<Container>::value,
146+
typename Container::value_type,
147+
typename Container::key_type>::type>&>()());
149148

150149
// Naive wrapper that performs a linear search of previous values.
151150
// Beware this is O(SQR), which is reasonable for smaller kMaxValues.

absl/container/internal/hashtablez_sampler_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "absl/base/attributes.h"
2828
#include "absl/base/config.h"
2929
#include "absl/profiling/internal/sample_recorder.h"
30+
#include "absl/random/random.h"
3031
#include "absl/synchronization/blocking_counter.h"
3132
#include "absl/synchronization/internal/thread_pool.h"
3233
#include "absl/synchronization/mutex.h"
@@ -439,8 +440,7 @@ TEST(HashtablezSamplerTest, MultiThreaded) {
439440
const size_t value_size = 13 + i % 5;
440441
pool.Schedule([&sampler, &stop, sampling_stride, elt_size, key_size,
441442
value_size]() {
442-
std::random_device rd;
443-
std::mt19937 gen(rd());
443+
absl::InsecureBitGen gen;
444444

445445
std::vector<HashtablezInfo*> infoz;
446446
while (!stop.HasBeenNotified()) {

absl/container/internal/raw_hash_set_benchmark.cc

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,7 @@ struct string_generator {
172172
//
173173
// On a table of size N, keep deleting the LRU entry and add a random one.
174174
void BM_CacheInSteadyState(benchmark::State& state) {
175-
std::random_device rd;
176-
std::mt19937 rng(rd());
175+
absl::InsecureBitGen rng;
177176
string_generator gen{12};
178177
StringTable t;
179178
std::deque<std::string> keys;
@@ -250,8 +249,7 @@ void BM_EndComparison(benchmark::State& state) {
250249
BENCHMARK(BM_EndComparison);
251250

252251
void BM_Iteration(benchmark::State& state) {
253-
std::random_device rd;
254-
std::mt19937 rng(rd());
252+
absl::InsecureBitGen rng;
255253
string_generator gen{12};
256254
StringTable t;
257255

@@ -293,8 +291,7 @@ BENCHMARK(BM_Iteration)
293291
->ArgPair(1000, 10);
294292

295293
void BM_CopyCtorSparseInt(benchmark::State& state) {
296-
std::random_device rd;
297-
std::mt19937 rng(rd());
294+
absl::InsecureBitGen rng;
298295
IntTable t;
299296
std::uniform_int_distribution<uint64_t> dist(0, ~uint64_t{});
300297

@@ -312,8 +309,7 @@ void BM_CopyCtorSparseInt(benchmark::State& state) {
312309
BENCHMARK(BM_CopyCtorSparseInt)->Range(1, 4096);
313310

314311
void BM_CopyCtorInt(benchmark::State& state) {
315-
std::random_device rd;
316-
std::mt19937 rng(rd());
312+
absl::InsecureBitGen rng;
317313
IntTable t;
318314
std::uniform_int_distribution<uint64_t> dist(0, ~uint64_t{});
319315

@@ -330,8 +326,7 @@ void BM_CopyCtorInt(benchmark::State& state) {
330326
BENCHMARK(BM_CopyCtorInt)->Range(0, 4096);
331327

332328
void BM_CopyCtorString(benchmark::State& state) {
333-
std::random_device rd;
334-
std::mt19937 rng(rd());
329+
absl::InsecureBitGen rng;
335330
StringTable t;
336331
std::uniform_int_distribution<uint64_t> dist(0, ~uint64_t{});
337332

@@ -348,8 +343,7 @@ void BM_CopyCtorString(benchmark::State& state) {
348343
BENCHMARK(BM_CopyCtorString)->Range(0, 4096);
349344

350345
void BM_CopyAssign(benchmark::State& state) {
351-
std::random_device rd;
352-
std::mt19937 rng(rd());
346+
absl::InsecureBitGen rng;
353347
IntTable t;
354348
std::uniform_int_distribution<uint64_t> dist(0, ~uint64_t{});
355349
while (t.size() < state.range(0)) {
@@ -365,8 +359,7 @@ void BM_CopyAssign(benchmark::State& state) {
365359
BENCHMARK(BM_CopyAssign)->Range(128, 4096);
366360

367361
void BM_RangeCtor(benchmark::State& state) {
368-
std::random_device rd;
369-
std::mt19937 rng(rd());
362+
absl::InsecureBitGen rng;
370363
std::uniform_int_distribution<uint64_t> dist(0, ~uint64_t{});
371364
std::vector<int> values;
372365
const size_t desired_size = state.range(0);

absl/container/internal/raw_hash_set_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "absl/container/internal/container_memory.h"
5454
#include "absl/container/internal/hash_function_defaults.h"
5555
#include "absl/container/internal/hash_policy_testing.h"
56+
#include "absl/random/random.h"
5657
// TODO(b/382423690): Separate tests that depend only on
5758
// hashtable_control_bytes.
5859
#include "absl/container/internal/hashtable_control_bytes.h"
@@ -1922,8 +1923,7 @@ ProbeStats CollectProbeStatsOnLinearlyTransformedKeys(
19221923
const std::vector<int64_t>& keys, size_t num_iters) {
19231924
ProbeStats stats;
19241925

1925-
std::random_device rd;
1926-
std::mt19937 rng(rd());
1926+
absl::InsecureBitGen rng;
19271927
auto linear_transform = [](size_t x, size_t y) { return x * 17 + y * 13; };
19281928
std::uniform_int_distribution<size_t> dist(0, keys.size() - 1);
19291929
while (num_iters--) {

absl/numeric/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ cc_binary(
124124
deps = [
125125
":int128",
126126
"//absl/base:config",
127+
"//absl/random",
127128
"@google_benchmark//:benchmark_main",
128129
],
129130
)

0 commit comments

Comments
 (0)