Jamoma API  0.6.0.a19
TTSampleMatrix.test.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup dspLibrary
4  *
5  * @brief Unit tests for the #TTSampleMatrix class
6  *
7  * @details
8  *
9  * @see TTSampleMatrix
10  *
11  * @authors Tim Place, Nathan Wolek
12  *
13  * @copyright Copyright © 2012 by Tim Place, 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 "TTSampleMatrix.h"
20 
22 {
23  int errorCount = 0;
24  int testAssertionCount = 0;
25 
26  // for tests
27  TTInt16 numChannels = 2;
28  TTUInt32 numSamples = 50000; // TODO: xcode says this is ambiguous when signed?
29  TTFloat32 duration = 1500;
30  int test9Index = 10;
31  int test10Index = 11;
32  TTInt32 test1Return, test2Return, test7Return;
33  TTFloat32 test3Return, test6Return;
34  TTSampleValue test9Return, test10Return, test11Return, test12return, test13return;
35 
36  TTTestLog("Test resizing of the SampleMatrix...");
37 
38 
39  // TEST 1: can we set the number of channels?
40  this->setAttributeValue("numChannels", numChannels);
41 
42  this->getAttributeValue("numChannels", test1Return);
43 
44  TTBoolean result = { numChannels == test1Return };
45 
46  TTTestAssertion("numChannels is set properly",
47  result,
48  testAssertionCount,
49  errorCount);
50 
51  if(!result)
52  {
53  TTTestLog("Expected a value of %i, but returned value was %i", numChannels, test1Return);
54  }
55 
56 
57  // TEST 2: can we set the number of samples?
58  //this->setAttributeValue("lengthInSamples", numSamples); // TODO: xcode says this is ambiguous?
59  this->setLengthInSamples(numSamples);
60 
61  this->getAttributeValue("lengthInSamples", test2Return);
62 
63  TTBoolean result2 = { numSamples == test2Return };
64 
65  TTTestAssertion("lengthInSamples is set properly",
66  result2,
67  testAssertionCount,
68  errorCount);
69  if(!result2)
70  {
71  TTTestLog("Expected a value of %i, but returned value was %i", numSamples, test2Return);
72  }
73 
74 
75  // TEST 3: is the length in sec computed properly after setting length in samples?
76  TTFloat32 computedDuration3 = (numSamples / this->mSampleRate);
77 
78  this->getAttributeValue("lengthInSeconds", test3Return);
79 
80  TTBoolean result3 = TTTestFloatEquivalence(computedDuration3, test3Return);
81 
82  TTTestAssertion("after lengthInSamples is set, lengthInSeconds is correct",
83  result3,
84  testAssertionCount,
85  errorCount);
86 
87  if(!result3)
88  {
89  TTTestLog("Expected a value of %f, but returned value was %f", computedDuration3, test3Return);
90  }
91 
92 
93  // TEST 4: is the matrix of samples the expected size? (lifted from TTMatrix.test.cpp)
94  TTUInt32 computedDataSize4 = sizeof(TTFloat64) * numChannels * numSamples;
95 
96  TTBoolean result4 = { computedDataSize4 == this->mDataSize };
97 
98  TTTestAssertion("correct amount of data storage calculated",
99  result4,
100  testAssertionCount,
101  errorCount);
102 
103  if(!result4)
104  {
105  TTTestLog("Expected a value of %i, but returned value was %i", computedDataSize4, this->mDataSize);
106  }
107 
108 
109  // TEST 5: Is the component stride right? (lifted from TTMatrix.test.cpp)
110  TTBoolean result5 = { sizeof(TTFloat64) == this->mComponentStride };
111 
112  TTTestAssertion("correct byte-stride between values calculated",
113  result5,
114  testAssertionCount,
115  errorCount);
116 
117  if(!result5)
118  {
119  TTTestLog("Expected a value of %i, but returned value was %i", sizeof(TTFloat64), this->mComponentStride);
120  }
121 
122 
123  // TEST 6: can we set the length in seconds?
124  this->setAttributeValue("lengthInSeconds", duration);
125 
126  this->getAttributeValue("lengthInSeconds", test6Return);
127 
128  TTBoolean result6 = TTTestFloatEquivalence(duration, test6Return);
129 
130  TTTestAssertion("lengthInSeconds is set properly",
131  result6,
132  testAssertionCount,
133  errorCount);
134 
135  if(!result6)
136  {
137  TTTestLog("Expected a value of %f, but returned value was %f", duration, test6Return);
138  }
139 
140 
141  // TEST 7: is the length in samples computed properly after setting length in ms?
142  TTUInt32 computedSamples7 = TTUInt32(duration * this->mSampleRate);
143 
144  this->getAttributeValue("lengthInSamples", test7Return);
145 
146  TTBoolean result7 = { computedSamples7 == test7Return };
147 
148  TTTestAssertion("after lengthInSeconds is set, lengthInSamples is correct",
149  result7,
150  testAssertionCount,
151  errorCount);
152 
153  if(!result7)
154  {
155  TTTestLog("Expected a value of %i, but returned value was %i", computedSamples7, test7Return);
156  }
157 
158 
159  // TEST 8 (REPEAT TEST 4 WITH NEW SIZE): is the matrix of samples the expected size?
160  TTUInt32 computedDataSize8 = sizeof(TTFloat64) * numChannels * test7Return;
161 
162  TTBoolean result8 = { computedDataSize8 == this->mDataSize };
163 
164  TTTestAssertion("correct amount of data storage calculated with new length",
165  result8,
166  testAssertionCount,
167  errorCount);
168 
169  if(!result8)
170  {
171  TTTestLog("Expected a value of %i, but returned value was %i", computedDataSize8, this->mDataSize);
172  }
173 
174 
175  // TEST 9 & 10: set the value of two consecutive samples
176  TTSampleValue pokeValue9 = TTRandom64();
177  TTSampleValue pokeValue10 = TTRandom64();
178 
179  this->poke(test9Index, 0, pokeValue9);
180  this->poke(test10Index, 0, pokeValue10);
181 
182  this->peek(test9Index, 0, test9Return);
183  this->peek(test10Index, 0, test10Return);
184 
185  TTBoolean result9 = { pokeValue9 == test9Return };
186 
187  TTTestAssertion("set value one of two consecutive samples",
188  result9,
189  testAssertionCount,
190  errorCount);
191 
192  if(!result9)
193  {
194  TTTestLog("Expected a value of %f, but returned value was %f", pokeValue9, test9Return);
195  }
196 
197  TTBoolean result10 = { pokeValue10 == test10Return };
198 
199  TTTestAssertion("set value two of two consecutive samples",
200  result10,
201  testAssertionCount,
202  errorCount);
203 
204  if(!result10)
205  {
206  TTTestLog("Expected a value of %f, but returned value was %f", pokeValue10, test10Return);
207  }
208 
209 
210  // TEST 10a: confirm that pulling value via "peek" message works too
211 
212  TTValue input(test10Index);
213  input.append(0);
214  TTValue output;
215 
216  this->sendMessage("peek", input, output);
217 
218  TTBoolean result10a = { TTTestFloatEquivalence(pokeValue10, TTSampleValue(output)) };
219 
220  TTTestAssertion("set value two of two consecutive samples",
221  result10a,
222  testAssertionCount,
223  errorCount);
224 
225  if(!result10a)
226  {
227  TTTestLog("Expected a value of %f, but returned value was %f", pokeValue10, TTSampleValue(output));
228  }
229 
230 
231  // TEST 11: test for interpolation between two consecutive samples
232  TTFloat64 computedInterpFraction = TTRandom64();
233  TTFloat64 computedInterpIndex = test9Index + computedInterpFraction;
234  TTSampleValue computedInterpValue11 = (computedInterpFraction * pokeValue10) + ((1.0 - computedInterpFraction) * pokeValue9);
235 
236  this->peeki(computedInterpIndex, 0, test11Return);
237 
238  TTBoolean result11 = TTTestFloatEquivalence(computedInterpValue11, test11Return);
239 
240  TTTestAssertion("interpolate between two consecutive samples",
241  result11,
242  testAssertionCount,
243  errorCount);
244 
245  if(!result11)
246  {
247  TTTestLog("Expected a value of %f, but returned value was %f", computedInterpValue11, test11Return);
248  }
249 
250 
251  // TODO: inbounds testing on hold until sorted out at TTMatrix parent class
252 
253  // TEST 12 & 13: test whether out of bounds indices produce errors at head
254 
255  TTInt32 computedIndex12 = -1; // 1 before the head
256  TTInt32 computedIndex13 = 0; // the head
257  TTErr test12Err = this->peek(computedIndex12, 0, test12return);
258  TTErr test13Err = this->peek(computedIndex13, 0, test13return);
259 
260  TTBoolean result12 = { test12Err == kTTErrOutOfBounds };
261  TTBoolean result13 = { test13Err == kTTErrNone };
262 
263  TTTestAssertion("peeking sample before index 0 produces an error",
264  result12,
265  testAssertionCount,
266  errorCount);
267 
268  if(!result12)
269  {
270  TTTestLog("Expected a value of %i, but returned value was %i", kTTErrOutOfBounds, test12Err);
271  }
272 
273 
274  TTTestAssertion("peeking sample at index 0 produces no error",
275  result13,
276  testAssertionCount,
277  errorCount);
278 
279  if(!result13)
280  {
281  TTTestLog("Expected a value of %i, but returned value was %i", kTTErrNone, test13Err);
282  }
283 
284  // TEST 14 & 15: test whether out of bounds indices produce errors at tail
285 
286  TTInt32 computedIndex14 = test7Return; // should be latest size in samples
287  TTInt32 computedIndex15 = test7Return - 1; // the tail is actually one less
288  TTErr test14Err = this->poke(computedIndex14, 0, test12return);
289  TTErr test15Err = this->poke(computedIndex15, 0, test13return);
290 
291  TTBoolean result14 = { test14Err == kTTErrOutOfBounds };
292  TTBoolean result15 = { test15Err == kTTErrNone };
293 
294  TTTestAssertion("poking sample after index max produces an error",
295  result14,
296  testAssertionCount,
297  errorCount);
298 
299  if(!result14)
300  {
301  TTTestLog("Expected a value of %i, but returned value was %i", kTTErrOutOfBounds, test14Err);
302  }
303 
304 
305  TTTestAssertion("poking sample at index max produces no error",
306  result15,
307  testAssertionCount,
308  errorCount);
309 
310  if(!result15)
311  {
312  TTTestLog("Expected a value of %i, but returned value was %i", kTTErrNone, test15Err);
313  }
314 
315 
316  // TEST 16 & 17: incrementing & decrementing userCount
317 
318  TTUInt16 test16expect = 4;
319  TTUInt16 test17expect = 2;
320 
321  this->incrementUserCount();
322  this->incrementUserCount();
323  this->incrementUserCount();
324  this->incrementUserCount();
325  TTUInt16 test16return = this->mUserCount;
326 
327  this->decrementUserCount();
328  this->decrementUserCount();
329  TTUInt16 test17return = this->mUserCount;
330 
331  TTBoolean result16 = { test16expect == test16return };
332  TTBoolean result17 = { test17expect == test17return };
333 
334  TTTestAssertion("incrementing userCount produces expected results",
335  result16,
336  testAssertionCount,
337  errorCount);
338 
339  if(!result16)
340  {
341  TTTestLog("Expected a value of %i, but returned value was %i", test16expect, test16return);
342  }
343 
344 
345  TTTestAssertion("decrementing userCount produces expected results",
346  result17,
347  testAssertionCount,
348  errorCount);
349 
350  if(!result17)
351  {
352  TTTestLog("Expected a value of %i, but returned value was %i", test17expect, test17return);
353  }
354 
355  // TEST 18 & 19: setting & testing bufferPoolStage
356 
357  TTBoolean test18expect = true;
358  TTBoolean test19expect = false;
359 
361  TTBoolean test18return = this->isBufferPoolStage(kSM_Active);
362  TTBoolean test19return = this->isBufferPoolStage(kSM_BecomingIdle);
363 
364  TTBoolean result18 = { test18expect == test18return };
365  TTBoolean result19 = { test19expect == test19return };
366 
367  TTTestAssertion("reports bufferPoolStage as active",
368  result18,
369  testAssertionCount,
370  errorCount);
371 
372  if(!result18)
373  {
374  TTTestLog("Expected a value of %i, but returned value was %i", test18expect, test18return);
375  }
376 
377 
378  TTTestAssertion("reports bufferPoolStage as NOT becoming idle",
379  result19,
380  testAssertionCount,
381  errorCount);
382 
383  if(!result19)
384  {
385  TTTestLog("Expected a value of %i, but returned value was %i", test19expect, test19return);
386  }
387 
388 
389 
390  /*
391 
392  int badSampleCount = 0;
393  TTAudioObjectBasePtr samplematrixObject = NULL;
394  TTAudioSignalPtr input = NULL;
395  TTAudioSignalPtr output = NULL;
396 
397  // TODO: test filling with sine wave
398  // TODO: test scaling (applying gain)
399  // TODO: test normalizing (with optional arg, and also without an optional arg)
400 
401  TTObjectBaseInstantiate("samplematrix", &samplematrixObject, kTTVal1);
402 
403  TTObjectBaseRelease(&input);
404  TTObjectBaseRelease(&output);
405  TTObjectBaseRelease(&samplematrixObject);
406 
407  */
408 
409 
410  // Wrap up the test results to pass back to whoever called this test
411  return TTTestFinish(testAssertionCount, errorCount, returnedTestInfo);
412 }
413 
TTErr sendMessage(const TTSymbol name)
TODO: Document this function.
bool TTBoolean
Boolean flag, same as Boolean on the Mac.
Definition: TTBase.h:167
std::uint16_t TTUInt16
16 bit unsigned integer
Definition: TTBase.h:176
Attempted to access memory outside a matrix or array (in a TTMatrix & TTSampleMatrix).
Definition: TTBase.h:355
TTErr decrementUserCount()
Decrease the user count by one.
no longer active, but waiting for remaining users to check in
TTSampleMatrix holds some audio in a chunk of memory.
TTErr getAttributeValue(const TTSymbol name, TTValue &value)
Get an attribute value for an object.
TTErr setAttributeValue(const TTSymbol name, TTValue &value)
Set an attribute value for an object.
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
TTUInt32 mDataSize
mTypeSizeInBytes * mDataCount
Definition: TTMatrixBase.h:57
void append(const T &anElementValueToAppend)
Insert a single TTElement at the end.
Definition: TTValue.h:243
std::int16_t TTInt16
16 bit signed integer
Definition: TTBase.h:175
TTErr incrementUserCount()
Increase the user count by one.
virtual TTErr test(TTValue &returnedTestInfo)
Unit testing.
float TTFloat32
32 bit floating point number
Definition: TTBase.h:187
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
std::uint32_t TTUInt32
32 bit unsigned integer
Definition: TTBase.h:178
TTUInt16 mUserCount
how many objects out there are currently using this TTSampleMatrix
No Error.
Definition: TTBase.h:343
TTUInt32 mComponentStride
how many bytes from one the beginning one matrix component to the next
Definition: TTMatrixBase.h:51
TTErr setLengthInSamples(const TTValue &newLengthInSamples)
Attribute accessor: set the buffer length specified as a number of samples.
TTErr setBufferPoolStage(TTBufferPoolStageEnum newValue)
Set the current bufferPoolStage to a new value.
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