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