Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:19:55

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 tests the built-in cardinalities.
0035 
0036 #include "gmock/gmock.h"
0037 #include "gtest/gtest.h"
0038 #include "gtest/gtest-spi.h"
0039 
0040 namespace {
0041 
0042 using std::stringstream;
0043 using testing::AnyNumber;
0044 using testing::AtLeast;
0045 using testing::AtMost;
0046 using testing::Between;
0047 using testing::Cardinality;
0048 using testing::CardinalityInterface;
0049 using testing::Exactly;
0050 using testing::IsSubstring;
0051 using testing::MakeCardinality;
0052 
0053 class MockFoo {
0054  public:
0055   MockFoo() {}
0056   MOCK_METHOD0(Bar, int());  // NOLINT
0057 
0058  private:
0059   GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
0060 };
0061 
0062 // Tests that Cardinality objects can be default constructed.
0063 TEST(CardinalityTest, IsDefaultConstructable) {
0064   Cardinality c;
0065 }
0066 
0067 // Tests that Cardinality objects are copyable.
0068 TEST(CardinalityTest, IsCopyable) {
0069   // Tests the copy constructor.
0070   Cardinality c = Exactly(1);
0071   EXPECT_FALSE(c.IsSatisfiedByCallCount(0));
0072   EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
0073   EXPECT_TRUE(c.IsSaturatedByCallCount(1));
0074 
0075   // Tests the assignment operator.
0076   c = Exactly(2);
0077   EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
0078   EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
0079   EXPECT_TRUE(c.IsSaturatedByCallCount(2));
0080 }
0081 
0082 TEST(CardinalityTest, IsOverSaturatedByCallCountWorks) {
0083   const Cardinality c = AtMost(5);
0084   EXPECT_FALSE(c.IsOverSaturatedByCallCount(4));
0085   EXPECT_FALSE(c.IsOverSaturatedByCallCount(5));
0086   EXPECT_TRUE(c.IsOverSaturatedByCallCount(6));
0087 }
0088 
0089 // Tests that Cardinality::DescribeActualCallCountTo() creates the
0090 // correct description.
0091 TEST(CardinalityTest, CanDescribeActualCallCount) {
0092   stringstream ss0;
0093   Cardinality::DescribeActualCallCountTo(0, &ss0);
0094   EXPECT_EQ("never called", ss0.str());
0095 
0096   stringstream ss1;
0097   Cardinality::DescribeActualCallCountTo(1, &ss1);
0098   EXPECT_EQ("called once", ss1.str());
0099 
0100   stringstream ss2;
0101   Cardinality::DescribeActualCallCountTo(2, &ss2);
0102   EXPECT_EQ("called twice", ss2.str());
0103 
0104   stringstream ss3;
0105   Cardinality::DescribeActualCallCountTo(3, &ss3);
0106   EXPECT_EQ("called 3 times", ss3.str());
0107 }
0108 
0109 // Tests AnyNumber()
0110 TEST(AnyNumber, Works) {
0111   const Cardinality c = AnyNumber();
0112   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
0113   EXPECT_FALSE(c.IsSaturatedByCallCount(0));
0114 
0115   EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
0116   EXPECT_FALSE(c.IsSaturatedByCallCount(1));
0117 
0118   EXPECT_TRUE(c.IsSatisfiedByCallCount(9));
0119   EXPECT_FALSE(c.IsSaturatedByCallCount(9));
0120 
0121   stringstream ss;
0122   c.DescribeTo(&ss);
0123   EXPECT_PRED_FORMAT2(IsSubstring, "called any number of times",
0124                       ss.str());
0125 }
0126 
0127 TEST(AnyNumberTest, HasCorrectBounds) {
0128   const Cardinality c = AnyNumber();
0129   EXPECT_EQ(0, c.ConservativeLowerBound());
0130   EXPECT_EQ(INT_MAX, c.ConservativeUpperBound());
0131 }
0132 
0133 // Tests AtLeast(n).
0134 
0135 TEST(AtLeastTest, OnNegativeNumber) {
0136   EXPECT_NONFATAL_FAILURE({  // NOLINT
0137     AtLeast(-1);
0138   }, "The invocation lower bound must be >= 0");
0139 }
0140 
0141 TEST(AtLeastTest, OnZero) {
0142   const Cardinality c = AtLeast(0);
0143   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
0144   EXPECT_FALSE(c.IsSaturatedByCallCount(0));
0145 
0146   EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
0147   EXPECT_FALSE(c.IsSaturatedByCallCount(1));
0148 
0149   stringstream ss;
0150   c.DescribeTo(&ss);
0151   EXPECT_PRED_FORMAT2(IsSubstring, "any number of times",
0152                       ss.str());
0153 }
0154 
0155 TEST(AtLeastTest, OnPositiveNumber) {
0156   const Cardinality c = AtLeast(2);
0157   EXPECT_FALSE(c.IsSatisfiedByCallCount(0));
0158   EXPECT_FALSE(c.IsSaturatedByCallCount(0));
0159 
0160   EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
0161   EXPECT_FALSE(c.IsSaturatedByCallCount(1));
0162 
0163   EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
0164   EXPECT_FALSE(c.IsSaturatedByCallCount(2));
0165 
0166   stringstream ss1;
0167   AtLeast(1).DescribeTo(&ss1);
0168   EXPECT_PRED_FORMAT2(IsSubstring, "at least once",
0169                       ss1.str());
0170 
0171   stringstream ss2;
0172   c.DescribeTo(&ss2);
0173   EXPECT_PRED_FORMAT2(IsSubstring, "at least twice",
0174                       ss2.str());
0175 
0176   stringstream ss3;
0177   AtLeast(3).DescribeTo(&ss3);
0178   EXPECT_PRED_FORMAT2(IsSubstring, "at least 3 times",
0179                       ss3.str());
0180 }
0181 
0182 TEST(AtLeastTest, HasCorrectBounds) {
0183   const Cardinality c = AtLeast(2);
0184   EXPECT_EQ(2, c.ConservativeLowerBound());
0185   EXPECT_EQ(INT_MAX, c.ConservativeUpperBound());
0186 }
0187 
0188 // Tests AtMost(n).
0189 
0190 TEST(AtMostTest, OnNegativeNumber) {
0191   EXPECT_NONFATAL_FAILURE({  // NOLINT
0192     AtMost(-1);
0193   }, "The invocation upper bound must be >= 0");
0194 }
0195 
0196 TEST(AtMostTest, OnZero) {
0197   const Cardinality c = AtMost(0);
0198   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
0199   EXPECT_TRUE(c.IsSaturatedByCallCount(0));
0200 
0201   EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
0202   EXPECT_TRUE(c.IsSaturatedByCallCount(1));
0203 
0204   stringstream ss;
0205   c.DescribeTo(&ss);
0206   EXPECT_PRED_FORMAT2(IsSubstring, "never called",
0207                       ss.str());
0208 }
0209 
0210 TEST(AtMostTest, OnPositiveNumber) {
0211   const Cardinality c = AtMost(2);
0212   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
0213   EXPECT_FALSE(c.IsSaturatedByCallCount(0));
0214 
0215   EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
0216   EXPECT_FALSE(c.IsSaturatedByCallCount(1));
0217 
0218   EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
0219   EXPECT_TRUE(c.IsSaturatedByCallCount(2));
0220 
0221   stringstream ss1;
0222   AtMost(1).DescribeTo(&ss1);
0223   EXPECT_PRED_FORMAT2(IsSubstring, "called at most once",
0224                       ss1.str());
0225 
0226   stringstream ss2;
0227   c.DescribeTo(&ss2);
0228   EXPECT_PRED_FORMAT2(IsSubstring, "called at most twice",
0229                       ss2.str());
0230 
0231   stringstream ss3;
0232   AtMost(3).DescribeTo(&ss3);
0233   EXPECT_PRED_FORMAT2(IsSubstring, "called at most 3 times",
0234                       ss3.str());
0235 }
0236 
0237 TEST(AtMostTest, HasCorrectBounds) {
0238   const Cardinality c = AtMost(2);
0239   EXPECT_EQ(0, c.ConservativeLowerBound());
0240   EXPECT_EQ(2, c.ConservativeUpperBound());
0241 }
0242 
0243 // Tests Between(m, n).
0244 
0245 TEST(BetweenTest, OnNegativeStart) {
0246   EXPECT_NONFATAL_FAILURE({  // NOLINT
0247     Between(-1, 2);
0248   }, "The invocation lower bound must be >= 0, but is actually -1");
0249 }
0250 
0251 TEST(BetweenTest, OnNegativeEnd) {
0252   EXPECT_NONFATAL_FAILURE({  // NOLINT
0253     Between(1, -2);
0254   }, "The invocation upper bound must be >= 0, but is actually -2");
0255 }
0256 
0257 TEST(BetweenTest, OnStartBiggerThanEnd) {
0258   EXPECT_NONFATAL_FAILURE({  // NOLINT
0259     Between(2, 1);
0260   }, "The invocation upper bound (1) must be >= "
0261      "the invocation lower bound (2)");
0262 }
0263 
0264 TEST(BetweenTest, OnZeroStartAndZeroEnd) {
0265   const Cardinality c = Between(0, 0);
0266 
0267   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
0268   EXPECT_TRUE(c.IsSaturatedByCallCount(0));
0269 
0270   EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
0271   EXPECT_TRUE(c.IsSaturatedByCallCount(1));
0272 
0273   stringstream ss;
0274   c.DescribeTo(&ss);
0275   EXPECT_PRED_FORMAT2(IsSubstring, "never called",
0276                       ss.str());
0277 }
0278 
0279 TEST(BetweenTest, OnZeroStartAndNonZeroEnd) {
0280   const Cardinality c = Between(0, 2);
0281 
0282   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
0283   EXPECT_FALSE(c.IsSaturatedByCallCount(0));
0284 
0285   EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
0286   EXPECT_TRUE(c.IsSaturatedByCallCount(2));
0287 
0288   EXPECT_FALSE(c.IsSatisfiedByCallCount(4));
0289   EXPECT_TRUE(c.IsSaturatedByCallCount(4));
0290 
0291   stringstream ss;
0292   c.DescribeTo(&ss);
0293   EXPECT_PRED_FORMAT2(IsSubstring, "called at most twice",
0294                       ss.str());
0295 }
0296 
0297 TEST(BetweenTest, OnSameStartAndEnd) {
0298   const Cardinality c = Between(3, 3);
0299 
0300   EXPECT_FALSE(c.IsSatisfiedByCallCount(2));
0301   EXPECT_FALSE(c.IsSaturatedByCallCount(2));
0302 
0303   EXPECT_TRUE(c.IsSatisfiedByCallCount(3));
0304   EXPECT_TRUE(c.IsSaturatedByCallCount(3));
0305 
0306   EXPECT_FALSE(c.IsSatisfiedByCallCount(4));
0307   EXPECT_TRUE(c.IsSaturatedByCallCount(4));
0308 
0309   stringstream ss;
0310   c.DescribeTo(&ss);
0311   EXPECT_PRED_FORMAT2(IsSubstring, "called 3 times",
0312                       ss.str());
0313 }
0314 
0315 TEST(BetweenTest, OnDifferentStartAndEnd) {
0316   const Cardinality c = Between(3, 5);
0317 
0318   EXPECT_FALSE(c.IsSatisfiedByCallCount(2));
0319   EXPECT_FALSE(c.IsSaturatedByCallCount(2));
0320 
0321   EXPECT_TRUE(c.IsSatisfiedByCallCount(3));
0322   EXPECT_FALSE(c.IsSaturatedByCallCount(3));
0323 
0324   EXPECT_TRUE(c.IsSatisfiedByCallCount(5));
0325   EXPECT_TRUE(c.IsSaturatedByCallCount(5));
0326 
0327   EXPECT_FALSE(c.IsSatisfiedByCallCount(6));
0328   EXPECT_TRUE(c.IsSaturatedByCallCount(6));
0329 
0330   stringstream ss;
0331   c.DescribeTo(&ss);
0332   EXPECT_PRED_FORMAT2(IsSubstring, "called between 3 and 5 times",
0333                       ss.str());
0334 }
0335 
0336 TEST(BetweenTest, HasCorrectBounds) {
0337   const Cardinality c = Between(3, 5);
0338   EXPECT_EQ(3, c.ConservativeLowerBound());
0339   EXPECT_EQ(5, c.ConservativeUpperBound());
0340 }
0341 
0342 // Tests Exactly(n).
0343 
0344 TEST(ExactlyTest, OnNegativeNumber) {
0345   EXPECT_NONFATAL_FAILURE({  // NOLINT
0346     Exactly(-1);
0347   }, "The invocation lower bound must be >= 0");
0348 }
0349 
0350 TEST(ExactlyTest, OnZero) {
0351   const Cardinality c = Exactly(0);
0352   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
0353   EXPECT_TRUE(c.IsSaturatedByCallCount(0));
0354 
0355   EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
0356   EXPECT_TRUE(c.IsSaturatedByCallCount(1));
0357 
0358   stringstream ss;
0359   c.DescribeTo(&ss);
0360   EXPECT_PRED_FORMAT2(IsSubstring, "never called",
0361                       ss.str());
0362 }
0363 
0364 TEST(ExactlyTest, OnPositiveNumber) {
0365   const Cardinality c = Exactly(2);
0366   EXPECT_FALSE(c.IsSatisfiedByCallCount(0));
0367   EXPECT_FALSE(c.IsSaturatedByCallCount(0));
0368 
0369   EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
0370   EXPECT_TRUE(c.IsSaturatedByCallCount(2));
0371 
0372   stringstream ss1;
0373   Exactly(1).DescribeTo(&ss1);
0374   EXPECT_PRED_FORMAT2(IsSubstring, "called once",
0375                       ss1.str());
0376 
0377   stringstream ss2;
0378   c.DescribeTo(&ss2);
0379   EXPECT_PRED_FORMAT2(IsSubstring, "called twice",
0380                       ss2.str());
0381 
0382   stringstream ss3;
0383   Exactly(3).DescribeTo(&ss3);
0384   EXPECT_PRED_FORMAT2(IsSubstring, "called 3 times",
0385                       ss3.str());
0386 }
0387 
0388 TEST(ExactlyTest, HasCorrectBounds) {
0389   const Cardinality c = Exactly(3);
0390   EXPECT_EQ(3, c.ConservativeLowerBound());
0391   EXPECT_EQ(3, c.ConservativeUpperBound());
0392 }
0393 
0394 // Tests that a user can make his own cardinality by implementing
0395 // CardinalityInterface and calling MakeCardinality().
0396 
0397 class EvenCardinality : public CardinalityInterface {
0398  public:
0399   // Returns true iff call_count calls will satisfy this cardinality.
0400   virtual bool IsSatisfiedByCallCount(int call_count) const {
0401     return (call_count % 2 == 0);
0402   }
0403 
0404   // Returns true iff call_count calls will saturate this cardinality.
0405   virtual bool IsSaturatedByCallCount(int /* call_count */) const {
0406     return false;
0407   }
0408 
0409   // Describes self to an ostream.
0410   virtual void DescribeTo(::std::ostream* ss) const {
0411     *ss << "called even number of times";
0412   }
0413 };
0414 
0415 TEST(MakeCardinalityTest, ConstructsCardinalityFromInterface) {
0416   const Cardinality c = MakeCardinality(new EvenCardinality);
0417 
0418   EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
0419   EXPECT_FALSE(c.IsSatisfiedByCallCount(3));
0420 
0421   EXPECT_FALSE(c.IsSaturatedByCallCount(10000));
0422 
0423   stringstream ss;
0424   c.DescribeTo(&ss);
0425   EXPECT_EQ("called even number of times", ss.str());
0426 }
0427 
0428 }  // Unnamed namespace