xref: /freebsd/contrib/googletest/googlemock/include/gmock/gmock-cardinalities.h (revision b89a7cc2ed6e4398d5be502f5bb5885d1ec6ff0f)
1*b89a7cc2SEnji Cooper // Copyright 2007, Google Inc.
2*b89a7cc2SEnji Cooper // All rights reserved.
3*b89a7cc2SEnji Cooper //
4*b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5*b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are
6*b89a7cc2SEnji Cooper // met:
7*b89a7cc2SEnji Cooper //
8*b89a7cc2SEnji Cooper //     * Redistributions of source code must retain the above copyright
9*b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10*b89a7cc2SEnji Cooper //     * Redistributions in binary form must reproduce the above
11*b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12*b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the
13*b89a7cc2SEnji Cooper // distribution.
14*b89a7cc2SEnji Cooper //     * Neither the name of Google Inc. nor the names of its
15*b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from
16*b89a7cc2SEnji Cooper // this software without specific prior written permission.
17*b89a7cc2SEnji Cooper //
18*b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21*b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23*b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24*b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28*b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*b89a7cc2SEnji Cooper 
30*b89a7cc2SEnji Cooper 
31*b89a7cc2SEnji Cooper // Google Mock - a framework for writing C++ mock classes.
32*b89a7cc2SEnji Cooper //
33*b89a7cc2SEnji Cooper // This file implements some commonly used cardinalities.  More
34*b89a7cc2SEnji Cooper // cardinalities can be defined by the user implementing the
35*b89a7cc2SEnji Cooper // CardinalityInterface interface if necessary.
36*b89a7cc2SEnji Cooper 
37*b89a7cc2SEnji Cooper // GOOGLETEST_CM0002 DO NOT DELETE
38*b89a7cc2SEnji Cooper 
39*b89a7cc2SEnji Cooper #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_
40*b89a7cc2SEnji Cooper #define GMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_
41*b89a7cc2SEnji Cooper 
42*b89a7cc2SEnji Cooper #include <limits.h>
43*b89a7cc2SEnji Cooper #include <ostream>  // NOLINT
44*b89a7cc2SEnji Cooper #include "gmock/internal/gmock-port.h"
45*b89a7cc2SEnji Cooper #include "gtest/gtest.h"
46*b89a7cc2SEnji Cooper 
47*b89a7cc2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
48*b89a7cc2SEnji Cooper /* class A needs to have dll-interface to be used by clients of class B */)
49*b89a7cc2SEnji Cooper 
50*b89a7cc2SEnji Cooper namespace testing {
51*b89a7cc2SEnji Cooper 
52*b89a7cc2SEnji Cooper // To implement a cardinality Foo, define:
53*b89a7cc2SEnji Cooper //   1. a class FooCardinality that implements the
54*b89a7cc2SEnji Cooper //      CardinalityInterface interface, and
55*b89a7cc2SEnji Cooper //   2. a factory function that creates a Cardinality object from a
56*b89a7cc2SEnji Cooper //      const FooCardinality*.
57*b89a7cc2SEnji Cooper //
58*b89a7cc2SEnji Cooper // The two-level delegation design follows that of Matcher, providing
59*b89a7cc2SEnji Cooper // consistency for extension developers.  It also eases ownership
60*b89a7cc2SEnji Cooper // management as Cardinality objects can now be copied like plain values.
61*b89a7cc2SEnji Cooper 
62*b89a7cc2SEnji Cooper // The implementation of a cardinality.
63*b89a7cc2SEnji Cooper class CardinalityInterface {
64*b89a7cc2SEnji Cooper  public:
65*b89a7cc2SEnji Cooper   virtual ~CardinalityInterface() {}
66*b89a7cc2SEnji Cooper 
67*b89a7cc2SEnji Cooper   // Conservative estimate on the lower/upper bound of the number of
68*b89a7cc2SEnji Cooper   // calls allowed.
69*b89a7cc2SEnji Cooper   virtual int ConservativeLowerBound() const { return 0; }
70*b89a7cc2SEnji Cooper   virtual int ConservativeUpperBound() const { return INT_MAX; }
71*b89a7cc2SEnji Cooper 
72*b89a7cc2SEnji Cooper   // Returns true iff call_count calls will satisfy this cardinality.
73*b89a7cc2SEnji Cooper   virtual bool IsSatisfiedByCallCount(int call_count) const = 0;
74*b89a7cc2SEnji Cooper 
75*b89a7cc2SEnji Cooper   // Returns true iff call_count calls will saturate this cardinality.
76*b89a7cc2SEnji Cooper   virtual bool IsSaturatedByCallCount(int call_count) const = 0;
77*b89a7cc2SEnji Cooper 
78*b89a7cc2SEnji Cooper   // Describes self to an ostream.
79*b89a7cc2SEnji Cooper   virtual void DescribeTo(::std::ostream* os) const = 0;
80*b89a7cc2SEnji Cooper };
81*b89a7cc2SEnji Cooper 
82*b89a7cc2SEnji Cooper // A Cardinality is a copyable and IMMUTABLE (except by assignment)
83*b89a7cc2SEnji Cooper // object that specifies how many times a mock function is expected to
84*b89a7cc2SEnji Cooper // be called.  The implementation of Cardinality is just a linked_ptr
85*b89a7cc2SEnji Cooper // to const CardinalityInterface, so copying is fairly cheap.
86*b89a7cc2SEnji Cooper // Don't inherit from Cardinality!
87*b89a7cc2SEnji Cooper class GTEST_API_ Cardinality {
88*b89a7cc2SEnji Cooper  public:
89*b89a7cc2SEnji Cooper   // Constructs a null cardinality.  Needed for storing Cardinality
90*b89a7cc2SEnji Cooper   // objects in STL containers.
91*b89a7cc2SEnji Cooper   Cardinality() {}
92*b89a7cc2SEnji Cooper 
93*b89a7cc2SEnji Cooper   // Constructs a Cardinality from its implementation.
94*b89a7cc2SEnji Cooper   explicit Cardinality(const CardinalityInterface* impl) : impl_(impl) {}
95*b89a7cc2SEnji Cooper 
96*b89a7cc2SEnji Cooper   // Conservative estimate on the lower/upper bound of the number of
97*b89a7cc2SEnji Cooper   // calls allowed.
98*b89a7cc2SEnji Cooper   int ConservativeLowerBound() const { return impl_->ConservativeLowerBound(); }
99*b89a7cc2SEnji Cooper   int ConservativeUpperBound() const { return impl_->ConservativeUpperBound(); }
100*b89a7cc2SEnji Cooper 
101*b89a7cc2SEnji Cooper   // Returns true iff call_count calls will satisfy this cardinality.
102*b89a7cc2SEnji Cooper   bool IsSatisfiedByCallCount(int call_count) const {
103*b89a7cc2SEnji Cooper     return impl_->IsSatisfiedByCallCount(call_count);
104*b89a7cc2SEnji Cooper   }
105*b89a7cc2SEnji Cooper 
106*b89a7cc2SEnji Cooper   // Returns true iff call_count calls will saturate this cardinality.
107*b89a7cc2SEnji Cooper   bool IsSaturatedByCallCount(int call_count) const {
108*b89a7cc2SEnji Cooper     return impl_->IsSaturatedByCallCount(call_count);
109*b89a7cc2SEnji Cooper   }
110*b89a7cc2SEnji Cooper 
111*b89a7cc2SEnji Cooper   // Returns true iff call_count calls will over-saturate this
112*b89a7cc2SEnji Cooper   // cardinality, i.e. exceed the maximum number of allowed calls.
113*b89a7cc2SEnji Cooper   bool IsOverSaturatedByCallCount(int call_count) const {
114*b89a7cc2SEnji Cooper     return impl_->IsSaturatedByCallCount(call_count) &&
115*b89a7cc2SEnji Cooper         !impl_->IsSatisfiedByCallCount(call_count);
116*b89a7cc2SEnji Cooper   }
117*b89a7cc2SEnji Cooper 
118*b89a7cc2SEnji Cooper   // Describes self to an ostream
119*b89a7cc2SEnji Cooper   void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); }
120*b89a7cc2SEnji Cooper 
121*b89a7cc2SEnji Cooper   // Describes the given actual call count to an ostream.
122*b89a7cc2SEnji Cooper   static void DescribeActualCallCountTo(int actual_call_count,
123*b89a7cc2SEnji Cooper                                         ::std::ostream* os);
124*b89a7cc2SEnji Cooper 
125*b89a7cc2SEnji Cooper  private:
126*b89a7cc2SEnji Cooper   internal::linked_ptr<const CardinalityInterface> impl_;
127*b89a7cc2SEnji Cooper };
128*b89a7cc2SEnji Cooper 
129*b89a7cc2SEnji Cooper // Creates a cardinality that allows at least n calls.
130*b89a7cc2SEnji Cooper GTEST_API_ Cardinality AtLeast(int n);
131*b89a7cc2SEnji Cooper 
132*b89a7cc2SEnji Cooper // Creates a cardinality that allows at most n calls.
133*b89a7cc2SEnji Cooper GTEST_API_ Cardinality AtMost(int n);
134*b89a7cc2SEnji Cooper 
135*b89a7cc2SEnji Cooper // Creates a cardinality that allows any number of calls.
136*b89a7cc2SEnji Cooper GTEST_API_ Cardinality AnyNumber();
137*b89a7cc2SEnji Cooper 
138*b89a7cc2SEnji Cooper // Creates a cardinality that allows between min and max calls.
139*b89a7cc2SEnji Cooper GTEST_API_ Cardinality Between(int min, int max);
140*b89a7cc2SEnji Cooper 
141*b89a7cc2SEnji Cooper // Creates a cardinality that allows exactly n calls.
142*b89a7cc2SEnji Cooper GTEST_API_ Cardinality Exactly(int n);
143*b89a7cc2SEnji Cooper 
144*b89a7cc2SEnji Cooper // Creates a cardinality from its implementation.
145*b89a7cc2SEnji Cooper inline Cardinality MakeCardinality(const CardinalityInterface* c) {
146*b89a7cc2SEnji Cooper   return Cardinality(c);
147*b89a7cc2SEnji Cooper }
148*b89a7cc2SEnji Cooper 
149*b89a7cc2SEnji Cooper }  // namespace testing
150*b89a7cc2SEnji Cooper 
151*b89a7cc2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_()  //  4251
152*b89a7cc2SEnji Cooper 
153*b89a7cc2SEnji Cooper #endif  // GMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_
154