diff --git a/Makefile b/Makefile index 1e4d0d2..21020a1 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,8 @@ all: $(BIN_NAME) test: obj/test.o obj/networking.o obj/linked.o $(LD) $(LDFLAGS) -o $@ $^ +valgrind: test + valgrind --leak-check=yes ./test obj/test.o: src/networking.h obj/networking.o: src/networking.h src/headers.h diff --git a/src/linked.c b/src/linked.c index ec9a9ef..7e725af 100644 --- a/src/linked.c +++ b/src/linked.c @@ -12,12 +12,12 @@ linkedList_t linked_create() { return list; } -size_t linked_push(linkedList_t* list, void* data) { +size_t linked_push(linkedList_t* list, const void* data) { link_t* new = malloc(sizeof (link_t)); if (new == NULL) return -1; - new->data = data; + new->data = (void*) data; sem_init(&(new->modify_sem), 0, 1); new->inUse = 0; new->list = list; diff --git a/src/linked.h b/src/linked.h index 5dff07f..5680ac9 100644 --- a/src/linked.h +++ b/src/linked.h @@ -21,7 +21,7 @@ typedef struct link { } link_t; linkedList_t linked_create(); -size_t linked_push(linkedList_t* list, void* data); +size_t linked_push(linkedList_t* list, const void* data); link_t* linked_first(linkedList_t* list); link_t* linked_next(link_t* link); size_t linked_length(linkedList_t* list); diff --git a/src/test.c b/src/test.c index 8b21a2e..ce7072e 100644 --- a/src/test.c +++ b/src/test.c @@ -1,39 +1,87 @@ #include +#include +#include +#include #include "networking.h" #include "linked.h" -handler_t handlerGetter(struct metaData metaData, const char* host) { - return NULL; +bool global = true; + +void checkBool(bool ok, const char* check) { + const char* result; + if (ok) { + result = "[ OK ]"; + } else { + result = "[FAILED]"; + global = false; + } + + printf("%s:%*s%s\n", check, (int) (30 - strlen(check)), "", result); +} +void checkInt(int value, int compare, const char* check) { + checkBool(value == compare, check); +} +void checkString(const char* value, const char* compare, const char* check) { + checkBool(strcmp(value, compare) == 0, check); +} +void checkVoid(const void* value, const void* compare, const char* check) { + checkBool(value == compare, check); +} +void checkNull(void* value, const char* check) { + checkBool(value != NULL, check); } void testLinkedList() { linkedList_t list = linked_create(); - linked_push(&list, "Entry 0"); - linked_push(&list, "Entry 1"); - linked_push(&list, "Entry 2"); - linked_push(&list, "Entry 3"); + + const char* testString = "Test"; - link_t* current = linked_first(&list); - while(current != NULL) { - printf("%s\n", (char*) current->data); - current = linked_next(current); - } + checkInt(linked_length(&list), 0, "empty list length"); + checkInt(linked_push(&list, testString), 0, "insert position"); + checkInt(linked_length(&list), 1, "list length"); + link_t* link = linked_get(&list, 0); + checkNull(link, "get not null"); + checkVoid(link->data, testString, "test string value"); + checkInt(link->inUse, 1, "inUse counter value"); + linked_release(link); + checkInt(link->inUse, 0, "inUse counter value"); - current = linked_get(&list, 2); - printf("%s\n", (char*) current->data); - linked_unlink(current); - linked_release(current); + checkInt(linked_push(&list, (void*) 1), 1, "insert position"); + checkInt(linked_push(&list, (void*) 2), 2, "insert position"); + checkInt(linked_push(&list, (void*) 3), 3, "insert position"); + checkInt(linked_length(&list), 4, "list length"); - current = linked_first(&list); - while(current != NULL) { - printf("%s\n", (char*) current->data); - current = linked_next(current); - } + link = linked_first(&list); + checkInt(linked_unlink(link), 0, "unlink first result"); + linked_release(link); + checkInt(linked_length(&list), 3, "link length"); + + link = linked_first(&list); + checkNull(link, "get not null"); + checkInt((long) link->data, 1, "get value"); + + link = linked_next(link); + checkNull(link, "get not null"); + checkInt((long) link->data, 2, "get value"); + + checkInt(linked_unlink(link), 0, "unlink not first result"); + + link = linked_next(link); + checkNull(link, "get not null"); + checkInt((long) link->data, 3, "get value"); + + linked_release(link); + + checkInt(linked_length(&list), 2, "list length"); linked_destroy(&list); } +handler_t handlerGetter(struct metaData metaData, const char* host) { + return NULL; +} + void testNetworking() { initNetworking((struct networkingConfig) { .connectionTimeout = 30000, @@ -45,5 +93,16 @@ void testNetworking() { } int main(int argc, char** argv) { + bool overall = true; + + printf("linked lists\n"); + printf("============\n\n"); testLinkedList(); + if (!global) + overall = false; + printf("linked lists: %s\n\n", global ? "OK" : "FAILED"); + global = true; + + + printf("\nOverall: %s\n", overall ? "OK" : "FAILED"); }