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

Revision 419, 14.2 kB (checked in by pieterpalmers, 15 years ago)

namespace simplification

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