Apostolos Fanakis
6 years ago
5 changed files with 127 additions and 2 deletions
@ -0,0 +1,5 @@ |
|||
*.d |
|||
*.o |
|||
*.obj |
|||
*.exe |
|||
*.out |
@ -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) |
@ -0,0 +1,71 @@ |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <stdint.h> |
|||
|
|||
#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)); |
|||
} |
Loading…
Reference in new issue