xref: /freebsd/contrib/googletest/googlemock/test/gmock-cardinalities_test.cc (revision 28f6c2f292806bf31230a959bc4b19d7081669a7)
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 // Google Mock - a framework for writing C++ mock classes.
31 //
32 // This file tests the built-in cardinalities.
33 
34 #include <ostream>
35 
36 #include "gmock/gmock.h"
37 #include "gtest/gtest-spi.h"
38 #include "gtest/gtest.h"
39 
40 namespace {
41 
42 using std::stringstream;
43 using testing::AnyNumber;
44 using testing::AtLeast;
45 using testing::AtMost;
46 using testing::Between;
47 using testing::Cardinality;
48 using testing::CardinalityInterface;
49 using testing::Exactly;
50 using testing::IsSubstring;
51 using testing::MakeCardinality;
52 
53 class MockFoo {
54  public:
55   MockFoo() = default;
56   MOCK_METHOD0(Bar, int());  // NOLINT
57 
58  private:
59   MockFoo(const MockFoo&) = delete;
60   MockFoo& operator=(const MockFoo&) = delete;
61 };
62 
63 // Tests that Cardinality objects can be default constructed.
TEST(CardinalityTest,IsDefaultConstructable)64 TEST(CardinalityTest, IsDefaultConstructable) { Cardinality c; }
65 
66 // Tests that Cardinality objects are copyable.
TEST(CardinalityTest,IsCopyable)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 
TEST(CardinalityTest,IsOverSaturatedByCallCountWorks)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.
TEST(CardinalityTest,CanDescribeActualCallCount)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()
TEST(AnyNumber,Works)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", ss.str());
123 }
124 
TEST(AnyNumberTest,HasCorrectBounds)125 TEST(AnyNumberTest, HasCorrectBounds) {
126   const Cardinality c = AnyNumber();
127   EXPECT_EQ(0, c.ConservativeLowerBound());
128   EXPECT_EQ(INT_MAX, c.ConservativeUpperBound());
129 }
130 
131 // Tests AtLeast(n).
132 
TEST(AtLeastTest,OnNegativeNumber)133 TEST(AtLeastTest, OnNegativeNumber) {
134   EXPECT_NONFATAL_FAILURE(
135       {  // NOLINT
136         AtLeast(-1);
137       },
138       "The invocation lower bound must be >= 0");
139 }
140 
TEST(AtLeastTest,OnZero)141 TEST(AtLeastTest, OnZero) {
142   const Cardinality c = AtLeast(0);
143   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
144   EXPECT_FALSE(c.IsSaturatedByCallCount(0));
145 
146   EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
147   EXPECT_FALSE(c.IsSaturatedByCallCount(1));
148 
149   stringstream ss;
150   c.DescribeTo(&ss);
151   EXPECT_PRED_FORMAT2(IsSubstring, "any number of times", ss.str());
152 }
153 
TEST(AtLeastTest,OnPositiveNumber)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", ss1.str());
168 
169   stringstream ss2;
170   c.DescribeTo(&ss2);
171   EXPECT_PRED_FORMAT2(IsSubstring, "at least twice", ss2.str());
172 
173   stringstream ss3;
174   AtLeast(3).DescribeTo(&ss3);
175   EXPECT_PRED_FORMAT2(IsSubstring, "at least 3 times", ss3.str());
176 }
177 
TEST(AtLeastTest,HasCorrectBounds)178 TEST(AtLeastTest, HasCorrectBounds) {
179   const Cardinality c = AtLeast(2);
180   EXPECT_EQ(2, c.ConservativeLowerBound());
181   EXPECT_EQ(INT_MAX, c.ConservativeUpperBound());
182 }
183 
184 // Tests AtMost(n).
185 
TEST(AtMostTest,OnNegativeNumber)186 TEST(AtMostTest, OnNegativeNumber) {
187   EXPECT_NONFATAL_FAILURE(
188       {  // NOLINT
189         AtMost(-1);
190       },
191       "The invocation upper bound must be >= 0");
192 }
193 
TEST(AtMostTest,OnZero)194 TEST(AtMostTest, OnZero) {
195   const Cardinality c = AtMost(0);
196   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
197   EXPECT_TRUE(c.IsSaturatedByCallCount(0));
198 
199   EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
200   EXPECT_TRUE(c.IsSaturatedByCallCount(1));
201 
202   stringstream ss;
203   c.DescribeTo(&ss);
204   EXPECT_PRED_FORMAT2(IsSubstring, "never called", ss.str());
205 }
206 
TEST(AtMostTest,OnPositiveNumber)207 TEST(AtMostTest, OnPositiveNumber) {
208   const Cardinality c = AtMost(2);
209   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
210   EXPECT_FALSE(c.IsSaturatedByCallCount(0));
211 
212   EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
213   EXPECT_FALSE(c.IsSaturatedByCallCount(1));
214 
215   EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
216   EXPECT_TRUE(c.IsSaturatedByCallCount(2));
217 
218   stringstream ss1;
219   AtMost(1).DescribeTo(&ss1);
220   EXPECT_PRED_FORMAT2(IsSubstring, "called at most once", ss1.str());
221 
222   stringstream ss2;
223   c.DescribeTo(&ss2);
224   EXPECT_PRED_FORMAT2(IsSubstring, "called at most twice", ss2.str());
225 
226   stringstream ss3;
227   AtMost(3).DescribeTo(&ss3);
228   EXPECT_PRED_FORMAT2(IsSubstring, "called at most 3 times", ss3.str());
229 }
230 
TEST(AtMostTest,HasCorrectBounds)231 TEST(AtMostTest, HasCorrectBounds) {
232   const Cardinality c = AtMost(2);
233   EXPECT_EQ(0, c.ConservativeLowerBound());
234   EXPECT_EQ(2, c.ConservativeUpperBound());
235 }
236 
237 // Tests Between(m, n).
238 
TEST(BetweenTest,OnNegativeStart)239 TEST(BetweenTest, OnNegativeStart) {
240   EXPECT_NONFATAL_FAILURE(
241       {  // NOLINT
242         Between(-1, 2);
243       },
244       "The invocation lower bound must be >= 0, but is actually -1");
245 }
246 
TEST(BetweenTest,OnNegativeEnd)247 TEST(BetweenTest, OnNegativeEnd) {
248   EXPECT_NONFATAL_FAILURE(
249       {  // NOLINT
250         Between(1, -2);
251       },
252       "The invocation upper bound must be >= 0, but is actually -2");
253 }
254 
TEST(BetweenTest,OnStartBiggerThanEnd)255 TEST(BetweenTest, OnStartBiggerThanEnd) {
256   EXPECT_NONFATAL_FAILURE(
257       {  // NOLINT
258         Between(2, 1);
259       },
260       "The invocation upper bound (1) must be >= "
261       "the invocation lower bound (2)");
262 }
263 
TEST(BetweenTest,OnZeroStartAndZeroEnd)264 TEST(BetweenTest, OnZeroStartAndZeroEnd) {
265   const Cardinality c = Between(0, 0);
266 
267   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
268   EXPECT_TRUE(c.IsSaturatedByCallCount(0));
269 
270   EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
271   EXPECT_TRUE(c.IsSaturatedByCallCount(1));
272 
273   stringstream ss;
274   c.DescribeTo(&ss);
275   EXPECT_PRED_FORMAT2(IsSubstring, "never called", ss.str());
276 }
277 
TEST(BetweenTest,OnZeroStartAndNonZeroEnd)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", ss.str());
293 }
294 
TEST(BetweenTest,OnSameStartAndEnd)295 TEST(BetweenTest, OnSameStartAndEnd) {
296   const Cardinality c = Between(3, 3);
297 
298   EXPECT_FALSE(c.IsSatisfiedByCallCount(2));
299   EXPECT_FALSE(c.IsSaturatedByCallCount(2));
300 
301   EXPECT_TRUE(c.IsSatisfiedByCallCount(3));
302   EXPECT_TRUE(c.IsSaturatedByCallCount(3));
303 
304   EXPECT_FALSE(c.IsSatisfiedByCallCount(4));
305   EXPECT_TRUE(c.IsSaturatedByCallCount(4));
306 
307   stringstream ss;
308   c.DescribeTo(&ss);
309   EXPECT_PRED_FORMAT2(IsSubstring, "called 3 times", ss.str());
310 }
311 
TEST(BetweenTest,OnDifferentStartAndEnd)312 TEST(BetweenTest, OnDifferentStartAndEnd) {
313   const Cardinality c = Between(3, 5);
314 
315   EXPECT_FALSE(c.IsSatisfiedByCallCount(2));
316   EXPECT_FALSE(c.IsSaturatedByCallCount(2));
317 
318   EXPECT_TRUE(c.IsSatisfiedByCallCount(3));
319   EXPECT_FALSE(c.IsSaturatedByCallCount(3));
320 
321   EXPECT_TRUE(c.IsSatisfiedByCallCount(5));
322   EXPECT_TRUE(c.IsSaturatedByCallCount(5));
323 
324   EXPECT_FALSE(c.IsSatisfiedByCallCount(6));
325   EXPECT_TRUE(c.IsSaturatedByCallCount(6));
326 
327   stringstream ss;
328   c.DescribeTo(&ss);
329   EXPECT_PRED_FORMAT2(IsSubstring, "called between 3 and 5 times", ss.str());
330 }
331 
TEST(BetweenTest,HasCorrectBounds)332 TEST(BetweenTest, HasCorrectBounds) {
333   const Cardinality c = Between(3, 5);
334   EXPECT_EQ(3, c.ConservativeLowerBound());
335   EXPECT_EQ(5, c.ConservativeUpperBound());
336 }
337 
338 // Tests Exactly(n).
339 
TEST(ExactlyTest,OnNegativeNumber)340 TEST(ExactlyTest, OnNegativeNumber) {
341   EXPECT_NONFATAL_FAILURE(
342       {  // NOLINT
343         Exactly(-1);
344       },
345       "The invocation lower bound must be >= 0");
346 }
347 
TEST(ExactlyTest,OnZero)348 TEST(ExactlyTest, OnZero) {
349   const Cardinality c = Exactly(0);
350   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
351   EXPECT_TRUE(c.IsSaturatedByCallCount(0));
352 
353   EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
354   EXPECT_TRUE(c.IsSaturatedByCallCount(1));
355 
356   stringstream ss;
357   c.DescribeTo(&ss);
358   EXPECT_PRED_FORMAT2(IsSubstring, "never called", ss.str());
359 }
360 
TEST(ExactlyTest,OnPositiveNumber)361 TEST(ExactlyTest, OnPositiveNumber) {
362   const Cardinality c = Exactly(2);
363   EXPECT_FALSE(c.IsSatisfiedByCallCount(0));
364   EXPECT_FALSE(c.IsSaturatedByCallCount(0));
365 
366   EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
367   EXPECT_TRUE(c.IsSaturatedByCallCount(2));
368 
369   stringstream ss1;
370   Exactly(1).DescribeTo(&ss1);
371   EXPECT_PRED_FORMAT2(IsSubstring, "called once", ss1.str());
372 
373   stringstream ss2;
374   c.DescribeTo(&ss2);
375   EXPECT_PRED_FORMAT2(IsSubstring, "called twice", ss2.str());
376 
377   stringstream ss3;
378   Exactly(3).DescribeTo(&ss3);
379   EXPECT_PRED_FORMAT2(IsSubstring, "called 3 times", ss3.str());
380 }
381 
TEST(ExactlyTest,HasCorrectBounds)382 TEST(ExactlyTest, HasCorrectBounds) {
383   const Cardinality c = Exactly(3);
384   EXPECT_EQ(3, c.ConservativeLowerBound());
385   EXPECT_EQ(3, c.ConservativeUpperBound());
386 }
387 
388 // Tests that a user can make their own cardinality by implementing
389 // CardinalityInterface and calling MakeCardinality().
390 
391 class EvenCardinality : public CardinalityInterface {
392  public:
393   // Returns true if and only if call_count calls will satisfy this
394   // cardinality.
IsSatisfiedByCallCount(int call_count) const395   bool IsSatisfiedByCallCount(int call_count) const override {
396     return (call_count % 2 == 0);
397   }
398 
399   // Returns true if and only if call_count calls will saturate this
400   // cardinality.
IsSaturatedByCallCount(int) const401   bool IsSaturatedByCallCount(int /* call_count */) const override {
402     return false;
403   }
404 
405   // Describes self to an ostream.
DescribeTo(::std::ostream * ss) const406   void DescribeTo(::std::ostream* ss) const override {
407     *ss << "called even number of times";
408   }
409 };
410 
TEST(MakeCardinalityTest,ConstructsCardinalityFromInterface)411 TEST(MakeCardinalityTest, ConstructsCardinalityFromInterface) {
412   const Cardinality c = MakeCardinality(new EvenCardinality);
413 
414   EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
415   EXPECT_FALSE(c.IsSatisfiedByCallCount(3));
416 
417   EXPECT_FALSE(c.IsSaturatedByCallCount(10000));
418 
419   stringstream ss;
420   c.DescribeTo(&ss);
421   EXPECT_EQ("called even number of times", ss.str());
422 }
423 
424 }  // Unnamed namespace
425