Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:27:21

0001 // Copyright 2024 The Abseil Authors
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //     https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 
0015 #ifndef ABSL_RANDOM_INTERNAL_MOCK_VALIDATORS_H_
0016 #define ABSL_RANDOM_INTERNAL_MOCK_VALIDATORS_H_
0017 
0018 #include <type_traits>
0019 
0020 #include "absl/base/config.h"
0021 #include "absl/base/internal/raw_logging.h"
0022 #include "absl/random/internal/iostream_state_saver.h"
0023 #include "absl/random/internal/uniform_helper.h"
0024 #include "absl/strings/str_cat.h"
0025 #include "absl/strings/string_view.h"
0026 
0027 namespace absl {
0028 ABSL_NAMESPACE_BEGIN
0029 namespace random_internal {
0030 
0031 template <typename NumType>
0032 class UniformDistributionValidator {
0033  public:
0034   // Handle absl::Uniform<NumType>(gen, absl::IntervalTag, lo, hi).
0035   template <typename TagType>
0036   static void Validate(NumType x, TagType tag, NumType lo, NumType hi) {
0037     // For invalid ranges, absl::Uniform() simply returns one of the bounds.
0038     if (x == lo && lo == hi) return;
0039 
0040     ValidateImpl(std::is_floating_point<NumType>{}, x, tag, lo, hi);
0041   }
0042 
0043   // Handle absl::Uniform<NumType>(gen, lo, hi).
0044   static void Validate(NumType x, NumType lo, NumType hi) {
0045     Validate(x, IntervalClosedOpenTag(), lo, hi);
0046   }
0047 
0048   // Handle absl::Uniform<NumType>(gen).
0049   static void Validate(NumType) {
0050     // absl::Uniform<NumType>(gen) spans the entire range of `NumType`, so any
0051     // value is okay. This overload exists because the validation logic attempts
0052     // to call it anyway rather than adding extra SFINAE.
0053   }
0054 
0055  private:
0056   static absl::string_view TagLbBound(IntervalClosedOpenTag) { return "["; }
0057   static absl::string_view TagLbBound(IntervalOpenOpenTag) { return "("; }
0058   static absl::string_view TagLbBound(IntervalClosedClosedTag) { return "["; }
0059   static absl::string_view TagLbBound(IntervalOpenClosedTag) { return "("; }
0060   static absl::string_view TagUbBound(IntervalClosedOpenTag) { return ")"; }
0061   static absl::string_view TagUbBound(IntervalOpenOpenTag) { return ")"; }
0062   static absl::string_view TagUbBound(IntervalClosedClosedTag) { return "]"; }
0063   static absl::string_view TagUbBound(IntervalOpenClosedTag) { return "]"; }
0064 
0065   template <typename TagType>
0066   static void ValidateImpl(std::true_type /* is_floating_point */, NumType x,
0067                            TagType tag, NumType lo, NumType hi) {
0068     UniformDistributionWrapper<NumType> dist(tag, lo, hi);
0069     NumType lb = dist.a();
0070     NumType ub = dist.b();
0071     // uniform_real_distribution is always closed-open, so the upper bound is
0072     // always non-inclusive.
0073     ABSL_INTERNAL_CHECK(lb <= x && x < ub,
0074                         absl::StrCat(x, " is not in ", TagLbBound(tag), lo,
0075                                      ", ", hi, TagUbBound(tag)));
0076   }
0077 
0078   template <typename TagType>
0079   static void ValidateImpl(std::false_type /* is_floating_point */, NumType x,
0080                            TagType tag, NumType lo, NumType hi) {
0081     using stream_type =
0082         typename random_internal::stream_format_type<NumType>::type;
0083 
0084     UniformDistributionWrapper<NumType> dist(tag, lo, hi);
0085     NumType lb = dist.a();
0086     NumType ub = dist.b();
0087     ABSL_INTERNAL_CHECK(
0088         lb <= x && x <= ub,
0089         absl::StrCat(stream_type{x}, " is not in ", TagLbBound(tag),
0090                      stream_type{lo}, ", ", stream_type{hi}, TagUbBound(tag)));
0091   }
0092 };
0093 
0094 }  // namespace random_internal
0095 ABSL_NAMESPACE_END
0096 }  // namespace absl
0097 
0098 #endif  // ABSL_RANDOM_INTERNAL_MOCK_VALIDATORS_H_