xref: /freebsd/contrib/googletest/googlemock/include/gmock/gmock-cardinalities.h (revision 2aaf9152a852aba9eb2036b95f4948ee77988826)
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 implements some commonly used cardinalities.  More
34 // cardinalities can be defined by the user implementing the
35 // CardinalityInterface interface if necessary.
36 
37 // GOOGLETEST_CM0002 DO NOT DELETE
38 
39 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_
40 #define GMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_
41 
42 #include <limits.h>
43 #include <ostream>  // NOLINT
44 #include "gmock/internal/gmock-port.h"
45 #include "gtest/gtest.h"
46 
47 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
48 /* class A needs to have dll-interface to be used by clients of class B */)
49 
50 namespace testing {
51 
52 // To implement a cardinality Foo, define:
53 //   1. a class FooCardinality that implements the
54 //      CardinalityInterface interface, and
55 //   2. a factory function that creates a Cardinality object from a
56 //      const FooCardinality*.
57 //
58 // The two-level delegation design follows that of Matcher, providing
59 // consistency for extension developers.  It also eases ownership
60 // management as Cardinality objects can now be copied like plain values.
61 
62 // The implementation of a cardinality.
63 class CardinalityInterface {
64  public:
65   virtual ~CardinalityInterface() {}
66 
67   // Conservative estimate on the lower/upper bound of the number of
68   // calls allowed.
69   virtual int ConservativeLowerBound() const { return 0; }
70   virtual int ConservativeUpperBound() const { return INT_MAX; }
71 
72   // Returns true iff call_count calls will satisfy this cardinality.
73   virtual bool IsSatisfiedByCallCount(int call_count) const = 0;
74 
75   // Returns true iff call_count calls will saturate this cardinality.
76   virtual bool IsSaturatedByCallCount(int call_count) const = 0;
77 
78   // Describes self to an ostream.
79   virtual void DescribeTo(::std::ostream* os) const = 0;
80 };
81 
82 // A Cardinality is a copyable and IMMUTABLE (except by assignment)
83 // object that specifies how many times a mock function is expected to
84 // be called.  The implementation of Cardinality is just a linked_ptr
85 // to const CardinalityInterface, so copying is fairly cheap.
86 // Don't inherit from Cardinality!
87 class GTEST_API_ Cardinality {
88  public:
89   // Constructs a null cardinality.  Needed for storing Cardinality
90   // objects in STL containers.
91   Cardinality() {}
92 
93   // Constructs a Cardinality from its implementation.
94   explicit Cardinality(const CardinalityInterface* impl) : impl_(impl) {}
95 
96   // Conservative estimate on the lower/upper bound of the number of
97   // calls allowed.
98   int ConservativeLowerBound() const { return impl_->ConservativeLowerBound(); }
99   int ConservativeUpperBound() const { return impl_->ConservativeUpperBound(); }
100 
101   // Returns true iff call_count calls will satisfy this cardinality.
102   bool IsSatisfiedByCallCount(int call_count) const {
103     return impl_->IsSatisfiedByCallCount(call_count);
104   }
105 
106   // Returns true iff call_count calls will saturate this cardinality.
107   bool IsSaturatedByCallCount(int call_count) const {
108     return impl_->IsSaturatedByCallCount(call_count);
109   }
110 
111   // Returns true iff call_count calls will over-saturate this
112   // cardinality, i.e. exceed the maximum number of allowed calls.
113   bool IsOverSaturatedByCallCount(int call_count) const {
114     return impl_->IsSaturatedByCallCount(call_count) &&
115         !impl_->IsSatisfiedByCallCount(call_count);
116   }
117 
118   // Describes self to an ostream
119   void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); }
120 
121   // Describes the given actual call count to an ostream.
122   static void DescribeActualCallCountTo(int actual_call_count,
123                                         ::std::ostream* os);
124 
125  private:
126   internal::linked_ptr<const CardinalityInterface> impl_;
127 };
128 
129 // Creates a cardinality that allows at least n calls.
130 GTEST_API_ Cardinality AtLeast(int n);
131 
132 // Creates a cardinality that allows at most n calls.
133 GTEST_API_ Cardinality AtMost(int n);
134 
135 // Creates a cardinality that allows any number of calls.
136 GTEST_API_ Cardinality AnyNumber();
137 
138 // Creates a cardinality that allows between min and max calls.
139 GTEST_API_ Cardinality Between(int min, int max);
140 
141 // Creates a cardinality that allows exactly n calls.
142 GTEST_API_ Cardinality Exactly(int n);
143 
144 // Creates a cardinality from its implementation.
145 inline Cardinality MakeCardinality(const CardinalityInterface* c) {
146   return Cardinality(c);
147 }
148 
149 }  // namespace testing
150 
151 GTEST_DISABLE_MSC_WARNINGS_POP_()  //  4251
152 
153 #endif  // GMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_
154