Commit 33dca3ef by Justin Bassett Committed by Copybara-Service

Turn on validation for out of bounds MockUniform in MockingBitGen

This makes MockUniform fail if the action is specified to return an out of bounds value.

Examples that will fail:

  absl::Uniform(gen, 1, 4) -> 42
  absl::Uniform(gen, 1, 4) -> 4: [1, 4)
  absl::Uniform(absl::IntervalOpenClosed, gen, 1, 4) -> 1: (1, 4]

Examples that will pass:

  absl::Uniform(gen, 1, 4) -> 3
  absl::Uniform(gen, 1, 4) -> 1: [1, 4)
  absl::Uniform(absl::IntervalClosed, gen, 1, 4) -> 4: [1, 4]

Special case: the empty range always returns its boundary, so this case passes:

  absl::Uniform(absl::IntervalOpen, 1, 1) -> 1: (1, 1)

If this breaks your test, your test has a bug: it's relying on an absl::Uniform() call that returns an impossible value. The UnvalidatedMockingBitGen type temporarily exists to allow for disabling the validation to give a bit of time to fix the test, but this type will go away soon.

PiperOrigin-RevId: 643090275
Change-Id: I23470fa9e1efbcb42fa3866237038414545c7be2
parent 7c03b80e
......@@ -76,14 +76,14 @@ TEST(MockDistributions, Examples) {
}
TEST(MockUniform, OutOfBoundsIsAllowed) {
absl::MockingBitGen gen;
absl::UnvalidatedMockingBitGen gen;
EXPECT_CALL(absl::MockUniform<int>(), Call(gen, 1, 100)).WillOnce(Return(0));
EXPECT_EQ(absl::Uniform<int>(gen, 1, 100), 0);
}
TEST(ValidatedMockDistributions, UniformUInt128Works) {
absl::random_internal::MockingBitGenImpl<true> gen;
absl::MockingBitGen gen;
EXPECT_CALL(absl::MockUniform<absl::uint128>(), Call(gen))
.WillOnce(Return(absl::Uint128Max()));
......@@ -91,7 +91,7 @@ TEST(ValidatedMockDistributions, UniformUInt128Works) {
}
TEST(ValidatedMockDistributions, UniformDoubleBoundaryCases) {
absl::random_internal::MockingBitGenImpl<true> gen;
absl::MockingBitGen gen;
EXPECT_CALL(absl::MockUniform<double>(), Call(gen, 1.0, 10.0))
.WillOnce(Return(
......@@ -115,7 +115,7 @@ TEST(ValidatedMockDistributions, UniformDoubleBoundaryCases) {
}
TEST(ValidatedMockDistributions, UniformDoubleEmptyRangeCases) {
absl::random_internal::MockingBitGenImpl<true> gen;
absl::MockingBitGen gen;
ON_CALL(absl::MockUniform<double>(), Call(absl::IntervalOpen, gen, 1.0, 1.0))
.WillByDefault(Return(1.0));
......@@ -135,7 +135,7 @@ TEST(ValidatedMockDistributions, UniformDoubleEmptyRangeCases) {
}
TEST(ValidatedMockDistributions, UniformIntEmptyRangeCases) {
absl::random_internal::MockingBitGenImpl<true> gen;
absl::MockingBitGen gen;
ON_CALL(absl::MockUniform<int>(), Call(absl::IntervalOpen, gen, 1, 1))
.WillByDefault(Return(1));
......@@ -151,7 +151,7 @@ TEST(ValidatedMockDistributions, UniformIntEmptyRangeCases) {
}
TEST(ValidatedMockUniformDeathTest, Examples) {
absl::random_internal::MockingBitGenImpl<true> gen;
absl::MockingBitGen gen;
EXPECT_DEATH_IF_SUPPORTED(
{
......@@ -253,7 +253,7 @@ TEST(ValidatedMockUniformDeathTest, Examples) {
}
TEST(ValidatedMockUniformDeathTest, DoubleBoundaryCases) {
absl::random_internal::MockingBitGenImpl<true> gen;
absl::MockingBitGen gen;
EXPECT_DEATH_IF_SUPPORTED(
{
......
......@@ -239,7 +239,7 @@ class MockingBitGenImpl {
// since the underlying implementation creates a type-specific pointer which
// will be distinct across different DLL boundaries.
//
using MockingBitGen = random_internal::MockingBitGenImpl<false>;
using MockingBitGen = random_internal::MockingBitGenImpl<true>;
// UnvalidatedMockingBitGen
//
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment