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