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