Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // Copyright 2018 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 // -----------------------------------------------------------------------------
0016 // File: mock_distributions.h
0017 // -----------------------------------------------------------------------------
0018 //
0019 // This file contains mock distribution functions for use alongside an
0020 // `absl::MockingBitGen` object within the Googletest testing framework. Such
0021 // mocks are useful to provide deterministic values as return values within
0022 // (otherwise random) Abseil distribution functions.
0023 //
0024 // The return type of each function is a mock expectation object which
0025 // is used to set the match result.
0026 //
0027 // More information about the Googletest testing framework is available at
0028 // https://github.com/google/googletest
0029 //
0030 // EXPECT_CALL and ON_CALL need to be made within the same DLL component as
0031 // the call to absl::Uniform and related methods, otherwise mocking will fail
0032 // since the  underlying implementation creates a type-specific pointer which
0033 // will be distinct across different DLL boundaries.
0034 //
0035 // Example:
0036 //
0037 //   absl::MockingBitGen mock;
0038 //   EXPECT_CALL(absl::MockUniform<int>(), Call(mock, 1, 1000))
0039 //     .WillRepeatedly(testing::ReturnRoundRobin({20, 40}));
0040 //
0041 //   EXPECT_EQ(absl::Uniform<int>(gen, 1, 1000), 20);
0042 //   EXPECT_EQ(absl::Uniform<int>(gen, 1, 1000), 40);
0043 //   EXPECT_EQ(absl::Uniform<int>(gen, 1, 1000), 20);
0044 //   EXPECT_EQ(absl::Uniform<int>(gen, 1, 1000), 40);
0045 
0046 #ifndef ABSL_RANDOM_MOCK_DISTRIBUTIONS_H_
0047 #define ABSL_RANDOM_MOCK_DISTRIBUTIONS_H_
0048 
0049 #include "absl/base/config.h"
0050 #include "absl/random/bernoulli_distribution.h"
0051 #include "absl/random/beta_distribution.h"
0052 #include "absl/random/distributions.h"
0053 #include "absl/random/exponential_distribution.h"
0054 #include "absl/random/gaussian_distribution.h"
0055 #include "absl/random/internal/mock_overload_set.h"
0056 #include "absl/random/internal/mock_validators.h"
0057 #include "absl/random/log_uniform_int_distribution.h"
0058 #include "absl/random/mocking_bit_gen.h"
0059 #include "absl/random/poisson_distribution.h"
0060 #include "absl/random/zipf_distribution.h"
0061 
0062 namespace absl {
0063 ABSL_NAMESPACE_BEGIN
0064 
0065 // -----------------------------------------------------------------------------
0066 // absl::MockUniform
0067 // -----------------------------------------------------------------------------
0068 //
0069 // Matches calls to absl::Uniform.
0070 //
0071 // `absl::MockUniform` is a class template used in conjunction with Googletest's
0072 // `ON_CALL()` and `EXPECT_CALL()` macros. To use it, default-construct an
0073 // instance of it inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the
0074 // same way one would define mocks on a Googletest `MockFunction()`.
0075 //
0076 // Example:
0077 //
0078 //  absl::MockingBitGen mock;
0079 //  EXPECT_CALL(absl::MockUniform<uint32_t>(), Call(mock))
0080 //     .WillOnce(Return(123456));
0081 //  auto x = absl::Uniform<uint32_t>(mock);
0082 //  assert(x == 123456)
0083 //
0084 template <typename R>
0085 using MockUniform = random_internal::MockOverloadSetWithValidator<
0086     random_internal::UniformDistributionWrapper<R>,
0087     random_internal::UniformDistributionValidator<R>,
0088     R(IntervalClosedOpenTag, MockingBitGen&, R, R),
0089     R(IntervalClosedClosedTag, MockingBitGen&, R, R),
0090     R(IntervalOpenOpenTag, MockingBitGen&, R, R),
0091     R(IntervalOpenClosedTag, MockingBitGen&, R, R), R(MockingBitGen&, R, R),
0092     R(MockingBitGen&)>;
0093 
0094 // -----------------------------------------------------------------------------
0095 // absl::MockBernoulli
0096 // -----------------------------------------------------------------------------
0097 //
0098 // Matches calls to absl::Bernoulli.
0099 //
0100 // `absl::MockBernoulli` is a class used in conjunction with Googletest's
0101 // `ON_CALL()` and `EXPECT_CALL()` macros. To use it, default-construct an
0102 // instance of it inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the
0103 // same way one would define mocks on a Googletest `MockFunction()`.
0104 //
0105 // Example:
0106 //
0107 //  absl::MockingBitGen mock;
0108 //  EXPECT_CALL(absl::MockBernoulli(), Call(mock, testing::_))
0109 //     .WillOnce(Return(false));
0110 //  assert(absl::Bernoulli(mock, 0.5) == false);
0111 //
0112 using MockBernoulli =
0113     random_internal::MockOverloadSet<absl::bernoulli_distribution,
0114                                      bool(MockingBitGen&, double)>;
0115 
0116 // -----------------------------------------------------------------------------
0117 // absl::MockBeta
0118 // -----------------------------------------------------------------------------
0119 //
0120 // Matches calls to absl::Beta.
0121 //
0122 // `absl::MockBeta` is a class used in conjunction with Googletest's `ON_CALL()`
0123 // and `EXPECT_CALL()` macros. To use it, default-construct an instance of it
0124 // inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the same way one
0125 // would define mocks on a Googletest `MockFunction()`.
0126 //
0127 // Example:
0128 //
0129 //  absl::MockingBitGen mock;
0130 //  EXPECT_CALL(absl::MockBeta(), Call(mock, 3.0, 2.0))
0131 //     .WillOnce(Return(0.567));
0132 //  auto x = absl::Beta<double>(mock, 3.0, 2.0);
0133 //  assert(x == 0.567);
0134 //
0135 template <typename RealType>
0136 using MockBeta =
0137     random_internal::MockOverloadSet<absl::beta_distribution<RealType>,
0138                                      RealType(MockingBitGen&, RealType,
0139                                               RealType)>;
0140 
0141 // -----------------------------------------------------------------------------
0142 // absl::MockExponential
0143 // -----------------------------------------------------------------------------
0144 //
0145 // Matches calls to absl::Exponential.
0146 //
0147 // `absl::MockExponential` is a class template used in conjunction with
0148 // Googletest's `ON_CALL()` and `EXPECT_CALL()` macros. To use it,
0149 // default-construct an instance of it inside `ON_CALL()` or `EXPECT_CALL()`,
0150 // and use `Call(...)` the same way one would define mocks on a
0151 // Googletest `MockFunction()`.
0152 //
0153 // Example:
0154 //
0155 //  absl::MockingBitGen mock;
0156 //  EXPECT_CALL(absl::MockExponential<double>(), Call(mock, 0.5))
0157 //     .WillOnce(Return(12.3456789));
0158 //  auto x = absl::Exponential<double>(mock, 0.5);
0159 //  assert(x == 12.3456789)
0160 //
0161 template <typename RealType>
0162 using MockExponential =
0163     random_internal::MockOverloadSet<absl::exponential_distribution<RealType>,
0164                                      RealType(MockingBitGen&, RealType)>;
0165 
0166 // -----------------------------------------------------------------------------
0167 // absl::MockGaussian
0168 // -----------------------------------------------------------------------------
0169 //
0170 // Matches calls to absl::Gaussian.
0171 //
0172 // `absl::MockGaussian` is a class template used in conjunction with
0173 // Googletest's `ON_CALL()` and `EXPECT_CALL()` macros. To use it,
0174 // default-construct an instance of it inside `ON_CALL()` or `EXPECT_CALL()`,
0175 // and use `Call(...)` the same way one would define mocks on a
0176 // Googletest `MockFunction()`.
0177 //
0178 // Example:
0179 //
0180 //  absl::MockingBitGen mock;
0181 //  EXPECT_CALL(absl::MockGaussian<double>(), Call(mock, 16.3, 3.3))
0182 //     .WillOnce(Return(12.3456789));
0183 //  auto x = absl::Gaussian<double>(mock, 16.3, 3.3);
0184 //  assert(x == 12.3456789)
0185 //
0186 template <typename RealType>
0187 using MockGaussian =
0188     random_internal::MockOverloadSet<absl::gaussian_distribution<RealType>,
0189                                      RealType(MockingBitGen&, RealType,
0190                                               RealType)>;
0191 
0192 // -----------------------------------------------------------------------------
0193 // absl::MockLogUniform
0194 // -----------------------------------------------------------------------------
0195 //
0196 // Matches calls to absl::LogUniform.
0197 //
0198 // `absl::MockLogUniform` is a class template used in conjunction with
0199 // Googletest's `ON_CALL()` and `EXPECT_CALL()` macros. To use it,
0200 // default-construct an instance of it inside `ON_CALL()` or `EXPECT_CALL()`,
0201 // and use `Call(...)` the same way one would define mocks on a
0202 // Googletest `MockFunction()`.
0203 //
0204 // Example:
0205 //
0206 //  absl::MockingBitGen mock;
0207 //  EXPECT_CALL(absl::MockLogUniform<int>(), Call(mock, 10, 10000, 10))
0208 //     .WillOnce(Return(1221));
0209 //  auto x = absl::LogUniform<int>(mock, 10, 10000, 10);
0210 //  assert(x == 1221)
0211 //
0212 template <typename IntType>
0213 using MockLogUniform = random_internal::MockOverloadSet<
0214     absl::log_uniform_int_distribution<IntType>,
0215     IntType(MockingBitGen&, IntType, IntType, IntType)>;
0216 
0217 // -----------------------------------------------------------------------------
0218 // absl::MockPoisson
0219 // -----------------------------------------------------------------------------
0220 //
0221 // Matches calls to absl::Poisson.
0222 //
0223 // `absl::MockPoisson` is a class template used in conjunction with Googletest's
0224 // `ON_CALL()` and `EXPECT_CALL()` macros. To use it, default-construct an
0225 // instance of it inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the
0226 // same way one would define mocks on a Googletest `MockFunction()`.
0227 //
0228 // Example:
0229 //
0230 //  absl::MockingBitGen mock;
0231 //  EXPECT_CALL(absl::MockPoisson<int>(), Call(mock, 2.0))
0232 //     .WillOnce(Return(1221));
0233 //  auto x = absl::Poisson<int>(mock, 2.0);
0234 //  assert(x == 1221)
0235 //
0236 template <typename IntType>
0237 using MockPoisson =
0238     random_internal::MockOverloadSet<absl::poisson_distribution<IntType>,
0239                                      IntType(MockingBitGen&, double)>;
0240 
0241 // -----------------------------------------------------------------------------
0242 // absl::MockZipf
0243 // -----------------------------------------------------------------------------
0244 //
0245 // Matches calls to absl::Zipf.
0246 //
0247 // `absl::MockZipf` is a class template used in conjunction with Googletest's
0248 // `ON_CALL()` and `EXPECT_CALL()` macros. To use it, default-construct an
0249 // instance of it inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the
0250 // same way one would define mocks on a Googletest `MockFunction()`.
0251 //
0252 // Example:
0253 //
0254 //  absl::MockingBitGen mock;
0255 //  EXPECT_CALL(absl::MockZipf<int>(), Call(mock, 1000000, 2.0, 1.0))
0256 //     .WillOnce(Return(1221));
0257 //  auto x = absl::Zipf<int>(mock, 1000000, 2.0, 1.0);
0258 //  assert(x == 1221)
0259 //
0260 template <typename IntType>
0261 using MockZipf =
0262     random_internal::MockOverloadSet<absl::zipf_distribution<IntType>,
0263                                      IntType(MockingBitGen&, IntType, double,
0264                                              double)>;
0265 
0266 ABSL_NAMESPACE_END
0267 }  // namespace absl
0268 
0269 #endif  // ABSL_RANDOM_MOCK_DISTRIBUTIONS_H_