~ubuntu-branches/ubuntu/oneiric/nux/oneiric

« back to all changes in this revision

Viewing changes to tests/test-object.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-12-17 13:59:57 UTC
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20101217135957-5gvg6fkjxaa252i0
Tags: upstream-0.9.12
ImportĀ upstreamĀ versionĀ 0.9.12

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2010 Inalogic Inc.
 
3
 *
 
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.
 
7
 *
 
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.
 
13
 *
 
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/>
 
17
 *
 
18
 * Authored by: Jay Taoko <jaytaoko@inalogic.com>
 
19
 *
 
20
 */
 
21
 
 
22
 
 
23
#include <glib.h>
 
24
#include "Nux/Nux.h"
 
25
 
 
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);
 
31
 
 
32
void
 
33
test_Object_suite (void)
 
34
{
 
35
#define TESTDOMAIN "/Object/"
 
36
 
 
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);
 
42
}
 
43
 
 
44
static const int ARRAY_SIZE = 1000;
 
45
 
 
46
class OwnedObject: public nux::Object
 
47
{
 
48
public:
 
49
  OwnedObject (NUX_FILE_LINE_PROTO) : nux::Object (true, NUX_FILE_LINE_PARAM)
 
50
  {
 
51
 
 
52
  }
 
53
 
 
54
  ~OwnedObject () {}
 
55
 
 
56
  int array [ARRAY_SIZE];
 
57
};
 
58
 
 
59
class ChildOwnedObject: public OwnedObject
 
60
{
 
61
public:
 
62
  ChildOwnedObject (NUX_FILE_LINE_PROTO) : OwnedObject (NUX_FILE_LINE_PARAM)
 
63
  {
 
64
 
 
65
  }
 
66
 
 
67
  ~ChildOwnedObject () {}
 
68
 
 
69
  int array [ARRAY_SIZE];
 
70
};
 
71
 
 
72
 
 
73
class UnOwnedObject: public nux::Object
 
74
{
 
75
public:
 
76
  UnOwnedObject (NUX_FILE_LINE_PROTO) : nux::Object (false, NUX_FILE_LINE_PARAM)
 
77
  {
 
78
 
 
79
  }
 
80
 
 
81
  ~UnOwnedObject () {}
 
82
 
 
83
  int array [ARRAY_SIZE];
 
84
};
 
85
 
 
86
class ChildUnOwnedObject: public UnOwnedObject
 
87
{
 
88
public:
 
89
  ChildUnOwnedObject (NUX_FILE_LINE_PROTO) : UnOwnedObject (NUX_FILE_LINE_PARAM)
 
90
  {
 
91
 
 
92
  }
 
93
 
 
94
  ~ChildUnOwnedObject () {}
 
95
 
 
96
  int array [ARRAY_SIZE];
 
97
};
 
98
 
 
99
static void
 
100
TestObject (void)
 
101
{
 
102
  OwnedObject *a = new OwnedObject (NUX_TRACKER_LOCATION);
 
103
  OwnedObject b(NUX_TRACKER_LOCATION);
 
104
 
 
105
  g_assert (a != NULL);
 
106
  g_assert (a->IsHeapAllocated ());
 
107
 
 
108
  g_assert (b.IsHeapAllocated () == false);
 
109
 
 
110
  int sz = a->GetObjectSize ();
 
111
  g_assert (sz >= ARRAY_SIZE);
 
112
 
 
113
  a->UnReference ();
 
114
}
 
115
 
 
116
static void
 
117
TestObjectReference (void)
 
