xref: /llvm-project/third-party/unittest/googlemock/src/gmock-cardinalities.cc (revision a866ce789eb99da4d7a486eeb60a53be6c75f4fd)
1a11cd0d9STom Stellard // Copyright 2007, Google Inc.
2a11cd0d9STom Stellard // All rights reserved.
3a11cd0d9STom Stellard //
4a11cd0d9STom Stellard // Redistribution and use in source and binary forms, with or without
5a11cd0d9STom Stellard // modification, are permitted provided that the following conditions are
6a11cd0d9STom Stellard // met:
7a11cd0d9STom Stellard //
8a11cd0d9STom Stellard //     * Redistributions of source code must retain the above copyright
9a11cd0d9STom Stellard // notice, this list of conditions and the following disclaimer.
10a11cd0d9STom Stellard //     * Redistributions in binary form must reproduce the above
11a11cd0d9STom Stellard // copyright notice, this list of conditions and the following disclaimer
12a11cd0d9STom Stellard // in the documentation and/or other materials provided with the
13a11cd0d9STom Stellard // distribution.
14a11cd0d9STom Stellard //     * Neither the name of Google Inc. nor the names of its
15a11cd0d9STom Stellard // contributors may be used to endorse or promote products derived from
16a11cd0d9STom Stellard // this software without specific prior written permission.
17a11cd0d9STom Stellard //
18a11cd0d9STom Stellard // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19a11cd0d9STom Stellard // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20a11cd0d9STom Stellard // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21a11cd0d9STom Stellard // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22a11cd0d9STom Stellard // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23a11cd0d9STom Stellard // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24a11cd0d9STom Stellard // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25a11cd0d9STom Stellard // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26a11cd0d9STom Stellard // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27a11cd0d9STom Stellard // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28a11cd0d9STom Stellard // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29a11cd0d9STom Stellard 
30a11cd0d9STom Stellard // Google Mock - a framework for writing C++ mock classes.
31a11cd0d9STom Stellard //
32a11cd0d9STom Stellard // This file implements cardinalities.
33a11cd0d9STom Stellard 
34a11cd0d9STom Stellard #include "gmock/gmock-cardinalities.h"
35a11cd0d9STom Stellard 
36a11cd0d9STom Stellard #include <limits.h>
37*a866ce78SHaowei Wu 
38a11cd0d9STom Stellard #include <ostream>  // NOLINT
39a11cd0d9STom Stellard #include <sstream>
40a11cd0d9STom Stellard #include <string>
41*a866ce78SHaowei Wu 
42a11cd0d9STom Stellard #include "gmock/internal/gmock-internal-utils.h"
43a11cd0d9STom Stellard #include "gtest/gtest.h"
44a11cd0d9STom Stellard 
45a11cd0d9STom Stellard namespace testing {
46a11cd0d9STom Stellard 
47a11cd0d9STom Stellard namespace {
48a11cd0d9STom Stellard 
49a11cd0d9STom Stellard // Implements the Between(m, n) cardinality.
50a11cd0d9STom Stellard class BetweenCardinalityImpl : public CardinalityInterface {
51a11cd0d9STom Stellard  public:
BetweenCardinalityImpl(int min,int max)52a11cd0d9STom Stellard   BetweenCardinalityImpl(int min, int max)
53*a866ce78SHaowei Wu       : min_(min >= 0 ? min : 0), max_(max >= min_ ? max : min_) {
54a11cd0d9STom Stellard     std::stringstream ss;
55a11cd0d9STom Stellard     if (min < 0) {
56a11cd0d9STom Stellard       ss << "The invocation lower bound must be >= 0, "
57a11cd0d9STom Stellard          << "but is actually " << min << ".";
58a11cd0d9STom Stellard       internal::Expect(false, __FILE__, __LINE__, ss.str());
59a11cd0d9STom Stellard     } else if (max < 0) {
60a11cd0d9STom Stellard       ss << "The invocation upper bound must be >= 0, "
61a11cd0d9STom Stellard          << "but is actually " << max << ".";
62a11cd0d9STom Stellard       internal::Expect(false, __FILE__, __LINE__, ss.str());
63a11cd0d9STom Stellard     } else if (min > max) {
64a11cd0d9STom Stellard       ss << "The invocation upper bound (" << max
65*a866ce78SHaowei Wu          << ") must be >= the invocation lower bound (" << min << ").";
66a11cd0d9STom Stellard       internal::Expect(false, __FILE__, __LINE__, ss.str());
67a11cd0d9STom Stellard     }
68a11cd0d9STom Stellard   }
69a11cd0d9STom Stellard 
70a11cd0d9STom Stellard   // Conservative estimate on the lower/upper bound of the number of
71a11cd0d9STom Stellard   // calls allowed.
ConservativeLowerBound() const72a11cd0d9STom Stellard   int ConservativeLowerBound() const override { return min_; }
ConservativeUpperBound() const73a11cd0d9STom Stellard   int ConservativeUpperBound() const override { return max_; }
74a11cd0d9STom Stellard 
IsSatisfiedByCallCount(int call_count) const75a11cd0d9STom Stellard   bool IsSatisfiedByCallCount(int call_count) const override {
76a11cd0d9STom Stellard     return min_ <= call_count && call_count <= max_;
77a11cd0d9STom Stellard   }
78a11cd0d9STom Stellard 
IsSaturatedByCallCount(int call_count) const79a11cd0d9STom Stellard   bool IsSaturatedByCallCount(int call_count) const override {
80a11cd0d9STom Stellard     return call_count >= max_;
81a11cd0d9STom Stellard   }
82a11cd0d9STom Stellard 
83a11cd0d9STom Stellard   void DescribeTo(::std::ostream* os) const override;
84a11cd0d9STom Stellard 
85a11cd0d9STom Stellard  private:
86a11cd0d9STom Stellard   const int min_;
87a11cd0d9STom Stellard   const int max_;
88a11cd0d9STom Stellard 
89*a866ce78SHaowei Wu   BetweenCardinalityImpl(const BetweenCardinalityImpl&) = delete;
90*a866ce78SHaowei Wu   BetweenCardinalityImpl& operator=(const BetweenCardinalityImpl&) = delete;
91a11cd0d9STom Stellard };
92a11cd0d9STom Stellard 
93a11cd0d9STom Stellard // Formats "n times" in a human-friendly way.
FormatTimes(int n)94a11cd0d9STom Stellard inline std::string FormatTimes(int n) {
95a11cd0d9STom Stellard   if (n == 1) {
96a11cd0d9STom Stellard     return "once";
97a11cd0d9STom Stellard   } else if (n == 2) {
98a11cd0d9STom Stellard     return "twice";
99a11cd0d9STom Stellard   } else {
100a11cd0d9STom Stellard     std::stringstream ss;
101a11cd0d9STom Stellard     ss << n << " times";
102a11cd0d9STom Stellard     return ss.str();
103a11cd0d9STom Stellard   }
104a11cd0d9STom Stellard }
105a11cd0d9STom Stellard 
106a11cd0d9STom Stellard // Describes the Between(m, n) cardinality in human-friendly text.
DescribeTo(::std::ostream * os) const107a11cd0d9STom Stellard void BetweenCardinalityImpl::DescribeTo(::std::ostream* os) const {
108a11cd0d9STom Stellard   if (min_ == 0) {
109a11cd0d9STom Stellard     if (max_ == 0) {
110a11cd0d9STom Stellard       *os << "never called";
111a11cd0d9STom Stellard     } else if (max_ == INT_MAX) {
112a11cd0d9STom Stellard       *os << "called any number of times";
113a11cd0d9STom Stellard     } else {
114a11cd0d9STom Stellard       *os << "called at most " << FormatTimes(max_);
115a11cd0d9STom Stellard     }
116a11cd0d9STom Stellard   } else if (min_ == max_) {
117a11cd0d9STom Stellard     *os << "called " << FormatTimes(min_);
118a11cd0d9STom Stellard   } else if (max_ == INT_MAX) {
119a11cd0d9STom Stellard     *os << "called at least " << FormatTimes(min_);
120a11cd0d9STom Stellard   } else {
121a11cd0d9STom Stellard     // 0 < min_ < max_ < INT_MAX
122a11cd0d9STom Stellard     *os << "called between " << min_ << " and " << max_ << " times";
123a11cd0d9STom Stellard   }
124a11cd0d9STom Stellard }
125a11cd0d9STom Stellard 
126a11cd0d9STom Stellard }  // Unnamed namespace
127a11cd0d9STom Stellard 
128a11cd0d9STom Stellard // Describes the given call count to an ostream.
DescribeActualCallCountTo(int actual_call_count,::std::ostream * os)129a11cd0d9STom Stellard void Cardinality::DescribeActualCallCountTo(int actual_call_count,
130a11cd0d9STom Stellard                                             ::std::ostream* os) {
131a11cd0d9STom Stellard   if (actual_call_count > 0) {
132a11cd0d9STom Stellard     *os << "called " << FormatTimes(actual_call_count);
133a11cd0d9STom Stellard   } else {
134a11cd0d9STom Stellard     *os << "never called";
135a11cd0d9STom Stellard   }
136a11cd0d9STom Stellard }
137a11cd0d9STom Stellard 
138a11cd0d9STom Stellard // Creates a cardinality that allows at least n calls.
AtLeast(int n)139a11cd0d9STom Stellard GTEST_API_ Cardinality AtLeast(int n) { return Between(n, INT_MAX); }
140a11cd0d9STom Stellard 
141a11cd0d9STom Stellard // Creates a cardinality that allows at most n calls.
AtMost(int n)142a11cd0d9STom Stellard GTEST_API_ Cardinality AtMost(int n) { return Between(0, n); }
143a11cd0d9STom Stellard 
144a11cd0d9STom Stellard // Creates a cardinality that allows any number of calls.
AnyNumber()145a11cd0d9STom Stellard GTEST_API_ Cardinality AnyNumber() { return AtLeast(0); }
146a11cd0d9STom Stellard 
147a11cd0d9STom Stellard // Creates a cardinality that allows between min and max calls.
Between(int min,int max)148a11cd0d9STom Stellard GTEST_API_ Cardinality Between(int min, int max) {
149a11cd0d9STom Stellard   return Cardinality(new BetweenCardinalityImpl(min, max));
150a11cd0d9STom Stellard }
151a11cd0d9STom Stellard 
152a11cd0d9STom Stellard // Creates a cardinality that allows exactly n calls.
Exactly(int n)153a11cd0d9STom Stellard GTEST_API_ Cardinality Exactly(int n) { return Between(n, n); }
154a11cd0d9STom Stellard 
155a11cd0d9STom Stellard }  // namespace testing
156