Path: blob/main/contrib/googletest/googlemock/src/gmock-cardinalities.cc
48255 views
// Copyright 2007, Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above10// copyright notice, this list of conditions and the following disclaimer11// in the documentation and/or other materials provided with the12// distribution.13// * Neither the name of Google Inc. nor the names of its14// contributors may be used to endorse or promote products derived from15// this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2829// Google Mock - a framework for writing C++ mock classes.30//31// This file implements cardinalities.3233#include "gmock/gmock-cardinalities.h"3435#include <limits.h>3637#include <ostream> // NOLINT38#include <sstream>39#include <string>4041#include "gmock/internal/gmock-internal-utils.h"42#include "gtest/gtest.h"4344namespace testing {4546namespace {4748// Implements the Between(m, n) cardinality.49class BetweenCardinalityImpl : public CardinalityInterface {50public:51BetweenCardinalityImpl(int min, int max)52: min_(min >= 0 ? min : 0), max_(max >= min_ ? max : min_) {53std::stringstream ss;54if (min < 0) {55ss << "The invocation lower bound must be >= 0, "56<< "but is actually " << min << ".";57internal::Expect(false, __FILE__, __LINE__, ss.str());58} else if (max < 0) {59ss << "The invocation upper bound must be >= 0, "60<< "but is actually " << max << ".";61internal::Expect(false, __FILE__, __LINE__, ss.str());62} else if (min > max) {63ss << "The invocation upper bound (" << max64<< ") must be >= the invocation lower bound (" << min << ").";65internal::Expect(false, __FILE__, __LINE__, ss.str());66}67}6869// Conservative estimate on the lower/upper bound of the number of70// calls allowed.71int ConservativeLowerBound() const override { return min_; }72int ConservativeUpperBound() const override { return max_; }7374bool IsSatisfiedByCallCount(int call_count) const override {75return min_ <= call_count && call_count <= max_;76}7778bool IsSaturatedByCallCount(int call_count) const override {79return call_count >= max_;80}8182void DescribeTo(::std::ostream* os) const override;8384private:85const int min_;86const int max_;8788BetweenCardinalityImpl(const BetweenCardinalityImpl&) = delete;89BetweenCardinalityImpl& operator=(const BetweenCardinalityImpl&) = delete;90};9192// Formats "n times" in a human-friendly way.93inline std::string FormatTimes(int n) {94if (n == 1) {95return "once";96} else if (n == 2) {97return "twice";98} else {99std::stringstream ss;100ss << n << " times";101return ss.str();102}103}104105// Describes the Between(m, n) cardinality in human-friendly text.106void BetweenCardinalityImpl::DescribeTo(::std::ostream* os) const {107if (min_ == 0) {108if (max_ == 0) {109*os << "never called";110} else if (max_ == INT_MAX) {111*os << "called any number of times";112} else {113*os << "called at most " << FormatTimes(max_);114}115} else if (min_ == max_) {116*os << "called " << FormatTimes(min_);117} else if (max_ == INT_MAX) {118*os << "called at least " << FormatTimes(min_);119} else {120// 0 < min_ < max_ < INT_MAX121*os << "called between " << min_ << " and " << max_ << " times";122}123}124125} // Unnamed namespace126127// Describes the given call count to an ostream.128void Cardinality::DescribeActualCallCountTo(int actual_call_count,129::std::ostream* os) {130if (actual_call_count > 0) {131*os << "called " << FormatTimes(actual_call_count);132} else {133*os << "never called";134}135}136137// Creates a cardinality that allows at least n calls.138GTEST_API_ Cardinality AtLeast(int n) { return Between(n, INT_MAX); }139140// Creates a cardinality that allows at most n calls.141GTEST_API_ Cardinality AtMost(int n) { return Between(0, n); }142143// Creates a cardinality that allows any number of calls.144GTEST_API_ Cardinality AnyNumber() { return AtLeast(0); }145146// Creates a cardinality that allows between min and max calls.147GTEST_API_ Cardinality Between(int min, int max) {148return Cardinality(new BetweenCardinalityImpl(min, max));149}150151// Creates a cardinality that allows exactly n calls.152GTEST_API_ Cardinality Exactly(int n) { return Between(n, n); }153154} // namespace testing155156157