Jamoma API  0.6.0.a19
TTObject.test.cpp
1 /*
2  * Unit tests for TTObject
3  * Copyright © 2014, Timothy Place
4  *
5  * License: This code is licensed under the terms of the "New BSD License"
6  * http://creativecommons.org/licenses/BSD/
7  */
8 
9 #include "TTObject.test.h"
10 #include "TTObjectTemplates.h"
11 
12 #define thisTTClass TTObjectTest
13 #define thisTTClassName "object.test"
14 #define thisTTClassTags "test, foundation"
15 
16 
18 {;}
19 
20 
21 TTObjectTest::~TTObjectTest()
22 {;}
23 
24 
25 void TTObjectTestBasic(int& errorCount, int& testAssertionCount)
26 {
27  TTTestLog("\n");
28  TTTestLog("Testing basic functionality of TTObject");
29 
30 
31  TTObject callback("callback"); // create a callback object
32  TTSymbol notificationName;
33 
34  callback.set("notification", "foo"); // set a symbol attr
35  callback.get("notification", notificationName); // attr we get back is what we just set?
36 
37  TTTestAssertion("symbol attr that we set is returnable to us with correct value",
38  notificationName == "foo",
39  testAssertionCount,
40  errorCount);
41 
42  TTObject o1 = callback;
43 
44  TTTestAssertion("two object refering to the same object instance are equal",
45  o1 == callback,
46  testAssertionCount,
47  errorCount);
48 
49 }
50 
51 
52 void TTObjectTestWithTTValue(int& errorCount, int& testAssertionCount)
53 {
54  TTObject callback("callback");
55 
56  TTTestAssertion("after creation the object is valid",
57  callback.valid(),
58  testAssertionCount,
59  errorCount);
60 
61  callback.set("notification", "foo"); // set an attr so we can verify that it survives
62 
63  {
64  TTValue v1(callback); // assign to value with copy constructor
65 
66  TTTestAssertion("after duplication the object is still valid",
67  callback.valid(),
68  testAssertionCount,
69  errorCount);
70 
71  TTValue v2 = callback; // assign to value with = operator
72 
73  TTTestAssertion("1 object and 2 values -- refcount should be 3",
74  callback.instance()->getReferenceCount() == 3,
75  testAssertionCount,
76  errorCount);
77 
78  // could check that v1 and v2 have correct and sane content
79 
80  TTObject o1 = v1[0];
81  TTObject o2 = v2[0];
82 
83  TTTestAssertion("3 objects and 2 values -- refcount should be 5",
84  callback.instance()->getReferenceCount() == 5,
85  testAssertionCount,
86  errorCount);
87  }
88 
89  TTTestAssertion("1 object, others went out of scope -- refcount should be 1",
90  callback.instance()->getReferenceCount() == 1,
91  testAssertionCount,
92  errorCount);
93 }
94 
95 
96 void TTObjectTestIterateTTList(int& errorCount, int& testAssertionCount)
97 {
98  TTTestLog("\n");
99  TTTestLog("Testing iteration on TTObjects stored inside a TTList");
100 
101  // create an object
102  TTObject callback("callback");
103 
104  TTTestAssertion("1 object -- refcount should be 1",
105  callback.instance()->getReferenceCount() == 1,
106  testAssertionCount,
107  errorCount);
108 
109  // store the object into a list
110  TTList testList;
111  testList.appendUnique(callback);
112 
113  TTTestAssertion("1 object and 1 stored inside the list -- refcount should be 2",
114  callback.instance()->getReferenceCount() == 2,
115  testAssertionCount,
116  errorCount);
117 
118  // iterate the list to send it a message
119  TTValue v;
120  testList.iterateObjectsSendingMessage("nothing", v);
121 
122  TTTestAssertion("1 object and 1 stored inside the list -- refcount should be 2",
123  callback.instance()->getReferenceCount() == 2,
124  testAssertionCount,
125  errorCount);
126 
127  // clear the list
128  testList.clear();
129 
130  TTTestAssertion("1 object -- refcount should be 1",
131  callback.instance()->getReferenceCount() == 1,
132  testAssertionCount,
133  errorCount);
134 }
135 
136 
137 void TTObjectTestAsClassMemberCallback(const TTValue& baton, const TTValue& value)
138 {
139  int *errorCount = (int*)TTPtr(baton[0]);
140  int *testAssertionCount = (int*)TTPtr(baton[1]);
141  TTObject anObject = value[0];
142 
143  TTTestAssertion("no more object as class member -- should be not valid",
144  !anObject.instance()->valid,
145  *testAssertionCount,
146  *errorCount);
147 }
148 
149 void TTObjectTestAsClassMemberAndObjectFreeing(int& errorCount, int& testAssertionCount)
150 
151 {
152  TTTestLog("\n");
153  TTTestLog("Testing TTObject as class member and object freeing observation");
154 
155  // create a Test instance
156  Test* testInstance = new Test(errorCount, testAssertionCount);
157 
158  // register for objectFreeing notification
159  TTObject callback("callback");
160  TTValue v = TTValue(TTPtr(&errorCount), TTPtr(&testAssertionCount));
161  callback.set("baton", v);
162  callback.set("function", TTPtr(TTObjectTestAsClassMemberCallback));
163  callback.set("notification", "objectFreeing");
164 
165  testInstance->mObject.registerObserverForNotifications(callback);
166 
167  // now the observer should be referenced also by the observers list of Test::mObject
168  TTTestAssertion("1 observer on a TTObject class member -- refcount should be 2",
169  callback.instance()->getReferenceCount() == 2,
170  testAssertionCount,
171  errorCount);
172 
173  // don't unregister for objectFreeing notification as we need to be notified when the object is freeing
174 
175  // delete the Test instance and its mObject (and we expect the Test::mObject to forget its observers)
176  delete testInstance;
177 
178  // now the observer should be referenced ony one time
179  TTTestAssertion("no more observer on a TTObject class member -- refcount should be 1",
180  callback.instance()->getReferenceCount() == 1,
181  testAssertionCount,
182  errorCount);
183 }
184 
185 
186 Test::Test(int& errorCount, int& testAssertionCount) :
187 mObject("callback")
188 {
189  TTTestAssertion("1 object as class member -- refcount should be 1",
190  mObject.instance()->getReferenceCount() == 1,
191  testAssertionCount,
192  errorCount);
193 }
194 
195 Test::~Test()
196 {
197  ; // we do nothing here as we expect the mObject member will be destroyed with the instance of the Test class
198 }
199 
200 
201 TTErr TTObjectTest::test(TTValue& returnedTestInfo)
202 {
203  int errorCount = 0;
204  int testAssertionCount = 0;
205 
206  TTObjectTestBasic(errorCount, testAssertionCount);
207  TTObjectTestWithTTValue(errorCount, testAssertionCount);
208  TTObjectTestIterateTTList(errorCount, testAssertionCount);
209  TTObjectTestAsClassMemberAndObjectFreeing(errorCount, testAssertionCount);
210 
211  return TTTestFinish(testAssertionCount, errorCount, returnedTestInfo);
212 }
Implementation of template code exported by TTObject.
Create and use Jamoma object instances.
Definition: TTObject.h:29
Declare a Test class for TTObjectTestAsClassMemberAndObjectFreeing.
Definition: TTObject.test.h:23
TTBoolean valid
If the object isn't completely built, or is in the process of freeing, this will be false...
Definition: TTObjectBase.h:124
void * TTPtr
A generic pointer.
Definition: TTBase.h:248
TTErr registerObserverForNotifications(const TTObject &anObservingObject)
Register an observer.
Definition: TTObject.cpp:155
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
TTObjectBase * instance() const
Return a direct pointer to the internal instance.
Definition: TTObject.cpp:105
TT_OBJECT_CONSTRUCTOR
Constructor macro.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34