LRAUV  revA
Blob_Test.h
Go to the documentation of this file.
1 
9 #ifndef _BLOB_TEST_H_
10 #define _BLOB_TEST_H_
11 
13 #include "BlobReader.h"
14 #include "BlobValue.h"
15 #include "BlobWriter.h"
16 #include "data/I2D.h"
17 
18 #include <cxxtest/TestSuite.h>
19 
25 class Blob_Test : public CxxTest::TestSuite
26 {
27 public:
28 
29  // Test basic BlobValue operations
30  void testBlobValue( void )
31  {
32  BlobURI waterTemps( "Test", "waterTemps", Units::FAHRENHEIT, BLOB_FLOAT64LE, 3 );
33 
34  // Test constructor with more than the fixed size
35  const double tempsInC[] = { -273.15, 0, 100, 374};
36  BlobValue bv( Units::CELSIUS, tempsInC, 4, waterTemps );
37  TS_ASSERT_EQUALS( bv.getElements(), 4ul );
38  TS_ASSERT_EQUALS( bv.getByteLength(), 32ul );
39  TS_ASSERT_DELTA( *( ( double* )bv.getBytes() + 0 ), -459.67, 0.01 );
40  TS_ASSERT_DELTA( *( ( double* )bv.getBytes() + 1 ), 32.0, 0.01 );
41  TS_ASSERT_DELTA( *( ( double* )bv.getBytes() + 2 ), 212.0, 0.01 );
42  TS_ASSERT_DELTA( *( ( double* )bv.getBytes() + 3 ), 705.0, 1.0 );
43 
44  // Test accessing more than the fixed size and unit conversion to default
45  double tempsInF[4] = {};
46  TS_ASSERT_EQUALS( bv.copyTo( Units::FAHRENHEIT, tempsInF, 4 ), 4 );
47  TS_ASSERT_DELTA( tempsInF[0], -459.67, 0.01 );
48  TS_ASSERT_DELTA( tempsInF[1], 32.0, 0.01 );
49  TS_ASSERT_DELTA( tempsInF[2], 212.0, 0.01 );
50  TS_ASSERT_DELTA( tempsInF[3], 705.0, 1.0 );
51 
52  // Test accessing less than the written size and unit conversion to default
53  double tempsInF2[2] = {};
54  TS_ASSERT_EQUALS( bv.copyTo( Units::FAHRENHEIT, tempsInF2, 2 ), 2 );
55  TS_ASSERT_DELTA( tempsInF2[0], -459.67, 0.01 );
56  TS_ASSERT_DELTA( tempsInF2[1], 32.0, 0.01 );
57 
58  // Test accessing the fixed size and unit conversion to base
59  double tempsInK[3] = {};
60  TS_ASSERT_EQUALS( bv.copyTo( Units::KELVIN, tempsInK, 3 ), 3 );
61  TS_ASSERT_DELTA( tempsInK[0], 0.0, 0.01 );
62  TS_ASSERT_DELTA( tempsInK[1], 273.15, 0.01 );
63  TS_ASSERT_DELTA( tempsInK[2], 373.15, 0.01 );
64 
65  }
66 
68  void testReadWrite( void )
69  {
70  TestAsyncComponent testComponent( "Blob_TestComponent" );
71  BlobURI waterTemps( testComponent.getName(), "waterTemps", Units::FAHRENHEIT, BLOB_FLOAT64LE, 4 );
72  BlobWriter* bw = testComponent.newBlobWriter( waterTemps );
73  BlobReader* br = testComponent.newBlobReader( waterTemps );
74 
75  // Test writing the fixed size
76  const double tempsInC[] = { -273.15, 0, 100, 374};
77  TS_ASSERT( bw->write1DArray( Units::CELSIUS, tempsInC ) );
78 
79  // Test readability
80  TS_ASSERT( br->isActive() );
81 
82  //Test reading as a BlobValue
83  bool success = false;
84  BlobValue bv = br->readBlob( success );
85  TS_ASSERT( success );
86  TS_ASSERT_DELTA( *( ( double* )bv.getBytes() + 0 ), -459.67, 0.01 );
87  TS_ASSERT_DELTA( *( ( double* )bv.getBytes() + 1 ), 32.0, 0.01 );
88  TS_ASSERT_DELTA( *( ( double* )bv.getBytes() + 2 ), 212.0, 0.01 );
89  TS_ASSERT_DELTA( *( ( double* )bv.getBytes() + 3 ), 705.0, 1.0 );
90 
91  // Test reading the fixed size and unit conversion to default
92  double tempsInF[4] = {};
93  TS_ASSERT_EQUALS( br->read1DArray( Units::FAHRENHEIT, tempsInF ), 4 );
94  TS_ASSERT_DELTA( tempsInF[0], -459.67, 0.01 );
95  TS_ASSERT_DELTA( tempsInF[1], 32.0, 0.01 );
96  TS_ASSERT_DELTA( tempsInF[2], 212.0, 0.01 );
97  TS_ASSERT_DELTA( tempsInF[3], 705.0, 1.0 );
98 
99  // Test reading the written size and unit conversion to default
100  double tempsInF2[4] = {};
101  TS_ASSERT_EQUALS( br->read1DArray( Units::FAHRENHEIT, tempsInF2 ), 4 );
102  TS_ASSERT_DELTA( tempsInF2[0], -459.67, 0.01 );
103  TS_ASSERT_DELTA( tempsInF2[1], 32.0, 0.01 );
104  TS_ASSERT_DELTA( tempsInF2[2], 212.0, 0.01 );
105  TS_ASSERT_DELTA( tempsInF2[3], 705.0, 1.0 );
106 
107  // Test reading the fixed size and unit conversion to base
108  double tempsInK[4] = {};
109  TS_ASSERT_EQUALS( br->read1DArray( Units::KELVIN, tempsInK ), 4 );
110  TS_ASSERT_DELTA( tempsInK[0], 0.0, 0.01 );
111  TS_ASSERT_DELTA( tempsInK[1], 273.15, 0.01 );
112  TS_ASSERT_DELTA( tempsInK[2], 373.15, 0.01 );
113  TS_ASSERT_DELTA( tempsInK[3], 647.15, 0.01 );
114  }
115 
118  {
119  TestAsyncComponent testComponent( "Blob_TestComponent" );
120  BlobURI waterTemps( testComponent.getName(), "waterTemps32BE", Units::FAHRENHEIT, BLOB_FLOAT32BE, 4 );
121  BlobWriter* bw = testComponent.newBlobWriter( waterTemps );
122  BlobReader* br = testComponent.newBlobReader( waterTemps );
123 
124  // Test writing the fixed size
125  const double tempsInC[] = { -273.15, 0, 100, 374};
126  TS_ASSERT( bw->write1DArray( Units::CELSIUS, tempsInC ) );
127 
128  // Test readability
129  TS_ASSERT( br->isActive() );
130 
131  //Test reading as a BlobValue
132  bool success = false;
133  BlobValue bv = br->readBlob( success );
134  float fVal = nanf( "" );
135  bv.getElement( 0, fVal, true );
136  TS_ASSERT_DELTA( fVal, -459.67, 0.01 );
137  bv.getElement( 1, fVal, true );
138  TS_ASSERT_DELTA( fVal, 32, 0.01 );
139  bv.getElement( 2, fVal, true );
140  TS_ASSERT_DELTA( fVal, 212, 0.01 );
141  bv.getElement( 3, fVal, true );
142  TS_ASSERT_DELTA( fVal, 705.0, 1.0 );
143 
144  // Test reading the fixed size and unit conversion to default
145  double tempsInF[4] = {};
146  TS_ASSERT_EQUALS( br->read1DArray( Units::FAHRENHEIT, tempsInF ), 4 );
147  TS_ASSERT_DELTA( tempsInF[0], -459.67, 0.01 );
148  TS_ASSERT_DELTA( tempsInF[1], 32.0, 0.01 );
149  TS_ASSERT_DELTA( tempsInF[2], 212.0, 0.01 );
150  TS_ASSERT_DELTA( tempsInF[3], 705.0, 1.0 );
151 
152  // Test reading the fixed size and unit conversion to base
153  double tempsInK[4] = {};
154  TS_ASSERT_EQUALS( br->read1DArray( Units::KELVIN, tempsInK ), 4 );
155  TS_ASSERT_DELTA( tempsInK[0], 0.0, 0.01 );
156  TS_ASSERT_DELTA( tempsInK[1], 273.15, 0.01 );
157  TS_ASSERT_DELTA( tempsInK[2], 373.15, 0.01 );
158  TS_ASSERT_DELTA( tempsInK[3], 647.15, 0.01 );
159  }
160 
161  // Test basic BlobValue operations
162  void testBlobValue2D( void )
163  {
164  const int m = 2, n = 3;
165  BlobURI waterTemps( "Test", "temp2D", Units::FAHRENHEIT, BLOB_FLOAT32LE, m, n );
166 
167  // Test basic operation with non-pointer arrays
168  const float dataF[m][n] = { { -40, 0, 32 }, { -459.67, 212, 705 } };
169  const float dataC[m][n] = { { -40, -17.78, 0 }, { -273.15, 100, 374 } };
170  float dataOut[m][n];
171  BlobValue bv( waterTemps );
172  bv.setFrom( Units::FAHRENHEIT, dataF );
173  bv.copyTo( Units::FAHRENHEIT, dataOut );
174  for( int i = 0; i < m; ++i )
175  {
176  for( int j = 0; j < n; ++j )
177  {
178  TS_ASSERT_DELTA( dataF[i][j], dataOut[i][j], 1.f );
179  }
180  }
181  bv.copyTo( Units::CELSIUS, dataOut );
182  for( int i = 0; i < m; ++i )
183  {
184  for( int j = 0; j < n; ++j )
185  {
186  TS_ASSERT_DELTA( dataC[i][j], dataOut[i][j], 1.f );
187  }
188  }
189  bv.setFrom( Units::CELSIUS, dataC );
190  bv.copyTo( Units::CELSIUS, dataOut );
191  for( int i = 0; i < m; ++i )
192  {
193  for( int j = 0; j < n; ++j )
194  {
195  TS_ASSERT_DELTA( dataC[i][j], dataOut[i][j], 1.f );
196  }
197  }
198  bv.copyTo( Units::FAHRENHEIT, dataOut );
199  for( int i = 0; i < m; ++i )
200  {
201  for( int j = 0; j < n; ++j )
202  {
203  TS_ASSERT_DELTA( dataF[i][j], dataOut[i][j], 1.f );
204  }
205  }
206 
207  // Test basic operation with pointer arrays
208  float** pDataF = I2D<float>::New2D( m, n );
209  float** pDataC = I2D<float>::New2D( m, n );
210  for( int i = 0; i < m; ++i )
211  {
212  for( int j = 0; j < n; ++j )
213  {
214  pDataF[i][j] = dataF[i][j];
215  pDataC[i][j] = dataC[i][j];
216  }
217  }
218  float** pDataOut = I2D<float>::New2D( m, n );
219  bv.setFrom( Units::FAHRENHEIT, ( const float** ) pDataF, m, n );
220  bv.copyTo( Units::FAHRENHEIT, pDataOut, m, n );
221  for( int i = 0; i < m; ++i )
222  {
223  for( int j = 0; j < n; ++j )
224  {
225  TS_ASSERT_DELTA( pDataF[i][j], pDataOut[i][j], 1.f );
226  }
227  }
228  bv.copyTo( Units::CELSIUS, pDataOut, m, n );
229  for( int i = 0; i < m; ++i )
230  {
231  for( int j = 0; j < n; ++j )
232  {
233  TS_ASSERT_DELTA( pDataC[i][j], pDataOut[i][j], 1.f );
234  }
235  }
236  bv.setFrom( Units::CELSIUS, ( const float** ) pDataC, m, n );
237  bv.copyTo( Units::CELSIUS, pDataOut, m, n );
238  for( int i = 0; i < m; ++i )
239  {
240  for( int j = 0; j < n; ++j )
241  {
242  TS_ASSERT_DELTA( pDataC[i][j], pDataOut[i][j], 1.f );
243  }
244  }
245  bv.copyTo( Units::FAHRENHEIT, pDataOut, m, n );
246  for( int i = 0; i < m; ++i )
247  {
248  for( int j = 0; j < n; ++j )
249  {
250  TS_ASSERT_DELTA( pDataF[i][j], pDataOut[i][j], 1.f );
251  }
252  }
253  I2D<float>::Delete2D( pDataF );
254  I2D<float>::Delete2D( pDataC );
255  I2D<float>::Delete2D( pDataOut );
256 
257  }
258 
259  // Test basic BlobValue operations
260  void testBlobValue3D( void )
261  {
262  const int m = 2, n = 3, o = 4;
263  BlobURI waterTemps( "Test", "temp3D", Units::FAHRENHEIT, BLOB_FLOAT32LE, m, n );
264 
265  // Test basic operation with non-pointer arrays
266  const float dataF[m][n][o] = { { { -40, 50, 140, 230}, {0, 90, 180, 270}, {32, 122, 212, 302} },
267  { { -459.67, -369.67, -279.67, -189.67}, {212, 302, 392, 482}, {705, 795, 885, 975} }
268  };
269  const float dataC[m][n][o] = { { { -40, 10, 60, 110}, { -17.78, 32.22, 82.22, 132.22}, {0, 50, 100, 150} },
270  { { -273.15, -223.15, -173.15, -123.15}, {100, 150, 200, 250}, {374, 424, 474, 524} }
271  };
272  float dataOut[m][n][o];
273  BlobValue bv( waterTemps );
274  bv.setFrom( Units::FAHRENHEIT, dataF );
275  bv.copyTo( Units::FAHRENHEIT, dataOut );
276  for( int i = 0; i < m; ++i )
277  {
278  for( int j = 0; j < n; ++j )
279  {
280  for( int k = 0; k < o; ++k )
281  {
282  TS_ASSERT_DELTA( dataF[i][j][k], dataOut[i][j][k], 1.f );
283  }
284  }
285  }
286  bv.copyTo( Units::CELSIUS, dataOut );
287  for( int i = 0; i < m; ++i )
288  {
289  for( int j = 0; j < n; ++j )
290  {
291  for( int k = 0; k < o; ++k )
292  {
293  TS_ASSERT_DELTA( dataC[i][j][k], dataOut[i][j][k], 1.f );
294  }
295  }
296  }
297  bv.setFrom( Units::CELSIUS, dataC );
298  bv.copyTo( Units::CELSIUS, dataOut );
299  for( int i = 0; i < m; ++i )
300  {
301  for( int j = 0; j < n; ++j )
302  {
303  for( int k = 0; k < o; ++k )
304  {
305  TS_ASSERT_DELTA( dataC[i][j][k], dataOut[i][j][k], 1.f );
306  }
307  }
308  }
309  bv.copyTo( Units::FAHRENHEIT, dataOut );
310  for( int i = 0; i < m; ++i )
311  {
312  for( int j = 0; j < n; ++j )
313  {
314  for( int k = 0; k < o; ++k )
315  {
316  TS_ASSERT_DELTA( dataF[i][j][k], dataOut[i][j][k], 1.f );
317  }
318  }
319  }
320 
321  // Test basic operation with pointer arrays
322  float*** pDataF = I3D<float>::New3D( m, n, o );
323  float*** pDataC = I3D<float>::New3D( m, n, o );
324  for( int i = 0; i < m; ++i )
325  {
326  for( int j = 0; j < n; ++j )
327  {
328  for( int k = 0; k < o; ++k )
329  {
330  pDataF[i][j][k] = dataF[i][j][k];
331  pDataC[i][j][k] = dataC[i][j][k];
332  }
333  }
334  }
335  float*** pDataOut = I3D<float>::New3D( m, n, o );
336  bv.setFrom( Units::FAHRENHEIT, ( const float*** ) pDataF, m, n, o );
337  bv.copyTo( Units::FAHRENHEIT, pDataOut, m, n, o );
338  for( int i = 0; i < m; ++i )
339  {
340  for( int j = 0; j < n; ++j )
341  {
342  for( int k = 0; k < o; ++k )
343  {
344  TS_ASSERT_DELTA( pDataF[i][j][k], pDataOut[i][j][k], 1.f );
345  }
346  }
347  }
348  bv.copyTo( Units::CELSIUS, pDataOut, m, n, o );
349  for( int i = 0; i < m; ++i )
350  {
351  for( int j = 0; j < n; ++j )
352  {
353  for( int k = 0; k < o; ++k )
354  {
355  TS_ASSERT_DELTA( pDataC[i][j][k], pDataOut[i][j][k], 1.f );
356  }
357  }
358  }
359  bv.setFrom( Units::CELSIUS, ( const float*** ) pDataC, m, n, o );
360  bv.copyTo( Units::CELSIUS, pDataOut, m, n, o );
361  for( int i = 0; i < m; ++i )
362  {
363  for( int j = 0; j < n; ++j )
364  {
365  for( int k = 0; k < o; ++k )
366  {
367  TS_ASSERT_DELTA( pDataC[i][j][k], pDataOut[i][j][k], 1.f );
368  }
369  }
370  }
371  bv.copyTo( Units::FAHRENHEIT, pDataOut, m, n, o );
372  for( int i = 0; i < m; ++i )
373  {
374  for( int j = 0; j < n; ++j )
375  {
376  for( int k = 0; k < o; ++k )
377  {
378  TS_ASSERT_DELTA( pDataF[i][j][k], pDataOut[i][j][k], 1.f );
379  }
380  }
381  }
382  I3D<float>::Delete3D( pDataF );
383  I3D<float>::Delete3D( pDataC );
384  I3D<float>::Delete3D( pDataOut );
385 
386  }
387 };
388 
389 #endif // _BLOB_TEST_H
void testReadWrite(void)
Test Read & Write.
Definition: Blob_Test.h:68
Contains the AsyncComponent class definition.
Contains the BlobReader class definition.
size_t getByteLength()
Definition: BlobValue.h:222
void * getBytes()
Definition: BlobValue.h:227
Definition: ElementURI.h:211
BlobValue readBlob(bool &success)
Definition: BlobReader.h:45
BlobReader * newBlobReader(const BlobURI &blobURI)
Definition: Component.cpp:547
Definition: BlobReader.h:212
void testBlobValue2D(void)
Definition: Blob_Test.h:162
Definition: BlobType.h:33
static T ** New2D(int m, int n, bool extra=false)
Definition: I2D.h:30
Definition: BlobWriter.h:227
size_t getElements()
Definition: BlobValue.h:217
int read1DArray(const Unit &unit, T(&x)[M])
Definition: BlobReader.h:53
static void Delete2D(T **array)
Definition: I2D.h:45
static const Unit FAHRENHEIT
Definition: Units.h:177
int copyTo(const Unit &unit, T *array, size_t elements)
Definition: BlobValue.cpp:272
void getElement(size_t index, T &value, bool flip)
Get an individual value, without bounds checking or scaling.
Definition: BlobValue.cpp:541
Wraps a StrValue as an arbitrary collection of binary values, with units.
Definition: BlobValue.h:26
BlobWriter * newBlobWriter(const BlobURI &blobURI, Logger::TimePrecisionType timePrecision=Logger::TIME_PRECISION_MILLIS)
Definition: Component.cpp:566
Definition: BlobType.h:34
static T *** New3D(int m, int n, int o, bool extra=false)
Definition: I3D.h:33
void testBlobValue3D(void)
Definition: Blob_Test.h:260
bool isActive(void)
Definition: DataReader.cpp:74
Definition: BlobType.h:35
static void Delete3D(T ***array)
Definition: I3D.h:58
void testBlobValue(void)
Definition: Blob_Test.h:30
Unit tests for the BlobValue, BlobReader, and BlobWriter classes.
Definition: Blob_Test.h:25
const CodedStr & getName(void) const
Returns the name.
Definition: Component.h:158
void setFrom(const Unit &unit, const T *array, size_t elements)
Definition: BlobValue.cpp:93
static const Unit CELSIUS
Definition: Units.h:110
Dummy implementation of AsyncComponent for testing purposes.
Definition: TestAsyncComponent.h:19
static const BaseUnit KELVIN
Definition: Units.h:40
Contains the BlobValue class definition.
Contains the BlobWriter class definition.
void testReadWriteConversions(void)
Test Read & Write with cast conversions.
Definition: Blob_Test.h:117