Jamoma API  0.6.0.a19
TTUnitTest.cpp
1 /*
2  * TTUnitTest
3  * Copyright © 2011, Timothy Place and Trond Lossius
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 "TTUnitTest.h"
10 #ifdef TT_PLATFORM_WIN
11 #include <Windows.h>
12 #endif
13 
14 
15 //static const TTFloat32 kTTTestFloat32Epsilon = 0.00001f;
16 //static const TTFloat64 kTTTestFloat64Epsilon = 0.000000001;
17 static const TTFloat32 kTTTestFloat32Infinity = std::numeric_limits<float>::infinity();
18 static const TTFloat64 kTTTestFloat64Infinity = std::numeric_limits<double>::infinity();
19 
20 
21 
22 TTBoolean TTTestFloatEquivalence(TTFloat32 aFloat, TTFloat32 bFloat, TTBoolean expectedResult, TTFloat32 epsilon)
23 {
24  if (epsilon <= 0.) {
25  TTLogMessage(" TTTestFloatEquivalence: epsilon must be a positive number\n");
26  return false;
27  }
28 
29  TTBoolean result;
30 
31  if ((aFloat == kTTTestFloat32Infinity)||(bFloat == kTTTestFloat32Infinity)) {
32  if (aFloat==bFloat)
33  result = true;
34  else
35  result = false;
36  }
37  else {
38  TTFloat32 aAbs = fabs(aFloat);
39  TTFloat32 bAbs = fabs(bFloat);
40  TTFloat32 absoluteOrRelative = (1.0f > aAbs ? 1.0f : aAbs);
41  absoluteOrRelative = (absoluteOrRelative > bAbs ? absoluteOrRelative : bAbs);
42  if (fabs(aFloat - bFloat) <= epsilon * absoluteOrRelative)
43  result = true;
44  else
45  result = false;
46  }
47  // Was this the expected result?
48  if (result == expectedResult)
49  return true;
50  else {
51 #ifdef NOISY_FAILURE
52  TTLogMessage("\n");
53  TTLogMessage(" TTTestFloatEquivalence: Unexpected result\n");
54  TTLogMessage("\n");
55  TTLogMessage(" aFloat = %.8e\n", aFloat);
56  TTLogMessage(" bFloat = %.8e\n", bFloat);
57  TTLogMessage(" result = %s\n", (result)?"true":"false");
58  TTLogMessage("\n");
59 #endif
60  return false;
61  }
62 }
63 
64 
65 TTBoolean TTTestFloatEquivalence(TTFloat64 aFloat, TTFloat64 bFloat, TTBoolean expectedResult, TTFloat64 epsilon)
66 {
67  if (epsilon <= 0.) {
68  TTLogMessage(" TTTestFloatEquivalence: epsilon must be a positive number\n");
69  return false;
70  }
71 
72  TTBoolean result;
73 
74  if ((aFloat == kTTTestFloat64Infinity)||(bFloat == kTTTestFloat64Infinity)) {
75  if (aFloat==bFloat)
76  result = true;
77  else
78  result = false;
79  }
80  else {
81  TTFloat64 aAbs = fabs(aFloat);
82  TTFloat64 bAbs = fabs(bFloat);
83  TTFloat64 absoluteOrRelative = (1.0f > aAbs ? 1.0f : aAbs);
84  absoluteOrRelative = (absoluteOrRelative > bAbs ? absoluteOrRelative : bAbs);
85  if (fabs(aFloat - bFloat) <= epsilon * absoluteOrRelative)
86  result = true;
87  else
88  result = false;
89  }
90  // Was this the expected result?
91  if (result == expectedResult)
92  return true;
93  else {
94 #ifdef NOISY_FAILURE
95  TTLogMessage("\n");
96  TTLogMessage(" TTTestFloatEquivalence: Unexpected result\n");
97  TTLogMessage("\n");
98  TTLogMessage(" aFloat = %.15e\n", aFloat);
99  TTLogMessage(" bFloat = %.15e\n", bFloat);
100  TTLogMessage(" result = %s\n", (result)?"true":"false");
101  TTLogMessage("\n");
102 #endif
103  return false;
104  }
105 }
106 
107 
108 TTBoolean TTTestFloat32ArrayEquivalence(TTValue &aFloat, TTValue &bFloat, TTBoolean expectedResult, TTFloat32 epsilon)
109 {
110  TTBoolean result;
111 
112  // Compare vector size
113  if (aFloat.size() != bFloat.size())
114  result = false;
115  else {
116  // Compare member by member
117  result = true;
118  for (size_t i=0; i<aFloat.size(); i++)
119  result = result && TTTestFloatEquivalence(TTFloat32(aFloat[i]), TTFloat32(bFloat[i]), expectedResult, epsilon);
120  }
121 
122  return result;
123 }
124 
125 
126 TTBoolean TTTestFloat64ArrayEquivalence(TTValue &aFloat, TTValue &bFloat, TTBoolean expectedResult, TTFloat64 epsilon)
127 {
128  TTBoolean result;
129 
130  // Compare vector size
131  if (aFloat.size() != bFloat.size())
132  result = false;
133  else {
134  // Compare member by member
135  result = true;
136  for (size_t i=0; i<aFloat.size(); i++)
137  result = result && TTTestFloatEquivalence(aFloat[i], bFloat[i], expectedResult, epsilon);
138  }
139 
140  return result;
141 }
142 
143 
144 void TTTestLog(const char *fmtstring, ...)
145 {
146  char str[4096];
147  char fullstr[4096];
148  va_list ap;
149 
150  va_start(ap, fmtstring);
151  vsnprintf(str, 4000, fmtstring, ap);
152  va_end(ap);
153  str[4095] = 0;
154 
155  strncpy(fullstr, " ", 4095);
156  strncat(fullstr, str, 4095);
157  strncat(fullstr, "\n", 4095);
158  TTLogMessage(fullstr);
159 }
160 
161 
162 void TTTestLogResult(const char *name, TTBoolean aResult, const char* filename = "", int linenumber = 0)
163 {
164  // Note: we don't post passing assertions because the log (at least in Xcode) starts suppressing the messages and failures won't be posted
165  if (aResult == false) {
166 #ifdef TT_PLATFORM_WIN
167  std::cout << filename << "(" << linenumber << ") : error: " << "Test Assertion Failed -- " << name << std::endl;
168 #else
169  std::cout << filename << ":" << linenumber << ": error: " << "Test Assertion Failed -- " << name << std::endl;
170 #endif
171  }
172 }
173 
174 
175 //void TTTestAssertion(const char* aTestName, TTBoolean aTestResult, int& testAssertionCount, int& errorCount)
176 void TTTestAssertionResult(const char* aTestName, TTBoolean aTestResult, int& testAssertionCount, int& errorCount, const char* filename, int linenumber)
177 {
178  testAssertionCount++;
179  TTTestLogResult(aTestName, aTestResult, filename, linenumber);
180 // if (aTestResult)
181 // TTLogMessage(" PASS -- ");
182 // else {
183 // TTLogMessage(" FAIL -- ");
184  if (!aTestResult)
185  errorCount++;
186 // }
187 // TTLogMessage(aTestName);
188 // TTLogMessage("\n");
189 // if (!aTestResult)
190  // TTLogMessage("\n");
191 }
192 
193 
194 TTErr TTTestFinish(int testAssertionCount, int errorCount, TTValue& returnedTestInfo)
195 {
196  TTDictionary d;
197 
198  d.setSchema("TestInfo");
199  d.append("testAssertionCount", testAssertionCount);
200  d.append("errorCount", errorCount);
201  returnedTestInfo = d;
202 
203  TTTestLog("\n");
204  TTTestLog("Number of assertions: %ld", testAssertionCount);
205  TTTestLog("Number of failed assertions: %ld", errorCount);
206  TTTestLog("\n");
207 
208  if (errorCount)
209  return kTTErrGeneric;
210  else
211  return kTTErrNone;
212 }
213 
bool TTBoolean
Boolean flag, same as Boolean on the Mac.
Definition: TTBase.h:167
TTErr append(const TTSymbol aKey, const TTValue aValue)
Insert an item into the hash table.
Definition: TTDictionary.h:212
size_type size() const noexcept
Return the number of elements.
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
A type that represents the key as a C-String and the value as a pointer to the matching TTSymbol obje...
Definition: TTDictionary.h:47
float TTFloat32
32 bit floating point number
Definition: TTBase.h:187
TTErr setSchema(const TTSymbol aSchemaName)
TODO: Add documentation schemaName TODO: Add documentation.
Definition: TTDictionary.h:172
void TTFOUNDATION_EXPORT TTLogMessage(TTImmutableCString message,...)
Platform and host independent method for posting log messages.
Definition: TTBase.cpp:534
Something went wrong, but what exactly is not known. Typically used for context-specific problems...
Definition: TTBase.h:344
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
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34