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

Revision 416, 14.2 kB (checked in by pieterpalmers, 17 years ago)

- Introduced a generic option mechanism. AvDevices? now automatically

inherit from OptionContainer? (via IAvDevice) and can specify options
(e.g. at construction). These can then be get/set using the container
functions from the outside.

- Modified the bebob, bounce, motu, mh, rme AvDevices? to make use of the

option system for their Id value and the 'snoopMode' option.

- Made M-Audio avdevice a subclass of the BeBoB avdevice to avoid code

duplication.

- Extended the bounce device

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 using namespace FreebobUtil;
30
31 ///////////////////////////////////////
32
33 class U0_SerializeMe {
34 public:
35     U0_SerializeMe();
36
37     bool operator == ( const U0_SerializeMe& rhs );
38
39     bool serialize( IOSerialize& ser );
40     bool deserialize( IODeserialize& deser );
41
42     byte_t m_byte;
43     quadlet_t m_quadlet;
44 };
45
46 U0_SerializeMe::U0_SerializeMe()
47     : m_byte( 0 )
48     , m_quadlet( 0 )
49 {
50 }
51
52
53 //--------------------------
54 bool
55 U0_SerializeMe::operator == ( const U0_SerializeMe& rhs )
56 {
57     return    ( m_byte == rhs.m_byte )
58            && ( m_quadlet == rhs.m_quadlet );
59 }
60
61 bool
62 U0_SerializeMe::serialize( IOSerialize& ser )
63 {
64     bool result;
65     result  = ser.write( "SerializeMe/m_byte", m_byte );
66     result &= ser.write( "SerializeMe/m_quadlet", m_quadlet );
67     return result;
68 }
69
70 bool
71 U0_SerializeMe::deserialize( IODeserialize& deser )
72 {
73     bool result;
74     result  = deser.read( "SerializeMe/m_byte", m_byte );
75     result &= deser.read( "SerializeMe/m_quadlet", m_quadlet );
76     return result;
77 }
78
79 static bool
80 testU0()
81 {
82     U0_SerializeMe sme1;
83
84     sme1.m_byte = 0x12;
85     sme1.m_quadlet = 0x12345678;
86
87     {
88         XMLSerialize xmlSerialize( "unittest_u0.xml" );
89         if ( !sme1.serialize( xmlSerialize ) ) {
90             printf( "(serializing failed)" );
91             return false;
92         }
93     }
94
95     U0_SerializeMe sme2;
96
97     {
98         XMLDeserialize xmlDeserialize( "unittest_u0.xml" );
99         if ( !sme2.deserialize( xmlDeserialize ) ) {
100             printf( "(deserializing failed)" );
101             return false;
102         }
103     }
104
105     bool result = sme1 == sme2;
106     if ( !result ) {
107         printf( "(wrong values)" );
108     }
109     return result;
110 }
111
112
113 ///////////////////////////////////////
114
115 class U1_SerializeMe {
116 public:
117     U1_SerializeMe();
118
119     bool operator == ( const U1_SerializeMe& rhs );
120
121     bool serialize( IOSerialize& ser );
122     bool deserialize( IODeserialize& deser );
123
124     quadlet_t m_quadlet0;
125     quadlet_t m_quadlet1;
126     quadlet_t m_quadlet2;
127 };
128
129 U1_SerializeMe::U1_SerializeMe()
130     : m_quadlet0( 0 )
131     , m_quadlet1( 0 )
132     , m_quadlet2( 0 )
133 {
134 }
135
136
137 //--------------------------
138 bool
139 U1_SerializeMe::operator == ( const U1_SerializeMe& rhs )
140 {
141     return    ( m_quadlet0 == rhs.m_quadlet0 )
142            && ( m_quadlet1 == rhs.m_quadlet1)
143            && ( m_quadlet2 == rhs.m_quadlet2);
144 }
145
146 bool
147 U1_SerializeMe::serialize( IOSerialize& ser )
148 {
149     bool result;
150     result  = ser.write( "here/and/not/there/m_quadlet0", m_quadlet0 );
151     result &= ser.write( "here/and/not/m_quadlet1", m_quadlet1 );
152     result &= ser.write( "here/and/m_quadlet2", m_quadlet2 );
153     return result;
154 }
155
156 bool
157 U1_SerializeMe::deserialize( IODeserialize& deser )
158 {
159     bool result;
160     result  = deser.read( "here/and/not/there/m_quadlet0", m_quadlet0 );
161     result &= deser.read( "here/and/not/m_quadlet1", m_quadlet1 );
162     result &= deser.read( "here/and/m_quadlet2", m_quadlet2 );
163     return result;
164 }
165
166 static bool
167 testU1()
168 {
169     U1_SerializeMe sme1;
170
171     sme1.m_quadlet0 = 0;
172     sme1.m_quadlet1 = 1;
173     sme1.m_quadlet2 = 2;
174
175     {
176         XMLSerialize xmlSerialize( "unittest_u1.xml" );
177         if ( !sme1.serialize( xmlSerialize ) ) {
178             printf( "(serializing failed)" );
179             return false;
180         }
181     }
182
183     U1_SerializeMe sme2;
184
185     {
186         XMLDeserialize xmlDeserialize( "unittest_u1.xml" );
187         if ( !sme2.deserialize( xmlDeserialize ) ) {
188             printf( "(deserializing failed)" );
189             return false;
190         }
191     }
192
193     bool result = sme1 == sme2;
194     if ( !result ) {
195         printf( "(wrong values)" );
196     }
197     return result;
198 }
199
200 ///////////////////////////////////////
201
202 class U2_SerializeMe {
203 public:
204     U2_SerializeMe();
205
206     bool operator == ( const U2_SerializeMe& rhs );
207
208     bool serialize( IOSerialize& ser );
209     bool deserialize( IODeserialize& deser );
210
211     char             m_char;
212     unsigned char    m_unsigned_char;
213     short            m_short;
214     unsigned short   m_unsigned_short;
215     int              m_int;
216     unsigned int     m_unsigned_int;
217 };
218
219 U2_SerializeMe::U2_SerializeMe()
220     : m_char( 0 )
221     , m_unsigned_char( 0 )
222     , m_short( 0 )
223     , m_unsigned_short( 0 )
224     , m_int( 0 )
225     , m_unsigned_int( 0 )
226 {
227 }
228
229 //--------------------------
230
231 bool
232 U2_SerializeMe::operator == ( const U2_SerializeMe& rhs )
233 {
234     return    ( m_char == rhs.m_char )
235            && ( m_unsigned_char == rhs.m_unsigned_char )
236            && ( m_short == rhs.m_short )
237            && ( m_unsigned_short == rhs.m_unsigned_short )
238            && ( m_int == rhs.m_int )
239            && ( m_unsigned_int == rhs.m_unsigned_int );
240 }
241
242 bool
243 U2_SerializeMe::serialize( IOSerialize& ser )
244 {
245     bool result;
246     result  = ser.write( "m_char", m_char );
247     result &= ser.write( "m_unsigned_char", m_unsigned_char );
248     result &= ser.write( "m_short", m_short );
249     result &= ser.write( "m_unsigned_short", m_unsigned_short );
250     result &= ser.write( "m_int", m_int );
251     result &= ser.write( "m_unsigned_int", m_unsigned_int );
252     return result;
253 }
254
255 bool
256 U2_SerializeMe::deserialize( IODeserialize& deser )
257 {
258     bool result;
259     result  = deser.read( "m_char", m_char );
260     result &= deser.read( "m_unsigned_char", m_unsigned_char );
261     result &= deser.read( "m_short", m_short );
262     result &= deser.read( "m_unsigned_short", m_unsigned_short );
263     result &= deser.read( "m_int", m_int );
264     result &= deser.read( "m_unsigned_int", m_unsigned_int );
265     return result;
266 }
267
268 static bool
269 testU2execute( U2_SerializeMe& sme1 )
270 {
271     {
272         XMLSerialize xmlSerialize( "unittest_u2.xml" );
273         if ( !sme1.serialize( xmlSerialize ) ) {
274             printf( "(serializing failed)" );
275             return false;
276         }
277     }
278
279     U2_SerializeMe sme2;
280
281     {
282         XMLDeserialize xmlDeserialize( "unittest_u2.xml" );
283         if ( !sme2.deserialize( xmlDeserialize ) ) {
284             printf( "(deserializing failed)" );
285             return false;
286         }
287     }
288
289     bool result = sme1 == sme2;
290     if ( !result ) {
291         printf( "(wrong values)" );
292     }
293
294     return result;
295 }
296
297 static bool
298 testU2()
299 {
300     U2_SerializeMe sme1;
301
302     sme1.m_char = 0;
303     sme1.m_unsigned_char = 1;
304     sme1.m_short = 2;
305     sme1.m_unsigned_short = 3;
306     sme1.m_int = 4;
307     sme1.m_unsigned_int = 5;
308
309     bool result;
310     result  = testU2execute( sme1 );
311
312     sme1.m_char = 0xff;
313     sme1.m_unsigned_char = 0xff;
314     sme1.m_short = 0xffff;
315     sme1.m_unsigned_short = 0xffff;
316     sme1.m_int = 0xffffffff;
317     sme1.m_unsigned_int = 0xffffffff;
318
319     result &= testU2execute( sme1 );
320
321     return result;
322 }
323
324 ///////////////////////////////////////
325
326 class U3_SerializeMe {
327 public:
328     U3_SerializeMe();
329     ~U3_SerializeMe();
330
331     bool operator == ( const U3_SerializeMe& rhs );
332
333     bool serialize( IOSerialize& ser );
334     bool deserialize( IODeserialize& deser );
335
336     const char* m_pString;
337 };
338
339 U3_SerializeMe::U3_SerializeMe()
340     : m_pString( 0 )
341 {
342 }
343
344
345 U3_SerializeMe::~U3_SerializeMe()
346 {
347     delete m_pString;
348 }
349 //--------------------------
350
351 bool
352 U3_SerializeMe::operator == ( const U3_SerializeMe& rhs )
353 {
354     return strcmp( m_pString, rhs.m_pString ) == 0;
355 }
356
357 bool
358 U3_SerializeMe::serialize( IOSerialize& ser )
359 {
360     bool result;
361     result  = ser.write( "m_pString", Glib::ustring( m_pString ) );
362     return result;
363 }
364
365 bool
366 U3_SerializeMe::deserialize( IODeserialize& deser )
367 {
368     bool result;
369     Glib::ustring str;
370     result  = deser.read( "m_pString", str );
371
372     m_pString = strdup( str.c_str() );
373
374     return result;
375 }
376
377 static bool
378 testU3()
379 {
380     U3_SerializeMe sme1;
381     sme1.m_pString = strdup( "fancy string" );
382
383     {
384         XMLSerialize xmlSerialize( "unittest_u3.xml" );
385         if ( !sme1.serialize( xmlSerialize ) ) {
386             printf( "(serializing failed)" );
387             return false;
388         }
389     }
390
391     U3_SerializeMe sme2;
392
393     {
394         XMLDeserialize xmlDeserialize( "unittest_u3.xml" );
395         if ( !sme2.deserialize( xmlDeserialize ) ) {
396             printf( "(deserializing failed)" );
397             return false;
398         }
399     }
400
401     bool result = sme1 == sme2;
402     if ( !result ) {
403         printf( "(wrong values)" );
404     }
405
406     return result;
407 }
408
409 /////////////////////////////////////
410 class testOC : public OptionContainer {
411 public:
412     testOC() {};
413     ~testOC() {};
414    
415     bool test() {
416         bool result=true;
417        
418         Option op1=Option();
419         if(addOption(op1)) {
420             printf( "adding an empty option should not succeed\n" );
421             result=false;
422         }
423        
424         op1=Option("option1");
425         if(addOption(op1)) {
426             printf( "adding an option without a value should not succeed\n" );
427             result=false;
428         }
429        
430         op1=Option("option1", (float)(1.0));
431         if(!addOption(op1)) {
432             printf( "could not add valid option (1)\n" );
433             result=false;
434         }
435         if(addOption(op1)) {
436             printf( "adding the same option twice should not succeed\n" );
437             result=false;
438         }
439         if(!removeOption(op1)) {
440             printf( "could not remove option by reference\n" );
441             result=false;
442         }
443         if(hasOption(op1)) {
444             printf( "option not removed (by reference)\n" );
445             result=false;
446         }
447        
448         op1=Option("option1", (int64_t)1);
449         if(!addOption(op1)) {
450             printf( "could not add valid option (2)\n" );
451             result=false;
452         }
453         if(!removeOption("option1")) {
454             printf( "could not remove option by name\n" );
455             result=false;
456         }
457         if(hasOption(op1)) {
458             printf( "option not removed (by name)\n" );
459             result=false;
460         }
461        
462         op1=Option("option1", (int64_t)(-1));
463         if(!addOption(op1)) {
464             printf( "could not add valid option (3)\n" );
465             result=false;
466         }
467         Option op2=Option("option1", (double)(1.75));
468         if(addOption(op2)) {
469             printf( "adding two options with the same name should not be allowed\n" );
470             result=false;
471         }
472        
473         op2=Option("option2", (double)(1.75));
474         if(!addOption(op2)) {
475             printf( "adding an option with a different name should be allowed (1)\n" );
476             result=false;
477         }
478         Option op3=Option("option3", (int64_t)(1.75));
479         if(!addOption(op3)) {
480             printf( "adding an option with a different name should be allowed (2)\n" );
481             result=false;
482         }
483        
484         if(countOptions() != 3) {
485             printf( "countOptions failed\n" );
486             result=false;
487         }
488            
489         int i=0;
490         for ( OptionContainer::iterator it = begin();
491           it != end();
492           ++it )
493         {
494     //         printf(" (%s)",(*it).getName().c_str());
495             i++;
496         }
497         if(i!=3) {
498             printf( "did not iterate through the right amount of options\n" );
499             result=false;
500         }
501        
502         clearOptions();
503         return result;
504     }
505    
506     void prepare() {
507         Option op1=Option("option1", (int64_t)(-1));
508         if(!addOption(op1)) {
509             printf( "prepare: could not add valid option (3)\n" );
510         }
511        
512         Option op2=Option("option2", (double)(1.75));
513         if(!addOption(op2)) {
514             printf( "prepare: adding an option with a different name should be allowed (1)\n" );
515         }
516         Option op3=Option("option3", (int64_t)(1.75));
517         if(!addOption(op3)) {
518             printf( "prepare: adding an option with a different name should be allowed (2)\n" );
519         }
520     }
521 };
522
523 static bool
524 testU4()
525 {
526     bool result=true;
527     testOC oc;
528     if(!oc.test()) {
529         printf( "OptionContainer test failed\n" );
530         result=false;
531     }
532    
533     // now manipulate it externally
534     oc.prepare();
535    
536     if(!oc.hasOption("option1")) {
537         printf( "option1 should be present\n" );
538         result=false;
539     }
540     if(!oc.hasOption("option2")) {
541         printf( "option2 should be present\n" );
542         result=false;
543     }
544     if(!oc.hasOption("option3")) {
545         printf( "option3 should be present\n" );
546         result=false;
547     }
548     if(oc.hasOption("option4")) {
549         printf( "option4 should not be present\n" );
550         result=false;
551     }
552
553     oc.setOption("option1", 1024);
554     int tst;
555     if (!oc.getOption("option1", tst)) {
556         printf( "could not get option1 value\n" );
557         result=false;
558     }
559    
560     if(tst != 1024) {
561         printf( "option1 should be 1024\n" );
562         result=false;
563     }
564    
565     return result;
566 }
567
568 /////////////////////////////////////
569 /////////////////////////////////////
570 /////////////////////////////////////
571
572
573 typedef bool ( *test_func ) ();
574 struct TestEntry {
575     const char* m_pName;
576     test_func m_pFunc;
577 };
578
579 TestEntry TestTable[] = {
580     { "serialize 0",  testU0 },
581     { "serialize 1",  testU1 },
582     { "serialize 2",  testU2 },
583     { "serialize 3",  testU3 },
584     { "OptionContainer 1",  testU4 },
585 };
586
587 int
588 main( int argc,  char** argv )
589 {
590     int iNrOfPassedTests = 0;
591     int iNrOfFailedTests = 0;
592     int iNrOfTests = sizeof( TestTable )/sizeof( TestTable[0] );
593
594     for ( int i = 0; i < iNrOfTests ; ++i ) {
595         TestEntry* pEntry = &TestTable[i];
596
597         printf( "Test \"%s\"... ", pEntry->m_pName );
598         if ( pEntry->m_pFunc() ) {
599             puts( " passed" );
600             iNrOfPassedTests++;
601         } else {
602             puts( " failed" );
603             iNrOfFailedTests++;
604         }
605     }
606
607     printf( "passed: %d\n", iNrOfPassedTests );
608     printf( "failed: %d\n", iNrOfFailedTests );
609     printf( "total:  %d\n", iNrOfTests );
610
611     return 0;
612 }
Note: See TracBrowser for help on using the browser.