Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "gmock/gmock-cardinalities.h"
00037
00038 #include <limits.h>
00039 #include <ostream>
00040 #include <sstream>
00041 #include <string>
00042 #include "gmock/internal/gmock-internal-utils.h"
00043 #include "gtest/gtest.h"
00044
00045 namespace testing {
00046
00047 namespace {
00048
00049
00050 class BetweenCardinalityImpl : public CardinalityInterface {
00051 public:
00052 BetweenCardinalityImpl(int min, int max)
00053 : min_(min >= 0 ? min : 0),
00054 max_(max >= min_ ? max : min_) {
00055 std::stringstream ss;
00056 if (min < 0) {
00057 ss << "The invocation lower bound must be >= 0, "
00058 << "but is actually " << min << ".";
00059 internal::Expect(false, __FILE__, __LINE__, ss.str());
00060 } else if (max < 0) {
00061 ss << "The invocation upper bound must be >= 0, "
00062 << "but is actually " << max << ".";
00063 internal::Expect(false, __FILE__, __LINE__, ss.str());
00064 } else if (min > max) {
00065 ss << "The invocation upper bound (" << max
00066 << ") must be >= the invocation lower bound (" << min
00067 << ").";
00068 internal::Expect(false, __FILE__, __LINE__, ss.str());
00069 }
00070 }
00071
00072
00073
00074 virtual int ConservativeLowerBound() const { return min_; }
00075 virtual int ConservativeUpperBound() const { return max_; }
00076
00077 virtual bool IsSatisfiedByCallCount(int call_count) const {
00078 return min_ <= call_count && call_count <= max_;
00079 }
00080
00081 virtual bool IsSaturatedByCallCount(int call_count) const {
00082 return call_count >= max_;
00083 }
00084
00085 virtual void DescribeTo(::std::ostream* os) const;
00086
00087 private:
00088 const int min_;
00089 const int max_;
00090
00091 GTEST_DISALLOW_COPY_AND_ASSIGN_(BetweenCardinalityImpl);
00092 };
00093
00094
00095 inline internal::string FormatTimes(int n) {
00096 if (n == 1) {
00097 return "once";
00098 } else if (n == 2) {
00099 return "twice";
00100 } else {
00101 std::stringstream ss;
00102 ss << n << " times";
00103 return ss.str();
00104 }
00105 }
00106
00107
00108 void BetweenCardinalityImpl::DescribeTo(::std::ostream* os) const {
00109 if (min_ == 0) {
00110 if (max_ == 0) {
00111 *os << "never called";
00112 } else if (max_ == INT_MAX) {
00113 *os << "called any number of times";
00114 } else {
00115 *os << "called at most " << FormatTimes(max_);
00116 }
00117 } else if (min_ == max_) {
00118 *os << "called " << FormatTimes(min_);
00119 } else if (max_ == INT_MAX) {
00120 *os << "called at least " << FormatTimes(min_);
00121 } else {
00122
00123 *os << "called between " << min_ << " and " << max_ << " times";
00124 }
00125 }
00126
00127 }
00128
00129
00130 void Cardinality::DescribeActualCallCountTo(int actual_call_count,
00131 ::std::ostream* os) {
00132 if (actual_call_count > 0) {
00133 *os << "called " << FormatTimes(actual_call_count);
00134 } else {
00135 *os << "never called";
00136 }
00137 }
00138
00139
00140 GTEST_API_ Cardinality AtLeast(int n) { return Between(n, INT_MAX); }
00141
00142
00143 GTEST_API_ Cardinality AtMost(int n) { return Between(0, n); }
00144
00145
00146 GTEST_API_ Cardinality AnyNumber() { return AtLeast(0); }
00147
00148
00149 GTEST_API_ Cardinality Between(int min, int max) {
00150 return Cardinality(new BetweenCardinalityImpl(min, max));
00151 }
00152
00153
00154 GTEST_API_ Cardinality Exactly(int n) { return Between(n, n); }
00155
00156 }