Blame googlemock/src/gmock-cardinalities.cc

Packit bd1cd8
// Copyright 2007, Google Inc.
Packit bd1cd8
// All rights reserved.
Packit bd1cd8
//
Packit bd1cd8
// Redistribution and use in source and binary forms, with or without
Packit bd1cd8
// modification, are permitted provided that the following conditions are
Packit bd1cd8
// met:
Packit bd1cd8
//
Packit bd1cd8
//     * Redistributions of source code must retain the above copyright
Packit bd1cd8
// notice, this list of conditions and the following disclaimer.
Packit bd1cd8
//     * Redistributions in binary form must reproduce the above
Packit bd1cd8
// copyright notice, this list of conditions and the following disclaimer
Packit bd1cd8
// in the documentation and/or other materials provided with the
Packit bd1cd8
// distribution.
Packit bd1cd8
//     * Neither the name of Google Inc. nor the names of its
Packit bd1cd8
// contributors may be used to endorse or promote products derived from
Packit bd1cd8
// this software without specific prior written permission.
Packit bd1cd8
//
Packit bd1cd8
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit bd1cd8
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit bd1cd8
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit bd1cd8
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit bd1cd8
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit bd1cd8
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit bd1cd8
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit bd1cd8
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit bd1cd8
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit bd1cd8
//
Packit bd1cd8
// Author: wan@google.com (Zhanyong Wan)
Packit bd1cd8
Packit bd1cd8
// Google Mock - a framework for writing C++ mock classes.
Packit bd1cd8
//
Packit bd1cd8
// This file implements cardinalities.
Packit bd1cd8
Packit bd1cd8
#include "gmock/gmock-cardinalities.h"
Packit bd1cd8
Packit bd1cd8
#include <limits.h>
Packit bd1cd8
#include <ostream>  // NOLINT
Packit bd1cd8
#include <sstream>
Packit bd1cd8
#include <string>
Packit bd1cd8
#include "gmock/internal/gmock-internal-utils.h"
Packit bd1cd8
#include "gtest/gtest.h"
Packit bd1cd8
Packit bd1cd8
namespace testing {
Packit bd1cd8
Packit bd1cd8
namespace {
Packit bd1cd8
Packit bd1cd8
// Implements the Between(m, n) cardinality.
Packit bd1cd8
class BetweenCardinalityImpl : public CardinalityInterface {
Packit bd1cd8
 public:
Packit bd1cd8
  BetweenCardinalityImpl(int min, int max)
Packit bd1cd8
      : min_(min >= 0 ? min : 0),
Packit bd1cd8
        max_(max >= min_ ? max : min_) {
Packit bd1cd8
    std::stringstream ss;
Packit bd1cd8
    if (min < 0) {
Packit bd1cd8
      ss << "The invocation lower bound must be >= 0, "
Packit bd1cd8
         << "but is actually " << min << ".";
Packit bd1cd8
      internal::Expect(false, __FILE__, __LINE__, ss.str());
Packit bd1cd8
    } else if (max < 0) {
Packit bd1cd8
      ss << "The invocation upper bound must be >= 0, "
Packit bd1cd8
         << "but is actually " << max << ".";
Packit bd1cd8
      internal::Expect(false, __FILE__, __LINE__, ss.str());
Packit bd1cd8
    } else if (min > max) {
Packit bd1cd8
      ss << "The invocation upper bound (" << max
Packit bd1cd8
         << ") must be >= the invocation lower bound (" << min
Packit bd1cd8
         << ").";
Packit bd1cd8
      internal::Expect(false, __FILE__, __LINE__, ss.str());
Packit bd1cd8
    }
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Conservative estimate on the lower/upper bound of the number of
Packit bd1cd8
  // calls allowed.
Packit bd1cd8
  virtual int ConservativeLowerBound() const { return min_; }
Packit bd1cd8
  virtual int ConservativeUpperBound() const { return max_; }
Packit bd1cd8
Packit bd1cd8
  virtual bool IsSatisfiedByCallCount(int call_count) const {
Packit bd1cd8
    return min_ <= call_count && call_count <= max_;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  virtual bool IsSaturatedByCallCount(int call_count) const {
Packit bd1cd8
    return call_count >= max_;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  virtual void DescribeTo(::std::ostream* os) const;
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  const int min_;
Packit bd1cd8
  const int max_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_COPY_AND_ASSIGN_(BetweenCardinalityImpl);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Formats "n times" in a human-friendly way.
Packit bd1cd8
inline internal::string FormatTimes(int n) {
Packit bd1cd8
  if (n == 1) {
Packit bd1cd8
    return "once";
Packit bd1cd8
  } else if (n == 2) {
Packit bd1cd8
    return "twice";
Packit bd1cd8
  } else {
Packit bd1cd8
    std::stringstream ss;
Packit bd1cd8
    ss << n << " times";
Packit bd1cd8
    return ss.str();
Packit bd1cd8
  }
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Describes the Between(m, n) cardinality in human-friendly text.
Packit bd1cd8
void BetweenCardinalityImpl::DescribeTo(::std::ostream* os) const {
Packit bd1cd8
  if (min_ == 0) {
Packit bd1cd8
    if (max_ == 0) {
Packit bd1cd8
      *os << "never called";
Packit bd1cd8
    } else if (max_ == INT_MAX) {
Packit bd1cd8
      *os << "called any number of times";
Packit bd1cd8
    } else {
Packit bd1cd8
      *os << "called at most " << FormatTimes(max_);
Packit bd1cd8
    }
Packit bd1cd8
  } else if (min_ == max_) {
Packit bd1cd8
    *os << "called " << FormatTimes(min_);
Packit bd1cd8
  } else if (max_ == INT_MAX) {
Packit bd1cd8
    *os << "called at least " << FormatTimes(min_);
Packit bd1cd8
  } else {
Packit bd1cd8
    // 0 < min_ < max_ < INT_MAX
Packit bd1cd8
    *os << "called between " << min_ << " and " << max_ << " times";
Packit bd1cd8
  }
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
}  // Unnamed namespace
Packit bd1cd8
Packit bd1cd8
// Describes the given call count to an ostream.
Packit bd1cd8
void Cardinality::DescribeActualCallCountTo(int actual_call_count,
Packit bd1cd8
                                            ::std::ostream* os) {
Packit bd1cd8
  if (actual_call_count > 0) {
Packit bd1cd8
    *os << "called " << FormatTimes(actual_call_count);
Packit bd1cd8
  } else {
Packit bd1cd8
    *os << "never called";
Packit bd1cd8
  }
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Creates a cardinality that allows at least n calls.
Packit bd1cd8
GTEST_API_ Cardinality AtLeast(int n) { return Between(n, INT_MAX); }
Packit bd1cd8
Packit bd1cd8
// Creates a cardinality that allows at most n calls.
Packit bd1cd8
GTEST_API_ Cardinality AtMost(int n) { return Between(0, n); }
Packit bd1cd8
Packit bd1cd8
// Creates a cardinality that allows any number of calls.
Packit bd1cd8
GTEST_API_ Cardinality AnyNumber() { return AtLeast(0); }
Packit bd1cd8
Packit bd1cd8
// Creates a cardinality that allows between min and max calls.
Packit bd1cd8
GTEST_API_ Cardinality Between(int min, int max) {
Packit bd1cd8
  return Cardinality(new BetweenCardinalityImpl(min, max));
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Creates a cardinality that allows exactly n calls.
Packit bd1cd8
GTEST_API_ Cardinality Exactly(int n) { return Between(n, n); }
Packit bd1cd8
Packit bd1cd8
}  // namespace testing