diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2fedcc8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.d +*.o +*.obj +*.exe +*.out \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e78c199 --- /dev/null +++ b/Makefile @@ -0,0 +1,49 @@ +SHELL := /bin/bash + +CC = gcc -std=gnu99 +RM = rm -f +CFLAGS_DEBUG=-O0 -ggdb3 -Wall -Werror -pedantic -D_FORTIFY_SOURCE=2 -fasynchronous-unwind-tables -grecord-gcc-switches -Werror=implicit-function-declaration -I. +CFLAGS=-O2 -Wall -Werror -pedantic -I. + +TESTS_SRC = $(wildcard test/*.c) +EXECS_SRC = $(wildcard zaqar/*.c) +DEPS_SRC = $(wildcard lib/*.c) + +DEPS_OBJ = $(DEPS_SRC:.c=.o) +TESTS_OBJ = $(DEPS_OBJ) \ + $(TESTS_SRC:.c=.o) +EXECS_OBJ = $(DEPS_OBJ) \ + $(EXECS_SRC:.c=.o) +ALL_OBJ = $(DEPS_OBJ) \ + $(TESTS_SRC:.c=.o) \ + $(EXECS_SRC:.c=.o) + +DEPS_TRACK = $(OBJ:.o=.d) + +TESTS = test/test_circ_buff +EXECUTABLES = zaqar + +.PHONY: all + +all: $(TESTS) $(EXECUTABLES) +execs: $(EXECUTABLES) +tests: $(TESTS) + +$(TESTS): $(TESTS_OBJ) + $(CC) -o $@ $^ $(CFLAGS) + +$(EXECUTABLES): $(EXECS_OBJ) + $(CC) -o $@ $^ $(CFLAGS) + +-include $(DEPS_TRACK) + +%.d: %.c + @$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@ + +.PHONY: clean +clean: + rm -f $(ALL_OBJ) $(TESTS) $(EXECUTABLES) + +.PHONY: cleandep +cleandep: + rm -f $(DEPS_TRACK) \ No newline at end of file diff --git a/lib/circ_buff.c b/lib/circ_buff.c index 66a055c..726eeb4 100644 --- a/lib/circ_buff.c +++ b/lib/circ_buff.c @@ -90,7 +90,7 @@ size_t circ_buf_capacity(cbuf_handle_t cbuf) { return cbuf->max; } -void circ_buf_put(cbuf_handle_t cbuf, char data) { +void circ_buf_put(cbuf_handle_t cbuf, char* data) { assert(cbuf && cbuf->buffer); strcpy(cbuf->buffer[cbuf->head], data); diff --git a/lib/circ_buff.h b/lib/circ_buff.h index b0f8a1f..af726b7 100644 --- a/lib/circ_buff.h +++ b/lib/circ_buff.h @@ -24,7 +24,7 @@ void circ_buf_free(cbuf_handle_t cbuf); void circ_buf_reset(cbuf_handle_t cbuf); // Adds data, even if the buffer is full. Old data is overwritten. -void circ_buf_put(cbuf_handle_t cbuf, char data); +void circ_buf_put(cbuf_handle_t cbuf, char* data); // Retrieves a value from the buffer. int circ_buf_get(cbuf_handle_t cbuf, char* data); diff --git a/test/test_circ_buff.c b/test/test_circ_buff.c new file mode 100644 index 0000000..14fa640 --- /dev/null +++ b/test/test_circ_buff.c @@ -0,0 +1,71 @@ +#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)); +} \ No newline at end of file