Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 // Copyright 2008, 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 // Tests Google Mock's output in various scenarios.  This ensures that
0033 // Google Mock's messages are readable and useful.
0034 
0035 #include "gmock/gmock.h"
0036 
0037 #include <stdio.h>
0038 #include <string>
0039 
0040 #include "gtest/gtest.h"
0041 
0042 using testing::_;
0043 using testing::AnyNumber;
0044 using testing::Ge;
0045 using testing::InSequence;
0046 using testing::NaggyMock;
0047 using testing::Ref;
0048 using testing::Return;
0049 using testing::Sequence;
0050 
0051 class MockFoo {
0052  public:
0053   MockFoo() {}
0054 
0055   MOCK_METHOD3(Bar, char(const std::string& s, int i, double x));
0056   MOCK_METHOD2(Bar2, bool(int x, int y));
0057   MOCK_METHOD2(Bar3, void(int x, int y));
0058 
0059  private:
0060   GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
0061 };
0062 
0063 class GMockOutputTest : public testing::Test {
0064  protected:
0065   NaggyMock<MockFoo> foo_;
0066 };
0067 
0068 TEST_F(GMockOutputTest, ExpectedCall) {
0069   testing::GMOCK_FLAG(verbose) = "info";
0070 
0071   EXPECT_CALL(foo_, Bar2(0, _));
0072   foo_.Bar2(0, 0);  // Expected call
0073 
0074   testing::GMOCK_FLAG(verbose) = "warning";
0075 }
0076 
0077 TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) {
0078   testing::GMOCK_FLAG(verbose) = "info";
0079 
0080   EXPECT_CALL(foo_, Bar3(0, _));
0081   foo_.Bar3(0, 0);  // Expected call
0082 
0083   testing::GMOCK_FLAG(verbose) = "warning";
0084 }
0085 
0086 TEST_F(GMockOutputTest, ExplicitActionsRunOut) {
0087   EXPECT_CALL(foo_, Bar2(_, _))
0088       .Times(2)
0089       .WillOnce(Return(false));
0090   foo_.Bar2(2, 2);
0091   foo_.Bar2(1, 1);  // Explicit actions in EXPECT_CALL run out.
0092 }
0093 
0094 TEST_F(GMockOutputTest, UnexpectedCall) {
0095   EXPECT_CALL(foo_, Bar2(0, _));
0096 
0097   foo_.Bar2(1, 0);  // Unexpected call
0098   foo_.Bar2(0, 0);  // Expected call
0099 }
0100 
0101 TEST_F(GMockOutputTest, UnexpectedCallToVoidFunction) {
0102   EXPECT_CALL(foo_, Bar3(0, _));
0103 
0104   foo_.Bar3(1, 0);  // Unexpected call
0105   foo_.Bar3(0, 0);  // Expected call
0106 }
0107 
0108 TEST_F(GMockOutputTest, ExcessiveCall) {
0109   EXPECT_CALL(foo_, Bar2(0, _));
0110 
0111   foo_.Bar2(0, 0);  // Expected call
0112   foo_.Bar2(0, 1);  // Excessive call
0113 }
0114 
0115 TEST_F(GMockOutputTest, ExcessiveCallToVoidFunction) {
0116   EXPECT_CALL(foo_, Bar3(0, _));
0117 
0118   foo_.Bar3(0, 0);  // Expected call
0119   foo_.Bar3(0, 1);  // Excessive call
0120 }
0121 
0122 TEST_F(GMockOutputTest, UninterestingCall) {
0123   foo_.Bar2(0, 1);  // Uninteresting call
0124 }
0125 
0126 TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) {
0127   foo_.Bar3(0, 1);  // Uninteresting call
0128 }
0129 
0130 TEST_F(GMockOutputTest, RetiredExpectation) {
0131   EXPECT_CALL(foo_, Bar2(_, _))
0132       .RetiresOnSaturation();
0133   EXPECT_CALL(foo_, Bar2(0, 0));
0134 
0135   foo_.Bar2(1, 1);
0136   foo_.Bar2(1, 1);  // Matches a retired expectation
0137   foo_.Bar2(0, 0);
0138 }
0139 
0140 TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) {
0141   {
0142     InSequence s;
0143     EXPECT_CALL(foo_, Bar(_, 0, _));
0144     EXPECT_CALL(foo_, Bar2(0, 0));
0145     EXPECT_CALL(foo_, Bar2(1, _));
0146   }
0147 
0148   foo_.Bar2(1, 0);  // Has one immediate unsatisfied pre-requisite
0149   foo_.Bar("Hi", 0, 0);
0150   foo_.Bar2(0, 0);
0151   foo_.Bar2(1, 0);
0152 }
0153 
0154 TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) {
0155   Sequence s1, s2;
0156 
0157   EXPECT_CALL(foo_, Bar(_, 0, _))
0158       .InSequence(s1);
0159   EXPECT_CALL(foo_, Bar2(0, 0))
0160       .InSequence(s2);
0161   EXPECT_CALL(foo_, Bar2(1, _))
0162       .InSequence(s1, s2);
0163 
0164   foo_.Bar2(1, 0);  // Has two immediate unsatisfied pre-requisites
0165   foo_.Bar("Hi", 0, 0);
0166   foo_.Bar2(0, 0);
0167   foo_.Bar2(1, 0);
0168 }
0169 
0170 TEST_F(GMockOutputTest, UnsatisfiedWith) {
0171   EXPECT_CALL(foo_, Bar2(_, _)).With(Ge());
0172 }
0173 
0174 TEST_F(GMockOutputTest, UnsatisfiedExpectation) {
0175   EXPECT_CALL(foo_, Bar(_, _, _));
0176   EXPECT_CALL(foo_, Bar2(0, _))
0177       .Times(2);
0178 
0179   foo_.Bar2(0, 1);
0180 }
0181 
0182 TEST_F(GMockOutputTest, MismatchArguments) {
0183   const std::string s = "Hi";
0184   EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0)));
0185 
0186   foo_.Bar("Ho", 0, -0.1);  // Mismatch arguments
0187   foo_.Bar(s, 0, 0);
0188 }
0189 
0190 TEST_F(GMockOutputTest, MismatchWith) {
0191   EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
0192       .With(Ge());
0193 
0194   foo_.Bar2(2, 3);  // Mismatch With()
0195   foo_.Bar2(2, 1);
0196 }
0197 
0198 TEST_F(GMockOutputTest, MismatchArgumentsAndWith) {
0199   EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
0200       .With(Ge());
0201 
0202   foo_.Bar2(1, 3);  // Mismatch arguments and mismatch With()
0203   foo_.Bar2(2, 1);
0204 }
0205 
0206 TEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) {
0207   ON_CALL(foo_, Bar2(_, _))
0208       .WillByDefault(Return(true));   // Default action #1
0209   ON_CALL(foo_, Bar2(1, _))
0210       .WillByDefault(Return(false));  // Default action #2
0211 
0212   EXPECT_CALL(foo_, Bar2(2, 2));
0213   foo_.Bar2(1, 0);  // Unexpected call, takes default action #2.
0214   foo_.Bar2(0, 0);  // Unexpected call, takes default action #1.
0215   foo_.Bar2(2, 2);  // Expected call.
0216 }
0217 
0218 TEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) {
0219   ON_CALL(foo_, Bar2(_, _))
0220       .WillByDefault(Return(true));   // Default action #1
0221   ON_CALL(foo_, Bar2(1, _))
0222       .WillByDefault(Return(false));  // Default action #2
0223 
0224   EXPECT_CALL(foo_, Bar2(2, 2));
0225   EXPECT_CALL(foo_, Bar2(1, 1));
0226 
0227   foo_.Bar2(2, 2);  // Expected call.
0228   foo_.Bar2(2, 2);  // Excessive call, takes default action #1.
0229   foo_.Bar2(1, 1);  // Expected call.
0230   foo_.Bar2(1, 1);  // Excessive call, takes default action #2.
0231 }
0232 
0233 TEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) {
0234   ON_CALL(foo_, Bar2(_, _))
0235       .WillByDefault(Return(true));   // Default action #1
0236   ON_CALL(foo_, Bar2(1, _))
0237       .WillByDefault(Return(false));  // Default action #2
0238 
0239   foo_.Bar2(2, 2);  // Uninteresting call, takes default action #1.
0240   foo_.Bar2(1, 1);  // Uninteresting call, takes default action #2.
0241 }
0242 
0243 TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) {
0244   ON_CALL(foo_, Bar2(_, _))
0245       .WillByDefault(Return(true));   // Default action #1
0246 
0247   EXPECT_CALL(foo_, Bar2(_, _))
0248       .Times(2)
0249       .WillOnce(Return(false));
0250   foo_.Bar2(2, 2);
0251   foo_.Bar2(1, 1);  // Explicit actions in EXPECT_CALL run out.
0252 }
0253 
0254 TEST_F(GMockOutputTest, CatchesLeakedMocks) {
0255   MockFoo* foo1 = new MockFoo;
0256   MockFoo* foo2 = new MockFoo;
0257 
0258   // Invokes ON_CALL on foo1.
0259   ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a'));
0260 
0261   // Invokes EXPECT_CALL on foo2.
0262   EXPECT_CALL(*foo2, Bar2(_, _));
0263   EXPECT_CALL(*foo2, Bar2(1, _));
0264   EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber());
0265   foo2->Bar2(2, 1);
0266   foo2->Bar2(1, 1);
0267 
0268   // Both foo1 and foo2 are deliberately leaked.
0269 }
0270 
0271 void TestCatchesLeakedMocksInAdHocTests() {
0272   MockFoo* foo = new MockFoo;
0273 
0274   // Invokes EXPECT_CALL on foo.
0275   EXPECT_CALL(*foo, Bar2(_, _));
0276   foo->Bar2(2, 1);
0277 
0278   // foo is deliberately leaked.
0279 }
0280 
0281 int main(int argc, char **argv) {
0282   testing::InitGoogleMock(&argc, argv);
0283 
0284   // Ensures that the tests pass no matter what value of
0285   // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
0286   testing::GMOCK_FLAG(catch_leaked_mocks) = true;
0287   testing::GMOCK_FLAG(verbose) = "warning";
0288 
0289   TestCatchesLeakedMocksInAdHocTests();
0290   return RUN_ALL_TESTS();
0291 }