Browse Source

Add Makefile, Add test

master
Apostolos Fanakis 6 years ago
parent
commit
34d3ac1985
  1. 5
      .gitignore
  2. 49
      Makefile
  3. 2
      lib/circ_buff.c
  4. 2
      lib/circ_buff.h
  5. 71
      test/test_circ_buff.c

5
.gitignore

@ -0,0 +1,5 @@
*.d
*.o
*.obj
*.exe
*.out

49
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)

2
lib/circ_buff.c

@ -90,7 +90,7 @@ size_t circ_buf_capacity(cbuf_handle_t cbuf) {
return cbuf->max; 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); assert(cbuf && cbuf->buffer);
strcpy(cbuf->buffer[cbuf->head], data); strcpy(cbuf->buffer[cbuf->head], data);

2
lib/circ_buff.h

@ -24,7 +24,7 @@ void circ_buf_free(cbuf_handle_t cbuf);
void circ_buf_reset(cbuf_handle_t cbuf); void circ_buf_reset(cbuf_handle_t cbuf);
// Adds data, even if the buffer is full. Old data is overwritten. // 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. // Retrieves a value from the buffer.
int circ_buf_get(cbuf_handle_t cbuf, char* data); int circ_buf_get(cbuf_handle_t cbuf, char* data);

71
test/test_circ_buff.c

@ -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…
Cancel
Save