Jamoma API  0.6.0.a19
TTBuffer.test.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup dspLibrary
4  *
5  * @brief Unit tests for the #TTBuffer class
6  *
7  * @details
8  *
9  * @see TTBuffer
10  *
11  * @authors Nathan Wolek
12  *
13  * @copyright Copyright © 2012 by Nathan Wolek @n
14  * This code is licensed under the terms of the "New BSD License" @n
15  * http://creativecommons.org/licenses/BSD/
16  */
17 
18 
19 #include "TTBuffer.h"
20 
21 
22 TTErr TTBuffer::test(TTValue& returnedTestInfo)
23 {
24  int errorCount = 0;
25  int testAssertionCount = 0;
26  //int badSampleCount = 0;
27 
28  // *** Tim's old list (we'll get there) ***
29  // TODO: test filling with sine wave
30  // TODO: test scaling (applying gain)
31  // TODO: test normalizing (with optional arg, and also without an optional arg)
32 
33  this->init(1,"myFirstBuffer");
34  this->setAttributeValue("lengthInSamples", 50);
35 
36  TTTestLog("\nTest checkout of first SampleMatrix...");
37 
38  // TEST 1: checking out a matrix returns something
39  TTSampleMatrixPtr myFirstCheckOut = NULL;
40  this->checkOutMatrix(myFirstCheckOut);
41 
42  TTBoolean result1 = { myFirstCheckOut != NULL };
43 
44  TTTestAssertion("checkOutMatrix returns a valid pointer",
45  result1,
46  testAssertionCount,
47  errorCount);
48 
49  // if it failed then we need to abort further testing to avoid a crash dereferencing a bogus pointer
50  if (!myFirstCheckOut)
51  return TTTestFinish(testAssertionCount, errorCount, returnedTestInfo);
52 
53  // TEST 2: how many channels does this matrix have?
54  TTInt32 test2expect = 1;
55 
56  TTInt32 test2return = 0;
57  myFirstCheckOut->getAttributeValue("numChannels", test2return);
58 
59  TTBoolean result2 = { test2expect == test2return };
60 
61  TTTestAssertion("numChannels is set properly",
62  result2,
63  testAssertionCount,
64  errorCount);
65 
66  if(!result2)
67  {
68  TTTestLog("Expected a value of %i, but returned value was %i", test2expect, test2return);
69  }
70 
71  // TEST 3: what is the user count?
72  TTInt32 test3expect = 1;
73 
74  TTInt32 test3return = 0;
75  myFirstCheckOut->getAttributeValue("userCount", test3return);
76 
77  TTBoolean result3 = { test3expect == test3return };
78 
79  TTTestAssertion("userCount reports proper value",
80  result3,
81  testAssertionCount,
82  errorCount);
83 
84  if(!result3)
85  {
86  TTTestLog("Expected a value of %i, but returned value was %i", test3expect, test3return);
87  }
88 
89  // TEST 4: what is the buffer stage?
90  TTBoolean test4expect = true;
91 
92  TTBoolean test4return = false;
93  test4return = myFirstCheckOut->isBufferPoolStage(kSM_Active);
94 
95  TTBoolean result4 = { test4expect == test4return };
96 
97  TTTestAssertion("bufferPoolStage reports proper value",
98  result4,
99  testAssertionCount,
100  errorCount);
101 
102  if(!result4)
103  {
104  TTTestLog("Expected a value of %i, but returned value was %i", test4expect, test4return);
105  }
106 
107  TTTestLog("\nTest second checkout of first SampleMatrix...");
108 
109  // TEST 5: checking out a matrix returns something
110  TTSampleMatrixPtr myFirstCheckOut2 = NULL;
111  this->checkOutMatrix(myFirstCheckOut2);
112 
113  TTBoolean result5 = { myFirstCheckOut == myFirstCheckOut2 };
114 
115  TTTestAssertion("checkOutMatrix returns the same pointer",
116  result5,
117  testAssertionCount,
118  errorCount);
119 
120  // TEST 6: what is the user count after 2 checkouts?
121  TTInt32 test6expect = 2;
122 
123  TTInt32 test6return = 0;
124  myFirstCheckOut->getAttributeValue("userCount", test6return);
125 
126  TTBoolean result6 = { test6expect == test6return };
127 
128  TTTestAssertion("userCount reports proper value after second checkout",
129  result6,
130  testAssertionCount,
131  errorCount);
132 
133  if(!result6)
134  {
135  TTTestLog("Expected a value of %i, but returned value was %i", test6expect, test6return);
136  }
137 
138  TTTestLog("\nTest if changing lengthInSamples attribute spawns new SampleMatrix...");
139 
140  // TEST 7: changing length at TTBuffer should spawn a new matrix
141  TTSampleMatrixPtr mySecondCheckOut = NULL;
142  this->setAttributeValue("lengthInSamples", 100);
143  this->checkOutMatrix(mySecondCheckOut);
144 
145  TTBoolean result7 = { mySecondCheckOut != myFirstCheckOut };
146 
147  TTTestAssertion("checkOutMatrix returns pointer to different SampleMatrix",
148  result7,
149  testAssertionCount,
150  errorCount);
151 
152  // TEST 8: what is the user count on new checkout?
153  TTInt32 test8expect = 1;
154 
155  TTInt32 test8return = 0;
156  mySecondCheckOut->getAttributeValue("userCount", test8return);
157 
158  TTBoolean result8 = { test8expect == test8return };
159 
160  TTTestAssertion("userCount reports proper value",
161  result8,
162  testAssertionCount,
163  errorCount);
164 
165  if(!result8)
166  {
167  TTTestLog("Expected a value of %i, but returned value was %i", test8expect, test8return);
168  }
169 
170  TTTestLog("\nRepeat with numChannels attribute...");
171 
172  // TEST 9: changing numChannels at TTBuffer should spawn a new matrix
173  TTSampleMatrixPtr myThirdCheckOut = NULL;
174  this->setAttributeValue("numChannels", 2);
175  this->checkOutMatrix(myThirdCheckOut);
176 
177  TTBoolean result9 = { mySecondCheckOut != myThirdCheckOut && myFirstCheckOut != myThirdCheckOut};
178 
179  TTTestAssertion("checkOutMatrix returns pointer to different SampleMatrix",
180  result9,
181  testAssertionCount,
182  errorCount);
183 
184  // TEST 10: what is the user count on new checkout?
185  TTInt32 test10expect = 1;
186 
187  TTInt32 test10return = 0;
188  myThirdCheckOut->getAttributeValue("userCount", test10return);
189 
190  TTBoolean result10 = { test10expect == test10return };
191 
192  TTTestAssertion("userCount reports proper value",
193  result10,
194  testAssertionCount,
195  errorCount);
196 
197  if(!result10)
198  {
199  TTTestLog("Expected a value of %i, but returned value was %i", test10expect, test10return);
200  }
201 
202 
203  /******/
204  TTTestLog("\nAt this point, 3 SampleMatrix objects are checked out via 4 pointers:");
205  TTTestLog("myFirstCheckOut: userCount %i, Active %i, Becoming Idle %i", myFirstCheckOut->getUserCount(), myFirstCheckOut->isBufferPoolStage(kSM_Active), myFirstCheckOut->isBufferPoolStage(kSM_BecomingIdle));
206  TTTestLog("myFirstCheckOut2: userCount %i, Active %i, Becoming Idle %i", myFirstCheckOut2->getUserCount(), myFirstCheckOut2->isBufferPoolStage(kSM_Active), myFirstCheckOut2->isBufferPoolStage(kSM_BecomingIdle));
207  TTTestLog("mySecondCheckOut: userCount %i, Active %i, Becoming Idle %i", mySecondCheckOut->getUserCount(), mySecondCheckOut->isBufferPoolStage(kSM_Active), mySecondCheckOut->isBufferPoolStage(kSM_BecomingIdle));
208  TTTestLog("myThirdCheckOut: userCount %i, Active %i, Becoming Idle %i", myThirdCheckOut->getUserCount(), myThirdCheckOut->isBufferPoolStage(kSM_Active), myThirdCheckOut->isBufferPoolStage(kSM_BecomingIdle));
209  /******/
210 
211 
212  TTTestLog("\nTesting check in process...");
213 
214  // TEST 11: checking out a matrix returns NULL pointer
215  this->checkInMatrix(myFirstCheckOut);
216 
217  TTBoolean result11 = { myFirstCheckOut == NULL };
218 
219  TTTestAssertion("checkInMatrix(myFirstCheckOut) resets pointer to NULL",
220  result11,
221  testAssertionCount,
222  errorCount);
223 
224  // TEST 12: second pointer to first matrix is still valid
225  TTBoolean result12 = { myFirstCheckOut2 != NULL };
226 
227  TTTestAssertion("myFirstCheckOut2 is still a valid pointer",
228  result12,
229  testAssertionCount,
230  errorCount);
231 
232  // TEST 13: poke/peek a sample into first matrix
233  TTSampleValue test13expect = TTRandom64();
234  myFirstCheckOut2->poke(10,0,test13expect);
235 
236  TTSampleValue test13return;
237  myFirstCheckOut2->peek(10,0,test13return);
238 
239  TTBoolean result13 = TTTestFloatEquivalence(test13expect,test13return);
240 
241  TTTestAssertion("poke/peek sample value still works",
242  result13,
243  testAssertionCount,
244  errorCount);
245 
246  if(!result13)
247  {
248  TTTestLog("Expected a value of %i, but returned value was %i", test13expect, test13return);
249  }
250 
251  // TEST 14: checking out a matrix returns NULL pointer
252  this->checkInMatrix(myFirstCheckOut2);
253 
254  TTBoolean result14 = { myFirstCheckOut2 == NULL };
255 
256  TTTestAssertion("checkInMatrix(myFirstCheckOut2) resets pointer to NULL",
257  result14,
258  testAssertionCount,
259  errorCount);
260 
261  // TEST 15: checking out a matrix returns NULL pointer
262  this->checkInMatrix(mySecondCheckOut);
263 
264  TTBoolean result15 = { mySecondCheckOut == NULL };
265 
266  TTTestAssertion("checkInMatrix(mySecondCheckOut) resets pointer to NULL",
267  result15,
268  testAssertionCount,
269  errorCount);
270 
271  // TEST 16: checking out a matrix returns NULL pointer
272  this->checkInMatrix(myThirdCheckOut);
273 
274  TTBoolean result16 = { myThirdCheckOut == NULL };
275 
276  TTTestAssertion("checkInMatrix(myThirdCheckOut) resets pointer to NULL",
277  result16,
278  testAssertionCount,
279  errorCount);
280 
281  // TEST 17: changing samplerate on TTBuffer results in change for TTSampleMatrix
282 
283  TTValue sampleRate17 = 88200;
284  //this->setSampleRate(sampleRate17);
285  this->setAttributeValue(kTTSym_sampleRate, sampleRate17);
286 
287  TTValue testBufferSampleRate17;
288  TTErr error17a = this->getAttributeValue(kTTSym_sampleRate, testBufferSampleRate17);
289 
290  TTBoolean result17a = { error17a == kTTErrNone };
291 
292  TTTestAssertion("getting sampleRate after setting reports no error",
293  result17a,
294  testAssertionCount,
295  errorCount);
296 
297  TTSampleMatrixPtr myMatrix17;
298  this->checkOutMatrix(myMatrix17);
299 
300  TTValue testSampleMatrixSampleRate17;
301  myMatrix17->getAttributeValue(kTTSym_sampleRate, testSampleMatrixSampleRate17);
302 
303  TTBoolean result17b = { testBufferSampleRate17[0] == testSampleMatrixSampleRate17[0] };
304 
305  TTTestAssertion("checked out SampleMatrix reports same sampleRate as Buffer",
306  result17b,
307  testAssertionCount,
308  errorCount);
309 
310  this->checkInMatrix(myMatrix17);
311 
312 
313  // The following is effectively taken care of through check in...
314  //TTObjectRelease(&myFirstCheckOut);
315  //TTObjectRelease(&mySecondCheckOut);
316  //TTObjectRelease(&myThirdCheckOut);
317 
318  // Wrap up the test results to pass back to whoever called this test
319  return TTTestFinish(testAssertionCount, errorCount, returnedTestInfo);
320 
321 
322 }
323 
bool TTBoolean
Boolean flag, same as Boolean on the Mac.
Definition: TTBase.h:167
no longer active, but waiting for remaining users to check in
TTErr getAttributeValue(const TTSymbol name, TTValue &value)
Get an attribute value for an object.
Container object that holds some audio in a chunk of memory.
TTUInt16 getUserCount()
Report the current user count.
in use and pointer to this TTSampleMatrix will be given to users at check out
std::int32_t TTInt32
32 bit signed integer
Definition: TTBase.h:177
TTBoolean isBufferPoolStage(TTBufferPoolStageEnum testValue)
Test to see if current bufferPoolStage matches a test value.
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
No Error.
Definition: TTBase.h:343
virtual TTErr test(TTValue &)
Default (empty) template for unit tests.
TTBuffer manages the check-in/out of TTSampleMatrix pointers.
TTFloat64 TTSampleValue
A value representing a single audio sample.
Definition: TTBase.h:230
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34
TTFOUNDATION_EXPORT TTFloat64 TTRandom64()
Produces a random-valued 64-bit floating-point number in the range [0.0, 1.0].
Definition: TTBase.cpp:611