Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:19:54

0001 // Copyright 2007, Google Inc.
0002 // All rights reserved.
0003 //
0004 // Redistribution and use in source and binary forms, with or without
0005 // modification, are permitted provided that the following conditions are
0006 // met:
0007 //
0008 //     * Redistributions of source code must retain the above copyright
0009 // notice, this list of conditions and the following disclaimer.
0010 //     * Redistributions in binary form must reproduce the above
0011 // copyright notice, this list of conditions and the following disclaimer
0012 // in the documentation and/or other materials provided with the
0013 // distribution.
0014 //     * Neither the name of Google Inc. nor the names of its
0015 // contributors may be used to endorse or promote products derived from
0016 // this software without specific prior written permission.
0017 //
0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0029 //
0030 // Author: wan@google.com (Zhanyong Wan)
0031 
0032 // Google Mock - a framework for writing C++ mock classes.
0033 //
0034 // This file implements cardinalities.
0035 
0036 #include "gmock/gmock-cardinalities.h"
0037 
0038 #include <limits.h>
0039 #include <ostream>  // NOLINT
0040 #include <sstream>
0041 #include <string>
0042 #include "gmock/internal/gmock-internal-utils.h"
0043 #include "gtest/gtest.h"
0044 
0045 namespace testing {
0046 
0047 namespace {
0048 
0049 // Implements the Between(m, n) cardinality.
0050 class BetweenCardinalityImpl : public CardinalityInterface {
0051  public:
0052   BetweenCardinalityImpl(int min, int max)
0053       : min_(min >= 0 ? min : 0),
0054         max_(max >= min_ ? max : min_) {
0055     std::stringstream ss;
0056     if (min < 0) {
0057       ss << "The invocation lower bound must be >= 0, "
0058          << "but is actually " << min << ".";
0059       internal::Expect(false, __FILE__, __LINE__, ss.str());
0060     } else if (max < 0) {
0061       ss << "The invocation upper bound must be >= 0, "
0062          << "but is actually " << max << ".";
0063       internal::Expect(false, __FILE__, __LINE__, ss.str());
0064     } else if (min > max) {
0065       ss << "The invocation upper bound (" << max
0066          << ") must be >= the invocation lower bound (" << min
0067          << ").";
0068       internal::Expect(false, __FILE__, __LINE__, ss.str());
0069     }
0070   }
0071 
0072   // Conservative estimate on the lower/upper bound of the number of
0073   // calls allowed.
0074   virtual int ConservativeLowerBound() const { return min_; }
0075   virtual int ConservativeUpperBound() const { return max_; }
0076 
0077   virtual bool IsSatisfiedByCallCount(int call_count) const {
0078     return min_ <= call_count && call_count <= max_;
0079   }
0080 
0081   virtual bool IsSaturatedByCallCount(int call_count) const {
0082     return call_count >= max_;
0083   }
0084 
0085   virtual void DescribeTo(::std::ostream* os) const;
0086 
0087  private:
0088   const int min_;
0089   const int max_;
0090 
0091   GTEST_DISALLOW_COPY_AND_ASSIGN_(BetweenCardinalityImpl);
0092 };
0093 
0094 // Formats "n times" in a human-friendly way.
0095 inline internal::string FormatTimes(int n) {
0096   if (n == 1) {
0097     return "once";
0098   } else if (n == 2) {
0099     return "twice";
0100   } else {
0101     std::stringstream ss;
0102     ss << n << " times";
0103     return ss.str();
0104   }
0105 }
0106 
0107 // Describes the Between(m, n) cardinality in human-friendly text.
0108 void BetweenCardinalityImpl::DescribeTo(::std::ostream* os) const {
0109   if (min_ == 0) {
0110     if (max_ == 0) {
0111       *os << "never called";
0112     } else if (max_ == INT_MAX) {
0113       *os << "called any number of times";
0114     } else {
0115       *os << "called at most " << FormatTimes(max_);
0116     }
0117   } else if (min_ == max_) {
0118     *os << "called " << FormatTimes(min_);
0119   } else if (max_ == INT_MAX) {
0120     *os << "called at least " << FormatTimes(min_);
0121   } else {
0122     // 0 < min_ < max_ < INT_MAX
0123     *os << "called between " << min_ << " and " << max_ << " times";
0124   }
0125 }
0126 
0127 }  // Unnamed namespace
0128 
0129 // Describes the given call count to an ostream.
0130 void Cardinality::DescribeActualCallCountTo(int actual_call_count,
0131                                             ::std::ostream* os) {
0132   if (actual_call_count > 0) {
0133     *os << "called " << FormatTimes(actual_call_count);
0134   } else {
0135     *os << "never called";
0136   }
0137 }
0138 
0139 // Creates a cardinality that allows at least n calls.
0140 GTEST_API_ Cardinality AtLeast(int n) { return Between(n, INT_MAX); }
0141 
0142 // Creates a cardinality that allows at most n calls.
0143 GTEST_API_ Cardinality AtMost(int n) { return Between(0, n); }
0144 
0145 // Creates a cardinality that allows any number of calls.
0146 GTEST_API_ Cardinality AnyNumber() { return AtLeast(0); }
0147 
0148 // Creates a cardinality that allows between min and max calls.
0149 GTEST_API_ Cardinality Between(int min, int max) {
0150   return Cardinality(new BetweenCardinalityImpl(min, max));
0151 }
0152 
0153 // Creates a cardinality that allows exactly n calls.
0154 GTEST_API_ Cardinality Exactly(int n) { return Between(n, n); }
0155 
0156 }  // namespace testing