File indexing completed on 2025-08-06 08:19:54
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
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
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
0073
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
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
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
0123 *os << "called between " << min_ << " and " << max_ << " times";
0124 }
0125 }
0126
0127 }
0128
0129
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
0140 GTEST_API_ Cardinality AtLeast(int n) { return Between(n, INT_MAX); }
0141
0142
0143 GTEST_API_ Cardinality AtMost(int n) { return Between(0, n); }
0144
0145
0146 GTEST_API_ Cardinality AnyNumber() { return AtLeast(0); }
0147
0148
0149 GTEST_API_ Cardinality Between(int min, int max) {
0150 return Cardinality(new BetweenCardinalityImpl(min, max));
0151 }
0152
0153
0154 GTEST_API_ Cardinality Exactly(int n) { return Between(n, n); }
0155
0156 }