1 // Copyright 2018 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 // 15 // ----------------------------------------------------------------------------- 16 // File: mock_distributions.h 17 // ----------------------------------------------------------------------------- 18 // 19 // This file contains mock distribution functions for use alongside an 20 // `absl::MockingBitGen` object within the GoogleTest testing framework. Such 21 // mocks are useful to provide deterministic values as return values within 22 // (otherwise random) Abseil distribution functions. 23 // 24 // The return type of each function is a mock expectation object which 25 // is used to set the match result. 26 // 27 // More information about the GoogleTest testing framework is available at 28 // https://github.com/google/googletest 29 // 30 // EXPECT_CALL and ON_CALL need to be made within the same DLL component as 31 // the call to absl::Uniform and related methods, otherwise mocking will fail 32 // since the underlying implementation creates a type-specific pointer which 33 // will be distinct across different DLL boundaries. 34 // 35 // Example: 36 // 37 // absl::MockingBitGen mock; 38 // EXPECT_CALL(absl::MockUniform<int>(), Call(mock, 1, 1000)) 39 // .WillRepeatedly(testing::ReturnRoundRobin({20, 40})); 40 // 41 // EXPECT_EQ(absl::Uniform<int>(gen, 1, 1000), 20); 42 // EXPECT_EQ(absl::Uniform<int>(gen, 1, 1000), 40); 43 // EXPECT_EQ(absl::Uniform<int>(gen, 1, 1000), 20); 44 // EXPECT_EQ(absl::Uniform<int>(gen, 1, 1000), 40); 45 46 #ifndef ABSL_RANDOM_MOCK_DISTRIBUTIONS_H_ 47 #define ABSL_RANDOM_MOCK_DISTRIBUTIONS_H_ 48 49 #include "absl/base/config.h" 50 #include "absl/random/bernoulli_distribution.h" 51 #include "absl/random/beta_distribution.h" 52 #include "absl/random/distributions.h" 53 #include "absl/random/exponential_distribution.h" 54 #include "absl/random/gaussian_distribution.h" 55 #include "absl/random/internal/mock_overload_set.h" 56 #include "absl/random/internal/mock_validators.h" 57 #include "absl/random/log_uniform_int_distribution.h" 58 #include "absl/random/mocking_bit_gen.h" 59 #include "absl/random/poisson_distribution.h" 60 #include "absl/random/zipf_distribution.h" 61 62 namespace absl { 63 ABSL_NAMESPACE_BEGIN 64 65 // ----------------------------------------------------------------------------- 66 // absl::MockUniform 67 // ----------------------------------------------------------------------------- 68 // 69 // Matches calls to absl::Uniform. 70 // 71 // `absl::MockUniform` is a class template used in conjunction with Googletest's 72 // `ON_CALL()` and `EXPECT_CALL()` macros. To use it, default-construct an 73 // instance of it inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the 74 // same way one would define mocks on a Googletest `MockFunction()`. 75 // 76 // Example: 77 // 78 // absl::MockingBitGen mock; 79 // EXPECT_CALL(absl::MockUniform<uint32_t>(), Call(mock)) 80 // .WillOnce(Return(123456)); 81 // auto x = absl::Uniform<uint32_t>(mock); 82 // assert(x == 123456) 83 // 84 template <typename R> 85 using MockUniform = random_internal::MockOverloadSetWithValidator< 86 random_internal::UniformDistributionWrapper<R>, 87 random_internal::UniformDistributionValidator<R>, 88 R(IntervalClosedOpenTag, MockingBitGen&, R, R), 89 R(IntervalClosedClosedTag, MockingBitGen&, R, R), 90 R(IntervalOpenOpenTag, MockingBitGen&, R, R), 91 R(IntervalOpenClosedTag, MockingBitGen&, R, R), R(MockingBitGen&, R, R), 92 R(MockingBitGen&)>; 93 94 // ----------------------------------------------------------------------------- 95 // absl::MockBernoulli 96 // ----------------------------------------------------------------------------- 97 // 98 // Matches calls to absl::Bernoulli. 99 // 100 // `absl::MockBernoulli` is a class used in conjunction with Googletest's 101 // `ON_CALL()` and `EXPECT_CALL()` macros. To use it, default-construct an 102 // instance of it inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the 103 // same way one would define mocks on a Googletest `MockFunction()`. 104 // 105 // Example: 106 // 107 // absl::MockingBitGen mock; 108 // EXPECT_CALL(absl::MockBernoulli(), Call(mock, testing::_)) 109 // .WillOnce(Return(false)); 110 // assert(absl::Bernoulli(mock, 0.5) == false); 111 // 112 using MockBernoulli = 113 random_internal::MockOverloadSet<absl::bernoulli_distribution, 114 bool(MockingBitGen&, double)>; 115 116 // ----------------------------------------------------------------------------- 117 // absl::MockBeta 118 // ----------------------------------------------------------------------------- 119 // 120 // Matches calls to absl::Beta. 121 // 122 // `absl::MockBeta` is a class used in conjunction with Googletest's `ON_CALL()` 123 // and `EXPECT_CALL()` macros. To use it, default-construct an instance of it 124 // inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the same way one 125 // would define mocks on a Googletest `MockFunction()`. 126 // 127 // Example: 128 // 129 // absl::MockingBitGen mock; 130 // EXPECT_CALL(absl::MockBeta(), Call(mock, 3.0, 2.0)) 131 // .WillOnce(Return(0.567)); 132 // auto x = absl::Beta<double>(mock, 3.0, 2.0); 133 // assert(x == 0.567); 134 // 135 template <typename RealType> 136 using MockBeta = 137 random_internal::MockOverloadSet<absl::beta_distribution<RealType>, 138 RealType(MockingBitGen&, RealType, 139 RealType)>; 140 141 // ----------------------------------------------------------------------------- 142 // absl::MockExponential 143 // ----------------------------------------------------------------------------- 144 // 145 // Matches calls to absl::Exponential. 146 // 147 // `absl::MockExponential` is a class template used in conjunction with 148 // Googletest's `ON_CALL()` and `EXPECT_CALL()` macros. To use it, 149 // default-construct an instance of it inside `ON_CALL()` or `EXPECT_CALL()`, 150 // and use `Call(...)` the same way one would define mocks on a 151 // Googletest `MockFunction()`. 152 // 153 // Example: 154 // 155 // absl::MockingBitGen mock; 156 // EXPECT_CALL(absl::MockExponential<double>(), Call(mock, 0.5)) 157 // .WillOnce(Return(12.3456789)); 158 // auto x = absl::Exponential<double>(mock, 0.5); 159 // assert(x == 12.3456789) 160 // 161 template <typename RealType> 162 using MockExponential = 163 random_internal::MockOverloadSet<absl::exponential_distribution<RealType>, 164 RealType(MockingBitGen&, RealType)>; 165 166 // ----------------------------------------------------------------------------- 167 // absl::MockGaussian 168 // ----------------------------------------------------------------------------- 169 // 170 // Matches calls to absl::Gaussian. 171 // 172 // `absl::MockGaussian` is a class template used in conjunction with 173 // Googletest's `ON_CALL()` and `EXPECT_CALL()` macros. To use it, 174 // default-construct an instance of it inside `ON_CALL()` or `EXPECT_CALL()`, 175 // and use `Call(...)` the same way one would define mocks on a 176 // Googletest `MockFunction()`. 177 // 178 // Example: 179 // 180 // absl::MockingBitGen mock; 181 // EXPECT_CALL(absl::MockGaussian<double>(), Call(mock, 16.3, 3.3)) 182 // .WillOnce(Return(12.3456789)); 183 // auto x = absl::Gaussian<double>(mock, 16.3, 3.3); 184 // assert(x == 12.3456789) 185 // 186 template <typename RealType> 187 using MockGaussian = 188 random_internal::MockOverloadSet<absl::gaussian_distribution<RealType>, 189 RealType(MockingBitGen&, RealType, 190 RealType)>; 191 192 // ----------------------------------------------------------------------------- 193 // absl::MockLogUniform 194 // ----------------------------------------------------------------------------- 195 // 196 // Matches calls to absl::LogUniform. 197 // 198 // `absl::MockLogUniform` is a class template used in conjunction with 199 // Googletest's `ON_CALL()` and `EXPECT_CALL()` macros. To use it, 200 // default-construct an instance of it inside `ON_CALL()` or `EXPECT_CALL()`, 201 // and use `Call(...)` the same way one would define mocks on a 202 // Googletest `MockFunction()`. 203 // 204 // Example: 205 // 206 // absl::MockingBitGen mock; 207 // EXPECT_CALL(absl::MockLogUniform<int>(), Call(mock, 10, 10000, 10)) 208 // .WillOnce(Return(1221)); 209 // auto x = absl::LogUniform<int>(mock, 10, 10000, 10); 210 // assert(x == 1221) 211 // 212 template <typename IntType> 213 using MockLogUniform = random_internal::MockOverloadSet< 214 absl::log_uniform_int_distribution<IntType>, 215 IntType(MockingBitGen&, IntType, IntType, IntType)>; 216 217 // ----------------------------------------------------------------------------- 218 // absl::MockPoisson 219 // ----------------------------------------------------------------------------- 220 // 221 // Matches calls to absl::Poisson. 222 // 223 // `absl::MockPoisson` is a class template used in conjunction with Googletest's 224 // `ON_CALL()` and `EXPECT_CALL()` macros. To use it, default-construct an 225 // instance of it inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the 226 // same way one would define mocks on a Googletest `MockFunction()`. 227 // 228 // Example: 229 // 230 // absl::MockingBitGen mock; 231 // EXPECT_CALL(absl::MockPoisson<int>(), Call(mock, 2.0)) 232 // .WillOnce(Return(1221)); 233 // auto x = absl::Poisson<int>(mock, 2.0); 234 // assert(x == 1221) 235 // 236 template <typename IntType> 237 using MockPoisson = 238 random_internal::MockOverloadSet<absl::poisson_distribution<IntType>, 239 IntType(MockingBitGen&, double)>; 240 241 // ----------------------------------------------------------------------------- 242 // absl::MockZipf 243 // ----------------------------------------------------------------------------- 244 // 245 // Matches calls to absl::Zipf. 246 // 247 // `absl::MockZipf` is a class template used in conjunction with Googletest's 248 // `ON_CALL()` and `EXPECT_CALL()` macros. To use it, default-construct an 249 // instance of it inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the 250 // same way one would define mocks on a Googletest `MockFunction()`. 251 // 252 // Example: 253 // 254 // absl::MockingBitGen mock; 255 // EXPECT_CALL(absl::MockZipf<int>(), Call(mock, 1000000, 2.0, 1.0)) 256 // .WillOnce(Return(1221)); 257 // auto x = absl::Zipf<int>(mock, 1000000, 2.0, 1.0); 258 // assert(x == 1221) 259 // 260 template <typename IntType> 261 using MockZipf = 262 random_internal::MockOverloadSet<absl::zipf_distribution<IntType>, 263 IntType(MockingBitGen&, IntType, double, 264 double)>; 265 266 ABSL_NAMESPACE_END 267 } // namespace absl 268 269 #endif // ABSL_RANDOM_MOCK_DISTRIBUTIONS_H_ 270