hash_generator_testing.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #include "absl/container/internal/hash_generator_testing.h"
  15. #include <deque>
  16. namespace absl {
  17. ABSL_NAMESPACE_BEGIN
  18. namespace container_internal {
  19. namespace hash_internal {
  20. namespace {
  21. class RandomDeviceSeedSeq {
  22. public:
  23. using result_type = typename std::random_device::result_type;
  24. template <class Iterator>
  25. void generate(Iterator start, Iterator end) {
  26. while (start != end) {
  27. *start = gen_();
  28. ++start;
  29. }
  30. }
  31. private:
  32. std::random_device gen_;
  33. };
  34. } // namespace
  35. std::mt19937_64* GetSharedRng() {
  36. static auto* rng = [] {
  37. RandomDeviceSeedSeq seed_seq;
  38. return new std::mt19937_64(seed_seq);
  39. }();
  40. return rng;
  41. }
  42. std::string Generator<std::string>::operator()() const {
  43. // NOLINTNEXTLINE(runtime/int)
  44. std::uniform_int_distribution<short> chars(0x20, 0x7E);
  45. std::string res;
  46. res.resize(32);
  47. std::generate(res.begin(), res.end(),
  48. [&]() { return chars(*GetSharedRng()); });
  49. return res;
  50. }
  51. absl::string_view Generator<absl::string_view>::operator()() const {
  52. static auto* arena = new std::deque<std::string>();
  53. // NOLINTNEXTLINE(runtime/int)
  54. std::uniform_int_distribution<short> chars(0x20, 0x7E);
  55. arena->emplace_back();
  56. auto& res = arena->back();
  57. res.resize(32);
  58. std::generate(res.begin(), res.end(),
  59. [&]() { return chars(*GetSharedRng()); });
  60. return res;
  61. }
  62. } // namespace hash_internal
  63. } // namespace container_internal
  64. ABSL_NAMESPACE_END
  65. } // namespace absl