File indexing completed on 2025-01-18 09:27:21
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
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
0035 template <typename TagType>
0036 static void Validate(NumType x, TagType tag, NumType lo, NumType hi) {
0037
0038 if (x == lo && lo == hi) return;
0039
0040 ValidateImpl(std::is_floating_point<NumType>{}, x, tag, lo, hi);
0041 }
0042
0043
0044 static void Validate(NumType x, NumType lo, NumType hi) {
0045 Validate(x, IntervalClosedOpenTag(), lo, hi);
0046 }
0047
0048
0049 static void Validate(NumType) {
0050
0051
0052
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 , 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
0072
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 , 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 }
0095 ABSL_NAMESPACE_END
0096 }
0097
0098 #endif