diff options
author | Justin Bassett <jbassett@google.com> | 2024-05-21 16:56:45 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2024-05-21 16:57:23 -0700 |
commit | e7f1a950e97b805d634909124fa4c75b690d0475 (patch) | |
tree | 8d0c6417c27f468083cc36126da4282678607eaf /absl | |
parent | a2625a648dc69c5b3d0330f25004454716cacfc8 (diff) |
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
Diffstat (limited to 'absl')
-rw-r--r-- | absl/random/BUILD.bazel | 1 | ||||
-rw-r--r-- | absl/random/CMakeLists.txt | 1 | ||||
-rw-r--r-- | absl/random/internal/mock_validators.h | 10 | ||||
-rw-r--r-- | absl/random/mock_distributions_test.cc | 9 |
4 files changed, 16 insertions, 5 deletions
diff --git a/absl/random/BUILD.bazel b/absl/random/BUILD.bazel index 0f31d919..fa1200d5 100644 --- a/absl/random/BUILD.bazel +++ b/absl/random/BUILD.bazel @@ -483,6 +483,7 @@ cc_test( ":mock_distributions", ":mocking_bit_gen", ":random", + "//absl/numeric:int128", "@com_google_googletest//:gtest", "@com_google_googletest//:gtest_main", ], diff --git a/absl/random/CMakeLists.txt b/absl/random/CMakeLists.txt index 3cf65b69..56bbfd81 100644 --- a/absl/random/CMakeLists.txt +++ b/absl/random/CMakeLists.txt @@ -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. diff --git a/absl/random/internal/mock_validators.h b/absl/random/internal/mock_validators.h index 0ab2ee9b..d76d169c 100644 --- a/absl/random/internal/mock_validators.h +++ b/absl/random/internal/mock_validators.h @@ -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: diff --git a/absl/random/mock_distributions_test.cc b/absl/random/mock_distributions_test.cc index 917799f0..80df8714 100644 --- a/absl/random/mock_distributions_test.cc +++ b/absl/random/mock_distributions_test.cc @@ -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; |