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