Jamoma API  0.6.0.a19
TTInterpolate.test.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup foundationLibrary
4  *
5  * @brief #TTInterpolateTest is an class used for unit tests of the interpolation algorithms in #TTInterpolate.h.
6  *
7  * @details
8  *
9  * @author Trond Lossius
10  *
11  * @copyright Copyright © 2008, Timothy Place @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 "TTInterpolate.test.h"
18 
19 #define thisTTClass TTInterpolateTest
20 #define thisTTClassName "interpolate.test"
21 #define thisTTClassTags "test, foundation"
22 
23 
24 /** Constructor macro
25  */
27 {;}
28 
29 
30 
31 /** Destructor
32  */
33 TTInterpolateTest::~TTInterpolateTest()
34 {;}
35 
36 #pragma mark -
37 #pragma mark Test for the various interpolation algorithms
38 #pragma mark -
39 
40 
41 /** Perform one linear interpolation and compare the outcome to expected value.
42  @param x0 Sample value at prior integer index
43  @param x1 Sample value at next integer index
44  @param delta The fractional value for which we want to perform the interpolation.
45  delta=0 => x0 @n
46  delta=1 => x1
47  @param expectedValue The expected outcome of the interpolation
48  @return TRUE if the interpolat returnes the expected value, else FALSE
49  */
50 TTFloat64 InterpolateAndTestLinear(const TTFloat64 x0, const TTFloat64 x1, const TTFloat64& aDelta, TTFloat64 anExpectedValue)
51 {
52  TTFloat64 interpolatedValue = TTInterpolateLinear(x0, x1, aDelta);
53  TTBoolean result = TTTestFloatEquivalence(interpolatedValue , anExpectedValue);
54  if (!result)
55  TTTestLog("BAD INTERPOLATION @ delta=%.5f ( value=%.10f expected=%.10f )", aDelta, interpolatedValue, anExpectedValue);
56  return result;
57 }
58 
59 
60 
61 /** Test for correct linear interpolations performance.
62  @param anErrorCount The number of asserts that failed
63  @param aTestAssertionCount The numebr of asserts carried out
64  */
65 void TestLinear(int& anErrorCount, int& aTestAssertionCount)
66 {
67  int badSampleCount = 0;
68 
69  TTFloat64 x0 = 3.0;
70  TTFloat64 x1 = -1.0;
71 
72  TTTestLog("");
73  TTTestLog("Testing linear interpolation");
74 
75  badSampleCount += !InterpolateAndTestLinear(x0, x1, 0.00, 3.0);
76  badSampleCount += !InterpolateAndTestLinear(x0, x1, 0.25, 2.0);
77  badSampleCount += !InterpolateAndTestLinear(x0, x1, 0.50, 1.0);
78  badSampleCount += !InterpolateAndTestLinear(x0, x1, 0.75, 0.0);
79  badSampleCount += !InterpolateAndTestLinear(x0, x1, 1.00, -1.0);
80 
81  TTTestAssertion("Produces expected results with linear interpolation",
82  badSampleCount == 0,
83  aTestAssertionCount,
84  anErrorCount);
85 }
86 
87 
88 /** Perform one cosine interpolation and compare the outcome to expected value.
89  @param x0 Sample value at prior integer index
90  @param x1 Sample value at next integer index
91  @param aDelta The fractional value for which we want to perform the interpolation.
92  aDelta=0 => x0 @n
93  aDelta=1 => x1
94  @param expectedValue The expected outcome of the interpolation
95  @return TRUE if the interpolat returnes the expected value, else FALSE
96  */
97 TTFloat64 InterpolateAndTestCosine(const TTFloat64 x0, const TTFloat64 x1, const TTFloat64& aDelta, TTFloat64 anExpectedValue)
98 {
99  TTFloat64 interpolatedValue = TTInterpolateCosine(x0, x1, aDelta);
100  TTBoolean result = TTTestFloatEquivalence(interpolatedValue , anExpectedValue);
101  if (!result)
102  TTTestLog("BAD INTERPOLATION @ delta=%.5f ( value=%.10f expected=%.10f )", aDelta, interpolatedValue, anExpectedValue);
103  return result;
104 }
105 
106 
107 /** Test for correct cosine interpolations performance.
108  @param anErrorCount The number of asserts that failed
109  @param aTestAssertionCount The numebr of asserts carried out
110  */
111 void TestCosine(int& anErrorCount, int& aTestAssertionCount)
112 {
113  int badSampleCount = 0;
114 
115  TTFloat64 x0 = 3.0;
116  TTFloat64 x1 = -1.0;
117 
118  TTTestLog("");
119  TTTestLog("Testing cosine interpolation");
120 
121  badSampleCount += !InterpolateAndTestCosine(x0, x1, 0.00, 3.0);
122  badSampleCount += !InterpolateAndTestCosine(x0, x1, (1./3.), 2.0);
123  badSampleCount += !InterpolateAndTestCosine(x0, x1, 0.50, 1.0);
124  badSampleCount += !InterpolateAndTestCosine(x0, x1, (2./3.), 0.0);
125  badSampleCount += !InterpolateAndTestCosine(x0, x1, 1.00, -1.0);
126 
127  TTTestAssertion("Produces expected results with linear interpolation",
128  badSampleCount == 0,
129  aTestAssertionCount,
130  anErrorCount);
131 }
132 
133 
134 /** Perform one cubic interpolation and compare the outcome to expected value.
135  @param x0 Sample value at integer index prior to x0
136  @param x1 Sample value at prior integer index
137  @param x2 Sample value at next integer index
138  @param x3 Sample value at integer index after y
139  @param aDelta Fractional location between x1 (0) and x2 (1)
140  @param delta The fractional value for which we want to perform the interpolation.
141  aDelta=0 => x0 @n
142  aDelta=1 => x1
143  @param expectedValue The expected outcome of the interpolation
144  @return TRUE if the interpolat returnes the expected value, else FALSE
145  */
146 TTFloat64 InterpolateAndTestCubic(const TTFloat64 x0, const TTFloat64 x1, const TTFloat64 x2, const TTFloat64 x3, const TTFloat64& aDelta, TTFloat64 anExpectedValue)
147 {
148  TTFloat64 interpolatedValue = TTInterpolateCubic(x0, x1, x2, x3, aDelta);
149  TTBoolean result = TTTestFloatEquivalence(interpolatedValue , anExpectedValue);
150  if (!result)
151  TTTestLog("BAD INTERPOLATION @ delta=%.5f ( value=%.10f expected=%.10f )", aDelta, interpolatedValue, anExpectedValue);
152  return result;
153 }
154 
155 
156 /** Test for correct cubic interpolations performance.
157  @param anErrorCount The number of asserts that failed
158  @param aTestAssertionCount The numebr of asserts carried out
159  */
160 void TestCubic(int& anErrorCount, int& aTestAssertionCount)
161 {
162  /* We perform the interpolation on four values of the following function:
163  g(delta) = cos (PI*delta) + delta + 1
164 
165  This gives the following input values to the interpolating function:
166 
167  g(-1) = -1
168  g( 0) = 2
169  g( 1) = 1
170  g( 2) = 4
171 
172  The difference is calculated as
173  ∆g(delta) = (g(delta+1)-g(delta-1)) / 2
174 
175  This leads to:
176 
177  ∆g(0) = ( 1 -(-1) )/2 = 1
178  ∆g(1) = ( 4 - 2)/2 = 1
179 
180  The cubic interpolation function f(delta) is then required to fullfil the following four conditions:
181 
182  (A) f( 0) = 2
183  (B) f( 1) = 1
184  (C) f'(0) = 1
185  (D) f'(1) = 1
186 
187  Analytically, when solved, these four conditions are fulfilled by the following 3rd order polynomial:
188  f(delta) = 4 delta^3 - 6 delta^2 + delta + 2
189 
190  The following test compares interpolated values to expected values for this function.
191  */
192 
193  int badSampleCount = 0;
194 
195  TTFloat64 x0 = -1.0;
196  TTFloat64 x1 = 2.0;
197  TTFloat64 x2 = 1.0;
198  TTFloat64 x3 = 4.0;
199 
200  TTTestLog("");
201  TTTestLog("Testing cubic interpolation");
202 
203  // Test for delta = -1 => f(delta) = -9
204  badSampleCount += !InterpolateAndTestCubic(x0, x1, x2, x3, -1.00, -9.0);
205  // Test for delta = 0 => f(delta) = 2
206  badSampleCount += !InterpolateAndTestCubic(x0, x1, x2, x3, 0.00, 2.0);
207  // Test for delta = 1 => f(delta) = 1
208  badSampleCount += !InterpolateAndTestCubic(x0, x1, x2, x3, 1.00, 1.0);
209  // Test for delta = 2 => f(delta) = 12
210  badSampleCount += !InterpolateAndTestCubic(x0, x1, x2, x3, 2.00, 12.0);
211 
212  TTTestAssertion("Produces expected results with cubic interpolation",
213  badSampleCount == 0,
214  aTestAssertionCount,
215  anErrorCount);
216 }
217 
218 
219 #pragma mark -
220 #pragma mark The main function
221 #pragma mark -
222 
223 
224 // The main test method
225 TTErr TTInterpolateTest::test(TTValue& returnedTestInfo)
226 {
227  int errorCount = 0;
228  int testAssertionCount = 0;
229 
230  TestLinear(errorCount, testAssertionCount);
231  TestCosine(errorCount, testAssertionCount);
232  TestCubic(errorCount, testAssertionCount);
233 
234  return TTTestFinish(testAssertionCount, errorCount, returnedTestInfo);
235 }
236 
T TTInterpolateLinear(const T &x0, const T &x1, const double &delta)
Isolate the fractional part from a double.
Definition: TTInterpolate.h:47
bool TTBoolean
Boolean flag, same as Boolean on the Mac.
Definition: TTBase.h:167
void TestCosine(int &anErrorCount, int &aTestAssertionCount)
Test for correct cosine interpolations performance.
TTFloat64 InterpolateAndTestCosine(const TTFloat64 x0, const TTFloat64 x1, const TTFloat64 &aDelta, TTFloat64 anExpectedValue)
Perform one cosine interpolation and compare the outcome to expected value.
void TestLinear(int &anErrorCount, int &aTestAssertionCount)
Test for correct linear interpolations performance.
TTFloat64 InterpolateAndTestLinear(const TTFloat64 x0, const TTFloat64 x1, const TTFloat64 &aDelta, TTFloat64 anExpectedValue)
Perform one linear interpolation and compare the outcome to expected value.
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
void TestCubic(int &anErrorCount, int &aTestAssertionCount)
Test for correct cubic interpolations performance.
TTInterpolateTest is an class used for unit tests of the interpolation algorithms in #TTInterpolate...
TTFloat64 InterpolateAndTestCubic(const TTFloat64 x0, const TTFloat64 x1, const TTFloat64 x2, const TTFloat64 x3, const TTFloat64 &aDelta, TTFloat64 anExpectedValue)
Perform one cubic interpolation and compare the outcome to expected value.
T TTInterpolateCubic(const T &x0, const T &x1, const T &x2, const T &x3, const double &aDelta)
Cubic interpolation.
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
T TTInterpolateCosine(const T &x, const T &y, const double &a)
Cosine interpolation.
Definition: TTInterpolate.h:65
TT_OBJECT_CONSTRUCTOR
Constructor macro.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34