Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / base / memory / linked_ptr_unittest.cc
blob8b938f2d6c939b854b7bbd090edf9164278248e4
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.
5 #include <string>
7 #include "base/memory/linked_ptr.h"
8 #include "base/strings/stringprintf.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace {
13 int num = 0;
15 std::string history;
17 // Class which tracks allocation/deallocation
18 struct A {
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); }
22 int mynum;
25 // Subclass
26 struct B: public A {
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);
34 } // namespace
36 TEST(LinkedPtrTest, Test) {
38 linked_ptr<A> a0, a1, a2;
39 a0 = a0;
40 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);
50 a0 = a3;
51 ASSERT_TRUE(a0 == a3);
52 ASSERT_TRUE(a0 != NULL);
53 ASSERT_TRUE(a0.get() == a3);
54 ASSERT_TRUE(a0 == a3.get());
55 linked_ptr<A> a4(a0);
56 a1 = a4;
57 linked_ptr<A> a5(new A);
58 ASSERT_TRUE(a5.get() != a3);
59 ASSERT_TRUE(a5 != a3.get());
60 a2 = a5;
61 linked_ptr<B> b0(new B);
62 linked_ptr<A> a6(b0);
63 ASSERT_TRUE(b0 == a6);
64 ASSERT_TRUE(a6 == b0);
65 ASSERT_TRUE(b0 != NULL);
66 a5 = b0;
67 a5 = b0;
68 a3->Use();
69 a4->Use();
70 a5->Use();
71 a6->Use();
72 b0->Use();
73 (*b0).Use();
74 b0.get()->Use();
77 a0->Use();
78 a1->Use();
79 a2->Use();
81 a1 = a2;
82 a2.reset(new A);
83 a0.reset();
85 linked_ptr<A> a7;
88 ASSERT_EQ(history,
89 "A0 ctor\n"
90 "A1 ctor\n"
91 "A2 ctor\n"
92 "B2 ctor\n"
93 "A0 use\n"
94 "A0 use\n"
95 "B2 use\n"
96 "B2 use\n"
97 "B2 use\n"
98 "B2 use\n"
99 "B2 use\n"
100 "B2 dtor\n"
101 "A2 dtor\n"
102 "A0 use\n"
103 "A0 use\n"
104 "A1 use\n"
105 "A3 ctor\n"
106 "A0 dtor\n"
107 "A3 dtor\n"
108 "A1 dtor\n"