]> git.donarmstrong.com Git - biopieces.git/blob - code_c/Maasha/src/test/test_list.c
f7660e1f6f682ffc8e7c29e13da11a2d3f55bd75
[biopieces.git] / code_c / Maasha / src / test / test_list.c
1 /* Martin Asser Hansen (mail@maasha.dk) Copyright (C) 2008 - All right reserved */
2
3 #include "common.h"
4 #include "mem.h"
5 #include "list.h"
6
7
8 static void test_list_sl_new();
9 static void test_node_sl_new();
10 static void test_list_sl_add_beg();
11 static void test_list_sl_add_after();
12 static void test_list_sl_remove_beg();
13 static void test_list_sl_remove_after();
14 static void test_list_sl_print();
15 static void test_list_sl_sort();
16 static void test_list_sl_destroy();
17
18 static void test_list_dl_new();
19 static void test_node_dl_new();
20 static void test_list_dl_add_beg();
21 static void test_list_dl_add_end();
22 static void test_list_dl_add_before();
23 static void test_list_dl_add_after();
24 static void test_list_dl_remove();
25 static void test_list_dl_print();
26 static void test_list_dl_destroy();
27
28 static void test_list_count();
29
30
31 int main()
32 {
33     fprintf( stderr, "Running all tests for list.c\n" );
34
35     test_list_sl_new();
36     test_node_sl_new();
37     test_list_sl_add_beg();
38     test_list_sl_add_after();
39     test_list_sl_remove_beg();
40     test_list_sl_remove_after();
41     test_list_sl_print();
42     test_list_sl_sort();
43     test_list_sl_destroy();
44
45     test_list_dl_new();
46     test_node_dl_new();
47     test_list_dl_add_beg();
48     test_list_dl_add_end();
49     test_list_dl_add_before();
50     test_list_dl_add_after();
51     test_list_dl_remove();
52     test_list_dl_print();
53     test_list_dl_destroy();
54
55     test_list_count();
56
57     fprintf( stderr, "Done\n\n" );
58
59     return EXIT_SUCCESS;
60 }
61
62
63 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SINGLY LINKED LIST <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
64
65
66 void test_list_sl_new()
67 {
68     fprintf( stderr, "   Testing list_sl_new ... " );
69
70     list_sl *list = NULL;
71
72     list = list_sl_new();
73
74     assert( list != NULL );
75     assert( list->first == NULL );
76
77     fprintf( stderr, "OK\n" );
78 }
79
80
81 void test_node_sl_new()
82 {
83     fprintf( stderr, "   Testing node_sl_new ... " );
84
85     node_sl *node = NULL;
86
87     node = node_sl_new();
88
89     assert( node != NULL );
90     assert( node->next == NULL );
91     assert( node->val  == NULL );
92
93     fprintf( stderr, "OK\n" );
94 }
95
96
97 void test_list_sl_add_beg()
98 {
99     fprintf( stderr, "   Testing list_sl_add_beg ... " );
100
101     char    *array[3] = { "test1", "test2", "test3" };
102     list_sl *list     = NULL;
103     node_sl *node     = NULL;
104     int      i        = 0;
105
106     list = list_sl_new();
107
108     for ( i = 0; i < 3; i++ )
109     {
110         node = node_sl_new();
111
112         node->val = array[ i ];
113
114         list_sl_add_beg( &list, &node );
115     }
116
117     i = 2;
118
119     for ( node = list->first; node != NULL; node = node->next )
120     {
121         assert( strcmp( array[ i ], ( char * ) node->val ) == 0 );
122
123         i--;
124     }
125
126     fprintf( stderr, "OK\n" );
127 }
128
129
130 void test_list_sl_add_after()
131 {
132     fprintf( stderr, "   Testing list_sl_add_after ... " );
133
134     char    *array[3] = { "test1", "test2", "test3" };
135     list_sl *list     = NULL;
136     node_sl *node     = NULL;
137     node_sl *new_node = NULL;
138     int      i        = 0;
139
140     list     = list_sl_new();
141     new_node = node_sl_new();
142
143     new_node->val = array[ 0 ];
144
145     list_sl_add_beg( &list, &new_node );
146
147     node = new_node;
148
149     for ( i = 1; i < 3; i++ )
150     {
151         new_node = node_sl_new();
152
153         new_node->val = array[ i ];
154
155         list_sl_add_after( &node, &new_node );
156
157         node = new_node;
158     }
159
160     i = 0;
161
162     for ( node = list->first; node != NULL; node = node->next )
163     {
164         assert( strcmp( array[ i ], ( char * ) node->val ) == 0 );
165
166         i++;
167     }
168
169     fprintf( stderr, "OK\n" );
170 }
171
172
173 void test_list_sl_remove_beg()
174 {
175     fprintf( stderr, "   Testing list_sl_remove_beg ... " );
176
177     char    *array[3] = { "test1", "test2", "test3" };
178     list_sl *list     = NULL;
179     node_sl *node     = NULL;
180     node_sl *new_node = NULL;
181     int      i        = 0;
182
183     list     = list_sl_new();
184     new_node = node_sl_new();
185
186     new_node->val = array[ 0 ];
187
188     list_sl_add_beg( &list, &new_node );
189
190     node = new_node;
191
192     for ( i = 1; i < 3; i++ )
193     {
194         new_node = node_sl_new();
195
196         new_node->val = array[ i ];
197
198         list_sl_add_after( &node, &new_node );
199
200         node = new_node;
201     }
202
203     i = 0;
204
205     node = list->first;
206
207     while ( node != NULL )
208     {
209         assert( strcmp( ( char * ) node->val, array[ i ] ) == 0 );
210
211         list_sl_remove_beg( &list );
212         
213         node = list->first;
214
215         i++;
216     }
217
218     fprintf( stderr, "OK\n" );
219 }
220
221
222 void test_list_sl_remove_after()
223 {
224     fprintf( stderr, "   Testing list_sl_remove_after ... " );
225
226     char    *array[3] = { "test1", "test2", "test3" };
227     list_sl *list     = NULL;
228     node_sl *node     = NULL;
229     node_sl *new_node = NULL;
230     int      i        = 0;
231
232     list     = list_sl_new();
233     new_node = node_sl_new();
234
235     new_node->val = array[ 0 ];
236
237     list_sl_add_beg( &list, &new_node );
238
239     node = new_node;
240
241     for ( i = 1; i < 3; i++ )
242     {
243         new_node = node_sl_new();
244
245         new_node->val = array[ i ];
246
247         list_sl_add_after( &node, &new_node );
248
249         node = new_node;
250     }
251
252     assert( strcmp( ( char * ) list->first->next->val, "test2" ) == 0 );
253
254     list_sl_remove_after( &list->first );
255
256     assert( strcmp( ( char * ) list->first->next->val, "test3" ) == 0 );
257
258     fprintf( stderr, "OK\n" );
259 }
260
261
262 void test_list_sl_print()
263 {
264     fprintf( stderr, "   Testing list_sl_print ... " );
265
266     list_sl *list  = NULL;
267     node_sl *node1 = NULL;
268     node_sl *node2 = NULL;
269     node_sl *node3 = NULL;
270
271     list = list_sl_new();
272
273     node1 = node_sl_new();
274     node2 = node_sl_new();
275     node3 = node_sl_new();
276
277     node1->val = "TEST1";
278     node2->val = "TEST2";
279     node3->val = "TEST3";
280
281     list_sl_add_beg( &list, &node1 );
282     list_sl_add_beg( &list, &node2 );
283     list_sl_add_beg( &list, &node3 );
284
285     // list_sl_print( list );
286
287     fprintf( stderr, "OK\n" );
288 }
289
290
291 void test_list_sl_sort()
292 {
293     fprintf( stderr, "   Testing list_sl_sort ... " );
294
295     /* Sorting a char list */
296
297     char    *array[3] = { "test1", "test2", "test3" };
298     list_sl *list     = NULL;
299     node_sl *node     = NULL;
300     int      i        = 0;
301
302     list = list_sl_new();
303
304     for ( i = 0; i < 3; i++ )
305     {
306         node = node_sl_new();
307
308         node->val = array[ i ];
309
310         list_sl_add_beg( &list, &node );
311     }
312
313     list_sl_sort( &list, cmp_list_sl_char_asc );
314     list_sl_sort( &list, cmp_list_sl_char_desc );
315
316 //    /* Sorting a int list */
317 //
318 //    int int_array[ 5 ] = { 345, 23, 23, 1, 400 }; 
319 //    list_sl *int_list  = NULL;
320 //    node_sl *int_node  = NULL;
321 //
322 //    int_list = list_sl_new();
323 //
324 //    for ( i = 0; i < 5; i++ )
325 //    {
326 //        printf( "int: %d\n", int_array[ i ] );
327 //
328 //        int_node = node_sl_new();
329 //
330 //        node->val = int_array[ i ];
331 //    }
332
333     fprintf( stderr, "OK\n" );
334 }
335
336
337 void test_list_sl_destroy()
338 {
339     fprintf( stderr, "   Testing list_sl_destroy ... " );
340
341     char    *array[3] = { "test1", "test2", "test3" };
342     list_sl *list     = NULL;
343     node_sl *node     = NULL;
344     int      i        = 0;
345
346     list = list_sl_new();
347
348     for ( i = 0; i < 3; i++ )
349     {
350         node = node_sl_new();
351
352         node->val = array[ i ];
353
354         list_sl_add_beg( &list, &node );
355     }
356
357     list_sl_destroy( &list );
358
359     assert( list == NULL );
360
361     fprintf( stderr, "OK\n" );
362 }
363
364
365 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DOUBLY LINKED LIST <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
366
367
368 void test_list_dl_new()
369 {
370     fprintf( stderr, "   Testing list_dl_new ... " );
371
372     list_dl *list = NULL;
373
374     list = list_dl_new();
375
376     assert( list->first == NULL );
377     assert( list->last  == NULL );
378
379     fprintf( stderr, "OK\n" );
380 }
381
382
383 void test_node_dl_new()
384 {
385     fprintf( stderr, "   Testing node_dl_new ... " );
386
387     node_dl *node = NULL;
388
389     node = node_dl_new();
390     
391     assert( node->next == NULL );
392     assert( node->prev == NULL );
393     assert( node->val  == NULL );
394
395     fprintf( stderr, "OK\n" );
396 }
397
398
399 void test_list_dl_add_beg()
400 {
401     fprintf( stderr, "   Testing list_dl_add_beg ... " );
402
403     list_dl *list  = list_dl_new();
404     node_dl *node1 = node_dl_new();
405     node_dl *node2 = node_dl_new();
406     node_dl *node3 = node_dl_new();
407
408     node1->val = "TEST1";
409     node2->val = "TEST2";
410     node3->val = "TEST3";
411
412     list_dl_add_beg( &list, &node1 );
413
414     assert( strcmp( list->first->val, "TEST1" ) == 0 );
415
416     list_dl_add_beg( &list, &node2 );
417
418     assert( strcmp( list->first->val, "TEST2" ) == 0 );
419
420     list_dl_add_beg( &list, &node3 );
421
422     assert( strcmp( list->first->val, "TEST3" ) == 0 );
423
424     fprintf( stderr, "OK\n" );
425 }
426
427
428 void test_list_dl_add_end()
429 {
430     fprintf( stderr, "   Testing list_dl_add_end ... " );
431
432     list_dl *list  = list_dl_new();
433     node_dl *node1 = node_dl_new();
434     node_dl *node2 = node_dl_new();
435     node_dl *node3 = node_dl_new();
436
437     node1->val = "TEST1";
438     node2->val = "TEST2";
439     node3->val = "TEST3";
440
441     list_dl_add_end( &list, &node1 );
442
443     assert( strcmp( list->last->val, "TEST1" ) == 0 );
444
445     list_dl_add_end( &list, &node2 );
446
447     assert( strcmp( list->last->val, "TEST2" ) == 0 );
448
449     list_dl_add_end( &list, &node3 );
450
451     assert( strcmp( list->last->val, "TEST3" ) == 0 );
452
453     fprintf( stderr, "OK\n" );
454 }
455
456
457 void test_list_dl_add_before()
458 {
459     fprintf( stderr, "   Testing list_dl_add_before ... " );
460
461     list_dl *list  = list_dl_new();
462     node_dl *node1 = node_dl_new();
463     node_dl *node2 = node_dl_new();
464     node_dl *node3 = node_dl_new();
465
466     node1->val = "TEST1";
467     node2->val = "TEST2";
468     node3->val = "TEST3";
469
470     list_dl_add_beg( &list, &node1 );
471
472     assert( strcmp( list->first->val, "TEST1" ) == 0 );
473
474     list_dl_add_before( &list, &node1, &node2 );
475
476     assert( strcmp( list->first->val, "TEST2" ) == 0 );
477
478     list_dl_add_before( &list, &node1, &node3 );
479
480     assert( strcmp( list->first->val, "TEST2" ) == 0 );
481
482     list_dl_add_before( &list, &node2, &node3 );
483
484     assert( strcmp( list->first->val, "TEST3" ) == 0 );
485
486     fprintf( stderr, "OK\n" );
487 }
488
489
490 void test_list_dl_add_after()
491 {
492     fprintf( stderr, "   Testing list_dl_add_after ... " );
493
494     list_dl *list  = list_dl_new();
495     node_dl *node1 = node_dl_new();
496     node_dl *node2 = node_dl_new();
497     node_dl *node3 = node_dl_new();
498
499     node1->val = "TEST1";
500     node2->val = "TEST2";
501     node3->val = "TEST3";
502
503     list_dl_add_beg( &list, &node1 );
504
505     assert( strcmp( list->first->val, "TEST1" ) == 0 );
506
507     list_dl_add_after( &list, &node1, &node2 );
508
509     assert( strcmp( list->last->val, "TEST2" ) == 0 );
510
511     list_dl_add_after( &list, &node1, &node3 );
512
513     assert( strcmp( list->last->val, "TEST2" ) == 0 );
514
515     list_dl_add_after( &list, &node2, &node3 );
516
517     assert( strcmp( list->last->val, "TEST3" ) == 0 );
518
519     fprintf( stderr, "OK\n" );
520 }
521
522
523 void test_list_dl_remove()
524 {
525     fprintf( stderr, "   Testing list_dl_remove ... " );
526
527     list_dl *list  = list_dl_new();
528     node_dl *node1 = node_dl_new();
529     node_dl *node2 = node_dl_new();
530     node_dl *node3 = node_dl_new();
531
532     node1->val = "TEST1";
533     node2->val = "TEST2";
534     node3->val = "TEST3";
535
536     list_dl_add_beg( &list, &node1 );
537
538     list_dl_add_after( &list, &node1, &node2 );
539     list_dl_add_after( &list, &node2, &node3 );
540
541     list_dl_remove( &list, &node3 );
542     assert( strcmp( list->last->val, "TEST2" ) == 0 );
543
544     list_dl_remove( &list, &node2 );
545     assert( strcmp( list->last->val, "TEST1" ) == 0 );
546
547     list_dl_remove( &list, &node1 );
548
549     assert( list->first == NULL );
550     assert( list->last  == NULL );
551
552     fprintf( stderr, "OK\n" );
553 }
554
555
556 void test_list_dl_print()
557 {
558     fprintf( stderr, "   Testing list_dl_print ... " );
559
560     list_dl *list  = list_dl_new();
561     node_dl *node1 = node_dl_new();
562     node_dl *node2 = node_dl_new();
563     node_dl *node3 = node_dl_new();
564
565     node1->val = "TEST1";
566     node2->val = "TEST2";
567     node3->val = "TEST3";
568
569     list_dl_add_beg( &list, &node1 );
570     list_dl_add_beg( &list, &node2 );
571     list_dl_add_beg( &list, &node3 );
572
573     // list_dl_print( list );
574
575     fprintf( stderr, "OK\n" );
576 }
577
578
579 void test_list_dl_destroy()
580 {
581     fprintf( stderr, "   Testing list_dl_destroy ... " );
582
583     list_dl *list  = list_dl_new();
584     node_dl *node1 = node_dl_new();
585     node_dl *node2 = node_dl_new();
586     node_dl *node3 = node_dl_new();
587
588     node1->val = "TEST1";
589     node2->val = "TEST2";
590     node3->val = "TEST3";
591
592     list_dl_add_beg( &list, &node1 );
593     list_dl_add_beg( &list, &node2 );
594     list_dl_add_beg( &list, &node3 );
595
596 //    list_dl_print( list );
597
598     list_dl_destroy( &list );
599
600     assert( list == NULL );
601
602     fprintf( stderr, "OK\n" );
603 }
604
605
606 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GENERIC LINKED LIST <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
607
608
609 void test_list_count()
610 {
611     fprintf( stderr, "   Testing list_count ... " );
612
613     /* Singly linked list. */
614
615     char    *array[3] = { "test1", "test2", "test3" };
616     list_sl *sllist   = NULL;
617     node_sl *node     = NULL;
618     node_sl *new_node = NULL;
619     int      i        = 0;
620
621     sllist   = list_sl_new();
622     new_node = node_sl_new();
623
624     new_node->val = array[ 0 ];
625
626     list_sl_add_beg( &sllist, &new_node );
627
628     node = new_node;
629
630     for ( i = 1; i < 3; i++ )
631     {
632         new_node = node_sl_new();
633
634         new_node->val = array[ i ];
635
636         list_sl_add_after( &node, &new_node );
637
638         node = new_node;
639     }
640
641     assert( list_count( sllist ) == 3 );
642
643     /* Doubly linked list. */
644
645     list_dl *dllist = list_dl_new();
646     node_dl *node1  = node_dl_new();
647     node_dl *node2  = node_dl_new();
648     node_dl *node3  = node_dl_new();
649
650     node1->val = "TEST1";
651     node2->val = "TEST2";
652     node3->val = "TEST3";
653
654     list_dl_add_beg( &dllist, &node1 );
655     list_dl_add_beg( &dllist, &node2 );
656     list_dl_add_beg( &dllist, &node3 );
657
658     assert( list_count( dllist ) == 3 );
659
660     fprintf( stderr, "OK\n" );
661 }
662
663
664 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
665
666