Skip to content

Commit a302705

Browse files
Quincunx271copybara-github
authored andcommitted
Delete UnvalidatedMockingBitGen
See some relevant context on this project in abseil/abseil-cpp@254b3a5. This type existed to support the incremental migration of added validation to MockingBitGen, validating that the returned value from absl::Uniform() is in bounds. All known cases where an out-of-bounds value was returned have been fixed and migrated to use MockingBitGen. PiperOrigin-RevId: 693836317 Change-Id: I04f54b7b4856f4280580b294194ce2c25a18e9b1
1 parent 78ed38c commit a302705

File tree

3 files changed

+60
-85
lines changed

3 files changed

+60
-85
lines changed

absl/random/internal/mock_overload_set.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,8 @@ struct MockSingleOverload<DistrT, ValidatorT, Ret(MockingBitGen&, Args...)> {
5151
auto gmock_Call(MockURBG& gen, const ::testing::Matcher<Args>&... matchers)
5252
-> decltype(MockHelpers::MockFor<KeyT>(gen, ValidatorT())
5353
.gmock_Call(matchers...)) {
54-
static_assert(
55-
std::is_base_of<MockingBitGenImpl<true>, MockURBG>::value ||
56-
std::is_base_of<MockingBitGenImpl<false>, MockURBG>::value,
57-
"Mocking requires an absl::MockingBitGen");
54+
static_assert(std::is_base_of<MockingBitGen, MockURBG>::value,
55+
"Mocking requires an absl::MockingBitGen");
5856
return MockHelpers::MockFor<KeyT>(gen, ValidatorT())
5957
.gmock_Call(matchers...);
6058
}
@@ -74,10 +72,8 @@ struct MockSingleOverload<DistrT, ValidatorT,
7472
const ::testing::Matcher<Args>&... matchers)
7573
-> decltype(MockHelpers::MockFor<KeyT>(gen, ValidatorT())
7674
.gmock_Call(matcher, matchers...)) {
77-
static_assert(
78-
std::is_base_of<MockingBitGenImpl<true>, MockURBG>::value ||
79-
std::is_base_of<MockingBitGenImpl<false>, MockURBG>::value,
80-
"Mocking requires an absl::MockingBitGen");
75+
static_assert(std::is_base_of<MockingBitGen, MockURBG>::value,
76+
"Mocking requires an absl::MockingBitGen");
8177
return MockHelpers::MockFor<KeyT>(gen, ValidatorT())
8278
.gmock_Call(matcher, matchers...);
8379
}

absl/random/mock_distributions_test.cc

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,15 @@ TEST(MockDistributions, Examples) {
7575
EXPECT_EQ(absl::LogUniform<int>(gen, 0, 1000000, 2), 2040);
7676
}
7777

