-
Notifications
You must be signed in to change notification settings - Fork 0
/
testcases.c
682 lines (516 loc) · 11.6 KB
/
testcases.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/**
* @file Ring data structure unit tests
* @author Markus Wanke
*/
/* ---- System Header -------------------------------------------------------------- */
#include <time.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
/* ---- Own Header ----------------------------------------------------------------- */
#include "ring.h"
/* ---- Helper Functions ----------------------------------------------------------- */
#define ES_none "\033[0m"
#define ES_bold "\033[1m"
#define ES_red "\033[31m"
#define ES_yellow "\033[33m"
#define ES_blue "\033[34m"
#define ES_white "\033[37m"
#define pinfo(format, ...) fprintf(stderr, ES_bold ES_blue "INFO " ES_none ES_white format ES_none "\n", ## __VA_ARGS__)
#define pwarn(format, ...) fprintf(stderr, ES_bold ES_yellow "WARN " ES_none ES_yellow format ES_none "\n", ## __VA_ARGS__)
#define perr(format, ...) fprintf(stderr, ES_bold ES_red "ERROR " ES_none ES_red format ES_none "\n", ## __VA_ARGS__)
void pfunc(FILE * f, cp c, uint64_t pos)
{
fprintf(f, " Pos: %3ld = %6d\n", pos, *(int*)c );
}
/* ---- Test Functions ----------------------------------------------------------- */
#define TEST_FUNC_ARRAY_SIZE 20
#define TEST_ARRAY_SIZE 2000
void( *tests[TEST_FUNC_ARRAY_SIZE] )( void );
int32_t a[TEST_ARRAY_SIZE];
void t_01(void)
{
int i = 0 ;
int* p = NULL ;
Ring r = ring_create();
for(i = 0; i < 10; ++i)
ring_push( r, a + i );
for(i = 9; i >= 0; --i)
{
p = ring_pop(r);
if( p != a + i && *p != i )
{
perr("T1: Push & Pop at i = %d", i);
return;
}
}
if( !ring_is_empty( r ) )
{
perr("T1: Push & Pop: RING not empty");
return;
}
ring_destroy(r, NULL);
pinfo("T1: ring_push & ring_pop success");
}
void t_02(void)
{
int i = 0;
int* p = NULL ;
Ring r = ring_create();
for(i = 0; i < 20; ++i)
ring_push(r, a+i);
for(i = 0; i < 20; ++i)
{
p = ring_chop(r);
if( p != a+i ){
perr( "T2: push & chop address fail at i = %d", i); return;}
if( *p != i )
perr( "T2: push & chop contend fail at i = %d != %d", i, *p);
}
if(!ring_is_empty(r))
{
perr( "T2: push & chop: RING not empty");
return;
}
ring_destroy(r, NULL);
pinfo("T2: ring_push & ring_chop success");
}
void t_03(void)
{
int i = 0;
Ring r = ring_create();
for(i = 0; i < 5; ++i)
{
ring_append(r, i+a);
}
i = 0;
for( ring_iterator( r ) )
{
int * c = ring_index;
if ( *c != i++ )
{
perr( "T3: iterator fail ");
return;
}
}
ring_destroy(r, NULL);
pinfo( "T3: ring_iterator success");
}
void t_04(void)
{
int i = 0;
int * p = NULL;
Ring r = ring_create();
for(i = 0; i < 10; ++i)
ring_append(r, a+i);
for(i = 0; i < 10; ++i)
{
p = ring_at(r, i);
if( p != a+i ) {
perr( "T4: append & At i addresse fail = %d", i); return;}
if( *p != i ){
perr( "T4: append & At i contend fail = %d", i); return;}
}
int x = 77;
ring_push(r, &x);
if(ring_at(r, 0) != &x)
perr( "T4: push & At addresse 77 fail");
ring_append(r, &x);
if(ring_at(r, ring_size(r)-1 ) != &x)
perr( "T4: append & At addresse 77 fail");
ring_destroy(r, NULL);
pinfo( "T4: ring_mixed stuff success ");
}
bool ret_true(cp c, cp ud) {return true;}
bool ret_false(cp c, cp ud) {return false;}
bool c_eq_ud(cp c, cp ud)
{
return (*(int*)c == *(int*)ud);
}
bool even(cp c, cp ud)
{
return ((*(int*)c) % 2 == 0);
}
bool odd(cp c, cp ud)
{
return ((*(int*)c) % 2 != 0);
}
bool dunknow(cp c, cp ud)
{
return (rand() % 2 == 0);
}
void t_05(void)
{
int i = 0;
int * p = NULL;
Ring r = ring_create();
Ring res = NULL;
// FOR ERROR PRINT USE
// ring_print(stderr, r, pfunc);
int x = 11;
int y = 44;
int z = 66;
ring_append(r, &z);
res = ring_remove_selected(r, ret_false, NULL);
if( ring_size(r) != 1 || ring_size(res) != 0 || ring_first(r) != &z )
{
perr( "T5: Case 01 failed" ); return;
}
ring_destroy( res, NULL);
// ------
res = ring_remove_selected(r, ret_true, NULL);
if( ring_size(r) != 0 || ring_size(res) != 1 || ring_first(res) != &z )
{
perr( "T5: Case 02 failed" ); return;
}
// ring_print(stderr, res, pfunc);
ring_destroy( res, NULL);
// ------
ring_append(r, &x);
ring_append(r, &y);
res = ring_remove_selected(r, c_eq_ud, &x);
if( ring_size(r) != 1 || ring_size(res) != 1
|| ring_first(res) != &x || ring_first(r) != &y )
{
perr( "T5: Case 03 failed" ); return;
}
ring_destroy( res, NULL);
ring_pop(r);
// ------
ring_append(r, &x);
ring_append(r, &y);
res = ring_remove_selected(r, c_eq_ud, &y);
if( ring_size(r) != 1 || ring_size(res) != 1
|| ring_first(r) != &x || ring_first(res) != &y )
{
perr( "T5: Case 04 failed" ); return;
}
ring_destroy( res, NULL);
ring_pop(r);
// ------
ring_append(r, &x);
ring_append(r, &y);
ring_append(r, &z);
res = ring_remove_selected(r, c_eq_ud, &z);
if( ring_size(r) != 2 || ring_size(res) != 1
|| ring_first(r) != &x || ring_first(res) != &z
|| ring_last(r) != &y)
{
perr( "T5: Case 05 failed" ); return;
}
ring_destroy( res, NULL);
ring_destroy( r, NULL);
// ------
// ------
r = ring_create();
for(i = 0; i < 6; ++i)
ring_append(r, a + i);
// ring_print(stderr, r, pfunc);
res = ring_remove_selected(r, even, NULL);
if( ring_size( r ) != 3 || ring_size( res ) != 3 )
{
perr( "T5: Case 06 failed" ); return;
}
// ring_print(stderr, r, pfunc);
// ring_print(stderr, res, pfunc);
for(i = 0; i < 6; ++i)
{
if( i % 2 == 0 )
{
int * p = ring_pop(res);
if ( i != *p )
perr( "T5: Case 07 failed" );
}
else
{
int * p = ring_pop(r);
if ( i != *p )
perr( "T5: Case 07 failed" );
}
}
ring_destroy( res, NULL);
ring_destroy( r, NULL);
// ------
r = ring_create();
for(i = 0; i < 6; ++i)
ring_append(r, a + i);
// ring_print(stderr, r, pfunc);
res = ring_remove_selected(r, odd, NULL);
if( ring_size( r ) != 3 || ring_size( res ) != 3 )
{
perr( "T5: Case 08 failed" ); return;
}
// ring_print(stderr, r, pfunc);
// ring_print(stderr, res, pfunc);
for(i = 0; i < 6; ++i)
{
if( i % 2 == 0 )
{
int * p = ring_pop(r);
if ( i != *p )
perr( "T5: Case 9 failed" );
}
else
{
int * p = ring_pop(res);
if ( i != *p )
perr( "T5: Case 9 failed" );
}
}
ring_destroy( res, NULL);
ring_destroy( r, NULL);
// ------
for(int times = 0; times < 100; ++times)
{
int32_t erg = rand() % 401;
r = ring_create();
for(i = 0; i < erg; ++i)
ring_append(r, a + i);
res = ring_remove_selected(r, dunknow, NULL);
if( ring_size( r ) + ring_size( res ) != (uint32_t)erg )
{
perr( "T5: Case 10 failed" ); return;
}
char * msg1 = ring_invariant( r );
char * msg2 = ring_invariant( res );
if( msg1 )
perr( "%s", msg1 );
if( msg2 )
perr( "%s", msg2 );
ring_destroy( res, NULL);
ring_destroy( r, NULL);
}
pinfo( "T5: ring_remove_selected success");
}
void t_06( void )
{
int i = 0;
int * p = NULL;
Ring res = NULL;
Ring r1 = NULL;
Ring r2 = NULL;
// FOR ERROR PRINT USE
// ring_print(stderr, r, pfunc);
int x = 11;
int y = 44;
int z = 66;
r1 = ring_create( );
r2 = ring_create( );
res = ring_concat( r1, r2 );
if( ring_size(res) != 0 )
{
perr( "T6: Case 01 failed" ); return;
}
ring_destroy( res, NULL);
// ---------
r1 = ring_create( );
r2 = ring_create( );
ring_append(r1, &x);
ring_append(r2, &y);
res = ring_concat( r1, r2 );
if( ring_size(res) != 2 || ring_first(res) != &x
|| ring_last(res) != &y)
{
perr( "T6: Case 02 failed" ); return;
}
ring_destroy( res, NULL);
// ---------
r1 = ring_create( );
r2 = ring_create( );
for(i = 1; i < 5; ++i)
ring_append(r1, a+i);
for(i = 5; i < 9; ++i)
ring_append(r2, a+i);
res = ring_concat(r1, r2);
if(ring_size(res) != 8)
{
perr( "T6: Case 03 failed" ); return;
}
for(i = 1; i < 9; ++i)
{
if(*((int*)ring_pop(res)) != i)
{
perr( "T6: Case 04 failed" ); return;
}
}
ring_destroy( res, NULL);
pinfo( "T6: ring_concat success");
}
void t_07(void)
{
int i = 0;
Ring r1 = ring_create();
ring_append(r1, a+10);
ring_append(r1, a+11);
ring_append(r1, a+12);
ring_extract(r1, 1);
if(ring_size(r1) != 2)
{
perr( "T7: Extract Size fail"); return;
}
ring_extract(r1, 1);
if(ring_size(r1) != 1)
{
perr( "T7: Extract Size fail"); return;
}
ring_append(r1, a+11);
ring_append(r1, a+12);
ring_append(r1, a+13);
ring_append(r1, a+14);
if(*(int*)ring_extract(r1, 2) != 12)
{
perr( "T7: extract (1) Contend fail"); return;
}
if(*(int*)ring_extract(r1, 3) != 14)
{
perr( "T7: extract (2) Contend fail"); return;
}
if(*(int*)ring_extract(r1, 1) != 11)
{
perr( "T7: extract (3) Contend fail"); return;
}
if(*(int*)ring_extract(r1, 0) != 10)
{
perr( "T7: extract (4) Contend fail"); return;
}
if(ring_size(r1) != 1)
{
perr( "T7: extract (5) Size fail"); return;
}
ring_destroy(r1, NULL);
pinfo( "T7: ring_extract success");
}
void t_08(void)
{
int i = 0;
Ring r1 = ring_create();
ring_insert_at(r1, a+10, 0);
ring_insert_at(r1, a+12, 1);
ring_insert_at(r1, a+11, 1);
if(ring_size(r1) != 3)
{
perr( "T08: insert_at Size fail"); return;
}
for(i = 10; i < 13; ++i)
{
if(*(int*)ring_pop(r1) != i)
{
perr( "T08: insert_at contend fail"); return;
}
}
if(!ring_is_empty(r1))
{
perr( "T08: insert_at (pop) is_empty fail"); return;
}
ring_insert_at(r1, a+10, 0);
ring_insert_at(r1, a+11, 1);
ring_insert_at(r1, a+14, 2);
ring_insert_at(r1, a+12, 2);
ring_insert_at(r1, a+13, 3);
for(i = 10; i < 15; ++i)
{
if(*(int*)ring_pop(r1) != i)
{
perr( "T08: insert_at contend fail"); return;
}
}
ring_destroy(r1, NULL);
pinfo( "T8: ring_insert_at success");
}
void t_09(void)
{
Ring r1 = ring_create();
if ( ring_at(r1, 0) != NULL
|| ring_at(r1, 1) != NULL
|| ring_at(r1, 2) != NULL)
{
perr( "T09: at empty fail"); return;
}
ring_append(r1, a+1);
ring_append(r1, a+2);
ring_append(r1, a+3);
if ( ring_at(r1, 0) != a+1
|| ring_at(r1, 1) != a+2
|| ring_at(r1, 2) != a+3)
{
perr( "T09: at fail"); return;
}
ring_destroy(r1, NULL);
pinfo( "T9: ring_at success");
}
void t_0A( void )
{
Ring r = ring_create();
for( uint32_t i = 0; i < 64; ++i )
{
ring_append( r, a+i );
}
Ring multi = ring_distribute(r, 4);
if ( ring_size(multi) != 4 )
{
perr( "T0A: multi size fail"); return;
}
Ring m1 = ring_pop(multi);
Ring m2 = ring_pop(multi);
Ring m3 = ring_pop(multi);
Ring m4 = ring_pop(multi);
ring_destroy(multi, NULL);
if ( ring_size(m1) + ring_size(m2) + ring_size(m3) + ring_size(m4) != 64 )
{
perr( "T0A: distribute fail"); return;
}
for( uint32_t i = 0; i < 64; ++i )
{
int * tmp = NULL;
int c = i % 4;
switch( c )
{
break; case 0:
tmp = ring_pop( m1 );
break; case 1:
tmp = ring_pop( m2 );
break; case 2:
tmp = ring_pop( m3 );
break; case 3:
tmp = ring_pop( m4 );
}
if( *tmp != (int32_t)i )
{
perr( "T0A: distribute contend fail, case: %d", i); return;
}
}
// ring_print(stderr, m1, pfunc);
// ring_print(stderr, m2, pfunc);
// ring_print(stderr, m3, pfunc);
// ring_print(stderr, m4, pfunc);
ring_destroy(r, NULL);
ring_destroy(m1, NULL);
ring_destroy(m2, NULL);
ring_destroy(m3, NULL);
ring_destroy(m4, NULL);
pinfo( "TA: ring_distribute success");
}
int main( void )
{
srand( time( NULL ) );
// just in case
for ( int i = 0; i < TEST_FUNC_ARRAY_SIZE ; ++i )
tests[i] = NULL;
for ( int i = 0; i < TEST_ARRAY_SIZE ; ++i )
a[i] = i;
// 0 reserved
tests[1] = t_01;
tests[2] = t_02;
tests[3] = t_03;
tests[4] = t_04;
tests[5] = t_05;
tests[6] = t_06;
tests[7] = t_07;
tests[8] = t_08;
tests[9] = t_09;
tests[10] = t_0A;
for ( int i = 0; i < TEST_FUNC_ARRAY_SIZE ; ++i )
if( tests[i] )
( *tests[i] )( );
return 0;
}