bit_gen_ref_test.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // Copyright 2018 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #include "absl/random/bit_gen_ref.h"
  17. #include "gmock/gmock.h"
  18. #include "gtest/gtest.h"
  19. #include "absl/base/internal/fast_type_id.h"
  20. #include "absl/random/internal/sequence_urbg.h"
  21. #include "absl/random/random.h"
  22. namespace absl {
  23. ABSL_NAMESPACE_BEGIN
  24. class ConstBitGen {
  25. public:
  26. // URBG interface
  27. using result_type = absl::BitGen::result_type;
  28. static constexpr result_type(min)() { return (absl::BitGen::min)(); }
  29. static constexpr result_type(max)() { return (absl::BitGen::max)(); }
  30. result_type operator()() { return 1; }
  31. // InvokeMock method
  32. bool InvokeMock(base_internal::FastTypeIdType index, void*, void* result) {
  33. *static_cast<int*>(result) = 42;
  34. return true;
  35. }
  36. };
  37. namespace {
  38. int FnTest(absl::BitGenRef gen_ref) { return absl::Uniform(gen_ref, 1, 7); }
  39. template <typename T>
  40. class BitGenRefTest : public testing::Test {};
  41. using BitGenTypes =
  42. ::testing::Types<absl::BitGen, absl::InsecureBitGen, std::mt19937,
  43. std::mt19937_64, std::minstd_rand>;
  44. TYPED_TEST_SUITE(BitGenRefTest, BitGenTypes);
  45. TYPED_TEST(BitGenRefTest, BasicTest) {
  46. TypeParam gen;
  47. auto x = FnTest(gen);
  48. EXPECT_NEAR(x, 4, 3);
  49. }
  50. TYPED_TEST(BitGenRefTest, Copyable) {
  51. TypeParam gen;
  52. absl::BitGenRef gen_ref(gen);
  53. FnTest(gen_ref); // Copy
  54. }
  55. TEST(BitGenRefTest, PassThroughEquivalence) {
  56. // sequence_urbg returns 64-bit results.
  57. absl::random_internal::sequence_urbg urbg(
  58. {0x0003eb76f6f7f755ull, 0xFFCEA50FDB2F953Bull, 0xC332DDEFBE6C5AA5ull,
  59. 0x6558218568AB9702ull, 0x2AEF7DAD5B6E2F84ull, 0x1521B62829076170ull,
  60. 0xECDD4775619F1510ull, 0x13CCA830EB61BD96ull, 0x0334FE1EAA0363CFull,
  61. 0xB5735C904C70A239ull, 0xD59E9E0BCBAADE14ull, 0xEECC86BC60622CA7ull});
  62. std::vector<uint64_t> output(12);
  63. {
  64. absl::BitGenRef view(urbg);
  65. for (auto& v : output) {
  66. v = view();
  67. }
  68. }
  69. std::vector<uint64_t> expected(
  70. {0x0003eb76f6f7f755ull, 0xFFCEA50FDB2F953Bull, 0xC332DDEFBE6C5AA5ull,
  71. 0x6558218568AB9702ull, 0x2AEF7DAD5B6E2F84ull, 0x1521B62829076170ull,
  72. 0xECDD4775619F1510ull, 0x13CCA830EB61BD96ull, 0x0334FE1EAA0363CFull,
  73. 0xB5735C904C70A239ull, 0xD59E9E0BCBAADE14ull, 0xEECC86BC60622CA7ull});
  74. EXPECT_THAT(output, testing::Eq(expected));
  75. }
  76. TEST(BitGenRefTest, MockingBitGenBaseOverrides) {
  77. ConstBitGen const_gen;
  78. EXPECT_EQ(FnTest(const_gen), 42);
  79. absl::BitGenRef gen_ref(const_gen);
  80. EXPECT_EQ(FnTest(gen_ref), 42); // Copy
  81. }
  82. } // namespace
  83. ABSL_NAMESPACE_END
  84. } // namespace absl