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