mock_validators.h 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2024 The Abseil Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ABSL_RANDOM_INTERNAL_MOCK_VALIDATORS_H_
  15. #define ABSL_RANDOM_INTERNAL_MOCK_VALIDATORS_H_
  16. #include <type_traits>
  17. #include "absl/base/config.h"
  18. #include "absl/base/internal/raw_logging.h"
  19. #include "absl/random/internal/iostream_state_saver.h"
  20. #include "absl/random/internal/uniform_helper.h"
  21. #include "absl/strings/str_cat.h"
  22. #include "absl/strings/string_view.h"
  23. namespace absl {
  24. ABSL_NAMESPACE_BEGIN
  25. namespace random_internal {
  26. template <typename NumType>
  27. class UniformDistributionValidator {
  28. public:
  29. // Handle absl::Uniform<NumType>(gen, absl::IntervalTag, lo, hi).
  30. template <typename TagType>
  31. static void Validate(NumType x, TagType tag, NumType lo, NumType hi) {
  32. // For invalid ranges, absl::Uniform() simply returns one of the bounds.
  33. if (x == lo && lo == hi) return;
  34. ValidateImpl(std::is_floating_point<NumType>{}, x, tag, lo, hi);
  35. }
  36. // Handle absl::Uniform<NumType>(gen, lo, hi).
  37. static void Validate(NumType x, NumType lo, NumType hi) {
  38. Validate(x, IntervalClosedOpenTag(), lo, hi);
  39. }
  40. // Handle absl::Uniform<NumType>(gen).
  41. static void Validate(NumType) {
  42. // absl::Uniform<NumType>(gen) spans the entire range of `NumType`, so any
  43. // value is okay. This overload exists because the validation logic attempts
  44. // to call it anyway rather than adding extra SFINAE.
  45. }
  46. private:
  47. static absl::string_view TagLbBound(IntervalClosedOpenTag) { return "["; }
  48. static absl::string_view TagLbBound(IntervalOpenOpenTag) { return "("; }
  49. static absl::string_view TagLbBound(IntervalClosedClosedTag) { return "["; }
  50. static absl::string_view TagLbBound(IntervalOpenClosedTag) { return "("; }
  51. static absl::string_view TagUbBound(IntervalClosedOpenTag) { return ")"; }
  52. static absl::string_view TagUbBound(IntervalOpenOpenTag) { return ")"; }
  53. static absl::string_view TagUbBound(IntervalClosedClosedTag) { return "]"; }
  54. static absl::string_view TagUbBound(IntervalOpenClosedTag) { return "]"; }
  55. template <typename TagType>
  56. static void ValidateImpl(std::true_type /* is_floating_point */, NumType x,
  57. TagType tag, NumType lo, NumType hi) {
  58. UniformDistributionWrapper<NumType> dist(tag, lo, hi);
  59. NumType lb = dist.a();
  60. NumType ub = dist.b();
  61. // uniform_real_distribution is always closed-open, so the upper bound is
  62. // always non-inclusive.
  63. ABSL_INTERNAL_CHECK(lb <= x && x < ub,
  64. absl::StrCat(x, " is not in ", TagLbBound(tag), lo,
  65. ", ", hi, TagUbBound(tag)));
  66. }
  67. template <typename TagType>
  68. static void ValidateImpl(std::false_type /* is_floating_point */, NumType x,
  69. TagType tag, NumType lo, NumType hi) {
  70. using stream_type =
  71. typename random_internal::stream_format_type<NumType>::type;
  72. UniformDistributionWrapper<NumType> dist(tag, lo, hi);
  73. NumType lb = dist.a();
  74. NumType ub = dist.b();
  75. ABSL_INTERNAL_CHECK(
  76. lb <= x && x <= ub,
  77. absl::StrCat(stream_type{x}, " is not in ", TagLbBound(tag),
  78. stream_type{lo}, ", ", stream_type{hi}, TagUbBound(tag)));
  79. }
  80. };
  81. } // namespace random_internal
  82. ABSL_NAMESPACE_END
  83. } // namespace absl
  84. #endif // ABSL_RANDOM_INTERNAL_MOCK_VALIDATORS_H_