michael@0: // Copyright 2007, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: // michael@0: // Author: wan@google.com (Zhanyong Wan) michael@0: michael@0: // Google Mock - a framework for writing C++ mock classes. michael@0: // michael@0: // This file implements cardinalities. michael@0: michael@0: #include "gmock/gmock-cardinalities.h" michael@0: michael@0: #include michael@0: #include // NOLINT michael@0: #include michael@0: #include michael@0: #include "gmock/internal/gmock-internal-utils.h" michael@0: #include "gtest/gtest.h" michael@0: michael@0: namespace testing { michael@0: michael@0: namespace { michael@0: michael@0: // Implements the Between(m, n) cardinality. michael@0: class BetweenCardinalityImpl : public CardinalityInterface { michael@0: public: michael@0: BetweenCardinalityImpl(int min, int max) michael@0: : min_(min >= 0 ? min : 0), michael@0: max_(max >= min_ ? max : min_) { michael@0: std::stringstream ss; michael@0: if (min < 0) { michael@0: ss << "The invocation lower bound must be >= 0, " michael@0: << "but is actually " << min << "."; michael@0: internal::Expect(false, __FILE__, __LINE__, ss.str()); michael@0: } else if (max < 0) { michael@0: ss << "The invocation upper bound must be >= 0, " michael@0: << "but is actually " << max << "."; michael@0: internal::Expect(false, __FILE__, __LINE__, ss.str()); michael@0: } else if (min > max) { michael@0: ss << "The invocation upper bound (" << max michael@0: << ") must be >= the invocation lower bound (" << min michael@0: << ")."; michael@0: internal::Expect(false, __FILE__, __LINE__, ss.str()); michael@0: } michael@0: } michael@0: michael@0: // Conservative estimate on the lower/upper bound of the number of michael@0: // calls allowed. michael@0: virtual int ConservativeLowerBound() const { return min_; } michael@0: virtual int ConservativeUpperBound() const { return max_; } michael@0: michael@0: virtual bool IsSatisfiedByCallCount(int call_count) const { michael@0: return min_ <= call_count && call_count <= max_ ; michael@0: } michael@0: michael@0: virtual bool IsSaturatedByCallCount(int call_count) const { michael@0: return call_count >= max_; michael@0: } michael@0: michael@0: virtual void DescribeTo(::std::ostream* os) const; michael@0: private: michael@0: const int min_; michael@0: const int max_; michael@0: michael@0: GTEST_DISALLOW_COPY_AND_ASSIGN_(BetweenCardinalityImpl); michael@0: }; michael@0: michael@0: // Formats "n times" in a human-friendly way. michael@0: inline internal::string FormatTimes(int n) { michael@0: if (n == 1) { michael@0: return "once"; michael@0: } else if (n == 2) { michael@0: return "twice"; michael@0: } else { michael@0: std::stringstream ss; michael@0: ss << n << " times"; michael@0: return ss.str(); michael@0: } michael@0: } michael@0: michael@0: // Describes the Between(m, n) cardinality in human-friendly text. michael@0: void BetweenCardinalityImpl::DescribeTo(::std::ostream* os) const { michael@0: if (min_ == 0) { michael@0: if (max_ == 0) { michael@0: *os << "never called"; michael@0: } else if (max_ == INT_MAX) { michael@0: *os << "called any number of times"; michael@0: } else { michael@0: *os << "called at most " << FormatTimes(max_); michael@0: } michael@0: } else if (min_ == max_) { michael@0: *os << "called " << FormatTimes(min_); michael@0: } else if (max_ == INT_MAX) { michael@0: *os << "called at least " << FormatTimes(min_); michael@0: } else { michael@0: // 0 < min_ < max_ < INT_MAX michael@0: *os << "called between " << min_ << " and " << max_ << " times"; michael@0: } michael@0: } michael@0: michael@0: } // Unnamed namespace michael@0: michael@0: // Describes the given call count to an ostream. michael@0: void Cardinality::DescribeActualCallCountTo(int actual_call_count, michael@0: ::std::ostream* os) { michael@0: if (actual_call_count > 0) { michael@0: *os << "called " << FormatTimes(actual_call_count); michael@0: } else { michael@0: *os << "never called"; michael@0: } michael@0: } michael@0: michael@0: // Creates a cardinality that allows at least n calls. michael@0: Cardinality AtLeast(int n) { return Between(n, INT_MAX); } michael@0: michael@0: // Creates a cardinality that allows at most n calls. michael@0: Cardinality AtMost(int n) { return Between(0, n); } michael@0: michael@0: // Creates a cardinality that allows any number of calls. michael@0: Cardinality AnyNumber() { return AtLeast(0); } michael@0: michael@0: // Creates a cardinality that allows between min and max calls. michael@0: Cardinality Between(int min, int max) { michael@0: return Cardinality(new BetweenCardinalityImpl(min, max)); michael@0: } michael@0: michael@0: // Creates a cardinality that allows exactly n calls. michael@0: Cardinality Exactly(int n) { return Between(n, n); } michael@0: michael@0: } // namespace testing