• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Example:
31 //
32 //   absl::MockingBitGen mock;
33 //   EXPECT_CALL(absl::MockUniform<int>(), Call(mock, 1, 1000))
34 //     .WillRepeatedly(testing::ReturnRoundRobin({20, 40}));
35 //
36 //   EXPECT_EQ(absl::Uniform<int>(gen, 1, 1000), 20);
37 //   EXPECT_EQ(absl::Uniform<int>(gen, 1, 1000), 40);
38 //   EXPECT_EQ(absl::Uniform<int>(gen, 1, 1000), 20);
39 //   EXPECT_EQ(absl::Uniform<int>(gen, 1, 1000), 40);
40 
41 #ifndef ABSL_RANDOM_MOCK_DISTRIBUTIONS_H_
42 #define ABSL_RANDOM_MOCK_DISTRIBUTIONS_H_
43 
44 #include <limits>
45 #include <type_traits>
46 #include <utility>
47 
48 #include "gmock/gmock.h"
49 #include "gtest/gtest.h"
50 #include "absl/meta/type_traits.h"
51 #include "absl/random/distributions.h"
52 #include "absl/random/internal/mock_overload_set.h"
53 #include "absl/random/mocking_bit_gen.h"
54 
55 namespace absl {
56 ABSL_NAMESPACE_BEGIN
57 
58 // -----------------------------------------------------------------------------
59 // absl::MockUniform
60 // -----------------------------------------------------------------------------
61 //
62 // Matches calls to absl::Uniform.
63 //
64 // `absl::MockUniform` is a class template used in conjunction with Googletest's
65 // `ON_CALL()` and `EXPECT_CALL()` macros. To use it, default-construct an
66 // instance of it inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the
67 // same way one would define mocks on a Googletest `MockFunction()`.
68 //
69 // Example:
70 //
71 //  absl::MockingBitGen mock;
72 //  EXPECT_CALL(absl::MockUniform<uint32_t>(), Call(mock))
73 //     .WillOnce(Return(123456));
74 //  auto x = absl::Uniform<uint32_t>(mock);
75 //  assert(x == 123456)
76 //
77 template <typename R>
78 using MockUniform = random_internal::MockOverloadSet<
79     random_internal::UniformDistributionWrapper<R>,
80     R(IntervalClosedOpenTag, MockingBitGen&, R, R),
81     R(IntervalClosedClosedTag, MockingBitGen&, R, R),
82     R(IntervalOpenOpenTag, MockingBitGen&, R, R),
83     R(IntervalOpenClosedTag, MockingBitGen&, R, R), R(MockingBitGen&, R, R),
84     R(MockingBitGen&)>;
85 
86 // -----------------------------------------------------------------------------
87 // absl::MockBernoulli
88 // -----------------------------------------------------------------------------
89 //
90 // Matches calls to absl::Bernoulli.
91 //
92 // `absl::MockBernoulli` is a class used in conjunction with Googletest's
93 // `ON_CALL()` and `EXPECT_CALL()` macros. To use it, default-construct an
94 // instance of it inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the
95 // same way one would define mocks on a Googletest `MockFunction()`.
96 //
97 // Example:
98 //
99 //  absl::MockingBitGen mock;
100 //  EXPECT_CALL(absl::MockBernoulli(), Call(mock, testing::_))
101 //     .WillOnce(Return(false));
102 //  assert(absl::Bernoulli(mock, 0.5) == false);
103 //
104 using MockBernoulli =
105     random_internal::MockOverloadSet<absl::bernoulli_distribution,
106                                      bool(MockingBitGen&, double)>;
107 
108 // -----------------------------------------------------------------------------
109 // absl::MockBeta
110 // -----------------------------------------------------------------------------
111 //
112 // Matches calls to absl::Beta.
113 //
114 // `absl::MockBeta` is a class used in conjunction with Googletest's `ON_CALL()`
115 // and `EXPECT_CALL()` macros. To use it, default-construct an instance of it
116 // inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the same way one
117 // would define mocks on a Googletest `MockFunction()`.
118 //
119 // Example:
120 //
121 //  absl::MockingBitGen mock;
122 //  EXPECT_CALL(absl::MockBeta(), Call(mock, 3.0, 2.0))
123 //     .WillOnce(Return(0.567));
124 //  auto x = absl::Beta<double>(mock, 3.0, 2.0);
125 //  assert(x == 0.567);
126 //
127 template <typename RealType>
128 using MockBeta =
129     random_internal::MockOverloadSet<absl::beta_distribution<RealType>,
130                                      RealType(MockingBitGen&, RealType,
131                                               RealType)>;
132 
133 // -----------------------------------------------------------------------------
134 // absl::MockExponential
135 // -----------------------------------------------------------------------------
136 //
137 // Matches calls to absl::Exponential.
138 //
139 // `absl::MockExponential` is a class template used in conjunction with
140 // Googletest's `ON_CALL()` and `EXPECT_CALL()` macros. To use it,
141 // default-construct an instance of it inside `ON_CALL()` or `EXPECT_CALL()`,
142 // and use `Call(...)` the same way one would define mocks on a
143 // Googletest `MockFunction()`.
144 //
145 // Example:
146 //
147 //  absl::MockingBitGen mock;
148 //  EXPECT_CALL(absl::MockExponential<double>(), Call(mock, 0.5))
149 //     .WillOnce(Return(12.3456789));
150 //  auto x = absl::Exponential<double>(mock, 0.5);
151 //  assert(x == 12.3456789)
152 //
153 template <typename RealType>
154 using MockExponential =
155     random_internal::MockOverloadSet<absl::exponential_distribution<RealType>,
156                                      RealType(MockingBitGen&, RealType)>;
157 
158 // -----------------------------------------------------------------------------
159 // absl::MockGaussian
160 // -----------------------------------------------------------------------------
161 //
162 // Matches calls to absl::Gaussian.
163 //
164 // `absl::MockGaussian` is a class template used in conjunction with
165 // Googletest's `ON_CALL()` and `EXPECT_CALL()` macros. To use it,
166 // default-construct an instance of it inside `ON_CALL()` or `EXPECT_CALL()`,
167 // and use `Call(...)` the same way one would define mocks on a
168 // Googletest `MockFunction()`.
169 //
170 // Example:
171 //
172 //  absl::MockingBitGen mock;
173 //  EXPECT_CALL(absl::MockGaussian<double>(), Call(mock, 16.3, 3.3))
174 //     .WillOnce(Return(12.3456789));
175 //  auto x = absl::Gaussian<double>(mock, 16.3, 3.3);
176 //  assert(x == 12.3456789)
177 //
178 template <typename RealType>
179 using MockGaussian =
180     random_internal::MockOverloadSet<absl::gaussian_distribution<RealType>,
181                                      RealType(MockingBitGen&, RealType,
182                                               RealType)>;
183 
184 // -----------------------------------------------------------------------------
185 // absl::MockLogUniform
186 // -----------------------------------------------------------------------------
187 //
188 // Matches calls to absl::LogUniform.
189 //
190 // `absl::MockLogUniform` is a class template used in conjunction with
191 // Googletest's `ON_CALL()` and `EXPECT_CALL()` macros. To use it,
192 // default-construct an instance of it inside `ON_CALL()` or `EXPECT_CALL()`,
193 // and use `Call(...)` the same way one would define mocks on a
194 // Googletest `MockFunction()`.
195 //
196 // Example:
197 //
198 //  absl::MockingBitGen mock;
199 //  EXPECT_CALL(absl::MockLogUniform<int>(), Call(mock, 10, 10000, 10))
200 //     .WillOnce(Return(1221));
201 //  auto x = absl::LogUniform<int>(mock, 10, 10000, 10);
202 //  assert(x == 1221)
203 //
204 template <typename IntType>
205 using MockLogUniform = random_internal::MockOverloadSet<
206     absl::log_uniform_int_distribution<IntType>,
207     IntType(MockingBitGen&, IntType, IntType, IntType)>;
208 
209 // -----------------------------------------------------------------------------
210 // absl::MockPoisson
211 // -----------------------------------------------------------------------------
212 //
213 // Matches calls to absl::Poisson.
214 //
215 // `absl::MockPoisson` is a class template used in conjunction with Googletest's
216 // `ON_CALL()` and `EXPECT_CALL()` macros. To use it, default-construct an
217 // instance of it inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the
218 // same way one would define mocks on a Googletest `MockFunction()`.
219 //
220 // Example:
221 //
222 //  absl::MockingBitGen mock;
223 //  EXPECT_CALL(absl::MockPoisson<int>(), Call(mock, 2.0))
224 //     .WillOnce(Return(1221));
225 //  auto x = absl::Poisson<int>(mock, 2.0);
226 //  assert(x == 1221)
227 //
228 template <typename IntType>
229 using MockPoisson =
230     random_internal::MockOverloadSet<absl::poisson_distribution<IntType>,
231                                      IntType(MockingBitGen&, double)>;
232 
233 // -----------------------------------------------------------------------------
234 // absl::MockZipf
235 // -----------------------------------------------------------------------------
236 //
237 // Matches calls to absl::Zipf.
238 //
239 // `absl::MockZipf` is a class template used in conjunction with Googletest's
240 // `ON_CALL()` and `EXPECT_CALL()` macros. To use it, default-construct an
241 // instance of it inside `ON_CALL()` or `EXPECT_CALL()`, and use `Call(...)` the
242 // same way one would define mocks on a Googletest `MockFunction()`.
243 //
244 // Example:
245 //
246 //  absl::MockingBitGen mock;
247 //  EXPECT_CALL(absl::MockZipf<int>(), Call(mock, 1000000, 2.0, 1.0))
248 //     .WillOnce(Return(1221));
249 //  auto x = absl::Zipf<int>(mock, 1000000, 2.0, 1.0);
250 //  assert(x == 1221)
251 //
252 template <typename IntType>
253 using MockZipf =
254     random_internal::MockOverloadSet<absl::zipf_distribution<IntType>,
255                                      IntType(MockingBitGen&, IntType, double,
256                                              double)>;
257 
258 ABSL_NAMESPACE_END
259 }  // namespace absl
260 
261 #endif  // ABSL_RANDOM_MOCK_DISTRIBUTIONS_H_
262