Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-07 08:19:49

0001 // Copyright 2007, Google Inc.
0002 // All rights reserved.
0003 //
0004 // Redistribution and use in source and binary forms, with or without
0005 // modification, are permitted provided that the following conditions are
0006 // met:
0007 //
0008 //     * Redistributions of source code must retain the above copyright
0009 // notice, this list of conditions and the following disclaimer.
0010 //     * Redistributions in binary form must reproduce the above
0011 // copyright notice, this list of conditions and the following disclaimer
0012 // in the documentation and/or other materials provided with the
0013 // distribution.
0014 //     * Neither the name of Google Inc. nor the names of its
0015 // contributors may be used to endorse or promote products derived from
0016 // this software without specific prior written permission.
0017 //
0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0029 //
0030 // Author: wan@google.com (Zhanyong Wan)
0031 
0032 // Google Mock - a framework for writing C++ mock classes.
0033 //
0034 // This file implements some commonly used cardinalities.  More
0035 // cardinalities can be defined by the user implementing the
0036 // CardinalityInterface interface if necessary.
0037 
0038 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_
0039 #define GMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_
0040 
0041 #include <limits.h>
0042 #include <ostream>  // NOLINT
0043 #include "gmock/internal/gmock-port.h"
0044 #include "gtest/gtest.h"
0045 
0046 namespace testing {
0047 
0048 // To implement a cardinality Foo, define:
0049 //   1. a class FooCardinality that implements the
0050 //      CardinalityInterface interface, and
0051 //   2. a factory function that creates a Cardinality object from a
0052 //      const FooCardinality*.
0053 //
0054 // The two-level delegation design follows that of Matcher, providing
0055 // consistency for extension developers.  It also eases ownership
0056 // management as Cardinality objects can now be copied like plain values.
0057 
0058 // The implementation of a cardinality.
0059 class CardinalityInterface {
0060  public:
0061   virtual ~CardinalityInterface() {}
0062 
0063   // Conservative estimate on the lower/upper bound of the number of
0064   // calls allowed.
0065   virtual int ConservativeLowerBound() const { return 0; }
0066   virtual int ConservativeUpperBound() const { return INT_MAX; }
0067 
0068   // Returns true iff call_count calls will satisfy this cardinality.
0069   virtual bool IsSatisfiedByCallCount(int call_count) const = 0;
0070 
0071   // Returns true iff call_count calls will saturate this cardinality.
0072   virtual bool IsSaturatedByCallCount(int call_count) const = 0;
0073 
0074   // Describes self to an ostream.
0075   virtual void DescribeTo(::std::ostream* os) const = 0;
0076 };
0077 
0078 // A Cardinality is a copyable and IMMUTABLE (except by assignment)
0079 // object that specifies how many times a mock function is expected to
0080 // be called.  The implementation of Cardinality is just a linked_ptr
0081 // to const CardinalityInterface, so copying is fairly cheap.
0082 // Don't inherit from Cardinality!
0083 class GTEST_API_ Cardinality {
0084  public:
0085   // Constructs a null cardinality.  Needed for storing Cardinality
0086   // objects in STL containers.
0087   Cardinality() {}
0088 
0089   // Constructs a Cardinality from its implementation.
0090   explicit Cardinality(const CardinalityInterface* impl) : impl_(impl) {}
0091 
0092   // Conservative estimate on the lower/upper bound of the number of
0093   // calls allowed.
0094   int ConservativeLowerBound() const { return impl_->ConservativeLowerBound(); }
0095   int ConservativeUpperBound() const { return impl_->ConservativeUpperBound(); }
0096 
0097   // Returns true iff call_count calls will satisfy this cardinality.
0098   bool IsSatisfiedByCallCount(int call_count) const {
0099     return impl_->IsSatisfiedByCallCount(call_count);
0100   }
0101 
0102   // Returns true iff call_count calls will saturate this cardinality.
0103   bool IsSaturatedByCallCount(int call_count) const {
0104     return impl_->IsSaturatedByCallCount(call_count);
0105   }
0106 
0107   // Returns true iff call_count calls will over-saturate this
0108   // cardinality, i.e. exceed the maximum number of allowed calls.
0109   bool IsOverSaturatedByCallCount(int call_count) const {
0110     return impl_->IsSaturatedByCallCount(call_count) &&
0111         !impl_->IsSatisfiedByCallCount(call_count);
0112   }
0113 
0114   // Describes self to an ostream
0115   void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); }
0116 
0117   // Describes the given actual call count to an ostream.
0118   static void DescribeActualCallCountTo(int actual_call_count,
0119                                         ::std::ostream* os);
0120 
0121  private:
0122   internal::linked_ptr<const CardinalityInterface> impl_;
0123 };
0124 
0125 // Creates a cardinality that allows at least n calls.
0126 GTEST_API_ Cardinality AtLeast(int n);
0127 
0128 // Creates a cardinality that allows at most n calls.
0129 GTEST_API_ Cardinality AtMost(int n);
0130 
0131 // Creates a cardinality that allows any number of calls.
0132 GTEST_API_ Cardinality AnyNumber();
0133 
0134 // Creates a cardinality that allows between min and max calls.
0135 GTEST_API_ Cardinality Between(int min, int max);
0136 
0137 // Creates a cardinality that allows exactly n calls.
0138 GTEST_API_ Cardinality Exactly(int n);
0139 
0140 // Creates a cardinality from its implementation.
0141 inline Cardinality MakeCardinality(const CardinalityInterface* c) {
0142   return Cardinality(c);
0143 }
0144 
0145 }  // namespace testing
0146 
0147 #endif  // GMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_