-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathringbuffer-cunit.c
More file actions
536 lines (408 loc) · 14.7 KB
/
ringbuffer-cunit.c
File metadata and controls
536 lines (408 loc) · 14.7 KB
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
/*
* Simple example of a CUnit unit test.
*
* This program (crudely) demonstrates a very simple "black box"
* test of the standard library functions fprintf() and fread().
* It uses suite initialization and cleanup functions to open
* and close a common temporary file used by the test functions.
* The test functions then write to and read from the temporary
* file in the course of testing the library functions.
*
* The 2 test functions are added to a single CUnit suite, and
* then run using the CUnit Basic interface. The output of the
* program (on CUnit version 2.0-2) is:
*
* CUnit : A Unit testing framework for C.
* http://cunit.sourceforge.net/
*
* Suite: Suite_1
* Test: test of fprintf() ... passed
* Test: test of fread() ... passed
*
* --Run Summary: Type Total Ran Passed Failed
* suites 1 1 n/a 0
* tests 2 2 2 0
* asserts 5 5 5 0
*/
// Update for new and improved ringbuffer tests.
// The semantics have changed since the original design.
// Now, for starters, wrap is not supported. The routines will
// not overflow data thats already in there. The other internal
// change is that rather than two modulo pointers, we have 32-bit
// indices that get masked down, so that you can always compare
// the read and write indices.
// Secondly, there is support for bulk operations. Those
// are useful for aggregating small writes into network buffers.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <time.h>
#include "ringbuffer.h"
#include "CUnit/Basic.h"
#define BUFFERSIZE 16384
#define RINGSIZE 16
#define FULLSIZE (RINGSIZE)
// This is twice as long so we can wrap off the end.
const uint8_t patternchars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
#define PATTERNLEN 32
uint8_t bufcontents[RINGSIZE];
uint8_t* source;
uint8_t* dest;
RINGBUF ring;
// --------------------------------------------------
// Utility Functions
// --------------------------------------------------
// A Dump that assumes printable data.
void dump_ring() {
for ( int i = 0; i < RINGSIZE; i++) printf("%c", bufcontents[i]);
printf(" ");
}
uint8_t* makerandbuffer() {
uint8_t* buf;
int i;
buf = malloc(BUFFERSIZE * sizeof(uint8_t));
for (i = 0; i < BUFFERSIZE; i++) {
buf[i] = patternchars[random() & 0x1f];
}
return(buf);
}
// Drain a buffer and tell us how many operations it took.
int drain() {
int count = 0;
int ret;
do {
count++;
ret = ringbuffer_getchar(&ring);
}
while ( count < 2 * RINGSIZE && ret != -1 );
return(count);
}
// Add a random number of characters, and then
// pull them all out. Make sure that things match.
void doPush(int count) {
CU_ASSERT( ringbuffer_free(&ring) == FULLSIZE);
for ( int i = 0; i < count; i++) {
ringbuffer_addchar(&ring, patternchars[i]);
}
// printf("Pre = %d, Post=%d\n", pre_free,ringbuffer_free(&ring));
CU_ASSERT( ringbuffer_free(&ring) == (FULLSIZE - count));
CU_ASSERT( ringbuffer_used(&ring) == count);
// Now pull them out and make sure that they match.
for ( int i = 0; i < count; i++) {
int c_out = ringbuffer_getchar(&ring);
CU_ASSERT( c_out = patternchars[i]);
}
}
// Add a random number of characters, and then
// pull them all out with bulk remove functions.
// Make sure that things match.
void doPushBulkRemove(int count) {
CU_ASSERT( ringbuffer_free(&ring) == FULLSIZE);
for ( int i = 0; i < count; i++) {
ringbuffer_addchar(&ring, patternchars[i]);
}
CU_ASSERT( ringbuffer_free(&ring) == (FULLSIZE - count));
CU_ASSERT( ringbuffer_used(&ring) == count);
uint8_t const *checkchars = patternchars;
// This will take up to two passes.
for (int i = 0; i < 2; i++ ) {
int before = ringbuffer_used(&ring);
int howmuch = ringbuffer_getbulkcount(&ring);
if ( howmuch == 0 ) break;
printf("bulkc=%02d ", howmuch);
dump_ring();
CU_ASSERT( howmuch <= FULLSIZE );
uint8_t *start = ringbuffer_getbulkpointer(&ring);
// It had better match!
int ret = memcmp(start, checkchars, howmuch);
checkchars += howmuch; // Sliding window...
memset(start, '*', howmuch); // Scrub for debug.
CU_ASSERT( ret == 0);
if ( ret != 0 ) {
printf("Compare Fail!");
return;
}
ringbuffer_bulkremove(&ring, howmuch);
CU_ASSERT( ringbuffer_used(&ring) == ( before - howmuch ) );
}
CU_ASSERT( ring.iWrite == ring.iRead );
CU_ASSERT( ring.Dropped == 0);
}
// Find a random length of the right size > 0 && <= max
static int next_rand(int max) {
int val;
do {
val = random() & (RINGSIZE - 1);
}
while ( (val == 0 ) || ( val > max ) );
return(val);
}
// ---------------------------------------------------
// ---------------------------------------------------
/* The suite initialization function.
* Returns zero on success, non-zero otherwise.
*/
int init_suite1(void) {
source = makerandbuffer();
dest = malloc(BUFFERSIZE * sizeof(uint8_t));
ringbuffer_init(&ring, bufcontents, RINGSIZE);
return(0);
}
/* The suite cleanup function.
* Closes the temporary file used by the tests.
* Returns zero on success, non-zero otherwise.
*/
int clean_suite1(void) {
return 0;
}
// ------------------------------------------------------
// ------------------------------------------------------
// Tests
// ------------------------------------------------------
// ------------------------------------------------------
// ----------------------------------------
// ----------------------------------------
void testNEW(void) {
CU_ASSERT(ringbuffer_free(&ring) == FULLSIZE );
CU_ASSERT(ringbuffer_used(&ring) == 0);
CU_ASSERT(ring.iWrite == 0 );
CU_ASSERT(ring.iRead == 0 );
CU_ASSERT( ring.Dropped == 0 );
}
// ----------------------------------------
// ----------------------------------------
// Push a lot of data into the ring buffer, and see of it comes
// back out. We should start with an empty buffer
void testSINGLES(void) {
int i;
uint8_t cin, cout;
// Push a lot of bytes through. Check lots of stuff
for (i = 0; i < 1000; i++) {
cin = source[i];
CU_ASSERT(ring.iWrite == ring.iRead );
// Make sure we return the right thing.
CU_ASSERT( ringbuffer_addchar(&ring, cin) == FULLSIZE - 1 )
CU_ASSERT( ring.iWrite > ring.iRead )
CU_ASSERT(ringbuffer_free(&ring) == FULLSIZE - 1);
// Nothing should be getting Dropped
CU_ASSERT( ring.Dropped == 0 );
CU_ASSERT(ringbuffer_used(&ring) == 1);
cout = ringbuffer_getchar(&ring);
CU_ASSERT(ring.iWrite == ring.iRead );
CU_ASSERT(ringbuffer_used(&ring) == 0);
CU_ASSERT(ringbuffer_free(&ring) == FULLSIZE);
CU_ASSERT(cin == cout);
}
}
// ----------------------------------------
// ----------------------------------------
void testUnderFlow(void) {
int i;
uint8_t cin;
int cout;
// Push a lot of bytes through. Check lots of stuff
for (i = 0; i < 10; i++) {
cin = source[i];
ringbuffer_addchar(&ring, cin);
}
CU_ASSERT( ring.iWrite > ring.iRead )
for (i = 0; i < 10; i++) {
cout = ringbuffer_getchar(&ring);
CU_ASSERT(cout == source[i]);
}
CU_ASSERT(ring.iWrite == ring.iRead );
for (i = 0; i < 10; i++) {
cout = ringbuffer_getchar(&ring);
CU_ASSERT(cout == -1);
}
}
// ----------------------------------------
// Overflow test. This one is sensitive
// to the nature of the buffer.
// We should be able to add RINGSIZE + characters
// and see exactly one get dropped.
// ----------------------------------------
void testOverflow() {
int count;
int ret = 0;
int dropped_before = ring.Dropped;
// Drain the buffer, make sure it takes a reasonable number of characters.
count = drain();
CU_ASSERT(count <= RINGSIZE);
ret = ringbuffer_free(&ring);
CU_ASSERT(ret == RINGSIZE);
// Now fill it up, and verify that we can insert the correct number
// of characters.
for (int i = 0; i < RINGSIZE; i++ ) {
uint8_t cin = i & 0xFF;
ret = ringbuffer_addchar(&ring, cin);
}
// The return code should be -1 - its full.
CU_ASSERT(ret == 0);
// Add one more.
ret = ringbuffer_addchar(&ring, 0xAA);
CU_ASSERT(ret == -1);
// Make sure that it got recorded as dropped.
CU_ASSERT(ring.Dropped = (dropped_before + 1) );
ring.Dropped = 0; // Put it back!
// Clean up.
drain();
CU_ASSERT(ringbuffer_free(&ring) == FULLSIZE);
}
// ----------------------------------------
// ----------------------------------------
// Push a random number of characters and
// pull them out.
void testRandAdd() {
printf("Chars ");
srandom(0); // Start with a known seed value.
drain(); // Known state
// Fill it with known data.
for (int i = 0; i < RINGSIZE; i++) bufcontents[i] = '_';
printf("before: iWrite=%02d, iRead=%02d ", ring.iWrite, ring.iRead);
for (int i = 0; i < 50; i++) {
int max = ringbuffer_free(&ring);
int size = next_rand(max);
// printf("%02d/%02d ",size,max);
// dump_ring();
CU_ASSERT( max > 0 );
doPush(size);
CU_ASSERT( ring.iWrite == ring.iRead );
}
}
void testRandAddBulkRemove() {
printf("Bulk Remove");
printf("\n before: iWrite=%02d, iRead=%02d\n", ring.iWrite, ring.iRead);
// Do it again, with the bulk remove operator.
srandom(0); // Start with a known seed value.
drain(); // Known state
CU_ASSERT( ringbuffer_used(&ring) == 0 );
for (int i = 0; i < RINGSIZE; i++) bufcontents[i] = '_';
for (int i = 0; i < 50; i++) {
int max = ringbuffer_free(&ring);
int size = next_rand(max);
printf("%03d %02d/%02d ", i, size, max);
dump_ring();
CU_ASSERT( max > 0 );
doPushBulkRemove(size);
CU_ASSERT( ring.iWrite == ring.iRead );
printf("\n");
}
fflush(stdout);
}
// ----------------------------------------
// ----------------------------------------
// Heres the full cycle.
// Pre-load a random number of characters
// Init the counters.
// Repeat N times:
// fill the buffer all the way up.
// take a maximal bite and check the results.
static void PrConOp(int offset, int loops, int drainmax) {
// Pre-load, and mark empty.
ring.iRead += offset;
ring.iWrite = ring.iRead;
printf("Pre-load %d ", offset);
dump_ring();
printf("\n");
// Keep an index so that we can do our checks.
int wr_index = 0;
int rd_index = 0;
for ( int i = 0; i < loops; i++ ) {
// Fill it up.
int count = 0;
while ( ringbuffer_free(&ring) ) {
ringbuffer_addchar(&ring, patternchars[wr_index & (PATTERNLEN - 1)]);
wr_index++;
count++;
}
printf("Pre-filled %d ", count);
dump_ring();
// Now for the main loop that tests the aggregation.
// if we alternate between filling and bulk removal, it should
// result in full - sized operations.
int drainpasses = drainmax;
while (drainpasses && ringbuffer_getbulkcount(&ring) ) {
uint8_t *start = ringbuffer_getbulkpointer(&ring);
int howmuch = ringbuffer_getbulkcount(&ring);
const uint8_t *ref = &patternchars[rd_index & (PATTERNLEN - 1)];
int ret = memcmp(start, ref, howmuch);
CU_ASSERT(ret == 0);
if ( ret != 0 )
return;
ringbuffer_bulkremove(&ring, howmuch);
rd_index += howmuch;
printf(" Removed %d", howmuch);
drainpasses--;
}
// This test only works for full drain scenarios.
if ( drainmax > 1 ) {
CU_ASSERT(rd_index == wr_index);
}
printf("\n");
}
}
// --------------------------------------------------
// Producer/Consumer Test.
// Add a random number of characters to the RB,
// then fill it all the way up.
// Drain it using bulk operations.
// Use two indices into the pattern space to verify that
// the data is being handled correctly.
void testProducerConsumer1() {
printf("Produce/Consume Fill/Drain\n");
srandom(0); // Start with a known seed value.
// Random # of characters between 0-4
for ( int i = 0; i < 5; i++ ) {
int preload = rand() & 0x3;
PrConOp(preload, 16, 5);
}
}
void testProducerConsumer2() {
printf("Produce/Consume Fill/Partial Drain\n");
srandom(0); // Start with a known seed value.
// Random # of characters between 0-4
for ( int i = 0; i < 5; i++ ) {
int preload = rand() & 0x3;
PrConOp(preload, 4, 1);
}
}
/* The main() function for setting up and running the tests.
* Returns a CUE_SUCCESS on successful running, another
* CUnit error code on failure.
*/
int main() {
int rcode;
CU_pSuite pSuite = NULL;
srandom(0);
/* initialize the CUnit test registry */
if (CUE_SUCCESS != CU_initialize_registry())
return CU_get_error();
/* add a suite to the registry */
pSuite = CU_add_suite("Suite_1", init_suite1, clean_suite1);
if (NULL == pSuite) {
CU_cleanup_registry();
return CU_get_error();
}
/* add the tests to the suite */
/* NOTE - ORDER IS IMPORTANT - MUST TEST fread() AFTER fprintf() */
rcode = (NULL == CU_add_test(pSuite, "Test of fresh structure", testNEW)) ||
(NULL == CU_add_test(pSuite, "push/get 1000 chars", testSINGLES)) ||
(NULL == CU_add_test(pSuite, "underflow ", testUnderFlow)) ||
(NULL == CU_add_test(pSuite, "Overflow test", testOverflow)) ||
(NULL == CU_add_test(pSuite, "Random Length Adds", testRandAdd)) ||
(NULL == CU_add_test(pSuite, "Random Length Adds", testRandAddBulkRemove)) ||
(NULL == CU_add_test(pSuite, "Producer/Consumer test 1", testProducerConsumer1)) ||
(NULL == CU_add_test(pSuite, "Producer/Consumer test 2", testProducerConsumer2))
;
if ( rcode ) {
CU_cleanup_registry();
return CU_get_error();
}
/* Run all tests using the CUnit Basic interface */
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
CU_cleanup_registry();
return CU_get_error();
}