3#include "externals/unity.h"
14static uint8_t buf_mock[100];
15static size_t buf_mock_size = 0;
26void test_circular_buffer_init_uint8(
void) {
28 uint8_t buf[CAPACITY];
37 printf(
"Uncaught exception: %u\n", e1);
38 TEST_FAIL_MESSAGE(
"Uncaught exception!");
42 TEST_FAIL_MESSAGE(
"Should have triggered an exception!");
45 TEST_ASSERT_EQUAL_MESSAGE(
46 ASC_ERROR_CB_BAD_CAPACITY, e1,
47 "Exception should be ASC_ERROR_CB_BAD_CAPACITY, it isn't!");
62void test_circular_buffer_print_uint8(
void) {
65 uint8_t buf[CAPACITY];
69 FILE *outfile = fopen(
"/dev/null",
"a");
71 perror(
"Couldn't open /dev/null");
72 TEST_FAIL_MESSAGE(
"Couldn't open output file /dev/null");
78 printf(
"Uncaught exception: %u\n", e1);
79 TEST_FAIL_MESSAGE(
"Uncaught exception!");
83void test_circular_buffer_push_pop_back_uint8(
void) {
86 uint8_t buf[CAPACITY];
102 for (uint8_t i = 0; i < 5; i++) {
110 for (uint8_t i = 0; i < 5; i++) {
119 for (uint8_t i = 0; i < 7; i++) {
136 for (uint8_t i = 0; i < 8; i++) {
147 printf(
"Uncaught exception: %u\n", e1);
148 TEST_FAIL_MESSAGE(
"Uncaught exception!");
152void test_circular_buffer_push_overfull_pop_back_uint8(
void) {
155 uint8_t buf[CAPACITY];
159 for (uint8_t i = 0; i < 33; i++) {
167 for (uint8_t i = 0; i < CAPACITY; i++) {
174 for (uint8_t i = 0; i < 29; i++) {
182 for (uint8_t i = 0; i < 43; i++) {
190 for (uint8_t i = 0; i < CAPACITY; i++) {
198 printf(
"Uncaught exception: %u\n", e1);
199 TEST_FAIL_MESSAGE(
"Uncaught exception!");
203void test_circular_buffer_push_pop_front_uint8(
void) {
206 uint8_t buf[CAPACITY];
222 for (uint8_t i = 0; i < 5; i++) {
230 for (uint8_t i = 0; i < 5; i++) {
239 for (uint8_t i = 0; i < CAPACITY - 1; i++) {
253 for (uint8_t i = 0; i < CAPACITY; i++) {
254 TEST_ASSERT_EQUAL_UINT8(CAPACITY - 1 - i,
265 printf(
"Uncaught exception: %u\n", e1);
266 TEST_FAIL_MESSAGE(
"Uncaught exception!");
270void test_circular_buffer_push_overfull_pop_front_uint8(
void) {
273 uint8_t buf[CAPACITY];
277 for (uint8_t i = 0; i < 33; i++) {
285 for (uint8_t i = 0; i < CAPACITY; i++) {
292 for (uint8_t i = 0; i < 29; i++) {
300 for (uint8_t i = 0; i < 43; i++) {
308 for (uint8_t i = 0; i < CAPACITY; i++) {
316 printf(
"Uncaught exception: %u\n", e1);
317 TEST_FAIL_MESSAGE(
"Uncaught exception!");
321void test_circular_buffer_push_pop_front_back_uint8(
void) {
324 uint8_t buf[CAPACITY];
328 for (uint8_t i = 0; i < 5; i++) {
332 for (uint8_t i = 0; i < 2; i++) {
352 printf(
"Uncaught exception: %u\n", e1);
353 TEST_FAIL_MESSAGE(
"Uncaught exception!");
357void test_circular_buffer_get_element_uint8(
void) {
360 uint8_t buf[CAPACITY];
364 for (uint8_t i = 0; i < 5; i++) {
368 for (uint8_t i = 0; i < 5; i++) {
377 printf(
"Uncaught exception: %u\n", e1);
378 TEST_FAIL_MESSAGE(
"Uncaught exception!");
382void test_circular_buffer_remove_front_to_uint8(
void) {
385 uint8_t buf[CAPACITY];
389 for (uint8_t i = 0; i < 8; i++) {
396 for (uint8_t i = 0; i < 3; i++) {
403 for (uint8_t i = 6; i < 8; i++) {
407 for (uint8_t i = 0; i < 3; i++) {
414 for (uint8_t i = 0; i < 20; i++) {
424 printf(
"Uncaught exception: %u\n", e1);
425 TEST_FAIL_MESSAGE(
"Uncaught exception!");
429void test_circular_buffer_remove_back_to_uint8(
void) {
433 uint8_t buf[CAPACITY];
437 for (uint8_t i = 0; i < 8; i++) {
444 for (uint8_t i = 0; i < 3; i++) {
451 for (uint8_t i = 0; i < 3; i++) {
459 for (uint8_t i = 0; i < 20; i++) {
469 printf(
"Uncaught exception: %u\n", e1);
470 TEST_FAIL_MESSAGE(
"Uncaught exception!");
474void test_circular_buffer_find_first_uint8(
void) {
478 uint8_t buf[CAPACITY];
484 "find_first should be >= size but isn't");
486 for (uint8_t i = 0; i < 5; i++) {
491 "find_first should be >= size but isn't");
494 for (uint8_t i = 0; i < 2; i++) {
499 for (uint8_t i = 0; i < 50; i++) {
504 for (uint8_t i = 0; i < CAPACITY; i++) {
514 printf(
"Uncaught exception: %u\n", e1);
515 TEST_FAIL_MESSAGE(
"Uncaught exception!");
519void test_circular_buffer_find_last_uint8(
void) {
523 uint8_t buf[CAPACITY];
529 "find_last should be >= size but isn't");
531 for (uint8_t i = 0; i < 5; i++) {
536 "find_last should be >= size but isn't");
539 for (uint8_t i = 0; i < 2; i++) {
544 for (uint8_t i = 0; i < 50; i++) {
549 for (uint8_t i = 0; i < CAPACITY; i++) {
559 printf(
"Uncaught exception: %u\n", e1);
560 TEST_FAIL_MESSAGE(
"Uncaught exception!");
564void test_circular_buffer_count_uint8(
void) {
568 uint8_t buf[CAPACITY];
575 for (uint8_t i = 0; i < CAPACITY; i++) {
586 for (uint8_t i = 0; i < 50; i++) {
590 for (uint8_t i = 0; i < CAPACITY; i++) {
596 printf(
"Uncaught exception: %u\n", e1);
597 TEST_FAIL_MESSAGE(
"Uncaught exception!");
601void test_circular_buffer_get_first_block_uint8(
void) {
605 uint8_t buf[CAPACITY];
608 const uint8_t *block = NULL;
610 TEST_ASSERT_EQUAL(0, blockSize);
612 for (uint8_t i = 0; i < 5; i++) {
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]);
624 for (uint8_t i = 0; i < 5; i++) {
627 TEST_ASSERT_EQUAL(5, cb.
iStart);
630 TEST_ASSERT_EQUAL(0, blockSize);
631 TEST_ASSERT_EQUAL_MESSAGE(cb.
buffer + 5, block,
632 "Pointers not equal cb buffer and block!");
634 for (uint8_t i = 0; i < 17; i++) {
638 TEST_ASSERT_EQUAL(2, blockSize);
639 for (uint8_t i = 0; i < blockSize; i++) {
640 TEST_ASSERT_EQUAL_UINT8(i + 9, block[i]);
644 printf(
"Uncaught exception: %u\n", e1);
645 TEST_FAIL_MESSAGE(
"Uncaught exception!");
649void test_circular_buffer_delete_first_block_uint8(
void) {
653 uint8_t buf[CAPACITY];
657 TEST_ASSERT_EQUAL(0, delSize);
659 for (uint8_t i = 0; i < 5; i++) {
663 TEST_ASSERT_EQUAL(5, delSize);
665 for (uint8_t i = 0; i < 5; i++) {
669 for (uint8_t i = 0; i < 20; i++) {
673 TEST_ASSERT_EQUAL(7, delSize);
675 const uint8_t *block = NULL;
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]);
684 TEST_ASSERT_EQUAL(1, delSize);
688 printf(
"Uncaught exception: %u\n", e1);
689 TEST_FAIL_MESSAGE(
"Uncaught exception!");
693void test_circular_buffer_push_back_block_uint8(
void) {
697 uint8_t buf[CAPACITY];
701 for (uint8_t i = 0; i < buf_mock_size; i++) {
709 for (uint8_t i = 0; i < buf_mock_size; i++) {
717 for (uint8_t i = 0; i < buf_mock_size; i++) {
727 for (uint8_t i = 0; i < buf_mock_size; i++) {
737 for (uint8_t i = 0; i < buf_mock_size; i++) {
744 for (uint8_t i = 0; i < 4; i++) {
752 for (uint8_t i = 0; i < buf_mock_size; i++) {
759 printf(
"Uncaught exception: %u\n", e1);
760 TEST_FAIL_MESSAGE(
"Uncaught exception!");
764void test_circular_buffer_pop_front_block_uint8(
void) {
768 uint8_t buf[CAPACITY];
771 uint8_t outputBuffer[100];
773 for (uint8_t i = 0; i < 5; i++) {
784 TEST_ASSERT_EQUAL(5, nPopped);
785 for (uint8_t i = 0; i < 5; i++) {
786 TEST_ASSERT_EQUAL_UINT8(i, outputBuffer[i]);
790 for (uint8_t i = 0; i < 5; i++) {
796 TEST_ASSERT_EQUAL(0, nPopped);
800 TEST_ASSERT_EQUAL(5, nPopped);
801 for (uint8_t i = 0; i < 5; i++) {
802 TEST_ASSERT_EQUAL_UINT8(i, outputBuffer[i]);
807 TEST_ASSERT_EQUAL(0, nPopped);
810 for (uint8_t i = 0; i < 5; i++) {
815 TEST_ASSERT_EQUAL(0, nPopped);
818 TEST_ASSERT_EQUAL(2, nPopped);
819 for (uint8_t i = 0; i < 2; i++) {
820 TEST_ASSERT_EQUAL_UINT8(i, outputBuffer[i]);
824 TEST_ASSERT_EQUAL(1, nPopped);
825 TEST_ASSERT_EQUAL_UINT8(2, outputBuffer[0]);
828 TEST_ASSERT_EQUAL(2, nPopped);
829 for (uint8_t i = 0; i < 2; i++) {
830 TEST_ASSERT_EQUAL_UINT8(i + 3, outputBuffer[i]);
834 TEST_ASSERT_EQUAL(0, nPopped);
839 for (uint8_t i = 0; i < CAPACITY; i++) {
845 TEST_ASSERT_EQUAL(0, nPopped);
849 TEST_ASSERT_EQUAL(CAPACITY, nPopped);
850 for (uint8_t i = 0; i < CAPACITY; i++) {
851 TEST_ASSERT_EQUAL_UINT8(i, outputBuffer[i]);
855 for (uint8_t i = 0; i < CAPACITY; i++) {
862 TEST_ASSERT_EQUAL(CAPACITY, nPopped);
863 for (uint8_t i = 0; i < CAPACITY; i++) {
864 TEST_ASSERT_EQUAL_UINT8(i, outputBuffer[i]);
868 for (uint8_t i = 0; i < CAPACITY; i++) {
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]);
882 TEST_ASSERT_EQUAL(1, nPopped);
883 TEST_ASSERT_EQUAL_UINT8(CAPACITY - 1, outputBuffer[0]);
887 TEST_ASSERT_EQUAL(0, nPopped);
892 for (uint8_t i = 0; i < CAPACITY + CAPACITY / 2; i++) {
898 TEST_ASSERT_EQUAL(0, nPopped);
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]);
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]);
914 for (uint8_t i = 0; i < CAPACITY + CAPACITY / 2; i++) {
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]);
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]);
933 for (uint8_t i = 0; i < CAPACITY + CAPACITY / 2; i++) {
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]);
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]);
954 TEST_ASSERT_EQUAL(1, nPopped);
955 TEST_ASSERT_EQUAL_UINT8(CAPACITY + CAPACITY / 2 - 1, outputBuffer[0]);
959 TEST_ASSERT_EQUAL(0, nPopped);
963 printf(
"Uncaught exception: %u\n", e1);
964 TEST_FAIL_MESSAGE(
"Uncaught exception!");
968void test_circular_buffer_push_back_from_fd_uint8(
void) {
972 uint8_t buf[CAPACITY];
975 const int infd = open(
"/dev/zero", O_RDONLY);
977 perror(
"Couldn't open /dev/zero");
978 TEST_FAIL_MESSAGE(
"Couldn't open input "
984 TEST_ASSERT_EQUAL(CAPACITY, nRead);
985 for (uint8_t i = 0; i < CAPACITY; i++) {
989 for (uint8_t i = 0; i < CAPACITY / 2; i++) {
994 TEST_ASSERT_EQUAL(CAPACITY / 2, nRead);
995 for (uint8_t i = 0; i < CAPACITY / 2; i++) {
998 for (uint8_t i = 0; i < CAPACITY / 2; i++) {
1002 for (uint8_t i = 0; i < CAPACITY + CAPACITY / 2; i++) {
1007 TEST_ASSERT_EQUAL(CAPACITY / 2, nRead);
1008 for (uint8_t i = 0; i < CAPACITY / 2; i++) {
1009 TEST_ASSERT_EQUAL_UINT8(i + CAPACITY,
1012 for (uint8_t i = 0; i < CAPACITY / 2; i++) {
1017 for (uint8_t i = 0; i < CAPACITY + 2; i++) {
1020 for (uint8_t i = 0; i < 3; i++) {
1025 TEST_ASSERT_EQUAL(3, nRead);
1027 for (uint8_t i = 0; i < CAPACITY - 3; i++) {
1030 for (uint8_t i = 0; i < 3; i++) {
1036 printf(
"Uncaught exception: %u\n", e1);
1037 TEST_FAIL_MESSAGE(
"Uncaught exception!");
1041void test_circular_buffer_pop_front_to_fd_uint8(
void) {
1045 uint8_t buf[CAPACITY];
1049 const int outfd = open(
"/dev/null", O_WRONLY | O_APPEND);
1051 perror(
"Couldn't open /dev/null");
1052 TEST_FAIL_MESSAGE(
"Couldn't open output file "
1057 TEST_ASSERT_EQUAL(0, nWritten);
1060 for (uint8_t i = 0; i < 1; i++) {
1065 TEST_ASSERT_EQUAL(1, nWritten);
1068 for (uint8_t i = 0; i < CAPACITY; i++) {
1073 TEST_ASSERT_EQUAL(CAPACITY, nWritten);
1076 for (uint8_t i = 0; i < CAPACITY + CAPACITY / 2; i++) {
1081 TEST_ASSERT_EQUAL(CAPACITY / 2, nWritten);
1083 for (uint8_t i = 0; i < CAPACITY / 2; i++) {
1084 TEST_ASSERT_EQUAL_UINT8(CAPACITY + i,
1089 printf(
"Uncaught exception: %u\n", e1);
1090 TEST_FAIL_MESSAGE(
"Uncaught exception!");
1094void test_circular_buffer_remove_front_unfinished_frames_uint8(
void) {
1097 uint8_t buf[CAPACITY];
1101 const char *testString =
"\nxxxxa";
1102 size_t resultSize = 0;
1107 testString =
"\nxxxxa>abcdefg";
1109 const char *resultString =
">abcdefg";
1113 for (
size_t i = 0; i < resultSize; i++) {
1114 TEST_ASSERT_EQUAL_CHAR(resultString[i],
1119 testString =
"\nxa>abc\n";
1121 resultString =
">abc\n";
1125 for (
size_t i = 0; i < resultSize; i++) {
1126 TEST_ASSERT_EQUAL_CHAR(resultString[i],
1131 testString =
"ab\nxa>abc\n";
1133 resultString =
">abc\n";
1137 for (
size_t i = 0; i < resultSize; i++) {
1138 TEST_ASSERT_EQUAL_CHAR(resultString[i],
1143 testString =
"abcdefg";
1148 for (
size_t i = 0; i < resultSize; i++) {
1149 TEST_ASSERT_EQUAL_CHAR(resultString[i],
1154 testString =
">a>b>c>d";
1156 resultString =
">d";
1160 for (
size_t i = 0; i < resultSize; i++) {
1161 TEST_ASSERT_EQUAL_CHAR(resultString[i],
1166 testString =
">a>b>c>d";
1168 resultString =
">d";
1172 for (
size_t i = 0; i < resultSize; i++) {
1173 TEST_ASSERT_EQUAL_CHAR(resultString[i],
1178 testString =
"c>d>abc\n";
1180 resultString =
">abc\n";
1184 for (
size_t i = 0; i < resultSize; i++) {
1185 TEST_ASSERT_EQUAL_CHAR(resultString[i],
1191 printf(
"Uncaught exception: %u\n", e1);
1192 TEST_FAIL_MESSAGE(
"Uncaught exception!");
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);
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