Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "gtest/internal/gtest-linked_ptr.h"
00034
00035 #include <stdlib.h>
00036 #include "gtest/gtest.h"
00037
00038 namespace {
00039
00040 using testing::Message;
00041 using testing::internal::linked_ptr;
00042
00043 int num;
00044 Message* history = NULL;
00045
00046
00047 class A {
00048 public:
00049 A(): mynum(num++) { *history << "A" << mynum << " ctor\n"; }
00050 virtual ~A() { *history << "A" << mynum << " dtor\n"; }
00051 virtual void Use() { *history << "A" << mynum << " use\n"; }
00052 protected:
00053 int mynum;
00054 };
00055
00056
00057 class B : public A {
00058 public:
00059 B() { *history << "B" << mynum << " ctor\n"; }
00060 ~B() { *history << "B" << mynum << " dtor\n"; }
00061 virtual void Use() { *history << "B" << mynum << " use\n"; }
00062 };
00063
00064 class LinkedPtrTest : public testing::Test {
00065 public:
00066 LinkedPtrTest() {
00067 num = 0;
00068 history = new Message;
00069 }
00070
00071 virtual ~LinkedPtrTest() {
00072 delete history;
00073 history = NULL;
00074 }
00075 };
00076
00077 TEST_F(LinkedPtrTest, GeneralTest) {
00078 {
00079 linked_ptr<A> a0, a1, a2;
00080
00081 a0.operator=(a0);
00082 a1 = a2;
00083 ASSERT_EQ(a0.get(), static_cast<A*>(NULL));
00084 ASSERT_EQ(a1.get(), static_cast<A*>(NULL));
00085 ASSERT_EQ(a2.get(), static_cast<A*>(NULL));
00086 ASSERT_TRUE(a0 == NULL);
00087 ASSERT_TRUE(a1 == NULL);
00088 ASSERT_TRUE(a2 == NULL);
00089
00090 {
00091 linked_ptr<A> a3(new A);
00092 a0 = a3;
00093 ASSERT_TRUE(a0 == a3);
00094 ASSERT_TRUE(a0 != NULL);
00095 ASSERT_TRUE(a0.get() == a3);
00096 ASSERT_TRUE(a0 == a3.get());
00097 linked_ptr<A> a4(a0);
00098 a1 = a4;
00099 linked_ptr<A> a5(new A);
00100 ASSERT_TRUE(a5.get() != a3);
00101 ASSERT_TRUE(a5 != a3.get());
00102 a2 = a5;
00103 linked_ptr<B> b0(new B);
00104 linked_ptr<A> a6(b0);
00105 ASSERT_TRUE(b0 == a6);
00106 ASSERT_TRUE(a6 == b0);
00107 ASSERT_TRUE(b0 != NULL);
00108 a5 = b0;
00109 a5 = b0;
00110 a3->Use();
00111 a4->Use();
00112 a5->Use();
00113 a6->Use();
00114 b0->Use();
00115 (*b0).Use();
00116 b0.get()->Use();
00117 }
00118
00119 a0->Use();
00120 a1->Use();
00121 a2->Use();
00122
00123 a1 = a2;
00124 a2.reset(new A);
00125 a0.reset();
00126
00127 linked_ptr<A> a7;
00128 }
00129
00130 ASSERT_STREQ(
00131 "A0 ctor\n"
00132 "A1 ctor\n"
00133 "A2 ctor\n"
00134 "B2 ctor\n"
00135 "A0 use\n"
00136 "A0 use\n"
00137 "B2 use\n"
00138 "B2 use\n"
00139 "B2 use\n"
00140 "B2 use\n"
00141 "B2 use\n"
00142 "B2 dtor\n"
00143 "A2 dtor\n"
00144 "A0 use\n"
00145 "A0 use\n"
00146 "A1 use\n"
00147 "A3 ctor\n"
00148 "A0 dtor\n"
00149 "A3 dtor\n"
00150 "A1 dtor\n",
00151 history->GetString().c_str());
00152 }
00153
00154 }