gmock-cardinalities.cc 5.2 KB

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