Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
michael@0 | 1 | // Copyright 2003, Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | // |
michael@0 | 30 | // Authors: Dan Egnor (egnor@google.com) |
michael@0 | 31 | // Ported to Windows: Vadim Berman (vadimb@google.com) |
michael@0 | 32 | |
michael@0 | 33 | #include "gtest/internal/gtest-linked_ptr.h" |
michael@0 | 34 | |
michael@0 | 35 | #include <stdlib.h> |
michael@0 | 36 | #include "gtest/gtest.h" |
michael@0 | 37 | |
michael@0 | 38 | namespace { |
michael@0 | 39 | |
michael@0 | 40 | using testing::Message; |
michael@0 | 41 | using testing::internal::linked_ptr; |
michael@0 | 42 | |
michael@0 | 43 | int num; |
michael@0 | 44 | Message* history = NULL; |
michael@0 | 45 | |
michael@0 | 46 | // Class which tracks allocation/deallocation |
michael@0 | 47 | class A { |
michael@0 | 48 | public: |
michael@0 | 49 | A(): mynum(num++) { *history << "A" << mynum << " ctor\n"; } |
michael@0 | 50 | virtual ~A() { *history << "A" << mynum << " dtor\n"; } |
michael@0 | 51 | virtual void Use() { *history << "A" << mynum << " use\n"; } |
michael@0 | 52 | protected: |
michael@0 | 53 | int mynum; |
michael@0 | 54 | }; |
michael@0 | 55 | |
michael@0 | 56 | // Subclass |
michael@0 | 57 | class B : public A { |
michael@0 | 58 | public: |
michael@0 | 59 | B() { *history << "B" << mynum << " ctor\n"; } |
michael@0 | 60 | ~B() { *history << "B" << mynum << " dtor\n"; } |
michael@0 | 61 | virtual void Use() { *history << "B" << mynum << " use\n"; } |
michael@0 | 62 | }; |
michael@0 | 63 | |
michael@0 | 64 | class LinkedPtrTest : public testing::Test { |
michael@0 | 65 | public: |
michael@0 | 66 | LinkedPtrTest() { |
michael@0 | 67 | num = 0; |
michael@0 | 68 | history = new Message; |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | virtual ~LinkedPtrTest() { |
michael@0 | 72 | delete history; |
michael@0 | 73 | history = NULL; |
michael@0 | 74 | } |
michael@0 | 75 | }; |
michael@0 | 76 | |
michael@0 | 77 | TEST_F(LinkedPtrTest, GeneralTest) { |
michael@0 | 78 | { |
michael@0 | 79 | linked_ptr<A> a0, a1, a2; |
michael@0 | 80 | // Use explicit function call notation here to suppress self-assign warning. |
michael@0 | 81 | a0.operator=(a0); |
michael@0 | 82 | a1 = a2; |
michael@0 | 83 | ASSERT_EQ(a0.get(), static_cast<A*>(NULL)); |
michael@0 | 84 | ASSERT_EQ(a1.get(), static_cast<A*>(NULL)); |
michael@0 | 85 | ASSERT_EQ(a2.get(), static_cast<A*>(NULL)); |
michael@0 | 86 | ASSERT_TRUE(a0 == NULL); |
michael@0 | 87 | ASSERT_TRUE(a1 == NULL); |
michael@0 | 88 | ASSERT_TRUE(a2 == NULL); |
michael@0 | 89 | |
michael@0 | 90 | { |
michael@0 | 91 | linked_ptr<A> a3(new A); |
michael@0 | 92 | a0 = a3; |
michael@0 | 93 | ASSERT_TRUE(a0 == a3); |
michael@0 | 94 | ASSERT_TRUE(a0 != NULL); |
michael@0 | 95 | ASSERT_TRUE(a0.get() == a3); |
michael@0 | 96 | ASSERT_TRUE(a0 == a3.get()); |
michael@0 | 97 | linked_ptr<A> a4(a0); |
michael@0 | 98 | a1 = a4; |
michael@0 | 99 | linked_ptr<A> a5(new A); |
michael@0 | 100 | ASSERT_TRUE(a5.get() != a3); |
michael@0 | 101 | ASSERT_TRUE(a5 != a3.get()); |
michael@0 | 102 | a2 = a5; |
michael@0 | 103 | linked_ptr<B> b0(new B); |
michael@0 | 104 | linked_ptr<A> a6(b0); |
michael@0 | 105 | ASSERT_TRUE(b0 == a6); |
michael@0 | 106 | ASSERT_TRUE(a6 == b0); |
michael@0 | 107 | ASSERT_TRUE(b0 != NULL); |
michael@0 | 108 | a5 = b0; |
michael@0 | 109 | a5 = b0; |
michael@0 | 110 | a3->Use(); |
michael@0 | 111 | a4->Use(); |
michael@0 | 112 | a5->Use(); |
michael@0 | 113 | a6->Use(); |
michael@0 | 114 | b0->Use(); |
michael@0 | 115 | (*b0).Use(); |
michael@0 | 116 | b0.get()->Use(); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | a0->Use(); |
michael@0 | 120 | a1->Use(); |
michael@0 | 121 | a2->Use(); |
michael@0 | 122 | |
michael@0 | 123 | a1 = a2; |
michael@0 | 124 | a2.reset(new A); |
michael@0 | 125 | a0.reset(); |
michael@0 | 126 | |
michael@0 | 127 | linked_ptr<A> a7; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | ASSERT_STREQ( |
michael@0 | 131 | "A0 ctor\n" |
michael@0 | 132 | "A1 ctor\n" |
michael@0 | 133 | "A2 ctor\n" |
michael@0 | 134 | "B2 ctor\n" |
michael@0 | 135 | "A0 use\n" |
michael@0 | 136 | "A0 use\n" |
michael@0 | 137 | "B2 use\n" |
michael@0 | 138 | "B2 use\n" |
michael@0 | 139 | "B2 use\n" |
michael@0 | 140 | "B2 use\n" |
michael@0 | 141 | "B2 use\n" |
michael@0 | 142 | "B2 dtor\n" |
michael@0 | 143 | "A2 dtor\n" |
michael@0 | 144 | "A0 use\n" |
michael@0 | 145 | "A0 use\n" |
michael@0 | 146 | "A1 use\n" |
michael@0 | 147 | "A3 ctor\n" |
michael@0 | 148 | "A0 dtor\n" |
michael@0 | 149 | "A3 dtor\n" |
michael@0 | 150 | "A1 dtor\n", |
michael@0 | 151 | history->GetString().c_str()); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | } // Unnamed namespace |