license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
| 5 | #include <string> |
| 6 | #include <iostream> |
| 7 | |
| 8 | #include "base/linked_ptr.h" |
| 9 | |
| 10 | #include "base/string_util.h" |
| 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | namespace { |
| 14 | |
| 15 | int num = 0; |
| 16 | |
| 17 | std::string history; |
| 18 | |
| 19 | // Class which tracks allocation/deallocation |
| 20 | struct A { |
| 21 | A(): mynum(num++) { history += StringPrintf("A%d ctor\n", mynum); } |
| 22 | virtual ~A() { history += StringPrintf("A%d dtor\n", mynum); } |
| 23 | virtual void Use() { history += StringPrintf("A%d use\n", mynum); } |
| 24 | int mynum; |
| 25 | }; |
| 26 | |
| 27 | // Subclass |
| 28 | struct B: public A { |
| 29 | B() { history += StringPrintf("B%d ctor\n", mynum); } |
| 30 | ~B() { history += StringPrintf("B%d dtor\n", mynum); } |
| 31 | virtual void Use() { history += StringPrintf("B%d use\n", mynum); } |
| 32 | }; |
| 33 | |
| 34 | } // namespace |
| 35 | |
| 36 | TEST(LinkedPtrTest, Test) { |
| 37 | { |
| 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); |
| 47 | |
| 48 | { |
| 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(); |
| 75 | } |
| 76 | |
| 77 | a0->Use(); |
| 78 | a1->Use(); |
| 79 | a2->Use(); |
| 80 | |
| 81 | a1 = a2; |
| 82 | a2.reset(new A); |
| 83 | a0.reset(); |
| 84 | |
| 85 | linked_ptr<A> a7; |
| 86 | } |
| 87 | |
| 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" |
| 109 | ); |
| 110 | } |