#include #include #include #include "../lib/circ_buff.h" #define BUFFER_SIZE 8 #define BUFFER_ELEMENT_SIZE 278 void print_buff_status(cbuf_handle_t circ_buf); int main(void) { // Creates and initializes a buffer char** buffer = (char **) malloc(BUFFER_SIZE * sizeof(char *)); for (uint8_t buff_el = 0; buff_el < BUFFER_SIZE; ++buff_el) { buffer[buff_el] = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); } printf("===== C Circular Buffer Check\n\n"); cbuf_handle_t circ_buf = circ_buf_init(buffer, BUFFER_SIZE); printf("Buffer initialized.\n"); print_buff_status(circ_buf); printf("\n===== Adding %d values\n", BUFFER_SIZE - 1); for(uint8_t i = 0; i < (BUFFER_SIZE - 1); i++) { char* temp = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); snprintf(temp, BUFFER_ELEMENT_SIZE, "string_%d", i); circ_buf_put(circ_buf, temp); printf("Added %u, Size now: %zu\n", i, circ_buf_size(circ_buf)); } print_buff_status(circ_buf); printf("\n===== Adding %d values\n", BUFFER_SIZE); for(uint8_t i = 0; i < BUFFER_SIZE; i++) { char* temp = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); snprintf(temp, BUFFER_ELEMENT_SIZE, "string_%d", BUFFER_SIZE - 1 + i); circ_buf_put(circ_buf, temp); printf("Added %u, Size now: %zu\n", i, circ_buf_size(circ_buf)); } print_buff_status(circ_buf); printf("\n===== Reading back values:\n"); while(!circ_buf_empty(circ_buf)) { char* temp = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); circ_buf_get(circ_buf, temp); printf("%s ", temp); } printf("\n"); print_buff_status(circ_buf); printf("\n===== Test done, cleaning up\n"); free(buffer); circ_buf_free(circ_buf); return 0; } void print_buff_status(cbuf_handle_t circ_buf) { printf("Full: %d, empty: %d, size: %zu\n", circ_buf_full(circ_buf), circ_buf_empty(circ_buf), circ_buf_size(circ_buf)); }