Back to index

nux  3.0.0
gtest-nuxcore-object.cpp
Go to the documentation of this file.
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   object_ptr.Release();
00137 }
00138 
00139 TEST(TestObject, TestObjectPtrAdopt) {
00140 
00141   OwnedObject* a = new OwnedObject(NUX_TRACKER_LOCATION);  // ref count = 1, owned
00142 
00143   nux::ObjectPtr<OwnedObject> object_ptr;
00144 
00145   object_ptr.Adopt(a);
00146 
00147   EXPECT_THAT(a->GetReferenceCount(), Eq(1));
00148 }
00149 
00150 TEST(TestObject, TestObjectPtrGetPointer) {
00151 
00152   OwnedObject* a = new OwnedObject(NUX_TRACKER_LOCATION);  // ref count = 1, owned
00153   nux::ObjectPtr<OwnedObject> object_ptr;
00154   object_ptr.Adopt(a);
00155 
00156   EXPECT_THAT(object_ptr.GetPointer(), Eq(a));
00157   // Const too
00158   nux::ObjectPtr<OwnedObject> const& object_ptr_ref = object_ptr;
00159   EXPECT_THAT(object_ptr_ref.GetPointer(), Eq(a));
00160 }
00161 
00162 TEST(TestObject, TestObjectPtr1) {
00163 
00164   ChildOwnedObject* c = new ChildOwnedObject(NUX_TRACKER_LOCATION);  // ref count = 1, owned
00165 
00166   nux::ObjectPtr<OwnedObject> object_ptr0(c); // ref count = 2
00167 
00168   EXPECT_THAT(c->GetReferenceCount(), Eq(2));
00169 
00170   nux::ObjectPtr<OwnedObject> object_ptr1(object_ptr0); // ref count = 3
00171 
00172   EXPECT_THAT(c->GetReferenceCount(), Eq(3));
00173 
00174   EXPECT_FALSE(c->UnReference()); // ref count = 2
00175 
00176   object_ptr1.Release();
00177 
00178   EXPECT_THAT(c->GetReferenceCount(), Eq(1));
00179 
00180   object_ptr0.Release ();
00181 }
00182 
00183 TEST(TestObject, TestObjectPtr2) {
00184 
00185   ChildOwnedObject *c = new ChildOwnedObject(NUX_TRACKER_LOCATION);
00186 
00187   nux::ObjectPtr<OwnedObject> obj_ptr(c);
00188   nux::ObjectWeakPtr<OwnedObject> weak_ptr(obj_ptr);
00189 
00190   EXPECT_THAT(c->GetReferenceCount(), Eq(2));
00191   EXPECT_FALSE(c->UnReference());
00192 
00193   EXPECT_THAT(c->GetReferenceCount(), Eq(1));
00194 
00195   // Clearing the smart pointer deletes the object.
00196   EXPECT_TRUE(obj_ptr.Release());
00197 
00198   EXPECT_FALSE(weak_ptr.IsValid());
00199   EXPECT_TRUE(weak_ptr.IsNull());
00200   EXPECT_FALSE(weak_ptr());
00201 }
00202 
00203 bool g_signal_called = false;
00204 
00205 void on_destroyed_cb (nux::Object *obj)
00206 {
00207   g_signal_called = true;
00208 }
00209 
00210 TEST(TestObject, TestObjectSignal) {
00211 
00212   nux::Object *obj = new nux::Object ();
00213   obj->OnDestroyed.connect(sigc::ptr_fun(on_destroyed_cb));
00214   EXPECT_FALSE(g_signal_called);
00215   obj->UnReference ();
00216   EXPECT_TRUE(g_signal_called);
00217 }
00218 
00219 }