diff options
author | Justin Bassett <jbassett@google.com> | 2024-06-13 12:43:39 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2024-06-13 12:44:29 -0700 |
commit | 33dca3ef75533ba0cc9b099b17409bb354344497 (patch) | |
tree | 08151f65827f46ac41a8dcff9423f2516a611c9a | |
parent | 7c03b80ef26eef1b34775b035b66a2531d917595 (diff) | |
download | abseil-33dca3ef75533ba0cc9b099b17409bb354344497.tar.gz abseil-33dca3ef75533ba0cc9b099b17409bb354344497.tar.bz2 abseil-33dca3ef75533ba0cc9b099b17409bb354344497.zip |
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
-rw-r--r-- | absl/random/mock_distributions_test.cc | 14 | ||||
-rw-r--r-- | absl/random/mocking_bit_gen.h | 2 |
2 files changed, 8 insertions, 8 deletions
diff --git a/absl/random/mock_distributions_test.cc b/absl/random/mock_distributions_test.cc index 80df8714..05e313cd 100644 --- a/absl/random/mock_distributions_test.cc +++ b/absl/random/mock_distributions_test.cc @@ -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( { diff --git a/absl/random/mocking_bit_gen.h b/absl/random/mocking_bit_gen.h index 92f2e4fc..041989de 100644 --- a/absl/random/mocking_bit_gen.h +++ b/absl/random/mocking_bit_gen.h @@ -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 // |