1 // Copyright 2018 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "absl/container/internal/hash_generator_testing.h"
16
17 #include <deque>
18
19 namespace absl {
20 ABSL_NAMESPACE_BEGIN
21 namespace container_internal {
22 namespace hash_internal {
23 namespace {
24
25 class RandomDeviceSeedSeq {
26 public:
27 using result_type = typename std::random_device::result_type;
28
29 template <class Iterator>
generate(Iterator start,Iterator end)30 void generate(Iterator start, Iterator end) {
31 while (start != end) {
32 *start = gen_();
33 ++start;
34 }
35 }
36
37 private:
38 std::random_device gen_;
39 };
40
41 } // namespace
42
GetSharedRng()43 std::mt19937_64* GetSharedRng() {
44 RandomDeviceSeedSeq seed_seq;
45 static auto* rng = new std::mt19937_64(seed_seq);
46 return rng;
47 }
48
operator ()() const49 std::string Generator<std::string>::operator()() const {
50 // NOLINTNEXTLINE(runtime/int)
51 std::uniform_int_distribution<short> chars(0x20, 0x7E);
52 std::string res;
53 res.resize(32);
54 std::generate(res.begin(), res.end(),
55 [&]() { return chars(*GetSharedRng()); });
56 return res;
57 }
58
operator ()() const59 absl::string_view Generator<absl::string_view>::operator()() const {
60 static auto* arena = new std::deque<std::string>();
61 // NOLINTNEXTLINE(runtime/int)
62 std::uniform_int_distribution<short> chars(0x20, 0x7E);
63 arena->emplace_back();
64 auto& res = arena->back();
65 res.resize(32);
66 std::generate(res.begin(), res.end(),
67 [&]() { return chars(*GetSharedRng()); });
68 return res;
69 }
70
71 } // namespace hash_internal
72 } // namespace container_internal
73 ABSL_NAMESPACE_END
74 } // namespace absl
75