1 //
2 // Copyright 2018 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #include "absl/random/mocking_bit_gen.h"
17
18 #include <numeric>
19 #include <random>
20
21 #include "gmock/gmock.h"
22 #include "gtest/gtest-spi.h"
23 #include "gtest/gtest.h"
24 #include "absl/random/bit_gen_ref.h"
25 #include "absl/random/mock_distributions.h"
26 #include "absl/random/random.h"
27
28 namespace {
29 using ::testing::Ne;
30 using ::testing::Return;
31
TEST(BasicMocking,AllDistributionsAreOverridable)32 TEST(BasicMocking, AllDistributionsAreOverridable) {
33 absl::MockingBitGen gen;
34
35 EXPECT_NE(absl::Uniform<int>(gen, 1, 1000000), 20);
36 EXPECT_CALL(absl::MockUniform<int>(), Call(gen, 1, 1000000))
37 .WillOnce(Return(20));
38 EXPECT_EQ(absl::Uniform<int>(gen, 1, 1000000), 20);
39
40 EXPECT_NE(absl::Uniform<double>(gen, 0.0, 100.0), 5.0);
41 EXPECT_CALL(absl::MockUniform<double>(), Call(gen, 0.0, 100.0))
42 .WillOnce(Return(5.0));
43 EXPECT_EQ(absl::Uniform<double>(gen, 0.0, 100.0), 5.0);
44
45 EXPECT_NE(absl::Exponential<double>(gen, 1.0), 42);
46 EXPECT_CALL(absl::MockExponential<double>(), Call(gen, 1.0))
47 .WillOnce(Return(42));
48 EXPECT_EQ(absl::Exponential<double>(gen, 1.0), 42);
49
50 EXPECT_NE(absl::Poisson<int>(gen, 1.0), 500);
51 EXPECT_CALL(absl::MockPoisson<int>(), Call(gen, 1.0)).WillOnce(Return(500));
52 EXPECT_EQ(absl::Poisson<int>(gen, 1.0), 500);
53
54 EXPECT_NE(absl::Bernoulli(gen, 0.000001), true);
55 EXPECT_CALL(absl::MockBernoulli(), Call(gen, 0.000001))
56 .WillOnce(Return(true));
57 EXPECT_EQ(absl::Bernoulli(gen, 0.000001), true);
58
59 EXPECT_NE(absl::Zipf<int>(gen, 1000000, 2.0, 1.0), 1221);
60 EXPECT_CALL(absl::MockZipf<int>(), Call(gen, 1000000, 2.0, 1.0))
61 .WillOnce(Return(1221));
62 EXPECT_EQ(absl::Zipf<int>(gen, 1000000, 2.0, 1.0), 1221);
63
64 EXPECT_NE(absl::Gaussian<double>(gen, 0.0, 1.0), 0.001);
65 EXPECT_CALL(absl::MockGaussian<double>(), Call(gen, 0.0, 1.0))
66 .WillOnce(Return(0.001));
67 EXPECT_EQ(absl::Gaussian<double>(gen, 0.0, 1.0), 0.001);
68
69 EXPECT_NE(absl::LogUniform<int>(gen, 0, 1000000, 2), 500000);
70 EXPECT_CALL(absl::MockLogUniform<int>(), Call(gen, 0, 1000000, 2))
71 .WillOnce(Return(500000));
72 EXPECT_EQ(absl::LogUniform<int>(gen, 0, 1000000, 2), 500000);
73 }
74
TEST(BasicMocking,OnDistribution)75 TEST(BasicMocking, OnDistribution) {
76 absl::MockingBitGen gen;
77
78 EXPECT_NE(absl::Uniform<int>(gen, 1, 1000000), 20);
79 ON_CALL(absl::MockUniform<int>(), Call(gen, 1, 1000000))
80 .WillByDefault(Return(20));
81 EXPECT_EQ(absl::Uniform<int>(gen, 1, 1000000), 20);
82
83 EXPECT_NE(absl::Uniform<double>(gen, 0.0, 100.0), 5.0);
84 ON_CALL(absl::MockUniform<double>(), Call(gen, 0.0, 100.0))
85 .WillByDefault(Return(5.0));
86 EXPECT_EQ(absl::Uniform<double>(gen, 0.0, 100.0), 5.0);
87
88 EXPECT_NE(absl::Exponential<double>(gen, 1.0), 42);
89 ON_CALL(absl::MockExponential<double>(), Call(gen, 1.0))
90 .WillByDefault(Return(42));
91 EXPECT_EQ(absl::Exponential<double>(gen, 1.0), 42);
92
93 EXPECT_NE(absl::Poisson<int>(gen, 1.0), 500);
94 ON_CALL(absl::MockPoisson<int>(), Call(gen, 1.0)).WillByDefault(Return(500));
95 EXPECT_EQ(absl::Poisson<int>(gen, 1.0), 500);
96
97 EXPECT_NE(absl::Bernoulli(gen, 0.000001), true);
98 ON_CALL(absl::MockBernoulli(), Call(gen, 0.000001))
99 .WillByDefault(Return(true));
100 EXPECT_EQ(absl::Bernoulli(gen, 0.000001), true);
101
102 EXPECT_NE(absl::Zipf<int>(gen, 1000000, 2.0, 1.0), 1221);
103 ON_CALL(absl::MockZipf<int>(), Call(gen, 1000000, 2.0, 1.0))
104 .WillByDefault(Return(1221));
105 EXPECT_EQ(absl::Zipf<int>(gen, 1000000, 2.0, 1.0), 1221);
106
107 EXPECT_NE(absl::Gaussian<double>(gen, 0.0, 1.0), 0.001);
108 ON_CALL(absl::MockGaussian<double>(), Call(gen, 0.0, 1.0))
109 .WillByDefault(Return(0.001));
110 EXPECT_EQ(absl::Gaussian<double>(gen, 0.0, 1.0), 0.001);
111
112 EXPECT_NE(absl::LogUniform<int>(gen, 0, 1000000, 2), 2040);
113 ON_CALL(absl::MockLogUniform<int>(), Call(gen, 0, 1000000, 2))
114 .WillByDefault(Return(2040));
115 EXPECT_EQ(absl::LogUniform<int>(gen, 0, 1000000, 2), 2040);
116 }
117
TEST(BasicMocking,GMockMatchers)118 TEST(BasicMocking, GMockMatchers) {
119 absl::MockingBitGen gen;
120
121 EXPECT_NE(absl::Zipf<int>(gen, 1000000, 2.0, 1.0), 1221);
122 ON_CALL(absl::MockZipf<int>(), Call(gen, 1000000, 2.0, 1.0))
123 .WillByDefault(Return(1221));
124 EXPECT_EQ(absl::Zipf<int>(gen, 1000000, 2.0, 1.0), 1221);
125 }
126
TEST(BasicMocking,OverridesWithMultipleGMockExpectations)127 TEST(BasicMocking, OverridesWithMultipleGMockExpectations) {
128 absl::MockingBitGen gen;
129
130 EXPECT_CALL(absl::MockUniform<int>(), Call(gen, 1, 10000))
131 .WillOnce(Return(20))
132 .WillOnce(Return(40))
133 .WillOnce(Return(60));
134 EXPECT_EQ(absl::Uniform(gen, 1, 10000), 20);
135 EXPECT_EQ(absl::Uniform(gen, 1, 10000), 40);
136 EXPECT_EQ(absl::Uniform(gen, 1, 10000), 60);
137 }
138
TEST(BasicMocking,DefaultArgument)139 TEST(BasicMocking, DefaultArgument) {
140 absl::MockingBitGen gen;
141
142 ON_CALL(absl::MockExponential<double>(), Call(gen, 1.0))
143 .WillByDefault(Return(200));
144
145 EXPECT_EQ(absl::Exponential<double>(gen), 200);
146 EXPECT_EQ(absl::Exponential<double>(gen, 1.0), 200);
147 }
148
TEST(BasicMocking,MultipleGenerators)149 TEST(BasicMocking, MultipleGenerators) {
150 auto get_value = [](absl::BitGenRef gen_ref) {
151 return absl::Uniform(gen_ref, 1, 1000000);
152 };
153 absl::MockingBitGen unmocked_generator;
154 absl::MockingBitGen mocked_with_3;
155 absl::MockingBitGen mocked_with_11;
156
157 EXPECT_CALL(absl::MockUniform<int>(), Call(mocked_with_3, 1, 1000000))
158 .WillOnce(Return(3))
159 .WillRepeatedly(Return(17));
160 EXPECT_CALL(absl::MockUniform<int>(), Call(mocked_with_11, 1, 1000000))
161 .WillOnce(Return(11))
162 .WillRepeatedly(Return(17));
163
164 // Ensure that unmocked generator generates neither value.
165 int unmocked_value = get_value(unmocked_generator);
166 EXPECT_NE(unmocked_value, 3);
167 EXPECT_NE(unmocked_value, 11);
168 // Mocked generators should generate their mocked values.
169 EXPECT_EQ(get_value(mocked_with_3), 3);
170 EXPECT_EQ(get_value(mocked_with_11), 11);
171 // Ensure that the mocks have expired.
172 EXPECT_NE(get_value(mocked_with_3), 3);
173 EXPECT_NE(get_value(mocked_with_11), 11);
174 }
175
TEST(BasicMocking,MocksNotTrigeredForIncorrectTypes)176 TEST(BasicMocking, MocksNotTrigeredForIncorrectTypes) {
177 absl::MockingBitGen gen;
178 EXPECT_CALL(absl::MockUniform<uint32_t>(), Call(gen)).WillOnce(Return(42));
179
180 EXPECT_NE(absl::Uniform<uint16_t>(gen), 42); // Not mocked
181 EXPECT_EQ(absl::Uniform<uint32_t>(gen), 42); // Mock triggered
182 }
183
TEST(BasicMocking,FailsOnUnsatisfiedMocks)184 TEST(BasicMocking, FailsOnUnsatisfiedMocks) {
185 EXPECT_NONFATAL_FAILURE(
186 []() {
187 absl::MockingBitGen gen;
188 EXPECT_CALL(absl::MockExponential<double>(), Call(gen, 1.0))
189 .WillOnce(Return(3.0));
190 // Does not call absl::Exponential().
191 }(),
192 "unsatisfied and active");
193 }
194
TEST(OnUniform,RespectsUniformIntervalSemantics)195 TEST(OnUniform, RespectsUniformIntervalSemantics) {
196 absl::MockingBitGen gen;
197
198 EXPECT_CALL(absl::MockUniform<int>(),
199 Call(absl::IntervalClosed, gen, 1, 1000000))
200 .WillOnce(Return(301));
201 EXPECT_NE(absl::Uniform(gen, 1, 1000000), 301); // Not mocked
202 EXPECT_EQ(absl::Uniform(absl::IntervalClosed, gen, 1, 1000000), 301);
203 }
204
TEST(OnUniform,RespectsNoArgUnsignedShorthand)205 TEST(OnUniform, RespectsNoArgUnsignedShorthand) {
206 absl::MockingBitGen gen;
207 EXPECT_CALL(absl::MockUniform<uint32_t>(), Call(gen)).WillOnce(Return(42));
208 EXPECT_EQ(absl::Uniform<uint32_t>(gen), 42);
209 }
210
TEST(RepeatedlyModifier,ForceSnakeEyesForManyDice)211 TEST(RepeatedlyModifier, ForceSnakeEyesForManyDice) {
212 auto roll_some_dice = [](absl::BitGenRef gen_ref) {
213 std::vector<int> results(16);
214 for (auto& r : results) {
215 r = absl::Uniform(absl::IntervalClosed, gen_ref, 1, 6);
216 }
217 return results;
218 };
219 std::vector<int> results;
220 absl::MockingBitGen gen;
221
222 // Without any mocked calls, not all dice roll a "6".
223 results = roll_some_dice(gen);
224 EXPECT_LT(std::accumulate(std::begin(results), std::end(results), 0),
225 results.size() * 6);
226
227 // Verify that we can force all "6"-rolls, with mocking.
228 ON_CALL(absl::MockUniform<int>(), Call(absl::IntervalClosed, gen, 1, 6))
229 .WillByDefault(Return(6));
230 results = roll_some_dice(gen);
231 EXPECT_EQ(std::accumulate(std::begin(results), std::end(results), 0),
232 results.size() * 6);
233 }
234
TEST(WillOnce,DistinctCounters)235 TEST(WillOnce, DistinctCounters) {
236 absl::MockingBitGen gen;
237 EXPECT_CALL(absl::MockUniform<int>(), Call(gen, 1, 1000000))
238 .Times(3)
239 .WillRepeatedly(Return(0));
240 EXPECT_CALL(absl::MockUniform<int>(), Call(gen, 1000001, 2000000))
241 .Times(3)
242 .WillRepeatedly(Return(1));
243 EXPECT_EQ(absl::Uniform(gen, 1000001, 2000000), 1);
244 EXPECT_EQ(absl::Uniform(gen, 1, 1000000), 0);
245 EXPECT_EQ(absl::Uniform(gen, 1000001, 2000000), 1);
246 EXPECT_EQ(absl::Uniform(gen, 1, 1000000), 0);
247 EXPECT_EQ(absl::Uniform(gen, 1000001, 2000000), 1);
248 EXPECT_EQ(absl::Uniform(gen, 1, 1000000), 0);
249 }
250
TEST(TimesModifier,ModifierSaturatesAndExpires)251 TEST(TimesModifier, ModifierSaturatesAndExpires) {
252 EXPECT_NONFATAL_FAILURE(
253 []() {
254 absl::MockingBitGen gen;
255 EXPECT_CALL(absl::MockUniform<int>(), Call(gen, 1, 1000000))
256 .Times(3)
257 .WillRepeatedly(Return(15))
258 .RetiresOnSaturation();
259
260 EXPECT_EQ(absl::Uniform(gen, 1, 1000000), 15);
261 EXPECT_EQ(absl::Uniform(gen, 1, 1000000), 15);
262 EXPECT_EQ(absl::Uniform(gen, 1, 1000000), 15);
263 // Times(3) has expired - Should get a different value now.
264
265 EXPECT_NE(absl::Uniform(gen, 1, 1000000), 15);
266 }(),
267 "");
268 }
269
TEST(TimesModifier,Times0)270 TEST(TimesModifier, Times0) {
271 absl::MockingBitGen gen;
272 EXPECT_CALL(absl::MockBernoulli(), Call(gen, 0.0)).Times(0);
273 EXPECT_CALL(absl::MockPoisson<int>(), Call(gen, 1.0)).Times(0);
274 }
275
TEST(AnythingMatcher,MatchesAnyArgument)276 TEST(AnythingMatcher, MatchesAnyArgument) {
277 using testing::_;
278
279 {
280 absl::MockingBitGen gen;
281 ON_CALL(absl::MockUniform<int>(), Call(absl::IntervalClosed, gen, _, 1000))
282 .WillByDefault(Return(11));
283 ON_CALL(absl::MockUniform<int>(),
284 Call(absl::IntervalClosed, gen, _, Ne(1000)))
285 .WillByDefault(Return(99));
286
287 EXPECT_EQ(absl::Uniform(absl::IntervalClosed, gen, 10, 1000000), 99);
288 EXPECT_EQ(absl::Uniform(absl::IntervalClosed, gen, 10, 1000), 11);
289 }
290
291 {
292 absl::MockingBitGen gen;
293 ON_CALL(absl::MockUniform<int>(), Call(gen, 1, _))
294 .WillByDefault(Return(25));
295 ON_CALL(absl::MockUniform<int>(), Call(gen, Ne(1), _))
296 .WillByDefault(Return(99));
297 EXPECT_EQ(absl::Uniform(gen, 3, 1000000), 99);
298 EXPECT_EQ(absl::Uniform(gen, 1, 1000000), 25);
299 }
300
301 {
302 absl::MockingBitGen gen;
303 ON_CALL(absl::MockUniform<int>(), Call(gen, _, _))
304 .WillByDefault(Return(145));
305 EXPECT_EQ(absl::Uniform(gen, 1, 1000), 145);
306 EXPECT_EQ(absl::Uniform(gen, 10, 1000), 145);
307 EXPECT_EQ(absl::Uniform(gen, 100, 1000), 145);
308 }
309 }
310
TEST(AnythingMatcher,WithWillByDefault)311 TEST(AnythingMatcher, WithWillByDefault) {
312 using testing::_;
313 absl::MockingBitGen gen;
314 std::vector<int> values = {11, 22, 33, 44, 55, 66, 77, 88, 99, 1010};
315
316 ON_CALL(absl::MockUniform<size_t>(), Call(gen, 0, _))
317 .WillByDefault(Return(0));
318 for (int i = 0; i < 100; i++) {
319 auto& elem = values[absl::Uniform(gen, 0u, values.size())];
320 EXPECT_EQ(elem, 11);
321 }
322 }
323
TEST(BasicMocking,WillByDefaultWithArgs)324 TEST(BasicMocking, WillByDefaultWithArgs) {
325 using testing::_;
326
327 absl::MockingBitGen gen;
328 ON_CALL(absl::MockPoisson<int>(), Call(gen, _))
329 .WillByDefault(
330 [](double lambda) { return static_cast<int>(lambda * 10); });
331 EXPECT_EQ(absl::Poisson<int>(gen, 1.7), 17);
332 EXPECT_EQ(absl::Poisson<int>(gen, 0.03), 0);
333 }
334
TEST(MockingBitGen,InSequenceSucceedsInOrder)335 TEST(MockingBitGen, InSequenceSucceedsInOrder) {
336 absl::MockingBitGen gen;
337
338 testing::InSequence seq;
339
340 EXPECT_CALL(absl::MockPoisson<int>(), Call(gen, 1.0)).WillOnce(Return(3));
341 EXPECT_CALL(absl::MockPoisson<int>(), Call(gen, 2.0)).WillOnce(Return(4));
342
343 EXPECT_EQ(absl::Poisson<int>(gen, 1.0), 3);
344 EXPECT_EQ(absl::Poisson<int>(gen, 2.0), 4);
345 }
346
347 } // namespace
348