• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     a0 = a0;
81     a1 = a2;
82     ASSERT_EQ(a0.get(), static_cast<A*>(NULL));
83     ASSERT_EQ(a1.get(), static_cast<A*>(NULL));
84     ASSERT_EQ(a2.get(), static_cast<A*>(NULL));
85     ASSERT_TRUE(a0 == NULL);
86     ASSERT_TRUE(a1 == NULL);
87     ASSERT_TRUE(a2 == NULL);
88 
89     {
90       linked_ptr<A> a3(new A);
91       a0 = a3;
92       ASSERT_TRUE(a0 == a3);
93       ASSERT_TRUE(a0 != NULL);
94       ASSERT_TRUE(a0.get() == a3);
95       ASSERT_TRUE(a0 == a3.get());
96       linked_ptr<A> a4(a0);
97       a1 = a4;
98       linked_ptr<A> a5(new A);
99       ASSERT_TRUE(a5.get() != a3);
100       ASSERT_TRUE(a5 != a3.get());
101       a2 = a5;
102       linked_ptr<B> b0(new B);
103       linked_ptr<A> a6(b0);
104       ASSERT_TRUE(b0 == a6);
105       ASSERT_TRUE(a6 == b0);
106       ASSERT_TRUE(b0 != NULL);
107       a5 = b0;
108       a5 = b0;
109       a3->Use();
110       a4->Use();
111       a5->Use();
112       a6->Use();
113       b0->Use();
114       (*b0).Use();
115       b0.get()->Use();
116     }
117 
118     a0->Use();
119     a1->Use();
120     a2->Use();
121 
122     a1 = a2;
123     a2.reset(new A);
124     a0.reset();
125 
126     linked_ptr<A> a7;
127   }
128 
129   ASSERT_STREQ(
130     "A0 ctor\n"
131     "A1 ctor\n"
132     "A2 ctor\n"
133     "B2 ctor\n"
134     "A0 use\n"
135     "A0 use\n"
136     "B2 use\n"
137     "B2 use\n"
138     "B2 use\n"
139     "B2 use\n"
140     "B2 use\n"
141     "B2 dtor\n"
142     "A2 dtor\n"
143     "A0 use\n"
144     "A0 use\n"
145     "A1 use\n"
146     "A3 ctor\n"
147     "A0 dtor\n"
148     "A3 dtor\n"
149     "A1 dtor\n",
150     history->GetString().c_str()
151   );
152 }
153 
154 }  // Unnamed namespace
155