• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   static auto* rng = [] {
45     RandomDeviceSeedSeq seed_seq;
46     return new std::mt19937_64(seed_seq);
47   }();
48   return rng;
49 }
50 
operator ()() const51 std::string Generator<std::string>::operator()() const {
52   // NOLINTNEXTLINE(runtime/int)
53   std::uniform_int_distribution<short> chars(0x20, 0x7E);
54   std::string res;
55   res.resize(32);
56   std::generate(res.begin(), res.end(),
57                 [&]() { return chars(*GetSharedRng()); });
58   return res;
59 }
60 
operator ()() const61 absl::string_view Generator<absl::string_view>::operator()() const {
62   static auto* arena = new std::deque<std::string>();
63   // NOLINTNEXTLINE(runtime/int)
64   std::uniform_int_distribution<short> chars(0x20, 0x7E);
65   arena->emplace_back();
66   auto& res = arena->back();
67   res.resize(32);
68   std::generate(res.begin(), res.end(),
69                 [&]() { return chars(*GetSharedRng()); });
70   return res;
71 }
72 
73 }  // namespace hash_internal
74 }  // namespace container_internal
75 ABSL_NAMESPACE_END
76 }  // namespace absl
77