118
{
 
119
  OwnedObject *a = new OwnedObject (NUX_TRACKER_LOCATION);  // ref count = 1, owned
 
120
  UnOwnedObject *b = new UnOwnedObject (NUX_TRACKER_LOCATION); // ref count = 1, unowned
 
121
 
 
122
  g_assert (a->GetReferenceCount () == 1);
 
123
  g_assert (b->GetReferenceCount () == 1);
 
124
  g_assert (b->OwnsTheReference () == false);
 
125
 
 
126
  a->Reference (); // ref count = 2
 
127
  a->Reference (); // ref count = 3
 
128
  b->Reference (); // ref count = 1, owned
 
129
 
 
130
  g_assert (a->GetReferenceCount () == 3);
 
131
  g_assert (b->GetReferenceCount () == 1);
 
132
  g_assert (b->OwnsTheReference () == true);
 
133
 
 
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
 
139
 
 
140
  g_assert (b->UnReference () == true); // object destroyed
 
141
}
 
142
 
 
143
static void
 
144
TestObjectPtr (void)
 
145
{
 
146
  OwnedObject *a = new OwnedObject (NUX_TRACKER_LOCATION);  // ref count = 1, owned
 
147
 
 
148
  nux::ObjectPtr<OwnedObject> object_ptr (a); // ref count = 2
 
149
 
 
150
  g_assert (a->GetReferenceCount () == 2);
 
151
 
 
152
  g_assert (a->UnReference () == false); // ref count = 1
 
153
 
 
154
  g_assert (object_ptr->GetReferenceCount () == 1);
 
155
 
 
156
  g_assert (a->UnReference() == false); // Calling UnReference repeatedly should not destroy the object when there are ObjectPtr's hosting it.
 
157
 
 
158
  g_assert (object_ptr->GetReferenceCount () == 1);
 
159
 
 
160
  object_ptr.Release ();
 
161
 
 
162
}
 
163
 
 
164
static void
 
165
TestObjectPtr1 (void)
 
166
{
 
167
  ChildOwnedObject *c = new ChildOwnedObject (NUX_TRACKER_LOCATION);  // ref count = 1, owned
 
168
 
 
169
  nux::ObjectPtr<OwnedObject> object_ptr0 (c); // ref count = 2
 
170
 
 
171
  g_assert (c->GetReferenceCount () == 2);
 
172
 
 
173
  nux::ObjectPtr<OwnedObject> object_ptr1 (object_ptr0); // ref count = 3
 
174
 
 
175
  g_assert (c->GetReferenceCount () == 3);
 
176
 
 
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
 
182
 
 
183
  g_assert (object_ptr1->GetReferenceCount () == 2);
 
184
 
 
185
  object_ptr1.Release ();
 
186
 
 
187
  g_assert (object_ptr0->GetReferenceCount () == 1);
 
188
 
 
189
  object_ptr0.Release ();
 
190
}
 
191
 
 
192
static void
 
193
TestObjectPtr2 (void)
 
194
{
 
195
  ChildOwnedObject *c = new ChildOwnedObject (NUX_TRACKER_LOCATION);  // ref count = 1, owned
 
196
 
 
197
  nux::ObjectPtr<OwnedObject> object_ptr0 (c); // ref count = 2
 
198
 
 
199
  nux::ObjectWeakPtr<OwnedObject> object_ptr1 = object_ptr0; // ref count = 2, weak ref count = 3
 
200
 
 
201
  nux::ObjectWeakPtr<OwnedObject> object_ptr2 = object_ptr1; // ref count = 2, weak ref count = 4
 
202
 
 
203
  g_assert (c->GetReferenceCount () == 2);
 
204
 
 
205
  g_assert (c->GetWeakReferenceCount() == 4);
 
206
 
 
207
  g_assert (c->UnReference () == false); // ref count = 2
 
208
 
 
209
  g_assert (object_ptr0->GetReferenceCount () == 1);
 
210
  g_assert (object_ptr0->GetWeakReferenceCount () == 3);
 
211
 
 
212
  g_assert (object_ptr0.Release () == true);
 
213
 
 
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);
 
218
 
 
219
  g_assert (object_ptr1.Release () == false);
 
220
 
 
221
 
 
222
  g_assert (object_ptr2.GetWeakReferenceCount () == 1);
 
223
  g_assert (object_ptr2.Release () == true);
 
224
}