78-
TEST(MockUniform, OutOfBoundsIsAllowed) {
79-
absl::UnvalidatedMockingBitGen gen;
80-
81-
EXPECT_CALL(absl::MockUniform<int>(), Call(gen, 1, 100)).WillOnce(Return(0));
82-
EXPECT_EQ(absl::Uniform<int>(gen, 1, 100), 0);
83-
}
84-
85-
TEST(ValidatedMockDistributions, UniformUInt128Works) {
78+
TEST(MockDistributions, UniformUInt128BoundariesAreAllowed) {
8679
absl::MockingBitGen gen;
8780

8881
EXPECT_CALL(absl::MockUniform<absl::uint128>(), Call(gen))
8982
.WillOnce(Return(absl::Uint128Max()));
9083
EXPECT_EQ(absl::Uniform<absl::uint128>(gen), absl::Uint128Max());
9184
}
9285

93-
TEST(ValidatedMockDistributions, UniformDoubleBoundaryCases) {
86+
TEST(MockDistributions, UniformDoubleBoundaryCasesAreAllowed) {
9487
absl::MockingBitGen gen;
9588

9689
EXPECT_CALL(absl::MockUniform<double>(), Call(gen, 1.0, 10.0))
@@ -114,7 +107,7 @@ TEST(ValidatedMockDistributions, UniformDoubleBoundaryCases) {
114107
std::nextafter(1.0, std::numeric_limits<double>::infinity()));
115108
}
116109

117-
TEST(ValidatedMockDistributions, UniformDoubleEmptyRangeCases) {
110+
TEST(MockDistributions, UniformDoubleEmptyRangesAllowTheBoundary) {
118111
absl::MockingBitGen gen;
119112

120113
ON_CALL(absl::MockUniform<double>(), Call(absl::IntervalOpen, gen, 1.0, 1.0))
@@ -134,7 +127,7 @@ TEST(ValidatedMockDistributions, UniformDoubleEmptyRangeCases) {
134127
1.0);
135128
}
136129

137-
TEST(ValidatedMockDistributions, UniformIntEmptyRangeCases) {
130+
TEST(MockDistributions, UniformIntEmptyRangeCasesAllowTheBoundary) {
138131
absl::MockingBitGen gen;
139132

140133
ON_CALL(absl::MockUniform<int>(), Call(absl::IntervalOpen, gen, 1, 1))
@@ -150,7 +143,7 @@ TEST(ValidatedMockDistributions, UniformIntEmptyRangeCases) {
150143
EXPECT_EQ(absl::Uniform<int>(absl::IntervalClosedOpen, gen, 1, 1), 1);
151144
}
152145

153-
TEST(ValidatedMockUniformDeathTest, Examples) {
146+
TEST(MockUniformDeathTest, OutOfBoundsValuesAreRejected) {
154147
absl::MockingBitGen gen;
155148

156149
EXPECT_DEATH_IF_SUPPORTED(
@@ -252,7 +245,7 @@ TEST(ValidatedMockUniformDeathTest, Examples) {
252245
" 101 is not in \\[1, 100\\]");
253246
}
254247

255-
TEST(ValidatedMockUniformDeathTest, DoubleBoundaryCases) {
248+
TEST(MockUniformDeathTest, OutOfBoundsDoublesAreRejected) {
256249
absl::MockingBitGen gen;
257250

258251
EXPECT_DEATH_IF_SUPPORTED(

absl/random/mocking_bit_gen.h

Lines changed: 50 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,53 @@ namespace random_internal {
5252
template <typename>
5353
struct DistributionCaller;
5454
class MockHelpers;
55+
} // namespace random_internal
5556

56-
// Implements MockingBitGen with an option to turn on extra validation.
57-
template <bool EnableValidation>
58-
class MockingBitGenImpl {
57+
// MockingBitGen
58+
//
59+
// `absl::MockingBitGen` is a mock Uniform Random Bit Generator (URBG) class
60+
// which can act in place of an `absl::BitGen` URBG within tests using the
61+
// Googletest testing framework.
62+
//
63+
// Usage:
64+
//
65+
// Use an `absl::MockingBitGen` along with a mock distribution object (within
66+
// mock_distributions.h) inside Googletest constructs such as ON_CALL(),
67+
// EXPECT_TRUE(), etc. to produce deterministic results conforming to the
68+
// distribution's API contract.
69+
//
70+
// Example:
71+
//
72+
// // Mock a call to an `absl::Bernoulli` distribution using Googletest
73+
// absl::MockingBitGen bitgen;
74+
//
75+
// ON_CALL(absl::MockBernoulli(), Call(bitgen, 0.5))
76+
// .WillByDefault(testing::Return(true));
77+
// EXPECT_TRUE(absl::Bernoulli(bitgen, 0.5));
78+
//
79+
// // Mock a call to an `absl::Uniform` distribution within Googletest
80+
// absl::MockingBitGen bitgen;
81+
//
82+
// ON_CALL(absl::MockUniform<int>(), Call(bitgen, testing::_, testing::_))
83+
// .WillByDefault([] (int low, int high) {
84+
// return low + (high - low) / 2;
85+
// });
86+
//
87+
// EXPECT_EQ(absl::Uniform<int>(gen, 0, 10), 5);
88+
// EXPECT_EQ(absl::Uniform<int>(gen, 30, 40), 35);
89+
//
90+
// At this time, only mock distributions supplied within the Abseil random
91+
// library are officially supported.
92+
//
93+
// EXPECT_CALL and ON_CALL need to be made within the same DLL component as
94+
// the call to absl::Uniform and related methods, otherwise mocking will fail
95+
// since the underlying implementation creates a type-specific pointer which
96+
// will be distinct across different DLL boundaries.
97+
//
98+
class MockingBitGen {
5999
public:
60-
MockingBitGenImpl() = default;
61-
~MockingBitGenImpl() = default;
100+
MockingBitGen() = default;
101+
~MockingBitGen() = default;
62102

63103
// URBG interface
64104
using result_type = absl::BitGen::result_type;
@@ -138,25 +178,23 @@ class MockingBitGenImpl {
138178
typename ValidatorT>
139179
auto RegisterMock(SelfT&, base_internal::FastTypeIdType type, ValidatorT)
140180
-> decltype(GetMockFnType(std::declval<ResultT>(),
141-
std::declval<ArgTupleT>()))& {
142-
using ActualValidatorT =
143-
std::conditional_t<EnableValidation, ValidatorT, NoOpValidator>;
181+
std::declval<ArgTupleT>())) & {
144182
using MockFnType = decltype(GetMockFnType(std::declval<ResultT>(),
145183
std::declval<ArgTupleT>()));
146184

147185
using WrappedFnType = absl::conditional_t<
148-
std::is_same<SelfT, ::testing::NiceMock<MockingBitGenImpl>>::value,
186+
std::is_same<SelfT, ::testing::NiceMock<MockingBitGen>>::value,
149187
::testing::NiceMock<MockFnType>,
150188
absl::conditional_t<
151-
std::is_same<SelfT, ::testing::NaggyMock<MockingBitGenImpl>>::value,
189+
std::is_same<SelfT, ::testing::NaggyMock<MockingBitGen>>::value,
152190
::testing::NaggyMock<MockFnType>,
153191
absl::conditional_t<
154192
std::is_same<SelfT,
155-
::testing::StrictMock<MockingBitGenImpl>>::value,
193+
::testing::StrictMock<MockingBitGen>>::value,
156194
::testing::StrictMock<MockFnType>, MockFnType>>>;
157195

158196
using ImplT =
159-
FunctionHolderImpl<WrappedFnType, ActualValidatorT, ResultT, ArgTupleT>;
197+
FunctionHolderImpl<WrappedFnType, ValidatorT, ResultT, ArgTupleT>;
160198
auto& mock = mocks_[type];
161199
if (!mock) {
162200
mock = absl::make_unique<ImplT>();
@@ -196,58 +234,6 @@ class MockingBitGenImpl {
196234
// InvokeMock
197235
};
198236

199-
} // namespace random_internal
200-
201-
// MockingBitGen
202-
//
203-
// `absl::MockingBitGen` is a mock Uniform Random Bit Generator (URBG) class
204-
// which can act in place of an `absl::BitGen` URBG within tests using the
205-
// Googletest testing framework.
206-
//
207-
// Usage:
208-
//
209-
// Use an `absl::MockingBitGen` along with a mock distribution object (within
210-
// mock_distributions.h) inside Googletest constructs such as ON_CALL(),
211-
// EXPECT_TRUE(), etc. to produce deterministic results conforming to the
212-
// distribution's API contract.
213-
//
214-
// Example:
215-
//
216-
// // Mock a call to an `absl::Bernoulli` distribution using Googletest
217-
// absl::MockingBitGen bitgen;
218-
//
219-
// ON_CALL(absl::MockBernoulli(), Call(bitgen, 0.5))
220-
// .WillByDefault(testing::Return(true));
221-
// EXPECT_TRUE(absl::Bernoulli(bitgen, 0.5));
222-
//
223-
// // Mock a call to an `absl::Uniform` distribution within Googletest
224-
// absl::MockingBitGen bitgen;
225-
//
226-
// ON_CALL(absl::MockUniform<int>(), Call(bitgen, testing::_, testing::_))
227-
// .WillByDefault([] (int low, int high) {
228-
// return low + (high - low) / 2;
229-
// });
230-
//
231-
// EXPECT_EQ(absl::Uniform<int>(gen, 0, 10), 5);
232-
// EXPECT_EQ(absl::Uniform<int>(gen, 30, 40), 35);
233-
//
234-
// At this time, only mock distributions supplied within the Abseil random
235-
// library are officially supported.
236-
//
237-
// EXPECT_CALL and ON_CALL need to be made within the same DLL component as
238-
// the call to absl::Uniform and related methods, otherwise mocking will fail
239-
// since the underlying implementation creates a type-specific pointer which
240-
// will be distinct across different DLL boundaries.
241-
//
242-
using MockingBitGen = random_internal::MockingBitGenImpl<true>;
243-
244-
// UnvalidatedMockingBitGen
245-
//
246-
// UnvalidatedMockingBitGen is a variant of MockingBitGen which does no extra
247-
// validation.
248-
using UnvalidatedMockingBitGen ABSL_DEPRECATED("Use MockingBitGen instead") =
249-
random_internal::MockingBitGenImpl<false>;
250-
251237
ABSL_NAMESPACE_END
252238
} // namespace absl
253239

0 commit comments

Comments
 (0)