1
// Copyright 2007, Google Inc.
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
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
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.
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.
30
// Author: wan@google.com (Zhanyong Wan)
32
// Google Mock - a framework for writing C++ mock classes.
34
// This file implements cardinalities.
36
#include "gmock/gmock-cardinalities.h"
39
#include <ostream> // NOLINT
42
#include "gmock/internal/gmock-internal-utils.h"
43
#include "gtest/gtest.h"
49
// Implements the Between(m, n) cardinality.
50
class BetweenCardinalityImpl : public CardinalityInterface {
52
BetweenCardinalityImpl(int min, int max)
53
: min_(min >= 0 ? min : 0),
54
max_(max >= min_ ? max : min_) {
57
ss << "The invocation lower bound must be >= 0, "
58
<< "but is actually " << min << ".";
59
internal::Expect(false, __FILE__, __LINE__, ss.str());
61
ss << "The invocation upper bound must be >= 0, "
62
<< "but is actually " << max << ".";
63
internal::Expect(false, __FILE__, __LINE__, ss.str());
64
} else if (min > max) {
65
ss << "The invocation upper bound (" << max
66
<< ") must be >= the invocation lower bound (" << min
68
internal::Expect(false, __FILE__, __LINE__, ss.str());
72
// Conservative estimate on the lower/upper bound of the number of
74
virtual int ConservativeLowerBound() const { return min_; }
75
virtual int ConservativeUpperBound() const { return max_; }
77
virtual bool IsSatisfiedByCallCount(int call_count) const {
78
return min_ <= call_count && call_count <= max_;
81
virtual bool IsSaturatedByCallCount(int call_count) const {
82
return call_count >= max_;
85
virtual void DescribeTo(::std::ostream* os) const;
91
GTEST_DISALLOW_COPY_AND_ASSIGN_(BetweenCardinalityImpl);
94
// Formats "n times" in a human-friendly way.
95
inline internal::string FormatTimes(int n) {
101
std::stringstream ss;
107
// Describes the Between(m, n) cardinality in human-friendly text.
108
void BetweenCardinalityImpl::DescribeTo(::std::ostream* os) const {
111
*os << "never called";
112
} else if (max_ == INT_MAX) {
113
*os << "called any number of times";
115
*os << "called at most " << FormatTimes(max_);
117
} else if (min_ == max_) {
118
*os << "called " << FormatTimes(min_);
119
} else if (max_ == INT_MAX) {
120
*os << "called at least " << FormatTimes(min_);
122
// 0 < min_ < max_ < INT_MAX
123
*os << "called between " << min_ << " and " << max_ << " times";
127
} // Unnamed namespace
129
// Describes the given call count to an ostream.
130
void Cardinality::DescribeActualCallCountTo(int actual_call_count,
131
::std::ostream* os) {
132
if (actual_call_count > 0) {
133
*os << "called " << FormatTimes(actual_call_count);
135
*os << "never called";
139
// Creates a cardinality that allows at least n calls.
140
GTEST_API_ Cardinality AtLeast(int n) { return Between(n, INT_MAX); }
142
// Creates a cardinality that allows at most n calls.
143
GTEST_API_ Cardinality AtMost(int n) { return Between(0, n); }
145
// Creates a cardinality that allows any number of calls.
146
GTEST_API_ Cardinality AnyNumber() { return AtLeast(0); }
148
// Creates a cardinality that allows between min and max calls.
149
GTEST_API_ Cardinality Between(int min, int max) {
150
return Cardinality(new BetweenCardinalityImpl(min, max));
153
// Creates a cardinality that allows exactly n calls.
154
GTEST_API_ Cardinality Exactly(int n) { return Between(n, n); }
156
} // namespace testing