1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/memory/linked_ptr.h"
8 #include "base/strings/stringprintf.h"
9 #include "testing/gtest/include/gtest/gtest.h"
17 // Class which tracks allocation/deallocation
19 A(): mynum(num
++) { history
+= base::StringPrintf("A%d ctor\n", mynum
); }
20 virtual ~A() { history
+= base::StringPrintf("A%d dtor\n", mynum
); }
21 virtual void Use() { history
+= base::StringPrintf("A%d use\n", mynum
); }
27 B() { history
+= base::StringPrintf("B%d ctor\n", mynum
); }
28 virtual ~B() { history
+= base::StringPrintf("B%d dtor\n", mynum
); }
29 virtual void Use() OVERRIDE
{
30 history
+= base::StringPrintf("B%d use\n", mynum
);
36 TEST(LinkedPtrTest
, Test
) {
38 linked_ptr
<A
> a0
, a1
, a2
;
41 ASSERT_EQ(a0
.get(), static_cast<A
*>(NULL
));
42 ASSERT_EQ(a1
.get(), static_cast<A
*>(NULL
));
43 ASSERT_EQ(a2
.get(), static_cast<A
*>(NULL
));
44 ASSERT_TRUE(a0
== NULL
);
45 ASSERT_TRUE(a1
== NULL
);
46 ASSERT_TRUE(a2
== NULL
);
49 linked_ptr
<A
> a3(new A
);
51 ASSERT_TRUE(a0
== a3
);
52 ASSERT_TRUE(a0
!= NULL
);
53 ASSERT_TRUE(a0
.get() == a3
);
54 ASSERT_TRUE(a0
== a3
.get());
57 linked_ptr
<A
> a5(new A
);
58 ASSERT_TRUE(a5
.get() != a3
);
59 ASSERT_TRUE(a5
!= a3
.get());
61 linked_ptr
<B
> b0(new B
);
63 ASSERT_TRUE(b0
== a6
);
64 ASSERT_TRUE(a6
== b0
);
65 ASSERT_TRUE(b0
!= NULL
);