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