Debian Codegraph @

llvm-project/third-party/unittest/googlemock/include/gmock/gmock-cardinalities.h

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