gmock-cardinalities.cc
Go to the documentation of this file.
00001 // Copyright 2007, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 
00032 // Google Mock - a framework for writing C++ mock classes.
00033 //
00034 // This file implements cardinalities.
00035 
00036 #include "gmock/gmock-cardinalities.h"
00037 
00038 #include <limits.h>
00039 #include <ostream>  // NOLINT
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 // Implements the Between(m, n) cardinality.
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   // Conservative estimate on the lower/upper bound of the number of
00073   // calls allowed.
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 // Formats "n times" in a human-friendly way.
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 // Describes the Between(m, n) cardinality in human-friendly text.
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     // 0 < min_ < max_ < INT_MAX
00123     *os << "called between " << min_ << " and " << max_ << " times";
00124   }
00125 }
00126 
00127 }  // Unnamed namespace
00128 
00129 // Describes the given call count to an ostream.
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 // Creates a cardinality that allows at least n calls.
00140 GTEST_API_ Cardinality AtLeast(int n) { return Between(n, INT_MAX); }
00141 
00142 // Creates a cardinality that allows at most n calls.
00143 GTEST_API_ Cardinality AtMost(int n) { return Between(0, n); }
00144 
00145 // Creates a cardinality that allows any number of calls.
00146 GTEST_API_ Cardinality AnyNumber() { return AtLeast(0); }
00147 
00148 // Creates a cardinality that allows between min and max calls.
00149 GTEST_API_ Cardinality Between(int min, int max) {
00150   return Cardinality(new BetweenCardinalityImpl(min, max));
00151 }
00152 
00153 // Creates a cardinality that allows exactly n calls.
00154 GTEST_API_ Cardinality Exactly(int n) { return Between(n, n); }
00155 
00156 }  // namespace testing


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:40