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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * Copyright 2010 Inalogic Inc.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3, as
 * published by the  Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of both the GNU Lesser General Public
 * License version 3 along with this program.  If not, see
 * <http://www.gnu.org/licenses/>
 *
 * Authored by: Jay Taoko <jaytaoko@inalogic.com>
 *
 */


//#include <glib.h>
#include "Nux/Nux.h"

static void TestObject (void);
static void TestObjectReference (void);
static void TestObjectPtr (void);
static void TestObjectPtr1 (void);
static void TestObjectPtr2 (void);
static void TestObjectSignal (void);

void
TestObjectSuite (void)
{
#define TESTDOMAIN "/Object/"

  g_test_add_func (TESTDOMAIN"/TestObject",           TestObject);
  g_test_add_func (TESTDOMAIN"/TestObjectReference",  TestObjectReference);
  g_test_add_func (TESTDOMAIN"/TestObjectPtr",        TestObjectPtr);
  g_test_add_func (TESTDOMAIN"/TestObjectPtr1",       TestObjectPtr1);
  g_test_add_func (TESTDOMAIN"/TestObjectPtr2",       TestObjectPtr2);
  g_test_add_func (TESTDOMAIN"/TestObjectSignal",     TestObjectSignal);
}

static const int ARRAY_SIZE = 1000;

class OwnedObject: public nux::Object
{
public:
  OwnedObject (NUX_FILE_LINE_PROTO) : nux::Object (true, NUX_FILE_LINE_PARAM)
  {

  }

  ~OwnedObject () {}

  int array [ARRAY_SIZE];
};

class ChildOwnedObject: public OwnedObject
{
public:
  ChildOwnedObject (NUX_FILE_LINE_PROTO) : OwnedObject (NUX_FILE_LINE_PARAM)
  {

  }

  ~ChildOwnedObject () {}

  int array [ARRAY_SIZE];
};


class UnOwnedObject: public nux::Object
{
public:
  UnOwnedObject (NUX_FILE_LINE_PROTO) : nux::Object (false, NUX_FILE_LINE_PARAM)
  {

  }

  ~UnOwnedObject () {}

  int array [ARRAY_SIZE];
};

class ChildUnOwnedObject: public UnOwnedObject
{
public:
  ChildUnOwnedObject (NUX_FILE_LINE_PROTO) : UnOwnedObject (NUX_FILE_LINE_PARAM)
  {

  }

  ~ChildUnOwnedObject () {}

  int array [ARRAY_SIZE];
};

static void
TestObject (void)
{
  OwnedObject *a = new OwnedObject (NUX_TRACKER_LOCATION);
  OwnedObject b(NUX_TRACKER_LOCATION);

  g_assert (a != NULL);
  g_assert (a->IsHeapAllocated ());

  g_assert (b.IsHeapAllocated () == false);

  int sz = a->GetObjectSize ();
  g_assert (sz >= ARRAY_SIZE);

  a->UnReference ();
}

static void
TestObjectReference (void)
{
  OwnedObject *a = new OwnedObject (NUX_TRACKER_LOCATION);  // ref count = 1, owned
  UnOwnedObject *b = new UnOwnedObject (NUX_TRACKER_LOCATION); // ref count = 1, unowned

  g_assert (a->GetReferenceCount () == 1);
  g_assert (b->GetReferenceCount () == 1);
  g_assert (b->OwnsTheReference () == false);

  a->Reference (); // ref count = 2
  a->Reference (); // ref count = 3
  b->Reference (); // ref count = 1, owned

  g_assert (a->GetReferenceCount () == 3);
  g_assert (b->GetReferenceCount () == 1);
  g_assert (b->OwnsTheReference () == true);

  g_assert (a->UnReference () == false); // ref count = 2
  g_assert (a->GetReferenceCount () == 2);
  g_assert (a->UnReference () == false); // ref count = 1
  g_assert (a->GetReferenceCount () == 1);
  g_assert (a->UnReference () == true); // object destroyed

  g_assert (b->UnReference () == true); // object destroyed
}

static void
TestObjectPtr (void)
{
  OwnedObject *a = new OwnedObject (NUX_TRACKER_LOCATION);  // ref count = 1, owned

  nux::ObjectPtr<OwnedObject> object_ptr (a); // ref count = 2

  g_assert (a->GetReferenceCount () == 2);

  g_assert (a->UnReference () == false); // ref count = 1

  g_assert (object_ptr->GetReferenceCount () == 1);

  g_assert (a->UnReference() == false); // Calling UnReference repeatedly should not destroy the object when there are ObjectPtr's hosting it.

  g_assert (object_ptr->GetReferenceCount () == 1);

  object_ptr.Release ();

}

static void
TestObjectPtr1 (void)
{
  ChildOwnedObject *c = new ChildOwnedObject (NUX_TRACKER_LOCATION);  // ref count = 1, owned

  nux::ObjectPtr<OwnedObject> object_ptr0 (c); // ref count = 2

  g_assert (c->GetReferenceCount () == 2);

  nux::ObjectPtr<OwnedObject> object_ptr1 (object_ptr0); // ref count = 3

  g_assert (c->GetReferenceCount () == 3);

  g_assert (c->UnReference () == false); // ref count = 2
  g_assert (c->UnReference () == false); // ref count = 2
  g_assert (c->UnReference () == false); // ref count = 2
  g_assert (c->UnReference () == false); // ref count = 2
  g_assert (c->UnReference () == false); // ref count = 2

  g_assert (object_ptr1->GetReferenceCount () == 2);

  object_ptr1.Release ();

  g_assert (object_ptr0->GetReferenceCount () == 1);

  object_ptr0.Release ();
}

static void
TestObjectPtr2 (void)
{
  ChildOwnedObject *c = new ChildOwnedObject (NUX_TRACKER_LOCATION);  // ref count = 1, owned

  nux::ObjectPtr<OwnedObject> object_ptr0 (c); // ref count = 2

  nux::ObjectWeakPtr<OwnedObject> object_ptr1 = object_ptr0; // ref count = 2, weak ref count = 3

  nux::ObjectWeakPtr<OwnedObject> object_ptr2 = object_ptr1; // ref count = 2, weak ref count = 4

  g_assert (c->GetReferenceCount () == 2);

  g_assert (c->GetWeakReferenceCount() == 4);

  g_assert (c->UnReference () == false); // ref count = 2

  g_assert (object_ptr0->GetReferenceCount () == 1);
  g_assert (object_ptr0->GetWeakReferenceCount () == 3);

  g_assert (object_ptr0.Release () == true);

  g_assert (object_ptr1.IsValid () == false);
  g_assert (object_ptr1.IsNull () == true);
  g_assert (object_ptr1 () == false);
  g_assert (object_ptr1.GetWeakReferenceCount () == 2);

  g_assert (object_ptr1.Release () == false);


  g_assert (object_ptr2.GetWeakReferenceCount () == 1);
  g_assert (object_ptr2.Release () == true);
}

static bool g_signal_called = false;

static void on_destroyed_cb (nux::Object *obj)
{
  g_signal_called = true;
}

static void
TestObjectSignal (void)
{
  nux::Object *obj = new nux::Object ();
  obj->OnDestroyed.connect (sigc::ptr_fun (on_destroyed_cb));
  g_assert (g_signal_called == false);
  obj->UnReference ();
  g_assert (g_signal_called == true);
}