root/trunk/libffado/src/libutil/unittests.cpp

Revision 742, 12.9 kB (checked in by ppalmers, 13 years ago)

- Remove some obsolete support files and dirs

- Clean up the license statements in the source files. Everything is

GPL version 3 now.

- Add license and copyright notices to scons scripts

- Clean up some other text files

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