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