bit_gen_ref_test.cc (3179B)
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 18 #include <cstdint> 19 #include <random> 20 #include <vector> 21 22 #include "gmock/gmock.h" 23 #include "gtest/gtest.h" 24 #include "absl/base/config.h" 25 #include "absl/base/internal/fast_type_id.h" 26 #include "absl/random/internal/sequence_urbg.h" 27 #include "absl/random/random.h" 28 29 namespace absl { 30 ABSL_NAMESPACE_BEGIN 31 32 class ConstBitGen { 33 public: 34 // URBG interface 35 using result_type = absl::BitGen::result_type; 36 37 static constexpr result_type(min)() { return (absl::BitGen::min)(); } 38 static constexpr result_type(max)() { return (absl::BitGen::max)(); } 39 result_type operator()() { return 1; } 40 41 // InvokeMock method 42 bool InvokeMock(base_internal::FastTypeIdType, void*, void* result) { 43 *static_cast<int*>(result) = 42; 44 return true; 45 } 46 }; 47 48 namespace { 49 50 int FnTest(absl::BitGenRef gen_ref) { return absl::Uniform(gen_ref, 1, 7); } 51 52 template <typename T> 53 class BitGenRefTest : public testing::Test {}; 54 55 using BitGenTypes = 56 ::testing::Types<absl::BitGen, absl::InsecureBitGen, std::mt19937, 57 std::mt19937_64, std::minstd_rand>; 58 TYPED_TEST_SUITE(BitGenRefTest, BitGenTypes); 59 60 TYPED_TEST(BitGenRefTest, BasicTest) { 61 TypeParam gen; 62 auto x = FnTest(gen); 63 EXPECT_NEAR(x, 4, 3); 64 } 65 66 TYPED_TEST(BitGenRefTest, Copyable) { 67 TypeParam gen; 68 absl::BitGenRef gen_ref(gen); 69 FnTest(gen_ref); // Copy 70 } 71 72 TEST(BitGenRefTest, PassThroughEquivalence) { 73 // sequence_urbg returns 64-bit results. 74 absl::random_internal::sequence_urbg urbg( 75 {0x0003eb76f6f7f755ull, 0xFFCEA50FDB2F953Bull, 0xC332DDEFBE6C5AA5ull, 76 0x6558218568AB9702ull, 0x2AEF7DAD5B6E2F84ull, 0x1521B62829076170ull, 77 0xECDD4775619F1510ull, 0x13CCA830EB61BD96ull, 0x0334FE1EAA0363CFull, 78 0xB5735C904C70A239ull, 0xD59E9E0BCBAADE14ull, 0xEECC86BC60622CA7ull}); 79 80 std::vector<uint64_t> output(12); 81 82 { 83 absl::BitGenRef view(urbg); 84 for (auto& v : output) { 85 v = view(); 86 } 87 } 88 89 std::vector<uint64_t> expected( 90 {0x0003eb76f6f7f755ull, 0xFFCEA50FDB2F953Bull, 0xC332DDEFBE6C5AA5ull, 91 0x6558218568AB9702ull, 0x2AEF7DAD5B6E2F84ull, 0x1521B62829076170ull, 92 0xECDD4775619F1510ull, 0x13CCA830EB61BD96ull, 0x0334FE1EAA0363CFull, 93 0xB5735C904C70A239ull, 0xD59E9E0BCBAADE14ull, 0xEECC86BC60622CA7ull}); 94 95 EXPECT_THAT(output, testing::Eq(expected)); 96 } 97 98 TEST(BitGenRefTest, MockingBitGenBaseOverrides) { 99 ConstBitGen const_gen; 100 EXPECT_EQ(FnTest(const_gen), 42); 101 102 absl::BitGenRef gen_ref(const_gen); 103 EXPECT_EQ(FnTest(gen_ref), 42); // Copy 104 } 105 } // namespace 106 ABSL_NAMESPACE_END 107 } // namespace absl