Skip to content

Commit

Permalink
split tests and data structures WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Perseo committed Nov 22, 2020
1 parent 99ce338 commit b277b26
Show file tree
Hide file tree
Showing 7 changed files with 576 additions and 360 deletions.
68 changes: 35 additions & 33 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,43 +1,51 @@
CC := gcc
CC := gcc
CFLAGS := -Wall -Wextra -Wfloat-equal -Wundef -Wshadow -Wpointer-arith -Wstrict-prototypes -Wstrict-overflow=5 -Wwrite-strings -Waggregate-return -Wcast-qual -Wswitch-enum -Wswitch-default -Wconversion -Wunreachable-code
PROFILE_FLAGS := -fprofile-arcs -ftest-coverage #--whole-archive
OBJ_D := obj
LIBS_I := -I inc/ -I Unity/src
SRC_D := src
BIN_D := bin
PROFILE_FLAGS := -fprofile-arcs -ftest-coverage
SRC_D ?= src
OBJ_D ?= obj
TEST_D := tests
BIN_D := bin
OBJ_SRC := $(OBJ_D)/$(SRC_D)
OBJ_TEST := $(OBJ_D)/$(TEST_D)

LIBS_I := -I inc/ -I Unity/src

TESTS_SRC := $(shell find $(TEST_D) -type f -name '*.c')
#TESTS_OBJ := $(TESTS_SRC:$(TEST_D)/%.c=$(OBJ_TEST)/%.o)
TESTS_OBJ := $(TESTS_SRC:$(TEST_D)/%.c=$(OBJ_TEST)/%.o)

ALL_SRC := $(shell find $(SRC_D) -type f -name '*.c')
ALL_OBJ := $(ALL_SRC:$(SRC_D)/%.c=$(OBJ_SRC)/%.o)

# Tests
TESTS_SRC := $(shell find $(TEST_D) -type f -name '*.c')
TESTS_OBJ := $(TESTS_SRC:$(TEST_D)/%.c=$(OBJ_TEST)/%.o)
TESTS_BIN := $(TESTS_SRC:$(TEST_D)/%.c=$(BIN_D)/%.out)

# Project libraries
UNITY_L := Unity/libunity.a
CLIB_L := Clib.a

#LINKED_LIST_TEST := $(OBJ_TEST)/linked-list-tests.o
LINKED_LIST_TEST := tests/linked-list-tests.c
# Tests objects
LINKED_LIST_TEST := $(OBJ_TEST)/linked-list-tests.o
HASH_MAP_TEST := $(OBJ_TEST)/hash-map-tests.o


all: prepare clib

#all: prepare $(ALL_OBJ) #lib #$(TESTS_OBJ)
all: prepare lib
clib: $(ALL_OBJ)
ar rcs $(CLIB_L) $^

lib: $(OBJ_SRC)/Clib.o
ar rcs $(CLIB_L) $<
$(OBJ_D)/%.o: %.c
@echo -e "Compiling $< -> $@"
$(CC) -g $(CFLAGS) $(PROFILE_FLAGS) $(LIBS_I) -c $< -o $@

$(OBJ_SRC)/Clib.o: $(SRC_D)/Clib.c
$(CC) $(CFLAGS) $(PROFILE_FLAGS) $(LIBS_I) $< -c -o $@

test: linked-list-tests
test: $(TEST_OBJ) linked-list-tests hash-map-tests


linked-list-tests: $(CLIB_L) $(UNITY_L)
@$(CC) -g $(PROFILE_FLAGS) $(LIBS_I) $(LINKED_LIST_TEST) -o $(BIN_D)/$@ $^
linked-list-tests: $(LINKED_LIST_TEST) $(CLIB_L) $(UNITY_L)
@$(CC) -g $(PROFILE_FLAGS) $(LIBS_I) -o $(BIN_D)/$@ $^
@./$(BIN_D)/$@

