nux-1.16.0
|
00001 /* 00002 * Copyright 2010 Inalogic Inc. 00003 * 00004 * This program is free software: you can redistribute it and/or modify it 00005 * under the terms of the GNU Lesser General Public License version 3, as 00006 * published by the Free Software Foundation. 00007 * 00008 * This program is distributed in the hope that it will be useful, but 00009 * WITHOUT ANY WARRANTY; without even the implied warranties of 00010 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR 00011 * PURPOSE. See the applicable version of the GNU Lesser General Public 00012 * License for more details. 00013 * 00014 * You should have received a copy of both the GNU Lesser General Public 00015 * License version 3 along with this program. If not, see 00016 * <http://www.gnu.org/licenses/> 00017 * 00018 * Authored by: Jay Taoko <jaytaoko@inalogic.com> 00019 * 00020 */ 00021 00022 #include <gmock/gmock.h> 00023 00024 #include "Nux/Nux.h" 00025 00026 using namespace testing; 00027 00028 namespace { 00029 00030 const int ARRAY_SIZE = 1000; 00031 00032 class OwnedObject: public nux::Object 00033 { 00034 public: 00035 OwnedObject(NUX_FILE_LINE_PROTO) 00036 : nux::Object (true, NUX_FILE_LINE_PARAM) 00037 { 00038 } 00039 00040 ~OwnedObject () {} 00041 00042 int array [ARRAY_SIZE]; 00043 }; 00044 00045 class ChildOwnedObject: public OwnedObject 00046 { 00047 public: 00048 ChildOwnedObject(NUX_FILE_LINE_PROTO) 00049 : OwnedObject (NUX_FILE_LINE_PARAM) 00050 { 00051 } 00052 00053 ~ChildOwnedObject () {} 00054 00055 int array [ARRAY_SIZE]; 00056 }; 00057 00058 00059 class UnOwnedObject: public nux::Object 00060 { 00061 public: 00062 UnOwnedObject(NUX_FILE_LINE_PROTO) 00063 : nux::Object (false, NUX_FILE_LINE_PARAM) 00064 { 00065 } 00066 00067 ~UnOwnedObject () {} 00068 00069 int array [ARRAY_SIZE]; 00070 }; 00071 00072 class ChildUnOwnedObject: public UnOwnedObject 00073 { 00074 public: 00075 ChildUnOwnedObject(NUX_FILE_LINE_PROTO) 00076 : UnOwnedObject (NUX_FILE_LINE_PARAM) 00077 { 00078 } 00079 00080 ~ChildUnOwnedObject () {} 00081 00082 int array [ARRAY_SIZE]; 00083 }; 00084 00085 TEST(TestObject, TestObject) { 00086 00087 OwnedObject *a = new OwnedObject (NUX_TRACKER_LOCATION); 00088 OwnedObject b(NUX_TRACKER_LOCATION); 00089 00090 EXPECT_THAT(a, NotNull()); 00091 EXPECT_TRUE(a->IsHeapAllocated()); 00092 00093 EXPECT_FALSE(b.IsHeapAllocated()); 00094 00095 EXPECT_THAT(a->GetObjectSize(), Ge(ARRAY_SIZE)); 00096 00097 a->UnReference(); 00098 } 00099 00100 TEST(TestObject, TestObjectReference) { 00101 00102 OwnedObject *a = new OwnedObject (NUX_TRACKER_LOCATION); // ref count = 1, owned 00103 UnOwnedObject *b = new UnOwnedObject (NUX_TRACKER_LOCATION); // ref count = 1, unowned 00104 00105 EXPECT_THAT(a->GetReferenceCount(), Eq(1)); 00106 EXPECT_THAT(b->GetReferenceCount(), Eq(1)); 00107 EXPECT_FALSE(b->OwnsTheReference()); 00108 00109 a->Reference (); // ref count = 2 00110 a->Reference (); // ref count = 3 00111 b->Reference (); // ref count = 1, owned 00112 00113 EXPECT_THAT(a->GetReferenceCount(), Eq(3)); 00114 EXPECT_THAT(b->GetReferenceCount(), Eq(1)); 00115 EXPECT_TRUE(b->OwnsTheReference()); 00116 00117 EXPECT_FALSE(a->UnReference()); 00118 EXPECT_THAT(a->GetReferenceCount(), Eq(2)); 00119 EXPECT_FALSE(a->UnReference()); 00120 EXPECT_THAT(a->GetReferenceCount(), Eq(1)); 00121 EXPECT_TRUE(a->UnReference()); // object destroyed 00122 00123 EXPECT_TRUE(b->UnReference()); // object destroyed 00124 } 00125 00126 TEST(TestObject, TestObjectPtr) { 00127 00128 OwnedObject *a = new OwnedObject (NUX_TRACKER_LOCATION); // ref count = 1, owned 00129 00130 nux::ObjectPtr<OwnedObject> object_ptr (a); // ref count = 2 00131 00132 EXPECT_THAT(a->GetReferenceCount(), Eq(2)); 00133 EXPECT_FALSE(a->UnReference()); // ref count = 1 00134 EXPECT_THAT(a->GetReferenceCount(), Eq(1)); 00135 00136 // Calling UnReference repeatedly should not destroy the object when there 00137 // are ObjectPtr's hosting it. 00138 EXPECT_FALSE(a->UnReference()); 00139 EXPECT_THAT(a->GetReferenceCount(), Eq(1)); 00140 00141 object_ptr.Release(); 00142 } 00143 00144 TEST(TestObject, TestObjectPtrAdopt) { 00145 00146 OwnedObject* a = new OwnedObject(NUX_TRACKER_LOCATION); // ref count = 1, owned 00147 00148 nux::ObjectPtr<OwnedObject> object_ptr; 00149 00150 object_ptr.Adopt(a); 00151 00152 EXPECT_THAT(a->GetReferenceCount(), Eq(1)); 00153 } 00154 00155 TEST(TestObject, TestObjectPtr1) { 00156 00157 ChildOwnedObject *c = new ChildOwnedObject (NUX_TRACKER_LOCATION); // ref count = 1, owned 00158 00159 nux::ObjectPtr<OwnedObject> object_ptr0 (c); // ref count = 2 00160 00161 EXPECT_THAT(c->GetReferenceCount(), Eq(2)); 00162 00163 nux::ObjectPtr<OwnedObject> object_ptr1 (object_ptr0); // ref count = 3 00164 00165 EXPECT_THAT(c->GetReferenceCount(), Eq(3)); 00166 00167 EXPECT_FALSE(c->UnReference()); // ref count = 2 00168 EXPECT_FALSE(c->UnReference()); // ref count = 2 00169 EXPECT_FALSE(c->UnReference()); // ref count = 2 00170 00171 EXPECT_THAT(c->GetReferenceCount(), Eq(2)); 00172 00173 object_ptr1.Release (); 00174 00175 EXPECT_THAT(c->GetReferenceCount(), Eq(1)); 00176 00177 object_ptr0.Release (); 00178 } 00179 00180 TEST(TestObject, TestObjectPtr2) { 00181 00182 ChildOwnedObject *c = new ChildOwnedObject(NUX_TRACKER_LOCATION); 00183 00184 nux::ObjectPtr<OwnedObject> obj_ptr(c); 00185 nux::ObjectWeakPtr<OwnedObject> weak_ptr(obj_ptr); 00186 00187 EXPECT_THAT(c->GetReferenceCount(), Eq(2)); 00188 EXPECT_FALSE(c->UnReference()); 00189 00190 EXPECT_THAT(c->GetReferenceCount(), Eq(1)); 00191 00192 // Clearing the smart pointer deletes the object. 00193 EXPECT_TRUE(obj_ptr.Release()); 00194 00195 EXPECT_FALSE(weak_ptr.IsValid()); 00196 EXPECT_TRUE(weak_ptr.IsNull()); 00197 EXPECT_FALSE(weak_ptr()); 00198 } 00199 00200 bool g_signal_called = false; 00201 00202 void on_destroyed_cb (nux::Object *obj) 00203 { 00204 g_signal_called = true; 00205 } 00206 00207 TEST(TestObject, TestObjectSignal) { 00208 00209 nux::Object *obj = new nux::Object (); 00210 obj->OnDestroyed.connect(sigc::ptr_fun(on_destroyed_cb)); 00211 EXPECT_FALSE(g_signal_called); 00212 obj->UnReference (); 00213 EXPECT_TRUE(g_signal_called); 00214 } 00215 00216 }