xref: /aosp_15_r20/external/abseil-cpp/absl/container/internal/hash_generator_testing.cc (revision 9356374a3709195abf420251b3e825997ff56c0f)
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 #include "absl/base/no_destructor.h"
20 
21 namespace absl {
22 ABSL_NAMESPACE_BEGIN
23 namespace container_internal {
24 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>
generate(Iterator start,Iterator end)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 
GetSharedRng()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 }
52 
operator ()() const53 std::string Generator<std::string>::operator()() const {
54   // NOLINTNEXTLINE(runtime/int)
55   std::uniform_int_distribution<short> chars(0x20, 0x7E);
56   std::string res;
57   res.resize(32);
58   std::generate(res.begin(), res.end(),
59                 [&]() { return chars(*GetSharedRng()); });
60   return res;
61 }
62 
operator ()() const63 absl::string_view Generator<absl::string_view>::operator()() const {
64   static absl::NoDestructor<std::deque<std::string>> arena;
65   // NOLINTNEXTLINE(runtime/int)
66   std::uniform_int_distribution<short> chars(0x20, 0x7E);
67   arena->emplace_back();
68   auto& res = arena->back();
69   res.resize(32);
70   std::generate(res.begin(), res.end(),
71                 [&]() { return chars(*GetSharedRng()); });
72   return res;
73 }
74 
75 }  // namespace hash_internal
76 }  // namespace container_internal
77 ABSL_NAMESPACE_END
78 }  // namespace absl
79