Jamoma API  0.6.0.a19
TTPowerFunction.test.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup dspFunctionLib
4  *
5  * @brief Unit tests for the Jamoma DSP #TTPowerFunction of the #TTFunctionLib
6  *
7  * @details
8  *
9  * @authors Trond Lossius
10  *
11  * @copyright Copyright © 2012 by Trond Lossius @n
12  * This code is licensed under the terms of the "New BSD License" @n
13  * http://creativecommons.org/licenses/BSD/
14  */
15 
16 
17 #include "TTFunction.h"
18 #include "TTPowerFunction.h"
19 
20 
21 /*
22 
23  Equations for the tests:
24 
25  We are using
26  powerValue = 1
27 
28  mK = pow(2, powerValue) = pow(2, 1.) = 2;
29 
30  Test 1: Currently we only test for the simple no symmetry case:
31 
32  y = pow(x, mK);
33 
34  The coefficients are calculated in Octave using:
35 
36  x = linspace(0,1,128);
37  printf("%.16e,\n", x);
38 
39  y = power(x, 2);
40  printf("%.16e,\n", y)
41 
42  */
43 
44 
46 {
47  int errorCount = 0;
48  int testAssertionCount = 0;
49  int badSampleCount = 0;
50  TTAudioSignalPtr input = NULL;
51  TTAudioSignalPtr output = NULL;
52  int N = 128;
53  TTValue v;
54 
55 
56  TTFloat64 inputSignal1[128] = {
57  0.0000000000000000e+00,
58  7.8740157480314960e-03,
59  1.5748031496062992e-02,
60  2.3622047244094488e-02,
61  3.1496062992125984e-02,
62  3.9370078740157480e-02,
63  4.7244094488188976e-02,
64  5.5118110236220472e-02,
65  6.2992125984251968e-02,
66  7.0866141732283464e-02,
67  7.8740157480314960e-02,
68  8.6614173228346455e-02,
69  9.4488188976377951e-02,
70  1.0236220472440945e-01,
71  1.1023622047244094e-01,
72  1.1811023622047244e-01,
73  1.2598425196850394e-01,
74  1.3385826771653542e-01,
75  1.4173228346456693e-01,
76  1.4960629921259844e-01,
77  1.5748031496062992e-01,
78  1.6535433070866140e-01,
79  1.7322834645669291e-01,
80  1.8110236220472442e-01,
81  1.8897637795275590e-01,
82  1.9685039370078738e-01,
83  2.0472440944881889e-01,
84  2.1259842519685040e-01,
85  2.2047244094488189e-01,
86  2.2834645669291337e-01,
87  2.3622047244094488e-01,
88  2.4409448818897639e-01,
89  2.5196850393700787e-01,
90  2.5984251968503935e-01,
91  2.6771653543307083e-01,
92  2.7559055118110237e-01,
93  2.8346456692913385e-01,
94  2.9133858267716534e-01,
95  2.9921259842519687e-01,
96  3.0708661417322836e-01,
97  3.1496062992125984e-01,
98  3.2283464566929132e-01,
99  3.3070866141732280e-01,
100  3.3858267716535434e-01,
101  3.4645669291338582e-01,
102  3.5433070866141730e-01,
103  3.6220472440944884e-01,
104  3.7007874015748032e-01,
105  3.7795275590551181e-01,
106  3.8582677165354329e-01,
107  3.9370078740157477e-01,
108  4.0157480314960631e-01,
109  4.0944881889763779e-01,
110  4.1732283464566927e-01,
111  4.2519685039370081e-01,
112  4.3307086614173229e-01,
113  4.4094488188976377e-01,
114  4.4881889763779526e-01,
115  4.5669291338582674e-01,
116  4.6456692913385828e-01,
117  4.7244094488188976e-01,
118  4.8031496062992124e-01,
119  4.8818897637795278e-01,
120  4.9606299212598426e-01,
121  5.0393700787401574e-01,
122  5.1181102362204722e-01,
123  5.1968503937007871e-01,
124  5.2755905511811019e-01,
125  5.3543307086614167e-01,
126  5.4330708661417326e-01,
127  5.5118110236220474e-01,
128  5.5905511811023623e-01,
129  5.6692913385826771e-01,
130  5.7480314960629919e-01,
131  5.8267716535433067e-01,
132  5.9055118110236215e-01,
133  5.9842519685039375e-01,
134  6.0629921259842523e-01,
135  6.1417322834645671e-01,
136  6.2204724409448819e-01,
137  6.2992125984251968e-01,
138  6.3779527559055116e-01,
139  6.4566929133858264e-01,
140  6.5354330708661412e-01,
141  6.6141732283464560e-01,
142  6.6929133858267720e-01,
143  6.7716535433070868e-01,
144  6.8503937007874016e-01,
145  6.9291338582677164e-01,
146  7.0078740157480313e-01,
147  7.0866141732283461e-01,
148  7.1653543307086609e-01,
149  7.2440944881889768e-01,
150  7.3228346456692917e-01,
151  7.4015748031496065e-01,
152  7.4803149606299213e-01,
153  7.5590551181102361e-01,
154  7.6377952755905509e-01,
155  7.7165354330708658e-01,
156  7.7952755905511806e-01,
157  7.8740157480314954e-01,
158  7.9527559055118113e-01,
159  8.0314960629921262e-01,
160  8.1102362204724410e-01,
161  8.1889763779527558e-01,
162  8.2677165354330706e-01,
163  8.3464566929133854e-01,
164  8.4251968503937003e-01,
165  8.5039370078740162e-01,
166  8.5826771653543310e-01,
167  8.6614173228346458e-01,
168  8.7401574803149606e-01,
169  8.8188976377952755e-01,
170  8.8976377952755903e-01,
171  8.9763779527559051e-01,
172  9.0551181102362199e-01,
173  9.1338582677165348e-01,
174  9.2125984251968507e-01,
175  9.2913385826771655e-01,
176  9.3700787401574803e-01,
177  9.4488188976377951e-01,
178  9.5275590551181100e-01,
179  9.6062992125984248e-01,
180  9.6850393700787396e-01,
181  9.7637795275590555e-01,
182  9.8425196850393704e-01,
183  9.9212598425196852e-01,
184  1.0000000000000000e+00
185  };
186 
187  TTFloat64 expectedSignal1[128] = {
188  0.0000000000000000e+00,
189  6.2000124000247993e-05,
190  2.4800049600099197e-04,
191  5.5800111600223194e-04,
192  9.9200198400396788e-04,
193  1.5500031000062000e-03,
194  2.2320044640089277e-03,
195  3.0380060760121521e-03,
196  3.9680079360158715e-03,
197  5.0220100440200883e-03,
198  6.2000124000248001e-03,
199  7.5020150040300080e-03,
200  8.9280178560357110e-03,
201  1.0478020956041912e-02,
202  1.2152024304048608e-02,
203  1.3950027900055799e-02,
204  1.5872031744063486e-02,
205  1.7918035836071669e-02,
206  2.0088040176080353e-02,
207  2.2382044764089531e-02,
208  2.4800049600099201e-02,
209  2.7342054684109364e-02,
210  3.0008060016120032e-02,
211  3.2798065596131194e-02,
212  3.5712071424142844e-02,
213  3.8750077500154995e-02,
214  4.1912083824167647e-02,
215  4.5198090396180800e-02,
216  4.8608097216194433e-02,
217  5.2142104284208561e-02,
218  5.5800111600223197e-02,
219  5.9582119164238334e-02,
220  6.3488126976253945e-02,
221  6.7518135036270063e-02,
222  7.1672143344286676e-02,
223  7.5950151900303811e-02,
224  8.0352160704321413e-02,
225  8.4878169756339508e-02,
226  8.9528179056358126e-02,
227  9.4302188604377210e-02,
228  9.9200198400396802e-02,
229  1.0422220844441688e-01,
230  1.0936821873643746e-01,
231  1.1463822927645856e-01,
232  1.2003224006448013e-01,
233  1.2555025110050219e-01,
234  1.3119226238452478e-01,
235  1.3695827391654783e-01,
236  1.4284828569657138e-01,
237  1.4886229772459544e-01,
238  1.5500031000061998e-01,
239  1.6126232252464506e-01,
240  1.6764833529667059e-01,
241  1.7415834831669660e-01,
242  1.8079236158472320e-01,
243  1.8755037510075021e-01,
244  1.9443238886477773e-01,
245  2.0143840287680573e-01,
246  2.0856841713683424e-01,
247  2.1582243164486328e-01,
248  2.2320044640089279e-01,
249  2.3070246140492279e-01,
250  2.3832847665695334e-01,
251  2.4607849215698432e-01,
252  2.5395250790501578e-01,
253  2.6195052390104778e-01,
254  2.7007254014508025e-01,
255  2.7831855663711325e-01,
256  2.8668857337714670e-01,
257  2.9518259036518074e-01,
258  3.0380060760121524e-01,
259  3.1254262508525016e-01,
260  3.2140864281728565e-01,
261  3.3039866079732155e-01,
262  3.3951267902535803e-01,
263  3.4875069750139492e-01,
264  3.5811271622543250e-01,
265  3.6759873519747044e-01,
266  3.7720875441750884e-01,
267  3.8694277388554776e-01,
268  3.9680079360158721e-01,
269  4.0678281356562712e-01,
270  4.1688883377766750e-01,
271  4.2711885423770840e-01,
272  4.3747287494574982e-01,
273  4.4795089590179182e-01,
274  4.5855291710583423e-01,
275  4.6927893855787711e-01,
276  4.8012896025792051e-01,
277  4.9110298220596438e-01,
278  5.0220100440200877e-01,
279  5.1342302684605357e-01,
280  5.2476904953809911e-01,
281  5.3623907247814495e-01,
282  5.4783309566619132e-01,
283  5.5955111910223820e-01,
284  5.7139314278628550e-01,
285  5.8335916671833343e-01,
286  5.9544919089838177e-01,
287  6.0766321532643053e-01,
288  6.2000124000247991e-01,
289  6.3246326492652993e-01,
290  6.4504929009858025e-01,
291  6.5775931551863109e-01,
292  6.7059334118668235e-01,
293  6.8355136710273412e-01,
294  6.9663339326678642e-01,
295  7.0983941967883923e-01,
296  7.2316944633889280e-01,
297  7.3662347324694655e-01,
298  7.5020150040300082e-01,
299  7.6390352780705562e-01,
300  7.7772955545911093e-01,
301  7.9167958335916666e-01,
302  8.0575361150722291e-01,
303  8.1995163990327968e-01,
304  8.3427366854733698e-01,
305  8.4871969743939490e-01,
306  8.6328972657945313e-01,
307  8.7798375596751199e-01,
308  8.9280178560357115e-01,
309  9.0774381548763095e-01,
310  9.2280984561969115e-01,
311  9.3799987599975188e-01,
312  9.5331390662781335e-01,
313  9.6875193750387512e-01,
314  9.8431396862793730e-01,
315  1.0000000000000000e+00
316  };
317 
318  // Setup function
319  this->setAttributeValue(TT("function"), TT("power"));
320 
321  // TEST 1: Default powerValue attribute
322  TTFloat64 defaultPowerValue;
323  this->getAttributeValue(TT("powerValue"), defaultPowerValue);
324  TTTestAssertion("Default value for power attribute",
325  TTTestFloatEquivalence(defaultPowerValue, 1.0),
326  testAssertionCount,
327  errorCount);
328 
329  // TEST 2: Default symmetry attribute
330  TTSymbol defaultSymmetry;
331  this->getAttributeValue(TT("symmetry"), defaultSymmetry);
332  TTTestAssertion("Default value for power attribute",
333  defaultSymmetry == "none",
334  testAssertionCount,
335  errorCount);
336 
337  // Create 1 channel audio signal objects
338  TTObjectBaseInstantiate(kTTSym_audiosignal, &input, 1);
339  TTObjectBaseInstantiate(kTTSym_audiosignal, &output, 1);
340  input->allocWithVectorSize(N);
341  output->allocWithVectorSize(N);
342 
343  // Create a signal to be transformed and then process it
344  input->clear();
345  for (int i=0; i<N; i++)
346  input->mSampleVectors[0][i] = inputSignal1[i];
347  this->process(input, output);
348 
349  // TEST 3: Processed signal with default attributes
350  for (int n=0; n<N; n++)
351  {
352  TTBoolean result = !TTTestFloatEquivalence(output->mSampleVectors[0][n], expectedSignal1[n]);
353  badSampleCount += result;
354  if (result)
355  TTTestLog("BAD SAMPLE @ n=%i ( value=%.10f expected=%.10f )", n, output->mSampleVectors[0][n], expectedSignal1[n]);
356  }
357 
358  if (badSampleCount)
359  TTTestLog("badSampleCount is %ld", badSampleCount);
360 
361  TTTestAssertion("Produces correct function values with default attributes",
362  badSampleCount == 0,
363  testAssertionCount,
364  errorCount);
365 
366 
367  // TEST 4: Set powerValue attribute to non-default value
368  TTFloat64 alteredPowerValue;
369  this->setAttributeValue(TT("powerValue"), 0.0);
370  this->getAttributeValue(TT("powerValue"), alteredPowerValue);
371  TTTestAssertion("Altered value for power attribute",
372  TTTestFloatEquivalence(alteredPowerValue, 0.0),
373  testAssertionCount,
374  errorCount);
375 
376  // TEST 5: Processed signal with powerValue=0. The result should be a straight line, and hence output should equal the inputSignal1 vector.
377  badSampleCount = 0;
378  this->process(input, output);
379  for (int n=0; n<N; n++)
380  {
381  TTBoolean result = !TTTestFloatEquivalence(output->mSampleVectors[0][n], inputSignal1[n]);
382  badSampleCount += result;
383  if (result)
384  TTTestLog("BAD SAMPLE @ n=%i ( value=%.10f expected=%.10f )", n, output->mSampleVectors[0][n], inputSignal1[n]);
385  }
386 
387  if (badSampleCount)
388  TTTestLog("badSampleCount is %ld", badSampleCount);
389 
390  TTTestAssertion("Processing with altered powerValue attribute",
391  badSampleCount == 0,
392  testAssertionCount,
393  errorCount);
394 
395  TTObjectBaseRelease(&input);
396  TTObjectBaseRelease(&output);
397 
398  // Wrap up test results and pass back to whoever called test
399  return TTTestFinish(testAssertionCount, errorCount, returnedTestInfo);
400 
401 }
bool TTBoolean
Boolean flag, same as Boolean on the Mac.
Definition: TTBase.h:167
TTErr TTObjectBaseRelease(TTObjectBasePtr *anObject)
DEPRECATED.
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
TTPowerFunction Unit for Jamoms DSP
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
TTErr clear()
Zero out all of the sample values in the audio signal.
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
TTErr TTObjectBaseInstantiate(const TTSymbol className, TTObjectBasePtr *returnedObjectPtr, const TTValue arguments)
DEPRECATED.
The TTAudioSignal class represents N vectors of audio samples for M channels.
Definition: TTAudioSignal.h:57
TTSampleValue ** mSampleVectors
An array of pointers to the first sample in each vector. Declared Public for fast access...
Definition: TTAudioSignal.h:74
TTErr process(TTAudioSignal &in, TTAudioSignal &out)
Process the input signal, resulting in an output signal.
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
TTFunction is a generalized function wrapper for Jamoma DSP
TTErr allocWithVectorSize(const TTUInt16 newVectorSize)
Allocate memory for all channels at the specified vectorsize, if the vectorsize is different from the...
virtual TTErr test(TTValue &returnedTestInfo)
Unit Tests.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34