2
* Copyright 2010 Inalogic Inc.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License version 3, as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the applicable version of the GNU Lesser General Public
12
* License for more details.
14
* You should have received a copy of both the GNU Lesser General Public
15
* License version 3 along with this program. If not, see
16
* <http://www.gnu.org/licenses/>
18
* Authored by: Jay Taoko <jaytaoko@inalogic.com>
26
static void TestObject (void);
27
static void TestObjectReference (void);
28
static void TestObjectPtr (void);
29
static void TestObjectPtr1 (void);
30
static void TestObjectPtr2 (void);
33
test_Object_suite (void)
35
#define TESTDOMAIN "/Object/"
37
g_test_add_func (TESTDOMAIN"/TestObject", TestObject);
38
g_test_add_func (TESTDOMAIN"/TestObjectReference", TestObjectReference);
39
g_test_add_func (TESTDOMAIN"/TestObjectPtr", TestObjectPtr);
40
g_test_add_func (TESTDOMAIN"/TestObjectPtr1", TestObjectPtr1);
41
g_test_add_func (TESTDOMAIN"/TestObjectPtr2", TestObjectPtr2);
44
static const int ARRAY_SIZE = 1000;
46
class OwnedObject: public nux::Object
49
OwnedObject (NUX_FILE_LINE_PROTO) : nux::Object (true, NUX_FILE_LINE_PARAM)
56
int array [ARRAY_SIZE];
59
class ChildOwnedObject: public OwnedObject
62
ChildOwnedObject (NUX_FILE_LINE_PROTO) : OwnedObject (NUX_FILE_LINE_PARAM)
67
~ChildOwnedObject () {}
69
int array [ARRAY_SIZE];
73
class UnOwnedObject: public nux::Object
76
UnOwnedObject (NUX_FILE_LINE_PROTO) : nux::Object (false, NUX_FILE_LINE_PARAM)
83
int array [ARRAY_SIZE];
86
class ChildUnOwnedObject: public UnOwnedObject
89
ChildUnOwnedObject (NUX_FILE_LINE_PROTO) : UnOwnedObject (NUX_FILE_LINE_PARAM)
94
~ChildUnOwnedObject () {}
96
int array [ARRAY_SIZE];
102
OwnedObject *a = new OwnedObject (NUX_TRACKER_LOCATION);
103
OwnedObject b(NUX_TRACKER_LOCATION);
105
g_assert (a != NULL);
106
g_assert (a->IsHeapAllocated ());
108
g_assert (b.IsHeapAllocated () == false);
110
int sz = a->GetObjectSize ();
111
g_assert (sz >= ARRAY_SIZE);
117
TestObjectReference (void)
119
OwnedObject *a = new OwnedObject (NUX_TRACKER_LOCATION); // ref count = 1, owned
120
UnOwnedObject *b = new UnOwnedObject (NUX_TRACKER_LOCATION); // ref count = 1, unowned
122
g_assert (a->GetReferenceCount () == 1);
123
g_assert (b->GetReferenceCount () == 1);
124
g_assert (b->OwnsTheReference () == false);
126
a->Reference (); // ref count = 2
127
a->Reference (); // ref count = 3
128
b->Reference (); // ref count = 1, owned
130
g_assert (a->GetReferenceCount () == 3);
131
g_assert (b->GetReferenceCount () == 1);
132
g_assert (b->OwnsTheReference () == true);
134
g_assert (a->UnReference () == false); // ref count = 2
135
g_assert (a->GetReferenceCount () == 2);
136
g_assert (a->UnReference () == false); // ref count = 1
137
g_assert (a->GetReferenceCount () == 1);
138
g_assert (a->UnReference () == true); // object destroyed
140
g_assert (b->UnReference () == true); // object destroyed
146
OwnedObject *a = new OwnedObject (NUX_TRACKER_LOCATION); // ref count = 1, owned
148
nux::ObjectPtr<OwnedObject> object_ptr (a); // ref count = 2
150
g_assert (a->GetReferenceCount () == 2);
152
g_assert (a->UnReference () == false); // ref count = 1
154
g_assert (object_ptr->GetReferenceCount () == 1);
156
g_assert (a->UnReference() == false); // Calling UnReference repeatedly should not destroy the object when there are ObjectPtr's hosting it.
158
g_assert (object_ptr->GetReferenceCount () == 1);
160
object_ptr.Release ();
165
TestObjectPtr1 (void)
167
ChildOwnedObject *c = new ChildOwnedObject (NUX_TRACKER_LOCATION); // ref count = 1, owned
169
nux::ObjectPtr<OwnedObject> object_ptr0 (c); // ref count = 2
171
g_assert (c->GetReferenceCount () == 2);
173
nux::ObjectPtr<OwnedObject> object_ptr1 (object_ptr0); // ref count = 3
175
g_assert (c->GetReferenceCount () == 3);
177
g_assert (c->UnReference () == false); // ref count = 2
178
g_assert (c->UnReference () == false); // ref count = 2
179
g_assert (c->UnReference () == false); // ref count = 2
180
g_assert (c->UnReference () == false); // ref count = 2
181
g_assert (c->UnReference () == false); // ref count = 2
183
g_assert (object_ptr1->GetReferenceCount () == 2);
185
object_ptr1.Release ();
187
g_assert (object_ptr0->GetReferenceCount () == 1);
189
object_ptr0.Release ();
193
TestObjectPtr2 (void)
195
ChildOwnedObject *c = new ChildOwnedObject (NUX_TRACKER_LOCATION); // ref count = 1, owned
197
nux::ObjectPtr<OwnedObject> object_ptr0 (c); // ref count = 2
199
nux::ObjectWeakPtr<OwnedObject> object_ptr1 = object_ptr0; // ref count = 2, weak ref count = 3
201
nux::ObjectWeakPtr<OwnedObject> object_ptr2 = object_ptr1; // ref count = 2, weak ref count = 4
203
g_assert (c->GetReferenceCount () == 2);
205
g_assert (c->GetWeakReferenceCount() == 4);
207
g_assert (c->UnReference () == false); // ref count = 2
209
g_assert (object_ptr0->GetReferenceCount () == 1);
210
g_assert (object_ptr0->GetWeakReferenceCount () == 3);
212
g_assert (object_ptr0.Release () == true);
214
g_assert (object_ptr1.IsValid () == false);
215
g_assert (object_ptr1.IsNull () == true);
216
g_assert (object_ptr1 () == false);
217
g_assert (object_ptr1.GetWeakReferenceCount () == 2);
219
g_assert (object_ptr1.Release () == false);
222
g_assert (object_ptr2.GetWeakReferenceCount () == 1);
223
g_assert (object_ptr2.Release () == true);