xref: /freebsd-src/contrib/googletest/googlemock/src/gmock-cardinalities.cc (revision 28f6c2f292806bf31230a959bc4b19d7081669a7)
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 cardinalities.
33b89a7cc2SEnji Cooper 
34b89a7cc2SEnji Cooper #include "gmock/gmock-cardinalities.h"
35b89a7cc2SEnji Cooper 
36b89a7cc2SEnji Cooper #include <limits.h>
37*28f6c2f2SEnji Cooper 
38b89a7cc2SEnji Cooper #include <ostream>  // NOLINT
39b89a7cc2SEnji Cooper #include <sstream>
40b89a7cc2SEnji Cooper #include <string>
41*28f6c2f2SEnji Cooper 
42b89a7cc2SEnji Cooper #include "gmock/internal/gmock-internal-utils.h"
43b89a7cc2SEnji Cooper #include "gtest/gtest.h"
44b89a7cc2SEnji Cooper 
45b89a7cc2SEnji Cooper namespace testing {
46b89a7cc2SEnji Cooper 
47b89a7cc2SEnji Cooper namespace {
48b89a7cc2SEnji Cooper 
49b89a7cc2SEnji Cooper // Implements the Between(m, n) cardinality.
50b89a7cc2SEnji Cooper class BetweenCardinalityImpl : public CardinalityInterface {
51b89a7cc2SEnji Cooper  public:
BetweenCardinalityImpl(int min,int max)52b89a7cc2SEnji Cooper   BetweenCardinalityImpl(int min, int max)
53*28f6c2f2SEnji Cooper       : min_(min >= 0 ? min : 0), max_(max >= min_ ? max : min_) {
54b89a7cc2SEnji Cooper     std::stringstream ss;
55b89a7cc2SEnji Cooper     if (min < 0) {
56b89a7cc2SEnji Cooper       ss << "The invocation lower bound must be >= 0, "
57b89a7cc2SEnji Cooper          << "but is actually " << min << ".";
58b89a7cc2SEnji Cooper       internal::Expect(false, __FILE__, __LINE__, ss.str());
59b89a7cc2SEnji Cooper     } else if (max < 0) {
60b89a7cc2SEnji Cooper       ss << "The invocation upper bound must be >= 0, "
61b89a7cc2SEnji Cooper          << "but is actually " << max << ".";
62b89a7cc2SEnji Cooper       internal::Expect(false, __FILE__, __LINE__, ss.str());
63b89a7cc2SEnji Cooper     } else if (min > max) {
64b89a7cc2SEnji Cooper       ss << "The invocation upper bound (" << max
65*28f6c2f2SEnji Cooper          << ") must be >= the invocation lower bound (" << min << ").";
66b89a7cc2SEnji Cooper       internal::Expect(false, __FILE__, __LINE__, ss.str());
67b89a7cc2SEnji Cooper     }
68b89a7cc2SEnji Cooper   }
69b89a7cc2SEnji Cooper 
70b89a7cc2SEnji Cooper   // Conservative estimate on the lower/upper bound of the number of
71b89a7cc2SEnji Cooper   // calls allowed.
ConservativeLowerBound() const72*28f6c2f2SEnji Cooper   int ConservativeLowerBound() const override { return min_; }
ConservativeUpperBound() const73*28f6c2f2SEnji Cooper   int ConservativeUpperBound() const override { return max_; }
74b89a7cc2SEnji Cooper 
IsSatisfiedByCallCount(int call_count) const75*28f6c2f2SEnji Cooper   bool IsSatisfiedByCallCount(int call_count) const override {
76b89a7cc2SEnji Cooper     return min_ <= call_count && call_count <= max_;
77b89a7cc2SEnji Cooper   }
78b89a7cc2SEnji Cooper 
IsSaturatedByCallCount(int call_count) const79*28f6c2f2SEnji Cooper   bool IsSaturatedByCallCount(int call_count) const override {
80b89a7cc2SEnji Cooper     return call_count >= max_;
81b89a7cc2SEnji Cooper   }
82b89a7cc2SEnji Cooper 
83*28f6c2f2SEnji Cooper   void DescribeTo(::std::ostream* os) const override;
84b89a7cc2SEnji Cooper 
85b89a7cc2SEnji Cooper  private:
86b89a7cc2SEnji Cooper   const int min_;
87b89a7cc2SEnji Cooper   const int max_;
88b89a7cc2SEnji Cooper 
89*28f6c2f2SEnji Cooper   BetweenCardinalityImpl(const BetweenCardinalityImpl&) = delete;
90*28f6c2f2SEnji Cooper   BetweenCardinalityImpl& operator=(const BetweenCardinalityImpl&) = delete;
91b89a7cc2SEnji Cooper };
92b89a7cc2SEnji Cooper 
93b89a7cc2SEnji Cooper // Formats "n times" in a human-friendly way.
FormatTimes(int n)94b89a7cc2SEnji Cooper inline std::string FormatTimes(int n) {
95b89a7cc2SEnji Cooper   if (n == 1) {
96b89a7cc2SEnji Cooper     return "once";
97b89a7cc2SEnji Cooper   } else if (n == 2) {
98b89a7cc2SEnji Cooper     return "twice";
99b89a7cc2SEnji Cooper   } else {
100b89a7cc2SEnji Cooper     std::stringstream ss;
101b89a7cc2SEnji Cooper     ss << n << " times";
102b89a7cc2SEnji Cooper     return ss.str();
103b89a7cc2SEnji Cooper   }
104b89a7cc2SEnji Cooper }
105b89a7cc2SEnji Cooper 
106b89a7cc2SEnji Cooper // Describes the Between(m, n) cardinality in human-friendly text.
DescribeTo(::std::ostream * os) const107b89a7cc2SEnji Cooper void BetweenCardinalityImpl::DescribeTo(::std::ostream* os) const {
108b89a7cc2SEnji Cooper   if (min_ == 0) {
109b89a7cc2SEnji Cooper     if (max_ == 0) {
110b89a7cc2SEnji Cooper       *os << "never called";
111b89a7cc2SEnji Cooper     } else if (max_ == INT_MAX) {
112b89a7cc2SEnji Cooper       *os << "called any number of times";
113b89a7cc2SEnji Cooper     } else {
114b89a7cc2SEnji Cooper       *os << "called at most " << FormatTimes(max_);
115b89a7cc2SEnji Cooper     }
116b89a7cc2SEnji Cooper   } else if (min_ == max_) {
117b89a7cc2SEnji Cooper     *os << "called " << FormatTimes(min_);
118b89a7cc2SEnji Cooper   } else if (max_ == INT_MAX) {
119b89a7cc2SEnji Cooper     *os << "called at least " << FormatTimes(min_);
120b89a7cc2SEnji Cooper   } else {
121b89a7cc2SEnji Cooper     // 0 < min_ < max_ < INT_MAX
122b89a7cc2SEnji Cooper     *os << "called between " << min_ << " and " << max_ << " times";
123b89a7cc2SEnji Cooper   }
124b89a7cc2SEnji Cooper }
125b89a7cc2SEnji Cooper 
126b89a7cc2SEnji Cooper }  // Unnamed namespace
127b89a7cc2SEnji Cooper 
128b89a7cc2SEnji Cooper // Describes the given call count to an ostream.
DescribeActualCallCountTo(int actual_call_count,::std::ostream * os)129b89a7cc2SEnji Cooper void Cardinality::DescribeActualCallCountTo(int actual_call_count,
130b89a7cc2SEnji Cooper                                             ::std::ostream* os) {
131b89a7cc2SEnji Cooper   if (actual_call_count > 0) {
132b89a7cc2SEnji Cooper     *os << "called " << FormatTimes(actual_call_count);
133b89a7cc2SEnji Cooper   } else {
134b89a7cc2SEnji Cooper     *os << "never called";
135b89a7cc2SEnji Cooper   }
136b89a7cc2SEnji Cooper }
137b89a7cc2SEnji Cooper 
138b89a7cc2SEnji Cooper // Creates a cardinality that allows at least n calls.
AtLeast(int n)139b89a7cc2SEnji Cooper GTEST_API_ Cardinality AtLeast(int n) { return Between(n, INT_MAX); }
140b89a7cc2SEnji Cooper 
141b89a7cc2SEnji Cooper // Creates a cardinality that allows at most n calls.
AtMost(int n)142b89a7cc2SEnji Cooper GTEST_API_ Cardinality AtMost(int n) { return Between(0, n); }
143b89a7cc2SEnji Cooper 
144b89a7cc2SEnji Cooper // Creates a cardinality that allows any number of calls.
AnyNumber()145b89a7cc2SEnji Cooper GTEST_API_ Cardinality AnyNumber() { return AtLeast(0); }
146b89a7cc2SEnji Cooper 
147b89a7cc2SEnji Cooper // Creates a cardinality that allows between min and max calls.
Between(int min,int max)148b89a7cc2SEnji Cooper GTEST_API_ Cardinality Between(int min, int max) {
149b89a7cc2SEnji Cooper   return Cardinality(new BetweenCardinalityImpl(min, max));
150b89a7cc2SEnji Cooper }
151b89a7cc2SEnji Cooper 
152b89a7cc2SEnji Cooper // Creates a cardinality that allows exactly n calls.
Exactly(int n)153b89a7cc2SEnji Cooper GTEST_API_ Cardinality Exactly(int n) { return Between(n, n); }
154b89a7cc2SEnji Cooper 
155b89a7cc2SEnji Cooper }  // namespace testing
156