abseil-cpp/absl/random/bit_gen_ref_test.cc
Go to the documentation of this file.
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 {
26 
27 class ConstBitGen {
28  public:
29  // URBG interface
31 
32  static constexpr result_type(min)() { return (absl::BitGen::min)(); }
33  static constexpr result_type(max)() { return (absl::BitGen::max)(); }
34  result_type operator()() { return 1; }
35 
36  // InvokeMock method
38  *static_cast<int*>(result) = 42;
39  return true;
40  }
41 };
42 
43 namespace {
44 
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 =
52  std::mt19937_64, std::minstd_rand>;
53 TYPED_TEST_SUITE(BitGenRefTest, BitGenTypes);
54 
55 TYPED_TEST(BitGenRefTest, BasicTest) {
56  TypeParam gen;
57  auto x = FnTest(gen);
58  EXPECT_NEAR(x, 4, 3);
59 }
60 
61 TYPED_TEST(BitGenRefTest, Copyable) {
62  TypeParam gen;
63  absl::BitGenRef gen_ref(gen);
64  FnTest(gen_ref); // Copy
65 }
66 
67 TEST(BitGenRefTest, PassThroughEquivalence) {
68  // sequence_urbg returns 64-bit results.
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 
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
102 } // namespace absl
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
EXPECT_THAT
#define EXPECT_THAT(value, matcher)
absl::ConstBitGen::result_type
absl::BitGen::result_type result_type
Definition: abseil-cpp/absl/random/bit_gen_ref_test.cc:30
absl::TEST
TEST(NotificationTest, SanityTest)
Definition: abseil-cpp/absl/synchronization/notification_test.cc:126
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
absl::ConstBitGen
Definition: abseil-cpp/absl/random/bit_gen_ref_test.cc:27
absl::random_internal::sequence_urbg
Definition: abseil-cpp/absl/random/internal/sequence_urbg.h:33
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
testing::internal::ProxyTypeList
Definition: googletest/googletest/include/gtest/internal/gtest-type-util.h:155
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::random_internal::NonsecureURBGBase< random_internal::randen_engine< uint64_t > >::result_type
typename random_internal::randen_engine< uint64_t > ::result_type result_type
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:98
absl::BitGen
random_internal::NonsecureURBGBase< random_internal::randen_engine< uint64_t > > BitGen
Definition: abseil-cpp/absl/random/random.h:121
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
TYPED_TEST
#define TYPED_TEST(CaseName, TestName)
Definition: googletest/googletest/include/gtest/gtest-typed-test.h:197
absl::base_internal::FastTypeIdType
const void * FastTypeIdType
Definition: abseil-cpp/absl/base/internal/fast_type_id.h:39
testing::Eq
internal::EqMatcher< T > Eq(T x)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8561
absl::InsecureBitGen
random_internal::NonsecureURBGBase< random_internal::pcg64_2018_engine > InsecureBitGen
Definition: abseil-cpp/absl/random/random.h:162
absl::ConstBitGen::InvokeMock
bool InvokeMock(base_internal::FastTypeIdType index, void *, void *result)
Definition: abseil-cpp/absl/random/bit_gen_ref_test.cc:37
absl::random_internal::NonsecureURBGBase< random_internal::randen_engine< uint64_t > >::max
static constexpr result_type() max()
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:123
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
absl::BitGenRef
Definition: abseil-cpp/absl/random/bit_gen_ref.h:87
gen
OPENSSL_EXPORT GENERAL_NAME * gen
Definition: x509v3.h:495
absl::ConstBitGen::max
static constexpr result_type() max()
Definition: abseil-cpp/absl/random/bit_gen_ref_test.cc:33
absl::random_internal::NonsecureURBGBase< random_internal::randen_engine< uint64_t > >::min
static constexpr result_type() min()
Definition: abseil-cpp/absl/random/internal/nonsecure_base.h:120
absl::ConstBitGen::operator()
result_type operator()()
Definition: abseil-cpp/absl/random/bit_gen_ref_test.cc:34
TYPED_TEST_SUITE
#define TYPED_TEST_SUITE(CaseName, Types,...)
Definition: googletest/googletest/include/gtest/gtest-typed-test.h:191
index
int index
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1184
absl::ConstBitGen::min
static constexpr result_type() min()
Definition: abseil-cpp/absl/random/bit_gen_ref_test.cc:32
EXPECT_NEAR
#define EXPECT_NEAR(val1, val2, abs_error)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2143
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::Uniform
absl::enable_if_t<!std::is_same< R, void >::value, R > Uniform(TagType tag, URBG &&urbg, R lo, R hi)
Definition: abseil-cpp/absl/random/distributions.h:123


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:48