ASCII Serial Com
Serial communication library between computers, microcontrollers, FPGAs, etc. Uses only ASCII. Not the most efficient protocol, but meant to be easy to read
Loading...
Searching...
No Matches
test_circular_buffer.c
1#include "asc_exception.h"
2#include "circular_buffer.h"
3#include "externals/unity.h"
4#include <fcntl.h>
5#include <stdio.h>
6#include <sys/stat.h>
7#include <sys/types.h>
8#include <unistd.h>
9
10CEXCEPTION_T e1;
11// CEXCEPTION_T e2;
12
13#define CAPACITY 8
14static uint8_t buf_mock[100];
15static size_t buf_mock_size = 0;
16
17void setUp(void) {
18 // set stuff up here
19 buf_mock_size = 0;
20}
21
22void tearDown(void) {
23 // clean stuff up here
24}
25
26void test_circular_buffer_init_uint8(void) {
28 uint8_t buf[CAPACITY];
29 Try {
30 circular_buffer_init_uint8(&cb, CAPACITY, (uint8_t *)(&buf));
31
32 TEST_ASSERT_TRUE(circular_buffer_is_empty_uint8(&cb));
33 TEST_ASSERT_FALSE(circular_buffer_is_full_uint8(&cb));
34 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
35 }
36 Catch(e1) {
37 printf("Uncaught exception: %u\n", e1);
38 TEST_FAIL_MESSAGE("Uncaught exception!");
39 }
40 Try {
41 circular_buffer_init_uint8(&cb, CAPACITY - 1, (uint8_t *)(&buf));
42 TEST_FAIL_MESSAGE("Should have triggered an exception!");
43 }
44 Catch(e1) {
45 TEST_ASSERT_EQUAL_MESSAGE(
46 ASC_ERROR_CB_BAD_CAPACITY, e1,
47 "Exception should be ASC_ERROR_CB_BAD_CAPACITY, it isn't!");
48 }
49 /* Not allowed with optimization on
50 Try {
51 circular_buffer_init_uint8(&cb, CAPACITY + 1, (uint8_t *)(&buf));
52 TEST_FAIL_MESSAGE("Should have triggered an exception!");
53 }
54 Catch(e1) {
55 TEST_ASSERT_EQUAL_MESSAGE(
56 ASC_ERROR_CB_BAD_CAPACITY, e1,
57 "Exception should be ASC_ERROR_CB_BAD_CAPACITY, it isn't!");
58 }
59 */
60}
61
62void test_circular_buffer_print_uint8(void) {
63 Try {
65 uint8_t buf[CAPACITY];
66
67 circular_buffer_init_uint8(&cb, CAPACITY, (uint8_t *)(&buf));
68
69 FILE *outfile = fopen("/dev/null", "a");
70 if (!outfile) {
71 perror("Couldn't open /dev/null");
72 TEST_FAIL_MESSAGE("Couldn't open output file /dev/null");
73 }
74
75 circular_buffer_print_uint8(&cb, outfile, 2);
76 }
77 Catch(e1) {
78 printf("Uncaught exception: %u\n", e1);
79 TEST_FAIL_MESSAGE("Uncaught exception!");
80 }
81}
82
83void test_circular_buffer_push_pop_back_uint8(void) {
84 Try {
86 uint8_t buf[CAPACITY];
87
88 circular_buffer_init_uint8(&cb, CAPACITY, (uint8_t *)(&buf));
89
91
92 TEST_ASSERT_FALSE(circular_buffer_is_empty_uint8(&cb));
93 TEST_ASSERT_FALSE(circular_buffer_is_full_uint8(&cb));
94 TEST_ASSERT_EQUAL(1, circular_buffer_get_size_uint8(&cb));
95
96 TEST_ASSERT_EQUAL_UINT8(8, circular_buffer_pop_back_uint8(&cb));
97
98 TEST_ASSERT_TRUE(circular_buffer_is_empty_uint8(&cb));
99 TEST_ASSERT_FALSE(circular_buffer_is_full_uint8(&cb));
100 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
101
102 for (uint8_t i = 0; i < 5; i++) {
104 }
105
106 TEST_ASSERT_FALSE(circular_buffer_is_empty_uint8(&cb));
107 TEST_ASSERT_FALSE(circular_buffer_is_full_uint8(&cb));
108 TEST_ASSERT_EQUAL(5, circular_buffer_get_size_uint8(&cb));
109
110 for (uint8_t i = 0; i < 5; i++) {
111 TEST_ASSERT_EQUAL_UINT8(4 - i, circular_buffer_pop_back_uint8(&cb));
112 TEST_ASSERT_EQUAL(4 - i, circular_buffer_get_size_uint8(&cb));
113 }
114
115 TEST_ASSERT_TRUE(circular_buffer_is_empty_uint8(&cb));
116 TEST_ASSERT_FALSE(circular_buffer_is_full_uint8(&cb));
117 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
118
119 for (uint8_t i = 0; i < 7; i++) {
121 }
122
123 TEST_ASSERT_FALSE(circular_buffer_is_empty_uint8(&cb));
124 TEST_ASSERT_FALSE(circular_buffer_is_full_uint8(&cb));
125 TEST_ASSERT_EQUAL(7, circular_buffer_get_size_uint8(&cb));
126
128
129 TEST_ASSERT_FALSE(circular_buffer_is_empty_uint8(&cb));
130 TEST_ASSERT_TRUE(circular_buffer_is_full_uint8(&cb));
131 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_get_size_uint8(&cb));
132
133 // printf("Should have 0 through 7:\n");
134 // circular_buffer_print_uint8(&cb,stdout,0);
135
136 for (uint8_t i = 0; i < 8; i++) {
137 TEST_ASSERT_EQUAL_UINT8(7 - i, circular_buffer_pop_back_uint8(&cb));
138 TEST_ASSERT_EQUAL(7 - i, circular_buffer_get_size_uint8(&cb));
139 TEST_ASSERT_FALSE(circular_buffer_is_full_uint8(&cb));
140 }
141
142 TEST_ASSERT_TRUE(circular_buffer_is_empty_uint8(&cb));
143 TEST_ASSERT_FALSE(circular_buffer_is_full_uint8(&cb));
144 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
145 }
146 Catch(e1) {
147 printf("Uncaught exception: %u\n", e1);
148 TEST_FAIL_MESSAGE("Uncaught exception!");
149 }
150}
151
152void test_circular_buffer_push_overfull_pop_back_uint8(void) {
153 Try {
155 uint8_t buf[CAPACITY];
156
157 circular_buffer_init_uint8(&cb, CAPACITY, (uint8_t *)(&buf));
158
159 for (uint8_t i = 0; i < 33; i++) {
161 }
162
163 TEST_ASSERT_FALSE(circular_buffer_is_empty_uint8(&cb));
164 TEST_ASSERT_TRUE(circular_buffer_is_full_uint8(&cb));
165 TEST_ASSERT_EQUAL(8, circular_buffer_get_size_uint8(&cb));
166
167 for (uint8_t i = 0; i < CAPACITY; i++) {
168 TEST_ASSERT_EQUAL_UINT8(32 - i, circular_buffer_pop_back_uint8(&cb));
169 TEST_ASSERT_EQUAL(CAPACITY - 1 - i, circular_buffer_get_size_uint8(&cb));
170 TEST_ASSERT_FALSE(circular_buffer_is_full_uint8(&cb));
171 }
172 TEST_ASSERT_TRUE(circular_buffer_is_empty_uint8(&cb));
173
174 for (uint8_t i = 0; i < 29; i++) {
176 }
177
178 TEST_ASSERT_FALSE(circular_buffer_is_empty_uint8(&cb));
179 TEST_ASSERT_TRUE(circular_buffer_is_full_uint8(&cb));
180 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_get_size_uint8(&cb));
181
182 for (uint8_t i = 0; i < 43; i++) {
184 }
185
186 TEST_ASSERT_FALSE(circular_buffer_is_empty_uint8(&cb));
187 TEST_ASSERT_TRUE(circular_buffer_is_full_uint8(&cb));
188 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_get_size_uint8(&cb));
189
190 for (uint8_t i = 0; i < CAPACITY; i++) {
191 TEST_ASSERT_EQUAL_UINT8(42 - i, circular_buffer_pop_back_uint8(&cb));
192 TEST_ASSERT_EQUAL(CAPACITY - 1 - i, circular_buffer_get_size_uint8(&cb));
193 TEST_ASSERT_FALSE(circular_buffer_is_full_uint8(&cb));
194 }
195 TEST_ASSERT_TRUE(circular_buffer_is_empty_uint8(&cb));
196 }
197 Catch(e1) {
198 printf("Uncaught exception: %u\n", e1);
199 TEST_FAIL_MESSAGE("Uncaught exception!");
200 }
201}
202
203void test_circular_buffer_push_pop_front_uint8(void) {
204 Try {
206 uint8_t buf[CAPACITY];
207
208 circular_buffer_init_uint8(&cb, CAPACITY, (uint8_t *)(&buf));
209
211
212 TEST_ASSERT_FALSE(circular_buffer_is_empty_uint8(&cb));
213 TEST_ASSERT_FALSE(circular_buffer_is_full_uint8(&cb));
214 TEST_ASSERT_EQUAL(1, circular_buffer_get_size_uint8(&cb));
215
216 TEST_ASSERT_EQUAL_UINT8(8, circular_buffer_pop_front_uint8(&cb));
217
218 TEST_ASSERT_TRUE(circular_buffer_is_empty_uint8(&cb));
219 TEST_ASSERT_FALSE(circular_buffer_is_full_uint8(&cb));
220 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
221
222 for (uint8_t i = 0; i < 5; i++) {
224 }
225
226 TEST_ASSERT_FALSE(circular_buffer_is_empty_uint8(&cb));
227 TEST_ASSERT_FALSE(circular_buffer_is_full_uint8(&cb));
228 TEST_ASSERT_EQUAL(5, circular_buffer_get_size_uint8(&cb));
229
230 for (uint8_t i = 0; i < 5; i++) {
231 TEST_ASSERT_EQUAL_UINT8(4 - i, circular_buffer_pop_front_uint8(&cb));
232 TEST_ASSERT_EQUAL(4 - i, circular_buffer_get_size_uint8(&cb));
233 }
234
235 TEST_ASSERT_TRUE(circular_buffer_is_empty_uint8(&cb));
236 TEST_ASSERT_FALSE(circular_buffer_is_full_uint8(&cb));
237 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
238
239 for (uint8_t i = 0; i < CAPACITY - 1; i++) {
241 }
242
243 TEST_ASSERT_FALSE(circular_buffer_is_empty_uint8(&cb));
244 TEST_ASSERT_FALSE(circular_buffer_is_full_uint8(&cb));
245 TEST_ASSERT_EQUAL(CAPACITY - 1, circular_buffer_get_size_uint8(&cb));
246
247 circular_buffer_push_front_uint8(&cb, CAPACITY - 1);
248
249 TEST_ASSERT_FALSE(circular_buffer_is_empty_uint8(&cb));
250 TEST_ASSERT_TRUE(circular_buffer_is_full_uint8(&cb));
251 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_get_size_uint8(&cb));
252
253 for (uint8_t i = 0; i < CAPACITY; i++) {
254 TEST_ASSERT_EQUAL_UINT8(CAPACITY - 1 - i,
256 TEST_ASSERT_EQUAL(CAPACITY - 1 - i, circular_buffer_get_size_uint8(&cb));
257 TEST_ASSERT_FALSE(circular_buffer_is_full_uint8(&cb));
258 }
259
260 TEST_ASSERT_TRUE(circular_buffer_is_empty_uint8(&cb));
261 TEST_ASSERT_FALSE(circular_buffer_is_full_uint8(&cb));
262 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
263 }
264 Catch(e1) {
265 printf("Uncaught exception: %u\n", e1);
266 TEST_FAIL_MESSAGE("Uncaught exception!");
267 }
268}
269
270void test_circular_buffer_push_overfull_pop_front_uint8(void) {
271 Try {
273 uint8_t buf[CAPACITY];
274
275 circular_buffer_init_uint8(&cb, CAPACITY, (uint8_t *)(&buf));
276
277 for (uint8_t i = 0; i < 33; i++) {
279 }
280
281 TEST_ASSERT_FALSE(circular_buffer_is_empty_uint8(&cb));
282 TEST_ASSERT_TRUE(circular_buffer_is_full_uint8(&cb));
283 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_get_size_uint8(&cb));
284
285 for (uint8_t i = 0; i < CAPACITY; i++) {
286 TEST_ASSERT_EQUAL_UINT8(32 - i, circular_buffer_pop_front_uint8(&cb));
287 TEST_ASSERT_EQUAL(CAPACITY - 1 - i, circular_buffer_get_size_uint8(&cb));
288 TEST_ASSERT_FALSE(circular_buffer_is_full_uint8(&cb));
289 }
290 TEST_ASSERT_TRUE(circular_buffer_is_empty_uint8(&cb));
291
292 for (uint8_t i = 0; i < 29; i++) {
294 }
295
296 TEST_ASSERT_FALSE(circular_buffer_is_empty_uint8(&cb));
297 TEST_ASSERT_TRUE(circular_buffer_is_full_uint8(&cb));
298 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_get_size_uint8(&cb));
299
300 for (uint8_t i = 0; i < 43; i++) {
302 }
303
304 TEST_ASSERT_FALSE(circular_buffer_is_empty_uint8(&cb));
305 TEST_ASSERT_TRUE(circular_buffer_is_full_uint8(&cb));
306 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_get_size_uint8(&cb));
307
308 for (uint8_t i = 0; i < CAPACITY; i++) {
309 TEST_ASSERT_EQUAL_UINT8(42 - i, circular_buffer_pop_front_uint8(&cb));
310 TEST_ASSERT_EQUAL(CAPACITY - 1 - i, circular_buffer_get_size_uint8(&cb));
311 TEST_ASSERT_FALSE(circular_buffer_is_full_uint8(&cb));
312 }
313 TEST_ASSERT_TRUE(circular_buffer_is_empty_uint8(&cb));
314 }
315 Catch(e1) {
316 printf("Uncaught exception: %u\n", e1);
317 TEST_FAIL_MESSAGE("Uncaught exception!");
318 }
319}
320
321void test_circular_buffer_push_pop_front_back_uint8(void) {
322 Try {
324 uint8_t buf[CAPACITY];
325
326 circular_buffer_init_uint8(&cb, CAPACITY, (uint8_t *)(&buf));
327
328 for (uint8_t i = 0; i < 5; i++) {
330 }
331
332 for (uint8_t i = 0; i < 2; i++) {
333 TEST_ASSERT_EQUAL_UINT8(4 - i, circular_buffer_pop_front_uint8(&cb));
334 TEST_ASSERT_EQUAL(4 - i, circular_buffer_get_size_uint8(&cb));
335 }
336
340 TEST_ASSERT_EQUAL(6, circular_buffer_get_size_uint8(&cb));
341
342 TEST_ASSERT_EQUAL_UINT8(255, circular_buffer_pop_back_uint8(&cb));
343 TEST_ASSERT_EQUAL_UINT8(222, circular_buffer_pop_back_uint8(&cb));
344 TEST_ASSERT_EQUAL_UINT8(111, circular_buffer_pop_back_uint8(&cb));
345 TEST_ASSERT_EQUAL_UINT8(0, circular_buffer_pop_back_uint8(&cb));
346 TEST_ASSERT_EQUAL_UINT8(1, circular_buffer_pop_back_uint8(&cb));
347 TEST_ASSERT_EQUAL(1, circular_buffer_get_size_uint8(&cb));
348 TEST_ASSERT_EQUAL_UINT8(2, circular_buffer_pop_back_uint8(&cb));
349 TEST_ASSERT_TRUE(circular_buffer_is_empty_uint8(&cb));
350 }
351 Catch(e1) {
352 printf("Uncaught exception: %u\n", e1);
353 TEST_FAIL_MESSAGE("Uncaught exception!");
354 }
355}
356
357void test_circular_buffer_get_element_uint8(void) {
358 Try {
360 uint8_t buf[CAPACITY];
361
362 circular_buffer_init_uint8(&cb, CAPACITY, (uint8_t *)(&buf));
363
364 for (uint8_t i = 0; i < 5; i++) {
366 }
367
368 for (uint8_t i = 0; i < 5; i++) {
369 TEST_ASSERT_EQUAL_UINT8(4 - i, circular_buffer_get_element_uint8(&cb, i));
370 }
371
372 // TEST_ASSERT_EQUAL_UINT8(0,
373 // circular_buffer_get_element_uint8(&cb,7)); // to test
374 // exception handling
375 }
376 Catch(e1) {
377 printf("Uncaught exception: %u\n", e1);
378 TEST_FAIL_MESSAGE("Uncaught exception!");
379 }
380}
381
382void test_circular_buffer_remove_front_to_uint8(void) {
383 Try {
385 uint8_t buf[CAPACITY];
386
387 circular_buffer_init_uint8(&cb, CAPACITY, (uint8_t *)(&buf));
388
389 for (uint8_t i = 0; i < 8; i++) {
391 }
392
394 TEST_ASSERT_EQUAL(3, circular_buffer_get_size_uint8(&cb));
395
396 for (uint8_t i = 0; i < 3; i++) {
398 }
399
401 TEST_ASSERT_EQUAL(5, circular_buffer_get_size_uint8(&cb));
402
403 for (uint8_t i = 6; i < 8; i++) {
404 TEST_ASSERT_EQUAL_UINT8(i, circular_buffer_pop_front_uint8(&cb));
405 }
406
407 for (uint8_t i = 0; i < 3; i++) {
408 TEST_ASSERT_EQUAL_UINT8(55, circular_buffer_pop_front_uint8(&cb));
409 }
410 TEST_ASSERT_TRUE(circular_buffer_is_empty_uint8(&cb));
412 TEST_ASSERT_TRUE(circular_buffer_is_empty_uint8(&cb));
413
414 for (uint8_t i = 0; i < 20; i++) {
416 }
418 TEST_ASSERT_TRUE(circular_buffer_is_empty_uint8(&cb));
419
421 TEST_ASSERT_TRUE(circular_buffer_is_empty_uint8(&cb));
422 }
423 Catch(e1) {
424 printf("Uncaught exception: %u\n", e1);
425 TEST_FAIL_MESSAGE("Uncaught exception!");
426 }
427}
428
429void test_circular_buffer_remove_back_to_uint8(void) {
430 Try {
431
433 uint8_t buf[CAPACITY];
434
435 circular_buffer_init_uint8(&cb, CAPACITY, (uint8_t *)(&buf));
436
437 for (uint8_t i = 0; i < 8; i++) {
439 }
440
442 TEST_ASSERT_EQUAL(4, circular_buffer_get_size_uint8(&cb));
443
444 for (uint8_t i = 0; i < 3; i++) {
446 }
447
449 TEST_ASSERT_EQUAL(3, circular_buffer_get_size_uint8(&cb));
450
451 for (uint8_t i = 0; i < 3; i++) {
452 TEST_ASSERT_EQUAL_UINT8(i, circular_buffer_pop_front_uint8(&cb));
453 }
454
455 TEST_ASSERT_TRUE(circular_buffer_is_empty_uint8(&cb));
457 TEST_ASSERT_TRUE(circular_buffer_is_empty_uint8(&cb));
458
459 for (uint8_t i = 0; i < 20; i++) {
461 }
463 TEST_ASSERT_TRUE(circular_buffer_is_empty_uint8(&cb));
464
466 TEST_ASSERT_TRUE(circular_buffer_is_empty_uint8(&cb));
467 }
468 Catch(e1) {
469 printf("Uncaught exception: %u\n", e1);
470 TEST_FAIL_MESSAGE("Uncaught exception!");
471 }
472}
473
474void test_circular_buffer_find_first_uint8(void) {
475 Try {
476
478 uint8_t buf[CAPACITY];
479
480 circular_buffer_init_uint8(&cb, CAPACITY, (uint8_t *)(&buf));
481
482 TEST_ASSERT_TRUE_MESSAGE(circular_buffer_find_first_uint8(&cb, 3) >=
484 "find_first should be >= size but isn't");
485
486 for (uint8_t i = 0; i < 5; i++) {
488 }
489 TEST_ASSERT_TRUE_MESSAGE(circular_buffer_find_first_uint8(&cb, 8) >=
491 "find_first should be >= size but isn't");
492 TEST_ASSERT_EQUAL(3, circular_buffer_find_first_uint8(&cb, 3));
493
494 for (uint8_t i = 0; i < 2; i++) {
496 }
497 TEST_ASSERT_EQUAL(5, circular_buffer_find_first_uint8(&cb, 3));
498
499 for (uint8_t i = 0; i < 50; i++) {
501 }
502 TEST_ASSERT_EQUAL(CAPACITY - 5, circular_buffer_find_first_uint8(&cb, 45));
503
504 for (uint8_t i = 0; i < CAPACITY; i++) {
506 }
507 TEST_ASSERT_EQUAL(3, circular_buffer_find_first_uint8(&cb, 3));
509 TEST_ASSERT_EQUAL(2, circular_buffer_find_first_uint8(&cb, 3));
511 TEST_ASSERT_EQUAL(0, circular_buffer_find_first_uint8(&cb, 3));
512 }
513 Catch(e1) {
514 printf("Uncaught exception: %u\n", e1);
515 TEST_FAIL_MESSAGE("Uncaught exception!");
516 }
517}
518
519void test_circular_buffer_find_last_uint8(void) {
520 Try {
521
523 uint8_t buf[CAPACITY];
524
525 circular_buffer_init_uint8(&cb, CAPACITY, (uint8_t *)(&buf));
526
527 TEST_ASSERT_TRUE_MESSAGE(circular_buffer_find_last_uint8(&cb, 3) >=
529 "find_last should be >= size but isn't");
530
531 for (uint8_t i = 0; i < 5; i++) {
533 }
534 TEST_ASSERT_TRUE_MESSAGE(circular_buffer_find_last_uint8(&cb, 8) >=
536 "find_last should be >= size but isn't");
537 TEST_ASSERT_EQUAL(3, circular_buffer_find_last_uint8(&cb, 3));
538
539 for (uint8_t i = 0; i < 2; i++) {
541 }
542 TEST_ASSERT_EQUAL(5, circular_buffer_find_last_uint8(&cb, 3));
543
544 for (uint8_t i = 0; i < 50; i++) {
546 }
547 TEST_ASSERT_EQUAL(CAPACITY - 5, circular_buffer_find_last_uint8(&cb, 45));
548
549 for (uint8_t i = 0; i < CAPACITY; i++) {
551 }
552 TEST_ASSERT_EQUAL(3, circular_buffer_find_last_uint8(&cb, 3));
554 TEST_ASSERT_EQUAL(4, circular_buffer_find_last_uint8(&cb, 3));
556 TEST_ASSERT_EQUAL(CAPACITY - 1, circular_buffer_find_last_uint8(&cb, 3));
557 }
558 Catch(e1) {
559 printf("Uncaught exception: %u\n", e1);
560 TEST_FAIL_MESSAGE("Uncaught exception!");
561 }
562}
563
564void test_circular_buffer_count_uint8(void) {
565 Try {
566
568 uint8_t buf[CAPACITY];
569
570 circular_buffer_init_uint8(&cb, CAPACITY, (uint8_t *)(&buf));
571
572 TEST_ASSERT_EQUAL(0, circular_buffer_count_uint8(&cb, 0));
573 TEST_ASSERT_EQUAL(0, circular_buffer_count_uint8(&cb, 5));
574
575 for (uint8_t i = 0; i < CAPACITY; i++) {
577 }
578 TEST_ASSERT_EQUAL(0, circular_buffer_count_uint8(&cb, 8));
579 TEST_ASSERT_EQUAL(1, circular_buffer_count_uint8(&cb, 7));
580 TEST_ASSERT_EQUAL(1, circular_buffer_count_uint8(&cb, 4));
581 TEST_ASSERT_EQUAL(1, circular_buffer_count_uint8(&cb, 0));
583 TEST_ASSERT_EQUAL(1, circular_buffer_count_uint8(&cb, 0));
585 TEST_ASSERT_EQUAL(2, circular_buffer_count_uint8(&cb, 5));
586 for (uint8_t i = 0; i < 50; i++) {
588 }
589 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_count_uint8(&cb, 255));
590 for (uint8_t i = 0; i < CAPACITY; i++) {
592 }
593 TEST_ASSERT_EQUAL(0, circular_buffer_count_uint8(&cb, 255));
594 }
595 Catch(e1) {
596 printf("Uncaught exception: %u\n", e1);
597 TEST_FAIL_MESSAGE("Uncaught exception!");
598 }
599}
600
601void test_circular_buffer_get_first_block_uint8(void) {
602 Try {
603
605 uint8_t buf[CAPACITY];
606
607 circular_buffer_init_uint8(&cb, CAPACITY, (uint8_t *)(&buf));
608 const uint8_t *block = NULL;
609 size_t blockSize = circular_buffer_get_first_block_uint8(&cb, &block);
610 TEST_ASSERT_EQUAL(0, blockSize);
611
612 for (uint8_t i = 0; i < 5; i++) {
614 }
615
616 blockSize = circular_buffer_get_first_block_uint8(&cb, &block);
617 TEST_ASSERT_EQUAL(5, blockSize);
618 TEST_ASSERT_EQUAL_MESSAGE(cb.buffer, block,
619 "Pointers not equal cb buffer and block!");
620 for (uint8_t i = 0; i < blockSize; i++) {
621 TEST_ASSERT_EQUAL_UINT8(i, block[i]);
622 }
623
624 for (uint8_t i = 0; i < 5; i++) {
626 }
627 TEST_ASSERT_EQUAL(5, cb.iStart);
628
629 blockSize = circular_buffer_get_first_block_uint8(&cb, &block);
630 TEST_ASSERT_EQUAL(0, blockSize);
631 TEST_ASSERT_EQUAL_MESSAGE(cb.buffer + 5, block,
632 "Pointers not equal cb buffer and block!");
633
634 for (uint8_t i = 0; i < 17; i++) {
636 }
637 blockSize = circular_buffer_get_first_block_uint8(&cb, &block);
638 TEST_ASSERT_EQUAL(2, blockSize);
639 for (uint8_t i = 0; i < blockSize; i++) {
640 TEST_ASSERT_EQUAL_UINT8(i + 9, block[i]);
641 }
642 }
643 Catch(e1) {
644 printf("Uncaught exception: %u\n", e1);
645 TEST_FAIL_MESSAGE("Uncaught exception!");
646 }
647}
648
649void test_circular_buffer_delete_first_block_uint8(void) {
650 Try {
651
653 uint8_t buf[CAPACITY];
654
655 circular_buffer_init_uint8(&cb, CAPACITY, (uint8_t *)(&buf));
656 size_t delSize = circular_buffer_delete_first_block_uint8(&cb);
657 TEST_ASSERT_EQUAL(0, delSize);
658
659 for (uint8_t i = 0; i < 5; i++) {
661 }
663 TEST_ASSERT_EQUAL(5, delSize);
664
665 for (uint8_t i = 0; i < 5; i++) {
668 }
669 for (uint8_t i = 0; i < 20; i++) {
671 }
673 TEST_ASSERT_EQUAL(7, delSize);
674 TEST_ASSERT_EQUAL(1, circular_buffer_get_size_uint8(&cb));
675 const uint8_t *block = NULL;
676 size_t blockSize = circular_buffer_get_first_block_uint8(&cb, &block);
677 TEST_ASSERT_EQUAL_MESSAGE(cb.buffer, block,
678 "Pointers not equal cb buffer and block!");
679 TEST_ASSERT_EQUAL(1, blockSize);
680 for (uint8_t i = 0; i < blockSize; i++) {
681 TEST_ASSERT_EQUAL_UINT8(i + 19, block[i]);
682 }
684 TEST_ASSERT_EQUAL(1, delSize);
685 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
686 }
687 Catch(e1) {
688 printf("Uncaught exception: %u\n", e1);
689 TEST_FAIL_MESSAGE("Uncaught exception!");
690 }
691}
692
693void test_circular_buffer_push_back_block_uint8(void) {
694 Try {
695
697 uint8_t buf[CAPACITY];
698 circular_buffer_init_uint8(&cb, CAPACITY, (uint8_t *)(&buf));
699
700 buf_mock_size = 3;
701 for (uint8_t i = 0; i < buf_mock_size; i++) {
702 buf_mock[i] = i;
703 }
704
705 circular_buffer_push_back_block_uint8(&cb, buf_mock, buf_mock_size);
706 TEST_ASSERT_EQUAL(3, circular_buffer_get_size_uint8(&cb));
707
708 buf_mock_size = 5;
709 for (uint8_t i = 0; i < buf_mock_size; i++) {
710 buf_mock[i] = i;
711 }
712 circular_buffer_push_back_block_uint8(&cb, buf_mock, buf_mock_size);
713 TEST_ASSERT_EQUAL(8, circular_buffer_get_size_uint8(&cb));
714
715 // already full!
716 buf_mock_size = 8;
717 for (uint8_t i = 0; i < buf_mock_size; i++) {
718 buf_mock[i] = i;
719 }
720 circular_buffer_push_back_block_uint8(&cb, buf_mock, buf_mock_size);
721 TEST_ASSERT_EQUAL(8, circular_buffer_get_size_uint8(&cb));
722
723 // test just one empty at back
725
726 buf_mock_size = 8;
727 for (uint8_t i = 0; i < buf_mock_size; i++) {
728 buf_mock[i] = i;
729 }
730 circular_buffer_push_back_block_uint8(&cb, buf_mock, buf_mock_size);
731 TEST_ASSERT_EQUAL(8, circular_buffer_get_size_uint8(&cb));
732
733 // test just one empty at front
735
736 buf_mock_size = 8;
737 for (uint8_t i = 0; i < buf_mock_size; i++) {
738 buf_mock[i] = i;
739 }
740 circular_buffer_push_back_block_uint8(&cb, buf_mock, buf_mock_size);
741 TEST_ASSERT_EQUAL(8, circular_buffer_get_size_uint8(&cb));
742
743 // Test start empty from middle
744 for (uint8_t i = 0; i < 4; i++) {
747 }
748 // circular_buffer_print_uint8(&cb);
749 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
750
751 buf_mock_size = 4;
752 for (uint8_t i = 0; i < buf_mock_size; i++) {
753 buf_mock[i] = i;
754 }
755 circular_buffer_push_back_block_uint8(&cb, buf_mock, buf_mock_size);
756 TEST_ASSERT_EQUAL(4, circular_buffer_get_size_uint8(&cb));
757 }
758 Catch(e1) {
759 printf("Uncaught exception: %u\n", e1);
760 TEST_FAIL_MESSAGE("Uncaught exception!");
761 }
762}
763
764void test_circular_buffer_pop_front_block_uint8(void) {
765 Try {
766
768 uint8_t buf[CAPACITY];
769 circular_buffer_init_uint8(&cb, CAPACITY, (uint8_t *)(&buf));
770
771 uint8_t outputBuffer[100];
772
773 for (uint8_t i = 0; i < 5; i++) {
775 }
776 TEST_ASSERT_EQUAL(5, circular_buffer_get_size_uint8(&cb));
777 size_t nPopped =
778 circular_buffer_pop_front_block_uint8(&cb, outputBuffer, 100);
779 // printf("outputBuffer: ");
780 // for (uint8_t i = 0; i < 5; i++) {
781 // printf("%hhu",outputBuffer[i]);
782 //}
783 // printf("\n");
784 TEST_ASSERT_EQUAL(5, nPopped);
785 for (uint8_t i = 0; i < 5; i++) {
786 TEST_ASSERT_EQUAL_UINT8(i, outputBuffer[i]);
787 }
788 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
789
790 for (uint8_t i = 0; i < 5; i++) {
792 }
793 TEST_ASSERT_EQUAL(5, circular_buffer_get_size_uint8(&cb));
794
795 nPopped = circular_buffer_pop_front_block_uint8(&cb, outputBuffer, 0);
796 TEST_ASSERT_EQUAL(0, nPopped);
797 TEST_ASSERT_EQUAL(5, circular_buffer_get_size_uint8(&cb));
798
799 nPopped = circular_buffer_pop_front_block_uint8(&cb, outputBuffer, 5);
800 TEST_ASSERT_EQUAL(5, nPopped);
801 for (uint8_t i = 0; i < 5; i++) {
802 TEST_ASSERT_EQUAL_UINT8(i, outputBuffer[i]);
803 }
804 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
805
806 nPopped = circular_buffer_pop_front_block_uint8(&cb, outputBuffer, 0);
807 TEST_ASSERT_EQUAL(0, nPopped);
808 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
809
810 for (uint8_t i = 0; i < 5; i++) {
812 }
813 TEST_ASSERT_EQUAL(5, circular_buffer_get_size_uint8(&cb));
814 nPopped = circular_buffer_pop_front_block_uint8(&cb, outputBuffer, 0);
815 TEST_ASSERT_EQUAL(0, nPopped);
816 TEST_ASSERT_EQUAL(5, circular_buffer_get_size_uint8(&cb));
817 nPopped = circular_buffer_pop_front_block_uint8(&cb, outputBuffer, 2);
818 TEST_ASSERT_EQUAL(2, nPopped);
819 for (uint8_t i = 0; i < 2; i++) {
820 TEST_ASSERT_EQUAL_UINT8(i, outputBuffer[i]);
821 }
822 TEST_ASSERT_EQUAL(3, circular_buffer_get_size_uint8(&cb));
823 nPopped = circular_buffer_pop_front_block_uint8(&cb, outputBuffer, 1);
824 TEST_ASSERT_EQUAL(1, nPopped);
825 TEST_ASSERT_EQUAL_UINT8(2, outputBuffer[0]);
826 TEST_ASSERT_EQUAL(2, circular_buffer_get_size_uint8(&cb));
827 nPopped = circular_buffer_pop_front_block_uint8(&cb, outputBuffer, 5);
828 TEST_ASSERT_EQUAL(2, nPopped);
829 for (uint8_t i = 0; i < 2; i++) {
830 TEST_ASSERT_EQUAL_UINT8(i + 3, outputBuffer[i]);
831 }
832 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
833 nPopped = circular_buffer_pop_front_block_uint8(&cb, outputBuffer, 5);
834 TEST_ASSERT_EQUAL(0, nPopped);
835 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
836
837 /////////////////////
838
839 for (uint8_t i = 0; i < CAPACITY; i++) {
841 }
842 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_get_size_uint8(&cb));
843 TEST_ASSERT_TRUE(circular_buffer_is_full_uint8(&cb));
844 nPopped = circular_buffer_pop_front_block_uint8(&cb, outputBuffer, 0);
845 TEST_ASSERT_EQUAL(0, nPopped);
846 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_get_size_uint8(&cb));
847 TEST_ASSERT_TRUE(circular_buffer_is_full_uint8(&cb));
848 nPopped = circular_buffer_pop_front_block_uint8(&cb, outputBuffer, 100);
849 TEST_ASSERT_EQUAL(CAPACITY, nPopped);
850 for (uint8_t i = 0; i < CAPACITY; i++) {
851 TEST_ASSERT_EQUAL_UINT8(i, outputBuffer[i]);
852 }
853 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
854
855 for (uint8_t i = 0; i < CAPACITY; i++) {
857 }
858 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_get_size_uint8(&cb));
859 TEST_ASSERT_TRUE(circular_buffer_is_full_uint8(&cb));
860 nPopped =
861 circular_buffer_pop_front_block_uint8(&cb, outputBuffer, CAPACITY);
862 TEST_ASSERT_EQUAL(CAPACITY, nPopped);
863 for (uint8_t i = 0; i < CAPACITY; i++) {
864 TEST_ASSERT_EQUAL_UINT8(i, outputBuffer[i]);
865 }
866 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
867
868 for (uint8_t i = 0; i < CAPACITY; i++) {
870 }
871 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_get_size_uint8(&cb));
872 TEST_ASSERT_TRUE(circular_buffer_is_full_uint8(&cb));
873 nPopped =
874 circular_buffer_pop_front_block_uint8(&cb, outputBuffer, CAPACITY - 1);
875 TEST_ASSERT_EQUAL(CAPACITY - 1, nPopped);
876 for (uint8_t i = 0; i < CAPACITY - 1; i++) {
877 TEST_ASSERT_EQUAL_UINT8(i, outputBuffer[i]);
878 }
879 TEST_ASSERT_EQUAL(1, circular_buffer_get_size_uint8(&cb));
880 nPopped =
881 circular_buffer_pop_front_block_uint8(&cb, outputBuffer, CAPACITY - 1);
882 TEST_ASSERT_EQUAL(1, nPopped);
883 TEST_ASSERT_EQUAL_UINT8(CAPACITY - 1, outputBuffer[0]);
884 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
885 nPopped =
886 circular_buffer_pop_front_block_uint8(&cb, outputBuffer, CAPACITY / 2);
887 TEST_ASSERT_EQUAL(0, nPopped);
888 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
889
890 ////////////////////////////////////////////
891 // test if buffer wraps around end of memory
892 for (uint8_t i = 0; i < CAPACITY + CAPACITY / 2; i++) {
894 }
895 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_get_size_uint8(&cb));
896 TEST_ASSERT_TRUE(circular_buffer_is_full_uint8(&cb));
897 nPopped = circular_buffer_pop_front_block_uint8(&cb, outputBuffer, 0);
898 TEST_ASSERT_EQUAL(0, nPopped);
899 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_get_size_uint8(&cb));
900 TEST_ASSERT_TRUE(circular_buffer_is_full_uint8(&cb));
901 nPopped = circular_buffer_pop_front_block_uint8(&cb, outputBuffer, 100);
902 TEST_ASSERT_EQUAL(CAPACITY / 2, nPopped);
903 for (uint8_t i = 0; i < CAPACITY / 2; i++) {
904 TEST_ASSERT_EQUAL_UINT8(i + CAPACITY / 2, outputBuffer[i]);
905 }
906 TEST_ASSERT_EQUAL(CAPACITY / 2, circular_buffer_get_size_uint8(&cb));
907 nPopped = circular_buffer_pop_front_block_uint8(&cb, outputBuffer, 100);
908 TEST_ASSERT_EQUAL(CAPACITY / 2, nPopped);
909 for (uint8_t i = 0; i < CAPACITY / 2; i++) {
910 TEST_ASSERT_EQUAL_UINT8(i + CAPACITY, outputBuffer[i]);
911 }
912 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
913
914 for (uint8_t i = 0; i < CAPACITY + CAPACITY / 2; i++) {
916 }
917 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_get_size_uint8(&cb));
918 TEST_ASSERT_TRUE(circular_buffer_is_full_uint8(&cb));
919 nPopped =
920 circular_buffer_pop_front_block_uint8(&cb, outputBuffer, CAPACITY);
921 TEST_ASSERT_EQUAL(CAPACITY / 2, nPopped);
922 for (uint8_t i = 0; i < CAPACITY / 2; i++) {
923 TEST_ASSERT_EQUAL_UINT8(i + CAPACITY / 2, outputBuffer[i]);
924 }
925 TEST_ASSERT_EQUAL(CAPACITY / 2, circular_buffer_get_size_uint8(&cb));
926 nPopped = circular_buffer_pop_front_block_uint8(&cb, outputBuffer, 100);
927 TEST_ASSERT_EQUAL(CAPACITY / 2, nPopped);
928 for (uint8_t i = 0; i < CAPACITY / 2; i++) {
929 TEST_ASSERT_EQUAL_UINT8(i + CAPACITY, outputBuffer[i]);
930 }
931 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
932
933 for (uint8_t i = 0; i < CAPACITY + CAPACITY / 2; i++) {
935 }
936 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_get_size_uint8(&cb));
937 TEST_ASSERT_TRUE(circular_buffer_is_full_uint8(&cb));
938 nPopped =
939 circular_buffer_pop_front_block_uint8(&cb, outputBuffer, CAPACITY - 1);
940 TEST_ASSERT_EQUAL(CAPACITY / 2, nPopped);
941 for (uint8_t i = 0; i < CAPACITY / 2; i++) {
942 TEST_ASSERT_EQUAL_UINT8(i + CAPACITY / 2, outputBuffer[i]);
943 }
944 TEST_ASSERT_EQUAL(CAPACITY / 2, circular_buffer_get_size_uint8(&cb));
945 nPopped = circular_buffer_pop_front_block_uint8(&cb, outputBuffer,
946 CAPACITY / 2 - 1);
947 TEST_ASSERT_EQUAL(CAPACITY / 2 - 1, nPopped);
948 for (uint8_t i = 0; i < CAPACITY / 2 - 1; i++) {
949 TEST_ASSERT_EQUAL_UINT8(i + CAPACITY, outputBuffer[i]);
950 }
951 TEST_ASSERT_EQUAL(1, circular_buffer_get_size_uint8(&cb));
952 nPopped =
953 circular_buffer_pop_front_block_uint8(&cb, outputBuffer, CAPACITY - 1);
954 TEST_ASSERT_EQUAL(1, nPopped);
955 TEST_ASSERT_EQUAL_UINT8(CAPACITY + CAPACITY / 2 - 1, outputBuffer[0]);
956 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
957 nPopped =
958 circular_buffer_pop_front_block_uint8(&cb, outputBuffer, CAPACITY / 2);
959 TEST_ASSERT_EQUAL(0, nPopped);
960 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
961 }
962 Catch(e1) {
963 printf("Uncaught exception: %u\n", e1);
964 TEST_FAIL_MESSAGE("Uncaught exception!");
965 }
966}
967
968void test_circular_buffer_push_back_from_fd_uint8(void) {
969 Try {
970
972 uint8_t buf[CAPACITY];
973 circular_buffer_init_uint8(&cb, CAPACITY, (uint8_t *)(&buf));
974
975 const int infd = open("/dev/zero", O_RDONLY);
976 if (infd == -1) {
977 perror("Couldn't open /dev/zero");
978 TEST_FAIL_MESSAGE("Couldn't open input "
979 "file /dev/zero");
980 }
981
982 size_t nRead = circular_buffer_push_back_from_fd_uint8(&cb, infd);
983 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_get_size_uint8(&cb));
984 TEST_ASSERT_EQUAL(CAPACITY, nRead);
985 for (uint8_t i = 0; i < CAPACITY; i++) {
986 TEST_ASSERT_EQUAL_UINT8(0, circular_buffer_pop_front_uint8(&cb));
987 }
988
989 for (uint8_t i = 0; i < CAPACITY / 2; i++) {
991 }
993 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_get_size_uint8(&cb));
994 TEST_ASSERT_EQUAL(CAPACITY / 2, nRead);
995 for (uint8_t i = 0; i < CAPACITY / 2; i++) {
996 TEST_ASSERT_EQUAL_UINT8(i, circular_buffer_pop_front_uint8(&cb));
997 }
998 for (uint8_t i = 0; i < CAPACITY / 2; i++) {
999 TEST_ASSERT_EQUAL_UINT8(0, circular_buffer_pop_front_uint8(&cb));
1000 }
1001
1002 for (uint8_t i = 0; i < CAPACITY + CAPACITY / 2; i++) {
1004 }
1005 nRead = circular_buffer_push_back_from_fd_uint8(&cb, infd);
1006 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_get_size_uint8(&cb));
1007 TEST_ASSERT_EQUAL(CAPACITY / 2, nRead);
1008 for (uint8_t i = 0; i < CAPACITY / 2; i++) {
1009 TEST_ASSERT_EQUAL_UINT8(i + CAPACITY,
1011 }
1012 for (uint8_t i = 0; i < CAPACITY / 2; i++) {
1013 TEST_ASSERT_EQUAL_UINT8(0, circular_buffer_pop_front_uint8(&cb));
1014 }
1015
1016 // setup so that iStart > iStop
1017 for (uint8_t i = 0; i < CAPACITY + 2; i++) {
1019 }
1020 for (uint8_t i = 0; i < 3; i++) {
1022 }
1023 TEST_ASSERT_EQUAL(CAPACITY - 3, circular_buffer_get_size_uint8(&cb));
1024 nRead = circular_buffer_push_back_from_fd_uint8(&cb, infd);
1025 TEST_ASSERT_EQUAL(3, nRead);
1026 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_get_size_uint8(&cb));
1027 for (uint8_t i = 0; i < CAPACITY - 3; i++) {
1028 TEST_ASSERT_EQUAL_UINT8(5 + i, circular_buffer_pop_front_uint8(&cb));
1029 }
1030 for (uint8_t i = 0; i < 3; i++) {
1031 TEST_ASSERT_EQUAL_UINT8(0, circular_buffer_pop_front_uint8(&cb));
1032 }
1033 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
1034 }
1035 Catch(e1) {
1036 printf("Uncaught exception: %u\n", e1);
1037 TEST_FAIL_MESSAGE("Uncaught exception!");
1038 }
1039}
1040
1041void test_circular_buffer_pop_front_to_fd_uint8(void) {
1042 Try {
1043
1045 uint8_t buf[CAPACITY];
1046 circular_buffer_init_uint8(&cb, CAPACITY, (uint8_t *)(&buf));
1047 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
1048
1049 const int outfd = open("/dev/null", O_WRONLY | O_APPEND);
1050 if (outfd == -1) {
1051 perror("Couldn't open /dev/null");
1052 TEST_FAIL_MESSAGE("Couldn't open output file "
1053 "/dev/null");
1054 }
1055
1056 size_t nWritten = circular_buffer_pop_front_to_fd_uint8(&cb, outfd);
1057 TEST_ASSERT_EQUAL(0, nWritten);
1058 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
1059
1060 for (uint8_t i = 0; i < 1; i++) {
1062 }
1063 TEST_ASSERT_EQUAL(1, circular_buffer_get_size_uint8(&cb));
1064 nWritten = circular_buffer_pop_front_to_fd_uint8(&cb, outfd);
1065 TEST_ASSERT_EQUAL(1, nWritten);
1066 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
1067
1068 for (uint8_t i = 0; i < CAPACITY; i++) {
1070 }
1071 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_get_size_uint8(&cb));
1072 nWritten = circular_buffer_pop_front_to_fd_uint8(&cb, outfd);
1073 TEST_ASSERT_EQUAL(CAPACITY, nWritten);
1074 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
1075
1076 for (uint8_t i = 0; i < CAPACITY + CAPACITY / 2; i++) {
1078 }
1079 TEST_ASSERT_EQUAL(CAPACITY, circular_buffer_get_size_uint8(&cb));
1080 nWritten = circular_buffer_pop_front_to_fd_uint8(&cb, outfd);
1081 TEST_ASSERT_EQUAL(CAPACITY / 2, nWritten);
1082 TEST_ASSERT_EQUAL(CAPACITY / 2, circular_buffer_get_size_uint8(&cb));
1083 for (uint8_t i = 0; i < CAPACITY / 2; i++) {
1084 TEST_ASSERT_EQUAL_UINT8(CAPACITY + i,
1086 }
1087 }
1088 Catch(e1) {
1089 printf("Uncaught exception: %u\n", e1);
1090 TEST_FAIL_MESSAGE("Uncaught exception!");
1091 }
1092}
1093
1094void test_circular_buffer_remove_front_unfinished_frames_uint8(void) {
1095 Try {
1097 uint8_t buf[CAPACITY];
1098 circular_buffer_init_uint8(&cb, CAPACITY, (uint8_t *)(&buf));
1099 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
1100
1101 const char *testString = "\nxxxxa";
1102 size_t resultSize = 0;
1105 TEST_ASSERT_EQUAL(resultSize, circular_buffer_get_size_uint8(&cb));
1106
1107 testString = "\nxxxxa>abcdefg";
1108 resultSize = 8;
1109 const char *resultString = ">abcdefg";
1112 TEST_ASSERT_EQUAL(resultSize, circular_buffer_get_size_uint8(&cb));
1113 for (size_t i = 0; i < resultSize; i++) {
1114 TEST_ASSERT_EQUAL_CHAR(resultString[i],
1116 }
1117 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
1118
1119 testString = "\nxa>abc\n";
1120 resultSize = 5;
1121 resultString = ">abc\n";
1124 TEST_ASSERT_EQUAL(resultSize, circular_buffer_get_size_uint8(&cb));
1125 for (size_t i = 0; i < resultSize; i++) {
1126 TEST_ASSERT_EQUAL_CHAR(resultString[i],
1128 }
1129 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
1130
1131 testString = "ab\nxa>abc\n";
1132 resultSize = 5;
1133 resultString = ">abc\n";
1136 TEST_ASSERT_EQUAL(resultSize, circular_buffer_get_size_uint8(&cb));
1137 for (size_t i = 0; i < resultSize; i++) {
1138 TEST_ASSERT_EQUAL_CHAR(resultString[i],
1140 }
1141 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
1142
1143 testString = "abcdefg";
1144 resultSize = 0;
1147 TEST_ASSERT_EQUAL(resultSize, circular_buffer_get_size_uint8(&cb));
1148 for (size_t i = 0; i < resultSize; i++) {
1149 TEST_ASSERT_EQUAL_CHAR(resultString[i],
1151 }
1152 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
1153
1154 testString = ">a>b>c>d";
1155 resultSize = 2;
1156 resultString = ">d";
1159 TEST_ASSERT_EQUAL(resultSize, circular_buffer_get_size_uint8(&cb));
1160 for (size_t i = 0; i < resultSize; i++) {
1161 TEST_ASSERT_EQUAL_CHAR(resultString[i],
1163 }
1164 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
1165
1166 testString = ">a>b>c>d";
1167 resultSize = 2;
1168 resultString = ">d";
1171 TEST_ASSERT_EQUAL(resultSize, circular_buffer_get_size_uint8(&cb));
1172 for (size_t i = 0; i < resultSize; i++) {
1173 TEST_ASSERT_EQUAL_CHAR(resultString[i],
1175 }
1176 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
1177
1178 testString = "c>d>abc\n";
1179 resultSize = 5;
1180 resultString = ">abc\n";
1183 TEST_ASSERT_EQUAL(resultSize, circular_buffer_get_size_uint8(&cb));
1184 for (size_t i = 0; i < resultSize; i++) {
1185 TEST_ASSERT_EQUAL_CHAR(resultString[i],
1187 }
1188 TEST_ASSERT_EQUAL(0, circular_buffer_get_size_uint8(&cb));
1189 }
1190 Catch(e1) {
1191 printf("Uncaught exception: %u\n", e1);
1192 TEST_FAIL_MESSAGE("Uncaught exception!");
1193 }
1194}
1195
1196int main(void) {
1197 UNITY_BEGIN();
1198 RUN_TEST(test_circular_buffer_init_uint8);
1199 RUN_TEST(test_circular_buffer_print_uint8);
1200 RUN_TEST(test_circular_buffer_push_pop_back_uint8);
1201 RUN_TEST(test_circular_buffer_push_overfull_pop_back_uint8);
1202 RUN_TEST(test_circular_buffer_push_pop_front_uint8);
1203 RUN_TEST(test_circular_buffer_push_overfull_pop_front_uint8);
1204 RUN_TEST(test_circular_buffer_push_pop_front_back_uint8);
1205 RUN_TEST(test_circular_buffer_get_element_uint8);
1206 RUN_TEST(test_circular_buffer_remove_front_to_uint8);
1207 RUN_TEST(test_circular_buffer_remove_back_to_uint8);
1208 RUN_TEST(test_circular_buffer_find_first_uint8);
1209 RUN_TEST(test_circular_buffer_find_last_uint8);
1210 RUN_TEST(test_circular_buffer_count_uint8);
1211 RUN_TEST(test_circular_buffer_get_first_block_uint8);
1212 RUN_TEST(test_circular_buffer_delete_first_block_uint8);
1213 RUN_TEST(test_circular_buffer_push_back_block_uint8);
1214 RUN_TEST(test_circular_buffer_pop_front_block_uint8);
1215 RUN_TEST(test_circular_buffer_push_back_from_fd_uint8);
1216 RUN_TEST(test_circular_buffer_pop_front_to_fd_uint8);
1217 RUN_TEST(test_circular_buffer_remove_front_unfinished_frames_uint8);
1218 return UNITY_END();
1219}
size_t circular_buffer_pop_front_to_fd_uint8(circular_buffer_uint8 *circ_buf, const int fd)
circular buffer pop front writing to file descriptor
uint8_t circular_buffer_pop_back_uint8(circular_buffer_uint8 *circ_buf)
circular buffer pop back
void circular_buffer_remove_front_to_uint8(circular_buffer_uint8 *circ_buf, const uint8_t value, const bool inclusive)
circular buffer remove elements from front until you find the given value
size_t circular_buffer_count_uint8(const circular_buffer_uint8 *circ_buf, const uint8_t value)
circular buffer count the number of a given value
size_t circular_buffer_get_size_uint8(const circular_buffer_uint8 *circ_buf)
circular buffer get number of elements
size_t circular_buffer_pop_front_block_uint8(circular_buffer_uint8 *circ_buf, uint8_t *destination, size_t dest_size)
circular buffer pop from front into a block of memory
void circular_buffer_init_uint8(circular_buffer_uint8 *circ_buf, const size_t capacity, uint8_t *buffer)
circular buffer init method
size_t circular_buffer_remove_front_unfinished_frames_uint8(circular_buffer_uint8 *circ_buf, const char startChar, const char endChar)
circular buffer remove unfinished frames from front of buffer
size_t circular_buffer_push_back_from_fd_uint8(circular_buffer_uint8 *circ_buf, int fd)
circular buffer push back reading from file descriptor
uint8_t circular_buffer_get_element_uint8(const circular_buffer_uint8 *circ_buf, const size_t iElement)
circular buffer get element
size_t circular_buffer_find_first_uint8(const circular_buffer_uint8 *circ_buf, const uint8_t value)
circular buffer find index of first occurance of value
uint8_t circular_buffer_pop_front_uint8(circular_buffer_uint8 *circ_buf)
circular buffer pop front
bool circular_buffer_is_full_uint8(const circular_buffer_uint8 *circ_buf)
circular buffer get if full
size_t circular_buffer_get_first_block_uint8(const circular_buffer_uint8 *circ_buf, const uint8_t **outBlock)
circular buffer get first block
void circular_buffer_remove_back_to_uint8(circular_buffer_uint8 *circ_buf, const uint8_t value, const bool inclusive)
circular buffer remove elements from back until you find the given value
size_t circular_buffer_delete_first_block_uint8(circular_buffer_uint8 *circ_buf)
circular buffer delete first block
size_t circular_buffer_push_back_string_uint8(circular_buffer_uint8 *circ_buf, const char *string)
circular buffer push back string (null terminated)
size_t circular_buffer_find_last_uint8(const circular_buffer_uint8 *circ_buf, const uint8_t value)
circular buffer find index of last occurance of value
bool circular_buffer_is_empty_uint8(const circular_buffer_uint8 *circ_buf)
circular buffer get if empty
void circular_buffer_push_front_uint8(circular_buffer_uint8 *circ_buf, const uint8_t element)
circular buffer push front
void circular_buffer_push_back_block_uint8(circular_buffer_uint8 *circ_buf, const uint8_t *source, size_t source_size)
circular buffer push a block of memory on back
void circular_buffer_push_back_uint8(circular_buffer_uint8 *circ_buf, const uint8_t element)
circular buffer push back
void circular_buffer_print_uint8(const circular_buffer_uint8 *circ_buf, FILE *outfile, uint8_t verbosity)
circular buffer print contents
circular buffer struct