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>
22
#include <gmock/gmock.h>
26
using namespace testing;
30
const int ARRAY_SIZE = 1000;
32
class OwnedObject: public nux::Object
35
OwnedObject(NUX_FILE_LINE_PROTO)
36
: nux::Object (true, NUX_FILE_LINE_PARAM)
42
int array [ARRAY_SIZE];
45
class ChildOwnedObject: public OwnedObject
48
ChildOwnedObject(NUX_FILE_LINE_PROTO)
49
: OwnedObject (NUX_FILE_LINE_PARAM)
53
~ChildOwnedObject () {}
55
int array [ARRAY_SIZE];
59
class UnOwnedObject: public nux::Object
62
UnOwnedObject(NUX_FILE_LINE_PROTO)
63
: nux::Object (false, NUX_FILE_LINE_PARAM)
69
int array [ARRAY_SIZE];
72
class ChildUnOwnedObject: public UnOwnedObject
75
ChildUnOwnedObject(NUX_FILE_LINE_PROTO)
76
: UnOwnedObject (NUX_FILE_LINE_PARAM)
80
~ChildUnOwnedObject () {}
82
int array [ARRAY_SIZE];
85
TEST(TestObject, TestObject) {
87
OwnedObject *a = new OwnedObject (NUX_TRACKER_LOCATION);
88
OwnedObject b(NUX_TRACKER_LOCATION);
90
EXPECT_THAT(a, NotNull());
91
EXPECT_TRUE(a->IsHeapAllocated());
93
EXPECT_FALSE(b.IsHeapAllocated());
95
EXPECT_THAT(a->GetObjectSize(), Ge(ARRAY_SIZE));
100
TEST(TestObject, TestObjectReference) {
102
OwnedObject *a = new OwnedObject (NUX_TRACKER_LOCATION); // ref count = 1, owned
103
UnOwnedObject *b = new UnOwnedObject (NUX_TRACKER_LOCATION); // ref count = 1, unowned
105
EXPECT_THAT(a->GetReferenceCount(), Eq(1));
106
EXPECT_THAT(b->GetReferenceCount(), Eq(1));
107
EXPECT_FALSE(b->OwnsTheReference());
109
a->Reference (); // ref count = 2
110
a->Reference (); // ref count = 3
111
b->Reference (); // ref count = 1, owned
113
EXPECT_THAT(a->GetReferenceCount(), Eq(3));
114
EXPECT_THAT(b->GetReferenceCount(), Eq(1));
115
EXPECT_TRUE(b->OwnsTheReference());
117
EXPECT_FALSE(a->UnReference());
118
EXPECT_THAT(a->GetReferenceCount(), Eq(2));
119
EXPECT_FALSE(a->UnReference());
120
EXPECT_THAT(a->GetReferenceCount(), Eq(1));
121
EXPECT_TRUE(a->UnReference()); // object destroyed
123
EXPECT_TRUE(b->UnReference()); // object destroyed
126
TEST(TestObject, TestObjectPtr) {
128
OwnedObject *a = new OwnedObject (NUX_TRACKER_LOCATION); // ref count = 1, owned
130
nux::ObjectPtr<OwnedObject> object_ptr (a); // ref count = 2
132
EXPECT_THAT(a->GetReferenceCount(), Eq(2));
133
EXPECT_FALSE(a->UnReference()); // ref count = 1
134
EXPECT_THAT(a->GetReferenceCount(), Eq(1));
136
// Calling UnReference repeatedly should not destroy the object when there
137
// are ObjectPtr's hosting it.
138
EXPECT_FALSE(a->UnReference());
139
EXPECT_THAT(a->GetReferenceCount(), Eq(1));
141
object_ptr.Release();
144
TEST(TestObject, TestObjectPtrAdopt) {
146
OwnedObject* a = new OwnedObject(NUX_TRACKER_LOCATION); // ref count = 1, owned
148
nux::ObjectPtr<OwnedObject> object_ptr;
152
EXPECT_THAT(a->GetReferenceCount(), Eq(1));
155
TEST(TestObject, TestObjectPtrGetPointer) {
157
OwnedObject* a = new OwnedObject(NUX_TRACKER_LOCATION); // ref count = 1, owned
158
nux::ObjectPtr<OwnedObject> object_ptr;
161
EXPECT_THAT(object_ptr.GetPointer(), Eq(a));
163
nux::ObjectPtr<OwnedObject> const& object_ptr_ref = object_ptr;
164
EXPECT_THAT(object_ptr_ref.GetPointer(), Eq(a));
167
TEST(TestObject, TestObjectPtr1) {
169
ChildOwnedObject *c = new ChildOwnedObject (NUX_TRACKER_LOCATION); // ref count = 1, owned
171
nux::ObjectPtr<OwnedObject> object_ptr0 (c); // ref count = 2
173
EXPECT_THAT(c->GetReferenceCount(), Eq(2));
175
nux::ObjectPtr<OwnedObject> object_ptr1 (object_ptr0); // ref count = 3
177
EXPECT_THAT(c->GetReferenceCount(), Eq(3));
179
EXPECT_FALSE(c->UnReference()); // ref count = 2
180
EXPECT_FALSE(c->UnReference()); // ref count = 2
181
EXPECT_FALSE(c->UnReference()); // ref count = 2
183
EXPECT_THAT(c->GetReferenceCount(), Eq(2));
185
object_ptr1.Release ();
187
EXPECT_THAT(c->GetReferenceCount(), Eq(1));
189
object_ptr0.Release ();
192
TEST(TestObject, TestObjectPtr2) {
194
ChildOwnedObject *c = new ChildOwnedObject(NUX_TRACKER_LOCATION);
196
nux::ObjectPtr<OwnedObject> obj_ptr(c);
197
nux::ObjectWeakPtr<OwnedObject> weak_ptr(obj_ptr);
199
EXPECT_THAT(c->GetReferenceCount(), Eq(2));
200
EXPECT_FALSE(c->UnReference());
202
EXPECT_THAT(c->GetReferenceCount(), Eq(1));
204
// Clearing the smart pointer deletes the object.
205
EXPECT_TRUE(obj_ptr.Release());
207
EXPECT_FALSE(weak_ptr.IsValid());
208
EXPECT_TRUE(weak_ptr.IsNull());
209
EXPECT_FALSE(weak_ptr());
212
bool g_signal_called = false;
214
void on_destroyed_cb (nux::Object *obj)
216
g_signal_called = true;
219
TEST(TestObject, TestObjectSignal) {
221
nux::Object *obj = new nux::Object ();
222
obj->OnDestroyed.connect(sigc::ptr_fun(on_destroyed_cb));
223
EXPECT_FALSE(g_signal_called);
225
EXPECT_TRUE(g_signal_called);