root/branches/streaming-rework/src/libutil/unittests.cpp

Revision 435, 13.0 kB (checked in by pieterpalmers, 16 years ago)

src/devicemanager:
- start OSC server for the device manager

src/devicemanager,
src/iavdevice,
src/libieee1394/configrom:
- inherit from OscNode? to become Osc'able

src/bounce,
src/libstreaming/AmdtpStreamProcessor,
src/libstreaming/AmdtpSlaveStreamProcessor:
- fixed bounce device implementation, now working

src/bebob:
- fixed midi bug

General:
- removed 'intermediate XML'
- removed obsolete tests
- removed obsolete files
- removed obsolete API calls

Line 
1 /* unittests.cpp
2  * Copyright (C) 2006,07 by Daniel Wagner
3  *
4  * This file is part of FreeBoB.
5  *
6  * FreeBoB is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * FreeBoB is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with FreeBoB; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18  * MA 02111-1307 USA.
19  */
20
21 #include "serialize.h"
22 #include "OptionContainer.h"
23
24 #include <libraw1394/raw1394.h>
25
26 #include <stdio.h>
27
28 using namespace Util;
29
30 #define TEST_SHOULD_RETURN_TRUE(test) \
31     (test ? true : printf( "'" #test "' should return true\n") && false )
32 #define TEST_SHOULD_RETURN_FALSE(test) \
33     (test ? printf( "'" #test "' should return true\n") && false : true )
34
35 ///////////////////////////////////////
36
37 class U0_SerializeMe {
38 public:
39     U0_SerializeMe();
40
41     bool operator == ( const U0_SerializeMe& rhs );
42
43     bool serialize( IOSerialize& ser );
44     bool deserialize( IODeserialize& deser );
45
46     byte_t m_byte;
47     quadlet_t m_quadlet;
48 };
49
50 U0_SerializeMe::U0_SerializeMe()
51     : m_byte( 0 )
52     , m_quadlet( 0 )
53 {
54 }
55
56
57 //--------------------------
58 bool
59 U0_SerializeMe::operator == ( const U0_SerializeMe& rhs )
60 {
61     return    ( m_byte == rhs.m_byte )
62            && ( m_quadlet == rhs.m_quadlet );
63 }
64
65 bool
66 U0_SerializeMe::serialize( IOSerialize& ser )
67 {
68     bool result;
69     result  = ser.write( "SerializeMe/m_byte", m_byte );
70     result &= ser.write( "SerializeMe/m_quadlet", m_quadlet );
71     return result;
72 }
73
74 bool
75 U0_SerializeMe::deserialize( IODeserialize& deser )
76 {
77     bool result;
78     result  = deser.read( "SerializeMe/m_byte", m_byte );
79     result &= deser.read( "SerializeMe/m_quadlet", m_quadlet );
80     return result;
81 }
82
83 static bool
84 testU0()
85 {
86     U0_SerializeMe sme1;
87
88     sme1.m_byte = 0x12;
89     sme1.m_quadlet = 0x12345678;
90
91     {
92         XMLSerialize xmlSerialize( "unittest_u0.xml" );
93         if ( !sme1.serialize( xmlSerialize ) ) {
94             printf( "(serializing failed)" );
95             return false;
96         }
97     }
98
99     U0_SerializeMe sme2;
100
101     {
102         XMLDeserialize xmlDeserialize( "unittest_u0.xml" );
103         if ( !sme2.deserialize( xmlDeserialize ) ) {
104             printf( "(deserializing failed)" );
105             return false;
106         }
107     }
108
109     bool result = sme1 == sme2;
110     if ( !result ) {
111         printf( "(wrong values)" );
112     }
113     return result;
114 }
115
116
117 ///////////////////////////////////////
118
119 class U1_SerializeMe {
120 public:
121     U1_SerializeMe();
122
123     bool operator == ( const U1_SerializeMe& rhs );
124
125     bool serialize( IOSerialize& ser );
126     bool deserialize( IODeserialize& deser );
127
128     quadlet_t m_quadlet0;
129     quadlet_t m_quadlet1;
130     quadlet_t m_quadlet2;
131 };
132
133 U1_SerializeMe::U1_SerializeMe()
134     : m_quadlet0( 0 )
135     , m_quadlet1( 0 )
136     , m_quadlet2( 0 )
137 {
138 }
139
140
141 //--------------------------
142 bool
143 U1_SerializeMe::operator == ( const U1_SerializeMe& rhs )
144 {
145     return    ( m_quadlet0 == rhs.m_quadlet0 )
146            && ( m_quadlet1 == rhs.m_quadlet1)
147            && ( m_quadlet2 == rhs.m_quadlet2);
148 }
149
150 bool
151 U1_SerializeMe::serialize( IOSerialize& ser )
152 {
153     bool result;
154     result  = ser.write( "here/and/not/there/m_quadlet0", m_quadlet0 );
155     result &= ser.write( "here/and/not/m_quadlet1", m_quadlet1 );
156     result &= ser.write( "here/and/m_quadlet2", m_quadlet2 );
157     return result;
158 }
159
160 bool
161 U1_SerializeMe::deserialize( IODeserialize& deser )
162 {
163     bool result;
164     result  = deser.read( "here/and/not/there/m_quadlet0", m_quadlet0 );
165     result &= deser.read( "here/and/not/m_quadlet1", m_quadlet1 );
166     result &= deser.read( "here/and/m_quadlet2", m_quadlet2 );
167     return result;
168 }
169
170 static bool
171 testU1()
172 {
173     U1_SerializeMe sme1;
174
175     sme1.m_quadlet0 = 0;
176     sme1.m_quadlet1 = 1;
177     sme1.m_quadlet2 = 2;
178
179     {
180         XMLSerialize xmlSerialize( "unittest_u1.xml" );
181         if ( !sme1.serialize( xmlSerialize ) ) {
182             printf( "(serializing failed)" );
183             return false;
184         }
185     }
186
187     U1_SerializeMe sme2;
188
189     {
190         XMLDeserialize xmlDeserialize( "unittest_u1.xml" );
191         if ( !sme2.deserialize( xmlDeserialize ) ) {
192             printf( "(deserializing failed)" );
193             return false;
194         }
195     }
196
197     bool result = sme1 == sme2;
198     if ( !result ) {
199         printf( "(wrong values)" );
200     }
201     return result;
202 }
203
204 ///////////////////////////////////////
205
206 class U2_SerializeMe {
207 public:
208     U2_SerializeMe();
209
210     bool operator == ( const U2_SerializeMe& rhs );
211
212     bool serialize( IOSerialize& ser );
213     bool deserialize( IODeserialize& deser );
214
215     char             m_char;
216     unsigned char    m_unsigned_char;
217     short            m_short;
218     unsigned short   m_unsigned_short;
219     int              m_int;
220     unsigned int     m_unsigned_int;
221 };
222
223 U2_SerializeMe::U2_SerializeMe()
224     : m_char( 0 )
225     , m_unsigned_char( 0 )
226     , m_short( 0 )
227     , m_unsigned_short( 0 )
228     , m_int( 0 )
229     , m_unsigned_int( 0 )
230 {
231 }
232
233 //--------------------------
234
235 bool
236 U2_SerializeMe::operator == ( const U2_SerializeMe& rhs )
237 {
238     return    ( m_char == rhs.m_char )
239            && ( m_unsigned_char == rhs.m_unsigned_char )
240            && ( m_short == rhs.m_short )
241            && ( m_unsigned_short == rhs.m_unsigned_short )
242            && ( m_int == rhs.m_int )
243            && ( m_unsigned_int == rhs.m_unsigned_int );
244 }
245
246 bool
247 U2_SerializeMe::serialize( IOSerialize& ser )
248 {
249     bool result;
250     result  = ser.write( "m_char", m_char );
251     result &= ser.write( "m_unsigned_char", m_unsigned_char );
252     result &= ser.write( "m_short", m_short );
253     result &= ser.write( "m_unsigned_short", m_unsigned_short );
254     result &= ser.write( "m_int", m_int );
255     result &= ser.write( "m_unsigned_int", m_unsigned_int );
256     return result;
257 }
258
259 bool
260 U2_SerializeMe::deserialize( IODeserialize& deser )
261 {
262     bool result;
263     result  = deser.read( "m_char", m_char );
264     result &= deser.read( "m_unsigned_char", m_unsigned_char );
265     result &= deser.read( "m_short", m_short );
266     result &= deser.read( "m_unsigned_short", m_unsigned_short );
267     result &= deser.read( "m_int", m_int );
268     result &= deser.read( "m_unsigned_int", m_unsigned_int );
269     return result;
270 }
271
272 static bool
273 testU2execute( U2_SerializeMe& sme1 )
274 {
275     {
276         XMLSerialize xmlSerialize( "unittest_u2.xml" );
277         if ( !sme1.serialize( xmlSerialize ) ) {
278             printf( "(serializing failed)" );
279             return false;
280         }
281     }
282
283     U2_SerializeMe sme2;
284
285     {
286         XMLDeserialize xmlDeserialize( "unittest_u2.xml" );
287         if ( !sme2.deserialize( xmlDeserialize ) ) {
288             printf( "(deserializing failed)" );
289             return false;
290         }
291     }
292
293     bool result = sme1 == sme2;
294     if ( !result ) {
295         printf( "(wrong values)" );
296     }
297
298     return result;
299 }
300
301 static bool
302 testU2()
303 {
304     U2_SerializeMe sme1;
305
306     sme1.m_char = 0;
307     sme1.m_unsigned_char = 1;
308     sme1.m_short = 2;
309     sme1.m_unsigned_short = 3;
310     sme1.m_int = 4;
311     sme1.m_unsigned_int = 5;
312
313     bool result;
314     result  = testU2execute( sme1 );
315
316     sme1.m_char = 0xff;
317     sme1.m_unsigned_char = 0xff;
318     sme1.m_short = 0xffff;
319     sme1.m_unsigned_short = 0xffff;
320     sme1.m_int = 0xffffffff;
321     sme1.m_unsigned_int = 0xffffffff;
322
323     result &= testU2execute( sme1 );
324
325     return result;
326 }
327
328 ///////////////////////////////////////
329
330 class U3_SerializeMe {
331 public:
332     U3_SerializeMe();
333     ~U3_SerializeMe();
334
335     bool operator == ( const U3_SerializeMe& rhs );
336
337     bool serialize( IOSerialize& ser );
338     bool deserialize( IODeserialize& deser );
339
340     const char* m_pString;
341 };
342
343 U3_SerializeMe::U3_SerializeMe()
344     : m_pString( 0 )
345 {
346 }
347
348
349 U3_SerializeMe::~U3_SerializeMe()
350 {
351     delete m_pString;
352 }
353 //--------------------------
354
355 bool
356 U3_SerializeMe::operator == ( const U3_SerializeMe& rhs )
357 {
358     return strcmp( m_pString, rhs.m_pString ) == 0;
359 }
360
361 bool
362 U3_SerializeMe::serialize( IOSerialize& ser )
363 {
364     bool result;
365     result  = ser.write( "m_pString", Glib::ustring( m_pString ) );
366     return result;
367 }
368
369 bool
370 U3_SerializeMe::deserialize( IODeserialize& deser )
371 {
372     bool result;
373     Glib::ustring str;
374     result  = deser.read( "m_pString", str );
375
376     m_pString = strdup( str.c_str() );
377
378     return result;
379 }
380
381 static bool
382 testU3()
383 {
384     U3_SerializeMe sme1;
385     sme1.m_pString = strdup( "fancy string" );
386
387     {
388         XMLSerialize xmlSerialize( "unittest_u3.xml" );
389         if ( !sme1.serialize( xmlSerialize ) ) {
390             printf( "(serializing failed)" );
391             return false;
392         }
393     }
394
395     U3_SerializeMe sme2;
396
397     {
398         XMLDeserialize xmlDeserialize( "unittest_u3.xml" );
399         if ( !sme2.deserialize( xmlDeserialize ) ) {
400             printf( "(deserializing failed)" );
401             return false;
402         }
403     }
404
405     bool result = sme1 == sme2;
406     if ( !result ) {
407         printf( "(wrong values)" );
408     }
409
410     return result;
411 }
412
413 /////////////////////////////////////
414 class testOC : public OptionContainer {
415 public:
416     testOC() {};
417     ~testOC() {};
418    
419     bool test() {
420         bool result=true;
421        
422         Option op1=Option();
423         result &= TEST_SHOULD_RETURN_FALSE(addOption(op1));
424        
425         op1=Option("option1");
426         result &= TEST_SHOULD_RETURN_FALSE(addOption(op1));
427
428         op1=Option("option1", (float)(1.0));
429         result &= TEST_SHOULD_RETURN_TRUE(addOption(op1));
430         result &= TEST_SHOULD_RETURN_FALSE(addOption(op1));
431         result &= TEST_SHOULD_RETURN_TRUE(removeOption(op1));
432         result &= TEST_SHOULD_RETURN_FALSE(hasOption(op1));
433
434        
435         op1=Option("option1", (int64_t)1);
436         result &= TEST_SHOULD_RETURN_TRUE(addOption(op1));
437
438         result &= TEST_SHOULD_RETURN_TRUE(removeOption("option1"));
439
440         result &= TEST_SHOULD_RETURN_FALSE(hasOption(op1));
441
442        
443         op1=Option("option1", (int64_t)(-1));
444         result &= TEST_SHOULD_RETURN_TRUE(addOption(op1));
445
446         Option op2=Option("option1", (double)(1.75));
447         result &= TEST_SHOULD_RETURN_FALSE(addOption(op2));
448
449        
450         op2=Option("option2", (double)(1.75));
451         result &= TEST_SHOULD_RETURN_TRUE(addOption(op2));
452
453         Option op3=Option("option3", (int64_t)(1.75));
454         result &= TEST_SHOULD_RETURN_TRUE(addOption(op3));
455
456        
457         result &= TEST_SHOULD_RETURN_TRUE(countOptions() == 3);
458
459         int i=0;
460         for ( OptionContainer::iterator it = begin();
461           it != end();
462           ++it )
463         {
464     //         printf(" (%s)",(*it).getName().c_str());
465             i++;
466         }
467         result &= TEST_SHOULD_RETURN_TRUE(i==3);
468        
469         clearOptions();
470         return result;
471     }
472    
473     void prepare() {
474         Option op1=Option("option1", (int64_t)(-1));
475         if(!addOption(op1)) {
476             printf( "prepare: could not add valid option (3)\n" );
477         }
478        
479         Option op2=Option("option2", (double)(1.75));
480         if(!addOption(op2)) {
481             printf( "prepare: adding an option with a different name should be allowed (1)\n" );
482         }
483         Option op3=Option("option3", (int64_t)(1.75));
484         if(!addOption(op3)) {
485             printf( "prepare: adding an option with a different name should be allowed (2)\n" );
486         }
487     }
488 };
489
490 static bool
491 testU4()
492 {
493     bool result=true;
494     testOC oc;
495     result &= TEST_SHOULD_RETURN_TRUE(oc.test());
496    
497     // now manipulate it externally
498     oc.prepare();
499    
500     result &= TEST_SHOULD_RETURN_TRUE(oc.hasOption("option1"));
501     result &= TEST_SHOULD_RETURN_TRUE(oc.hasOption("option2"));
502     result &= TEST_SHOULD_RETURN_TRUE(oc.hasOption("option3"));
503     result &= TEST_SHOULD_RETURN_FALSE(oc.hasOption("option4"));
504    
505     oc.setOption("option1", 1024);
506     int tst;
507     result &= TEST_SHOULD_RETURN_TRUE(oc.getOption("option1", tst));
508     result &= TEST_SHOULD_RETURN_TRUE(tst == 1024);
509    
510     return result;
511 }
512
513 /////////////////////////////////////
514 /////////////////////////////////////
515 /////////////////////////////////////
516
517
518 typedef bool ( *test_func ) ();
519 struct TestEntry {
520     const char* m_pName;
521     test_func m_pFunc;
522 };
523
524 TestEntry TestTable[] = {
525     { "serialize 0",  testU0 },
526     { "serialize 1",  testU1 },
527     { "serialize 2",  testU2 },
528     { "serialize 3",  testU3 },
529     { "OptionContainer 1",  testU4 },
530 };
531
532 int
533 main( int argc,  char** argv )
534 {
535     int iNrOfPassedTests = 0;
536     int iNrOfFailedTests = 0;
537     int iNrOfTests = sizeof( TestTable )/sizeof( TestTable[0] );
538
539     for ( int i = 0; i < iNrOfTests ; ++i ) {
540         TestEntry* pEntry = &TestTable[i];
541
542         printf( "Test \"%s\"... ", pEntry->m_pName );
543         if ( pEntry->m_pFunc() ) {
544             puts( " passed" );
545             iNrOfPassedTests++;
546         } else {
547             puts( " failed" );
548             iNrOfFailedTests++;
549         }
550     }
551
552     printf( "passed: %d\n", iNrOfPassedTests );
553     printf( "failed: %d\n", iNrOfFailedTests );
554     printf( "total:  %d\n", iNrOfTests );
555
556     return 0;
557 }
Note: See TracBrowser for help on using the browser.