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 ~B() override
{ history
+= base::StringPrintf("B%d dtor\n", mynum
); }
29 void Use() override
{ history
+= base::StringPrintf("B%d use\n", mynum
); }
34 TEST(LinkedPtrTest
, Test
) {
36 linked_ptr
<A
> a0
, a1
, a2
;
39 ASSERT_EQ(a0
.get(), static_cast<A
*>(NULL
));
40 ASSERT_EQ(a1
.get(), static_cast<A
*>(NULL
));
41 ASSERT_EQ(a2
.get(), static_cast<A
*>(NULL
));
42 ASSERT_TRUE(a0
== NULL
);
43 ASSERT_TRUE(a1
== NULL
);
44 ASSERT_TRUE(a2
== NULL
);
47 linked_ptr
<A
> a3(new A
);
49 ASSERT_TRUE(a0
== a3
);
50 ASSERT_TRUE(a0
!= NULL
);
51 ASSERT_TRUE(a0
.get() == a3
);
52 ASSERT_TRUE(a0
== a3
.get());
55 linked_ptr
<A
> a5(new A
);
56 ASSERT_TRUE(a5
.get() != a3
);
57 ASSERT_TRUE(a5
!= a3
.get());
59 linked_ptr
<B
> b0(new B
);
61 ASSERT_TRUE(b0
== a6
);
62 ASSERT_TRUE(a6
== b0
);
63 ASSERT_TRUE(b0
!= NULL
);