Jamoma API  0.6.0.a19
DistanceDataspace.test.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup foundationDataspaceLib
4  *
5  * @brief Unit tests for the #DistanceDataspace.
6  *
7  * @authors Trond Lossius, Tim Place, Nils Peters, ...
8  *
9  * @copyright Copyright © 2011 Trond Lossius @n
10  * This code is licensed under the terms of the "New BSD License" @n
11  * http://creativecommons.org/licenses/BSD/
12  */
13 
14 
15 // Dataspaces and Units employ C++ double-inheritance and are thus unsuitable for direct use
16 // through the usual TTObject API
17 #define TT_NO_DEPRECATION_WARNINGS
18 
19 #include "DistanceDataspace.h"
20 
21 
22 TTErr DistanceDataspace::test(TTValue& returnedTestInfo)
23 {
24  int errorCount = 0;
25  int testAssertionCount = 0;
26 
27  // Create dataspace object and set to temperature
28  try {
29 
30  TTObject myDataspace("dataspace");
31  myDataspace.set(TT("dataspace"), TT("distance"));
32 
33  TTValue v;
34  TTValue expected;
35 
36 
37  /************************************************/
38  /* */
39  /* Test conversions to neutral unit */
40  /* */
41  /************************************************/
42 
43 
44  // meter => meters
45 
46  myDataspace.set(TT("inputUnit"), TT("meters"));
47  myDataspace.set(TT("outputUnit"), TT("meters"));
48 
49  v = TTValue(256.);
50  expected = TTValue(256.);
51 
52  myDataspace.send(TT("convert"), v, v);
53 
54  TTTestAssertion("meters to meters",
55  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
56  testAssertionCount,
57  errorCount);
58 
59 
60 
61  // cm => meters
62 
63  myDataspace.set(TT("inputUnit"), TT("cm"));
64  myDataspace.set(TT("outputUnit"), TT("meters"));
65 
66  v = TTValue(123.4);
67  expected = TTValue(1.234);
68 
69  myDataspace.send(TT("convert"), v, v);
70 
71  TTTestAssertion("cm to meters",
72  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
73  testAssertionCount,
74  errorCount);
75 
76 
77  // inches => meters
78  // Expected value according to Google search: "15 inch to meter"
79  // The Google result hasn't much resolution, but is sufficient to indicate that we're OK
80 
81  myDataspace.set(TT("inputUnit"), TT("inches"));
82  myDataspace.set(TT("outputUnit"), TT("meter"));
83 
84  v = TTValue(15.);
85  expected = TTValue(0.381);
86 
87  myDataspace.send(TT("convert"), v, v);
88 
89  TTTestAssertion("inch to meters",
90  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected), true, 0.001),
91  testAssertionCount,
92  errorCount);
93 
94 
95  // feet => meters
96  // Expected value according to Google search: "6 feet to meter"
97  // The Google result hasn't much resolution, but is sufficient to indicate that we're OK
98 
99  myDataspace.set(TT("inputUnit"), TT("feet"));
100  myDataspace.set(TT("outputUnit"), TT("meters"));
101 
102  v = TTValue(6.);
103  expected = TTValue(1.8288);
104 
105  myDataspace.send(TT("convert"), v, v);
106 
107  TTTestAssertion("inches to meters",
108  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected), true, 0.001),
109  testAssertionCount,
110  errorCount);
111 
112  /************************************************/
113  /* */
114  /* Test conversions from neutral unit */
115  /* */
116  /************************************************/
117 
118  // meters => cm
119 
120  myDataspace.set(TT("inputUnit"), TT("meters"));
121  myDataspace.set(TT("outputUnit"), TT("cm"));
122 
123  v = TTValue(1.234);
124  expected = TTValue(123.4);
125 
126  myDataspace.send(TT("convert"), v, v);
127 
128  TTTestAssertion("meters to cm",
129  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected)),
130  testAssertionCount,
131  errorCount);
132 
133 
134  // meters => inches
135 
136  myDataspace.set(TT("inputUnit"), TT("meters"));
137  myDataspace.set(TT("outputUnit"), TT("inches"));
138 
139  v = TTValue(0.381);
140  expected = TTValue(15.0);
141 
142  myDataspace.send(TT("convert"), v, v);
143 
144  TTTestAssertion("meters to inches",
145  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected), true, 0.001),
146  testAssertionCount,
147  errorCount);
148 
149  // meters => feet
150 
151  myDataspace.set(TT("inputUnit"), TT("meters"));
152  myDataspace.set(TT("outputUnit"), TT("feet"));
153 
154  v = TTValue(1.8288);
155  expected = TTValue(6.0);
156 
157  myDataspace.send(TT("convert"), v, v);
158 
159  TTTestAssertion("meters to feet",
160  TTTestFloatEquivalence(TTFloat64(v), TTFloat64(expected), true, 0.001),
161  testAssertionCount,
162  errorCount);
163  }
164  catch (...) {
165  TTLogMessage("DistanceDataspace::test TOTAL FAILURE");
166  errorCount = 1;
167  testAssertionCount = 1;
168  }
169 
170  return TTTestFinish(testAssertionCount, errorCount, returnedTestInfo);
171 }
Create and use Jamoma object instances.
Definition: TTObject.h:29
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
The DistanceDataspace converts between different measurement untis describing distance.
void set(const TTUInt16 index, const T &anElementValue)
DEPRECATED.
Definition: TTValue.h:569
void TTFOUNDATION_EXPORT TTLogMessage(TTImmutableCString message,...)
Platform and host independent method for posting log messages.
Definition: TTBase.cpp:534
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34