blob: 344ffa48d6301cc3b674eccdf0960356f5ba2fd9 [file] [log] [blame]
[email protected]44106182012-04-06 03:53:021// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
5#include <string>
initial.commitd7cae122008-07-26 21:49:386
[email protected]3b63f8f42011-03-28 01:54:157#include "base/memory/linked_ptr.h"
[email protected]d529cb02013-06-10 19:06:578#include "base/strings/stringprintf.h"
initial.commitd7cae122008-07-26 21:49:389#include "testing/gtest/include/gtest/gtest.h"
10
11namespace {
12
13int num = 0;
14
15std::string history;
16
17// Class which tracks allocation/deallocation
18struct A {
[email protected]f1633932010-08-17 23:05:2819 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); }
initial.commitd7cae122008-07-26 21:49:3822 int mynum;
23};
24
25// Subclass
26struct B: public A {
[email protected]f1633932010-08-17 23:05:2827 B() { history += base::StringPrintf("B%d ctor\n", mynum); }
dcheng56488182014-10-21 10:54:5128 ~B() override { history += base::StringPrintf("B%d dtor\n", mynum); }
29 void Use() override { history += base::StringPrintf("B%d use\n", mynum); }
initial.commitd7cae122008-07-26 21:49:3830};
31
32} // namespace
33
34TEST(LinkedPtrTest, Test) {
35 {
36 linked_ptr<A> a0, a1, a2;
Hans Wennborg792903f2018-04-09 11:20:0637 a0 = *&a0; // The *& defeats Clang's -Wself-assign warning.
initial.commitd7cae122008-07-26 21:49:3838 a1 = a2;
Ivan Kotenkova16212a52017-11-08 12:37:3339 ASSERT_EQ(a0.get(), static_cast<A*>(nullptr));
40 ASSERT_EQ(a1.get(), static_cast<A*>(nullptr));
41 ASSERT_EQ(a2.get(), static_cast<A*>(nullptr));
42 ASSERT_TRUE(a0 == nullptr);
43 ASSERT_TRUE(a1 == nullptr);
44 ASSERT_TRUE(a2 == nullptr);
initial.commitd7cae122008-07-26 21:49:3845
46 {
47 linked_ptr<A> a3(new A);
48 a0 = a3;
49 ASSERT_TRUE(a0 == a3);
Ivan Kotenkova16212a52017-11-08 12:37:3350 ASSERT_TRUE(a0 != nullptr);
initial.commitd7cae122008-07-26 21:49:3851 ASSERT_TRUE(a0.get() == a3);
52 ASSERT_TRUE(a0 == a3.get());
53 linked_ptr<A> a4(a0);
54 a1 = a4;
55 linked_ptr<A> a5(new A);
56 ASSERT_TRUE(a5.get() != a3);
57 ASSERT_TRUE(a5 != a3.get());
58 a2 = a5;
59 linked_ptr<B> b0(new B);
60 linked_ptr<A> a6(b0);
61 ASSERT_TRUE(b0 == a6);
62 ASSERT_TRUE(a6 == b0);
Ivan Kotenkova16212a52017-11-08 12:37:3363 ASSERT_TRUE(b0 != nullptr);
initial.commitd7cae122008-07-26 21:49:3864 a5 = b0;
65 a5 = b0;
66 a3->Use();
67 a4->Use();
68 a5->Use();
69 a6->Use();
70 b0->Use();
71 (*b0).Use();
72 b0.get()->Use();
73 }
74
75 a0->Use();
76 a1->Use();
77 a2->Use();
78
79 a1 = a2;
80 a2.reset(new A);
81 a0.reset();
82
83 linked_ptr<A> a7;
84 }
85
86 ASSERT_EQ(history,
87 "A0 ctor\n"
88 "A1 ctor\n"
89 "A2 ctor\n"
90 "B2 ctor\n"
91 "A0 use\n"
92 "A0 use\n"
93 "B2 use\n"
94 "B2 use\n"
95 "B2 use\n"
96 "B2 use\n"
97 "B2 use\n"
98 "B2 dtor\n"
99 "A2 dtor\n"
100 "A0 use\n"
101 "A0 use\n"
102 "A1 use\n"
103 "A3 ctor\n"
104 "A0 dtor\n"
105 "A3 dtor\n"
106 "A1 dtor\n"
107 );
108}