[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
| 5 | #include <string> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 6 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 7 | #include "base/memory/linked_ptr.h" |
[email protected] | d529cb0 | 2013-06-10 19:06:57 | [diff] [blame] | 8 | #include "base/strings/stringprintf.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | |
| 11 | namespace { |
| 12 | |
| 13 | int num = 0; |
| 14 | |
| 15 | std::string history; |
| 16 | |
| 17 | // Class which tracks allocation/deallocation |
| 18 | struct A { |
[email protected] | f163393 | 2010-08-17 23:05:28 | [diff] [blame] | 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); } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 22 | int mynum; |
| 23 | }; |
| 24 | |
| 25 | // Subclass |
| 26 | struct B: public A { |
[email protected] | f163393 | 2010-08-17 23:05:28 | [diff] [blame] | 27 | B() { history += base::StringPrintf("B%d ctor\n", mynum); } |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 28 | ~B() override { history += base::StringPrintf("B%d dtor\n", mynum); } |
| 29 | void Use() override { history += base::StringPrintf("B%d use\n", mynum); } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | } // namespace |
| 33 | |
| 34 | TEST(LinkedPtrTest, Test) { |
| 35 | { |
| 36 | linked_ptr<A> a0, a1, a2; |
Hans Wennborg | 792903f | 2018-04-09 11:20:06 | [diff] [blame^] | 37 | a0 = *&a0; // The *& defeats Clang's -Wself-assign warning. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 38 | a1 = a2; |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 39 | 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.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 45 | |
| 46 | { |
| 47 | linked_ptr<A> a3(new A); |
| 48 | a0 = a3; |
| 49 | ASSERT_TRUE(a0 == a3); |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 50 | ASSERT_TRUE(a0 != nullptr); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 51 | 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 Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 63 | ASSERT_TRUE(b0 != nullptr); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 64 | 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 | } |