]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/googletest/googlemock/test/gmock-cardinalities_test.cc
MFC r345203,r345205,r345353,r345645,r345708,r345709,r345735,r345770,r346081,r346270...
[FreeBSD/FreeBSD.git] / contrib / googletest / googlemock / test / gmock-cardinalities_test.cc
1 // Copyright 2007, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30
31 // Google Mock - a framework for writing C++ mock classes.
32 //
33 // This file tests the built-in cardinalities.
34
35 #include "gmock/gmock.h"
36 #include "gtest/gtest.h"
37 #include "gtest/gtest-spi.h"
38
39 namespace {
40
41 using std::stringstream;
42 using testing::AnyNumber;
43 using testing::AtLeast;
44 using testing::AtMost;
45 using testing::Between;
46 using testing::Cardinality;
47 using testing::CardinalityInterface;
48 using testing::Exactly;
49 using testing::IsSubstring;
50 using testing::MakeCardinality;
51
52 class MockFoo {
53  public:
54   MockFoo() {}
55   MOCK_METHOD0(Bar, int());  // NOLINT
56
57  private:
58   GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
59 };
60
61 // Tests that Cardinality objects can be default constructed.
62 TEST(CardinalityTest, IsDefaultConstructable) {
63   Cardinality c;
64 }
65
66 // Tests that Cardinality objects are copyable.
67 TEST(CardinalityTest, IsCopyable) {
68   // Tests the copy constructor.
69   Cardinality c = Exactly(1);
70   EXPECT_FALSE(c.IsSatisfiedByCallCount(0));
71   EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
72   EXPECT_TRUE(c.IsSaturatedByCallCount(1));
73
74   // Tests the assignment operator.
75   c = Exactly(2);
76   EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
77   EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
78   EXPECT_TRUE(c.IsSaturatedByCallCount(2));
79 }
80
81 TEST(CardinalityTest, IsOverSaturatedByCallCountWorks) {
82   const Cardinality c = AtMost(5);
83   EXPECT_FALSE(c.IsOverSaturatedByCallCount(4));
84   EXPECT_FALSE(c.IsOverSaturatedByCallCount(5));
85   EXPECT_TRUE(c.IsOverSaturatedByCallCount(6));
86 }
87
88 // Tests that Cardinality::DescribeActualCallCountTo() creates the
89 // correct description.
90 TEST(CardinalityTest, CanDescribeActualCallCount) {
91   stringstream ss0;
92   Cardinality::DescribeActualCallCountTo(0, &ss0);
93   EXPECT_EQ("never called", ss0.str());
94
95   stringstream ss1;
96   Cardinality::DescribeActualCallCountTo(1, &ss1);
97   EXPECT_EQ("called once", ss1.str());
98
99   stringstream ss2;
100   Cardinality::DescribeActualCallCountTo(2, &ss2);
101   EXPECT_EQ("called twice", ss2.str());
102
103   stringstream ss3;
104   Cardinality::DescribeActualCallCountTo(3, &ss3);
105   EXPECT_EQ("called 3 times", ss3.str());
106 }
107
108 // Tests AnyNumber()
109 TEST(AnyNumber, Works) {
110   const Cardinality c = AnyNumber();
111   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
112   EXPECT_FALSE(c.IsSaturatedByCallCount(0));
113
114   EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
115   EXPECT_FALSE(c.IsSaturatedByCallCount(1));
116
117   EXPECT_TRUE(c.IsSatisfiedByCallCount(9));
118   EXPECT_FALSE(c.IsSaturatedByCallCount(9));
119
120   stringstream ss;
121   c.DescribeTo(&ss);
122   EXPECT_PRED_FORMAT2(IsSubstring, "called any number of times",
123                       ss.str());
124 }
125
126 TEST(AnyNumberTest, HasCorrectBounds) {
127   const Cardinality c = AnyNumber();
128   EXPECT_EQ(0, c.ConservativeLowerBound());
129   EXPECT_EQ(INT_MAX, c.ConservativeUpperBound());
130 }
131
132 // Tests AtLeast(n).
133
134 TEST(AtLeastTest, OnNegativeNumber) {
135   EXPECT_NONFATAL_FAILURE({  // NOLINT
136     AtLeast(-1);
137   }, "The invocation lower bound must be >= 0");
138 }
139
140 TEST(AtLeastTest, OnZero) {
141   const Cardinality c = AtLeast(0);
142   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
143   EXPECT_FALSE(c.IsSaturatedByCallCount(0));
144
145   EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
146   EXPECT_FALSE(c.IsSaturatedByCallCount(1));
147
148   stringstream ss;
149   c.DescribeTo(&ss);
150   EXPECT_PRED_FORMAT2(IsSubstring, "any number of times",
151                       ss.str());
152 }
153
154 TEST(AtLeastTest, OnPositiveNumber) {
155   const Cardinality c = AtLeast(2);
156   EXPECT_FALSE(c.IsSatisfiedByCallCount(0));
157   EXPECT_FALSE(c.IsSaturatedByCallCount(0));
158
159   EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
160   EXPECT_FALSE(c.IsSaturatedByCallCount(1));
161
162   EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
163   EXPECT_FALSE(c.IsSaturatedByCallCount(2));
164
165   stringstream ss1;
166   AtLeast(1).DescribeTo(&ss1);
167   EXPECT_PRED_FORMAT2(IsSubstring, "at least once",
168                       ss1.str());
169
170   stringstream ss2;
171   c.DescribeTo(&ss2);
172   EXPECT_PRED_FORMAT2(IsSubstring, "at least twice",
173                       ss2.str());
174
175   stringstream ss3;
176   AtLeast(3).DescribeTo(&ss3);
177   EXPECT_PRED_FORMAT2(IsSubstring, "at least 3 times",
178                       ss3.str());
179 }
180
181 TEST(AtLeastTest, HasCorrectBounds) {
182   const Cardinality c = AtLeast(2);
183   EXPECT_EQ(2, c.ConservativeLowerBound());
184   EXPECT_EQ(INT_MAX, c.ConservativeUpperBound());
185 }
186
187 // Tests AtMost(n).
188
189 TEST(AtMostTest, OnNegativeNumber) {
190   EXPECT_NONFATAL_FAILURE({  // NOLINT
191     AtMost(-1);
192   }, "The invocation upper bound must be >= 0");
193 }
194
195 TEST(AtMostTest, OnZero) {
196   const Cardinality c = AtMost(0);
197   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
198   EXPECT_TRUE(c.IsSaturatedByCallCount(0));
199
200   EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
201   EXPECT_TRUE(c.IsSaturatedByCallCount(1));
202
203   stringstream ss;
204   c.DescribeTo(&ss);
205   EXPECT_PRED_FORMAT2(IsSubstring, "never called",
206                       ss.str());
207 }
208
209 TEST(AtMostTest, OnPositiveNumber) {
210   const Cardinality c = AtMost(2);
211   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
212   EXPECT_FALSE(c.IsSaturatedByCallCount(0));
213
214   EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
215   EXPECT_FALSE(c.IsSaturatedByCallCount(1));
216
217   EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
218   EXPECT_TRUE(c.IsSaturatedByCallCount(2));
219
220   stringstream ss1;
221   AtMost(1).DescribeTo(&ss1);
222   EXPECT_PRED_FORMAT2(IsSubstring, "called at most once",
223                       ss1.str());
224
225   stringstream ss2;
226   c.DescribeTo(&ss2);
227   EXPECT_PRED_FORMAT2(IsSubstring, "called at most twice",
228                       ss2.str());
229
230   stringstream ss3;
231   AtMost(3).DescribeTo(&ss3);
232   EXPECT_PRED_FORMAT2(IsSubstring, "called at most 3 times",
233                       ss3.str());
234 }
235
236 TEST(AtMostTest, HasCorrectBounds) {
237   const Cardinality c = AtMost(2);
238   EXPECT_EQ(0, c.ConservativeLowerBound());
239   EXPECT_EQ(2, c.ConservativeUpperBound());
240 }
241
242 // Tests Between(m, n).
243
244 TEST(BetweenTest, OnNegativeStart) {
245   EXPECT_NONFATAL_FAILURE({  // NOLINT
246     Between(-1, 2);
247   }, "The invocation lower bound must be >= 0, but is actually -1");
248 }
249
250 TEST(BetweenTest, OnNegativeEnd) {
251   EXPECT_NONFATAL_FAILURE({  // NOLINT
252     Between(1, -2);
253   }, "The invocation upper bound must be >= 0, but is actually -2");
254 }
255
256 TEST(BetweenTest, OnStartBiggerThanEnd) {
257   EXPECT_NONFATAL_FAILURE({  // NOLINT
258     Between(2, 1);
259   }, "The invocation upper bound (1) must be >= "
260      "the invocation lower bound (2)");
261 }
262
263 TEST(BetweenTest, OnZeroStartAndZeroEnd) {
264   const Cardinality c = Between(0, 0);
265
266   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
267   EXPECT_TRUE(c.IsSaturatedByCallCount(0));
268
269   EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
270   EXPECT_TRUE(c.IsSaturatedByCallCount(1));
271
272   stringstream ss;
273   c.DescribeTo(&ss);
274   EXPECT_PRED_FORMAT2(IsSubstring, "never called",
275                       ss.str());
276 }
277
278 TEST(BetweenTest, OnZeroStartAndNonZeroEnd) {
279   const Cardinality c = Between(0, 2);
280
281   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
282   EXPECT_FALSE(c.IsSaturatedByCallCount(0));
283
284   EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
285   EXPECT_TRUE(c.IsSaturatedByCallCount(2));
286
287   EXPECT_FALSE(c.IsSatisfiedByCallCount(4));
288   EXPECT_TRUE(c.IsSaturatedByCallCount(4));
289
290   stringstream ss;
291   c.DescribeTo(&ss);
292   EXPECT_PRED_FORMAT2(IsSubstring, "called at most twice",
293                       ss.str());
294 }
295
296 TEST(BetweenTest, OnSameStartAndEnd) {
297   const Cardinality c = Between(3, 3);
298
299   EXPECT_FALSE(c.IsSatisfiedByCallCount(2));
300   EXPECT_FALSE(c.IsSaturatedByCallCount(2));
301
302   EXPECT_TRUE(c.IsSatisfiedByCallCount(3));
303   EXPECT_TRUE(c.IsSaturatedByCallCount(3));
304
305   EXPECT_FALSE(c.IsSatisfiedByCallCount(4));
306   EXPECT_TRUE(c.IsSaturatedByCallCount(4));
307
308   stringstream ss;
309   c.DescribeTo(&ss);
310   EXPECT_PRED_FORMAT2(IsSubstring, "called 3 times",
311                       ss.str());
312 }
313
314 TEST(BetweenTest, OnDifferentStartAndEnd) {
315   const Cardinality c = Between(3, 5);
316
317   EXPECT_FALSE(c.IsSatisfiedByCallCount(2));
318   EXPECT_FALSE(c.IsSaturatedByCallCount(2));
319
320   EXPECT_TRUE(c.IsSatisfiedByCallCount(3));
321   EXPECT_FALSE(c.IsSaturatedByCallCount(3));
322
323   EXPECT_TRUE(c.IsSatisfiedByCallCount(5));
324   EXPECT_TRUE(c.IsSaturatedByCallCount(5));
325
326   EXPECT_FALSE(c.IsSatisfiedByCallCount(6));
327   EXPECT_TRUE(c.IsSaturatedByCallCount(6));
328
329   stringstream ss;
330   c.DescribeTo(&ss);
331   EXPECT_PRED_FORMAT2(IsSubstring, "called between 3 and 5 times",
332                       ss.str());
333 }
334
335 TEST(BetweenTest, HasCorrectBounds) {
336   const Cardinality c = Between(3, 5);
337   EXPECT_EQ(3, c.ConservativeLowerBound());
338   EXPECT_EQ(5, c.ConservativeUpperBound());
339 }
340
341 // Tests Exactly(n).
342
343 TEST(ExactlyTest, OnNegativeNumber) {
344   EXPECT_NONFATAL_FAILURE({  // NOLINT
345     Exactly(-1);
346   }, "The invocation lower bound must be >= 0");
347 }
348
349 TEST(ExactlyTest, OnZero) {
350   const Cardinality c = Exactly(0);
351   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
352   EXPECT_TRUE(c.IsSaturatedByCallCount(0));
353
354   EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
355   EXPECT_TRUE(c.IsSaturatedByCallCount(1));
356
357   stringstream ss;
358   c.DescribeTo(&ss);
359   EXPECT_PRED_FORMAT2(IsSubstring, "never called",
360                       ss.str());
361 }
362
363 TEST(ExactlyTest, OnPositiveNumber) {
364   const Cardinality c = Exactly(2);
365   EXPECT_FALSE(c.IsSatisfiedByCallCount(0));
366   EXPECT_FALSE(c.IsSaturatedByCallCount(0));
367
368   EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
369   EXPECT_TRUE(c.IsSaturatedByCallCount(2));
370
371   stringstream ss1;
372   Exactly(1).DescribeTo(&ss1);
373   EXPECT_PRED_FORMAT2(IsSubstring, "called once",
374                       ss1.str());
375
376   stringstream ss2;
377   c.DescribeTo(&ss2);
378   EXPECT_PRED_FORMAT2(IsSubstring, "called twice",
379                       ss2.str());
380
381   stringstream ss3;
382   Exactly(3).DescribeTo(&ss3);
383   EXPECT_PRED_FORMAT2(IsSubstring, "called 3 times",
384                       ss3.str());
385 }
386
387 TEST(ExactlyTest, HasCorrectBounds) {
388   const Cardinality c = Exactly(3);
389   EXPECT_EQ(3, c.ConservativeLowerBound());
390   EXPECT_EQ(3, c.ConservativeUpperBound());
391 }
392
393 // Tests that a user can make their own cardinality by implementing
394 // CardinalityInterface and calling MakeCardinality().
395
396 class EvenCardinality : public CardinalityInterface {
397  public:
398   // Returns true iff call_count calls will satisfy this cardinality.
399   virtual bool IsSatisfiedByCallCount(int call_count) const {
400     return (call_count % 2 == 0);
401   }
402
403   // Returns true iff call_count calls will saturate this cardinality.
404   virtual bool IsSaturatedByCallCount(int /* call_count */) const {
405     return false;
406   }
407
408   // Describes self to an ostream.
409   virtual void DescribeTo(::std::ostream* ss) const {
410     *ss << "called even number of times";
411   }
412 };
413
414 TEST(MakeCardinalityTest, ConstructsCardinalityFromInterface) {
415   const Cardinality c = MakeCardinality(new EvenCardinality);
416
417   EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
418   EXPECT_FALSE(c.IsSatisfiedByCallCount(3));
419
420   EXPECT_FALSE(c.IsSaturatedByCallCount(10000));
421
422   stringstream ss;
423   c.DescribeTo(&ss);
424   EXPECT_EQ("called even number of times", ss.str());
425 }
426
427 }  // Unnamed namespace