Commit e7f1a950 by Justin Bassett Committed by Copybara-Service

Support int128/uint128 in validated MockingBitGen

`absl::int128` and `absl::uint128` are not `std::is_integral`. There is an internal `IsIntegral` type trait we could use, but it actually makes more sense to remove the `static_assert` altogether. Any compile-time validation should be done in `absl::Uniform` itself, and duplicating that logic here just increases the chance of divergence.

PiperOrigin-RevId: 635971431
Change-Id: I9177ae64c86ee1abe6571e0b29aba1844553c972
parent a2625a64
......@@ -483,6 +483,7 @@ cc_test(
":mock_distributions",
":mocking_bit_gen",
":random",
"//absl/numeric:int128",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
......
......@@ -1206,6 +1206,7 @@ absl_cc_test(
DEPS
absl::random_internal_uniform_helper
GTest::gtest_main
absl::int128
)
# Internal-only target, do not depend on directly.
......
......@@ -31,6 +31,7 @@ namespace random_internal {
template <typename NumType>
class UniformDistributionValidator {
public:
// Handle absl::Uniform<NumType>(gen, absl::IntervalTag, lo, hi).
template <typename TagType>
static void Validate(NumType x, TagType tag, NumType lo, NumType hi) {
// For invalid ranges, absl::Uniform() simply returns one of the bounds.
......@@ -39,17 +40,16 @@ class UniformDistributionValidator {
ValidateImpl(std::is_floating_point<NumType>{}, x, tag, lo, hi);
}
// Handle absl::Uniform<NumType>(gen, lo, hi).
static void Validate(NumType x, NumType lo, NumType hi) {
Validate(x, IntervalClosedOpenTag(), lo, hi);
}
template <typename NumType_ = NumType>
// Handle absl::Uniform<NumType>(gen).
static void Validate(NumType) {
// absl::Uniform<NumType>(gen) spans the entire range of `NumType`, so any
// value is okay.
static_assert(std::is_integral<NumType_>{},
"Non-integer types may have valid values outside of the full "
"range (e.g. floating point NaN).");
// value is okay. This overload exists because the validation logic attempts
// to call it anyway rather than adding extra SFINAE.
}
private:
......
......@@ -19,6 +19,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/numeric/int128.h"
#include "absl/random/distributions.h"
#include "absl/random/mocking_bit_gen.h"
#include "absl/random/random.h"
......@@ -81,6 +82,14 @@ TEST(MockUniform, OutOfBoundsIsAllowed) {
EXPECT_EQ(absl::Uniform<int>(gen, 1, 100), 0);
}
TEST(ValidatedMockDistributions, UniformUInt128Works) {
absl::random_internal::MockingBitGenImpl<true> gen;
EXPECT_CALL(absl::MockUniform<absl::uint128>(), Call(gen))
.WillOnce(Return(absl::Uint128Max()));
EXPECT_EQ(absl::Uniform<absl::uint128>(gen), absl::Uint128Max());
}
TEST(ValidatedMockDistributions, UniformDoubleBoundaryCases) {
absl::random_internal::MockingBitGenImpl<true> gen;
......
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