hash-map-tests: $(HASH_MAP_TEST) $(CLIB_L) $(UNITY_L)
@$(CC) -g $(PROFILE_FLAGS) $(LIBS_I) -o $(BIN_D)/$@ $^
@./$(BIN_D)/$@

#rm unit-tests.gcda unit-tests.gcno
Expand All @@ -50,10 +58,7 @@ Unity/libunity.a:
@cd Unity && cmake CMakeLists.txt && make && cd ..

prepare:
@echo "SRC: " $(ALL_SRC)
@echo "OBJ: " $(ALL_OBJ)
@echo "TSRC: " $(TESTS_SRC)
@echo "TOBJ: " $(TESTS_OBJ)
@echo "tests" $(TESTS_BIN)
@mkdir -p $(OBJ_SRC)
@mkdir -p $(OBJ_TEST)
@mkdir -p $(BIN_D)
Expand All @@ -63,11 +68,8 @@ clean:
rm *.o *.a *.gcda *.gcov *.gcno


# This is not working yet
#$(TESTS_OBJ): $(OBJ_TEST)/%.o: $(TEST_D)/%.c:
#@echo -e "Compiling $< -> $@"
#$(CC) -g $(CFLAGS) $(PROFILE_FLAGS) -I Unity/src -c -o $@ $<

#$(ALL_OBJ): $(OBJ_SRC)/%.o: $(SRC_D)/%.c:
#@echo -e "Compiling $< -> $@"
#cc -g $(CFLAGS) $(PROFILE_FLAGS) -I $(INC_D) -I Unity/src -c -o $@ $<
# Not working yet
#tests: $(TEST_OBJ) $(TESTS_BIN)
#$(BIN_D)/%.out: %.o
#@echo -e "Generating test $< -> $@"
#@$(CC) -g $(PROFILE_FLAGS) $(LIBS_I) $< -o $(BIN_D)/$@ $(CLIB_L) $(UNITY_L)
31 changes: 27 additions & 4 deletions inc/Clib.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#include <stdbool.h>

// ****************************************************************************************
// ****************************** Definitions & Constants *********************************
Expand Down Expand Up @@ -71,10 +70,20 @@ typedef struct list_node ListNode;
typedef struct{
ListNode* head; //< Pointer to list head (this will not point to any content)
ListNode* tail; //< Pointer to list tail (this will not point to any content)
int size; //< Current Linked List size (could be calculated but this increase performance)
unsigned int size; //< Current Linked List size (could be calculated but this increase performance)
} LinkedList;


/// Comparator function definition
typedef bool(*ContentComparator)(void*,void*);

/// Primitive comparator functions
extern ContentComparator COMPARE_INT;
extern ContentComparator COMPARE_FLOAT;
extern ContentComparator COMPARE_DOUBLE;
extern ContentComparator COMPARE_STRING;


// ****************************************************************************************
// create_linked_list
// ****************************************************************************************
Expand Down Expand Up @@ -263,7 +272,7 @@ void * list_get_last(LinkedList *list);
* @return Size of list
*/
// ****************************************************************************************
int list_get_size(LinkedList *list);
unsigned int list_get_size(LinkedList *list);


// ****************************************************************************************
Expand All @@ -279,6 +288,20 @@ int list_get_size(LinkedList *list);
// ****************************************************************************************
void * list_get_element(LinkedList *list, unsigned int position);

// ****************************************************************************************
// list_find_node
// ****************************************************************************************
/**
* Find and return first #list node which match pattern
* @param[in] list Linked list to find node
* @param[in] pattern Content node to find
* @param[in] comparator Function which compares node contents
* @param[out] none
* @return Node with content matching given #pattern
*/
// ****************************************************************************************
ListNode * list_find_node(LinkedList *list, void * pattern, bool(*comparator)(void*, void*));


// ****************************************************************************************
// list_print
Expand Down
Loading

0 comments on commit b277b26

Please sign in to comment.