1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Author: wan@google.com (Zhanyong Wan)
32 // Google Mock - a framework for writing C++ mock classes.
34 // This file implements some commonly used cardinalities. More
35 // cardinalities can be defined by the user implementing the
36 // CardinalityInterface interface if necessary.
38 // IWYU pragma: private, include "gmock/gmock.h"
40 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_
41 #define GMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_
44 #include <ostream> // NOLINT
45 #include "gmock/internal/gmock-port.h"
46 #include "gtest/gtest.h"
50 // To implement a cardinality Foo, define:
51 // 1. a class FooCardinality that implements the
52 // CardinalityInterface interface, and
53 // 2. a factory function that creates a Cardinality object from a
54 // const FooCardinality*.
56 // The two-level delegation design follows that of Matcher, providing
57 // consistency for extension developers. It also eases ownership
58 // management as Cardinality objects can now be copied like plain values.
60 // The implementation of a cardinality.
61 class CardinalityInterface
{
63 virtual ~CardinalityInterface() {}
65 // Conservative estimate on the lower/upper bound of the number of
67 virtual int ConservativeLowerBound() const { return 0; }
68 virtual int ConservativeUpperBound() const { return INT_MAX
; }
70 // Returns true iff call_count calls will satisfy this cardinality.
71 virtual bool IsSatisfiedByCallCount(int call_count
) const = 0;
73 // Returns true iff call_count calls will saturate this cardinality.
74 virtual bool IsSaturatedByCallCount(int call_count
) const = 0;
76 // Describes self to an ostream.
77 virtual void DescribeTo(::std::ostream
* os
) const = 0;
80 // A Cardinality is a copyable and IMMUTABLE (except by assignment)
81 // object that specifies how many times a mock function is expected to
82 // be called. The implementation of Cardinality is just a linked_ptr
83 // to const CardinalityInterface, so copying is fairly cheap.
84 // Don't inherit from Cardinality!
85 class GTEST_API_ Cardinality
{
87 // Constructs a null cardinality. Needed for storing Cardinality
88 // objects in STL containers.
91 // Constructs a Cardinality from its implementation.
92 explicit Cardinality(const CardinalityInterface
* impl
) : impl_(impl
) {}
94 // Conservative estimate on the lower/upper bound of the number of
96 int ConservativeLowerBound() const { return impl_
->ConservativeLowerBound(); }
97 int ConservativeUpperBound() const { return impl_
->ConservativeUpperBound(); }
99 // Returns true iff call_count calls will satisfy this cardinality.
100 bool IsSatisfiedByCallCount(int call_count
) const {
101 return impl_
->IsSatisfiedByCallCount(call_count
);
104 // Returns true iff call_count calls will saturate this cardinality.
105 bool IsSaturatedByCallCount(int call_count
) const {
106 return impl_
->IsSaturatedByCallCount(call_count
);
109 // Returns true iff call_count calls will over-saturate this
110 // cardinality, i.e. exceed the maximum number of allowed calls.
111 bool IsOverSaturatedByCallCount(int call_count
) const {
112 return impl_
->IsSaturatedByCallCount(call_count
) &&
113 !impl_
->IsSatisfiedByCallCount(call_count
);
116 // Describes self to an ostream
117 void DescribeTo(::std::ostream
* os
) const { impl_
->DescribeTo(os
); }
119 // Describes the given actual call count to an ostream.
120 static void DescribeActualCallCountTo(int actual_call_count
,
124 internal::linked_ptr
<const CardinalityInterface
> impl_
;
127 // Creates a cardinality that allows at least n calls.
128 GTEST_API_ Cardinality
AtLeast(int n
);
130 // Creates a cardinality that allows at most n calls.
131 GTEST_API_ Cardinality
AtMost(int n
);
133 // Creates a cardinality that allows any number of calls.
134 GTEST_API_ Cardinality
AnyNumber();
136 // Creates a cardinality that allows between min and max calls.
137 GTEST_API_ Cardinality
Between(int min
, int max
);
139 // Creates a cardinality that allows exactly n calls.
140 GTEST_API_ Cardinality
Exactly(int n
);
142 // Creates a cardinality from its implementation.
143 inline Cardinality
MakeCardinality(const CardinalityInterface
* c
) {
144 return Cardinality(c
);
147 } // namespace testing
149 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_