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

Revision 2803, 13.0 kB (checked in by jwoithe, 3 years ago)

Cosmetic: capitalise "L" in "Linux".

"Linux" is a proper noun so it should start with a capital letter. These
changes are almost all within comments.

This patch was originally proposed by pander on the ffado-devel mailing
list. It has been expanded to cover all similar cases to maintain
consistency throughout the source tree.

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