From bd242c933ded55fb3c9bf6a5566d607c57196d5d Mon Sep 17 00:00:00 2001 From: franchioping <43936644+franchioping@users.noreply.github.com> Date: Thu, 27 Jun 2024 00:37:56 +0100 Subject: [PATCH] Fix Linked Lists, Fix testing for linked lists --- CMakeLists.txt | 12 ++-- src/http/message.c | 5 ++ src/http/message.h | 15 +++++ src/test/linked_list.c | 75 ------------------------ src/test/linked_list.h | 12 ---- src/test/test_linked_list.c | 75 ++++++++++++++++++++++++ src/test/test_linked_list.h | 12 ++++ src/test/test_main.c | 6 +- src/util/linkedlist/linkedlist.c | 99 ++++++++++++++++++++++++++------ src/util/linkedlist/linkedlist.h | 11 ++-- 10 files changed, 207 insertions(+), 115 deletions(-) create mode 100644 src/http/message.c create mode 100644 src/http/message.h delete mode 100644 src/test/linked_list.c delete mode 100644 src/test/linked_list.h create mode 100644 src/test/test_linked_list.c create mode 100644 src/test/test_linked_list.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d8ed754..079ee85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,13 +13,17 @@ set(COMMON_SOURCES src/request/handle_client.c src/request/headers.c src/request/headers.h src/util/linkedlist/linkedlist.c - src/util/linkedlist/linkedlist.h) + src/util/linkedlist/linkedlist.h + src/http/message.c + src/http/message.h +) -set(TARGET_SOURCES src/main.c) +set(TARGET_SOURCES src/main.c + ) set(TEST_SOURCES src/test/test_main.c - src/test/linked_list.c - src/test/linked_list.h) + src/test/test_linked_list.c + src/test/test_linked_list.h) add_executable(HttpServer ${COMMON_SOURCES} ${TARGET_SOURCES}) diff --git a/src/http/message.c b/src/http/message.c new file mode 100644 index 0000000..e453f15 --- /dev/null +++ b/src/http/message.c @@ -0,0 +1,5 @@ +// +// Created by franchioping on 6/26/24. +// + +#include "message.h" diff --git a/src/http/message.h b/src/http/message.h new file mode 100644 index 0000000..9acebc4 --- /dev/null +++ b/src/http/message.h @@ -0,0 +1,15 @@ +// +// Created by franchioping on 6/26/24. +// + +#ifndef HTTPSERVER_MESSAGE_H +#define HTTPSERVER_MESSAGE_H + + +struct generic_message { + char* start_line; + char** message_headers; + char* message_body; +}; + +#endif //HTTPSERVER_MESSAGE_H diff --git a/src/test/linked_list.c b/src/test/linked_list.c deleted file mode 100644 index ef16eed..0000000 --- a/src/test/linked_list.c +++ /dev/null @@ -1,75 +0,0 @@ -// -// Created by franchioping on 6/25/24. -// - -#include "linked_list.h" -#include -#include -#include "src/util/linkedlist/linkedlist.h" -#include - -#define TEST_LIST_LENGTH 9 - -int* generate_data(const int length){ - int* ints = (int*) malloc(sizeof(int) * length); - for(int i = 0; i < length; i++){ - ints[i] = i; - } - return ints; -} - -listnode* create_linked_list(const int list_length, const int* data){ - - listnode *start = (listnode*) malloc(sizeof(listnode)); - listnode* node = start; - for(int i = 0; i < list_length; i++){ - node->data = (void*) &data[i]; - node->next_node = (listnode*) malloc(sizeof(listnode)); - node = node->next_node; - } - return start; -} - -START_TEST (test_linked_list_get_index) -{ - int* ints= generate_data(TEST_LIST_LENGTH); - - listnode* list = create_linked_list(TEST_LIST_LENGTH, ints); - - for(int i = 0; i < TEST_LIST_LENGTH -1; i++){ - listnode* node = list_get_at_index(list, i); - ck_assert_int_eq(*((int*)(node->data)), i); - } - - free(ints); - list_free(list); -} -END_TEST - -START_TEST (test_linked_list_length) -{ - int* ints= generate_data(TEST_LIST_LENGTH); - - listnode* list = create_linked_list(TEST_LIST_LENGTH, ints); - - ck_assert_uint_eq(list_get_length(list), TEST_LIST_LENGTH); - free(ints); - list_free(list); -} -END_TEST - - -Suite *linked_list_suite(void) { - Suite *s; - TCase *tc_core; - - s = suite_create("Test Linked List Suite"); - tc_core = tcase_create("Core"); - - tcase_add_test(tc_core, test_linked_list_length); - tcase_add_test(tc_core, test_linked_list_get_index); - - suite_add_tcase(s, tc_core); - - return s; -} \ No newline at end of file diff --git a/src/test/linked_list.h b/src/test/linked_list.h deleted file mode 100644 index 58399e1..0000000 --- a/src/test/linked_list.h +++ /dev/null @@ -1,12 +0,0 @@ -// -// Created by franchioping on 6/25/24. -// - -#ifndef HTTPSERVER_LINKED_LIST_H -#define HTTPSERVER_LINKED_LIST_H - -#include - -Suite *linked_list_suite(void); - -#endif //HTTPSERVER_LINKED_LIST_H diff --git a/src/test/test_linked_list.c b/src/test/test_linked_list.c new file mode 100644 index 0000000..e0d7d91 --- /dev/null +++ b/src/test/test_linked_list.c @@ -0,0 +1,75 @@ +// +// Created by franchioping on 6/25/24. +// + +#include "test_linked_list.h" +#include +#include +#include "src/util/linkedlist/linkedlist.h" +#include + +#define TEST_LIST_LENGTH 2 + +int* generate_data(const int length){ + int* ints = (int*) malloc(sizeof(int) * length); + for(int i = 0; i < length; i++){ + ints[i] = i; + } + return ints; +} + +START_TEST (test_lnkdList_GetNth) +{ + int* ints= generate_data(TEST_LIST_LENGTH); + + listnode* list = lnkdList_FromIntArray(TEST_LIST_LENGTH, ints); + + for(ssize_t i = 0; i < TEST_LIST_LENGTH; i++){ + ck_assert_uint_eq(*((int*)lnkdList_GetNth(list, i)->data),i); + } + + free(ints); + lnkdList_Free(list); +} +END_TEST + + +START_TEST (test_lnkdList_GetLength){ + int* ints= generate_data(TEST_LIST_LENGTH); + + listnode* list = lnkdList_FromIntArray(TEST_LIST_LENGTH, ints); + + ck_assert_uint_eq(lnkdList_GetLength(list), TEST_LIST_LENGTH); + free(ints); + lnkdList_Free(list); +} +END_TEST + +START_TEST (test_lnkdList_Free) { + + + int *ints = generate_data(TEST_LIST_LENGTH); + + listnode *list = lnkdList_FromIntArray(TEST_LIST_LENGTH, ints); + + free(ints); + lnkdList_Free(list); +} +END_TEST + + +Suite *linked_list_suite(void) { + Suite *s; + TCase *tc_core; + + s = suite_create("Test Linked List Suite"); + tc_core = tcase_create("Core"); + + tcase_add_test(tc_core, test_lnkdList_GetLength); + tcase_add_test(tc_core, test_lnkdList_Free); + tcase_add_test(tc_core, test_lnkdList_GetNth); + + suite_add_tcase(s, tc_core); + + return s; +} \ No newline at end of file diff --git a/src/test/test_linked_list.h b/src/test/test_linked_list.h new file mode 100644 index 0000000..15931b1 --- /dev/null +++ b/src/test/test_linked_list.h @@ -0,0 +1,12 @@ +// +// Created by franchioping on 6/25/24. +// + +#ifndef HTTPSERVER_TEST_LINKED_LIST_H +#define HTTPSERVER_TEST_LINKED_LIST_H + +#include + +Suite *linked_list_suite(void); + +#endif //HTTPSERVER_TEST_LINKED_LIST_H diff --git a/src/test/test_main.c b/src/test/test_main.c index 670c7c5..45297ae 100644 --- a/src/test/test_main.c +++ b/src/test/test_main.c @@ -5,7 +5,8 @@ #include #include #include -#include "linked_list.h" +#include +#include "test_linked_list.h" int run_suite(Suite* suite){ @@ -17,7 +18,10 @@ int run_suite(Suite* suite){ return no_failed; } +void no_op(enum mcheck_status status) {} + int main(void) { + mcheck(&no_op); int no_failed = 0; no_failed += run_suite(linked_list_suite()); diff --git a/src/util/linkedlist/linkedlist.c b/src/util/linkedlist/linkedlist.c index d29e5b7..8c4299d 100644 --- a/src/util/linkedlist/linkedlist.c +++ b/src/util/linkedlist/linkedlist.c @@ -3,36 +3,99 @@ // #include +#include +#include #include "linkedlist.h" - -listnode* list_get_at_index(listnode* list_start, ssize_t index){ - listnode* node = list_start; - for(int i = 0; i < index; i++){ - node = node->next_node; - } +listnode* lnkdList_NewNode(){ + listnode* node = (listnode*) malloc(sizeof(listnode)); + memset(node, 0, sizeof(listnode)); return node; } -ssize_t list_get_length(listnode* list_start){ - ssize_t length = 1; - while(list_start->next_node){ - length++; - list_start = list_start->next_node; + +listnode* lnkdList_FromPtrArray(const ssize_t length, void** data){ + + listnode *start = lnkdList_NewNode(); + start->data = data[0]; + + listnode* node = start; + for(ssize_t i = 1; i < length; i++){ + node->next = lnkdList_NewNode(); + node = node->next; + node->data = data[i]; } - return length; + return start; } -void list_free(listnode* list_start){ - if(list_start == NULL) - return; - listnode* head = list_start; - listnode * tmp; +listnode* lnkdList_FromIntArray(const ssize_t length, int* data){ + + listnode *start = lnkdList_NewNode(); + start->data = &data[0]; + + listnode* node = start; + for(ssize_t i = 1; i < length; i++){ + node->next = lnkdList_NewNode(); + node = node->next; + node->data = &data[i]; + } + return start; +} + + +listnode* lnkdList_GetNth(listnode* head, ssize_t index){ + + listnode* current = head; + + + ssize_t count = 0; + while (current != NULL) { + if (count == index) + return current; + count++; + current = current->next; + } + + /* if we get to this line, + the caller was asking + for a non-existent element + so we assert fail */ + assert(0); +} + + +ssize_t lnkdList_GetLength(listnode* head) +{ + listnode* cur = head; + ssize_t size = 0; + + while (cur != NULL) + { + ++size; + cur = cur->next; + } + + return size; +} + +void lnkdList_FreeDataInList(listnode* head){ + listnode* cur = head; + while (cur != NULL) + { + free(cur->data); + cur = cur->next; + } + +} + +void lnkdList_Free(listnode* head){ + struct listnode* tmp; while (head != NULL) { tmp = head; - head = head->next_node; + head = head->next; free(tmp); } } + diff --git a/src/util/linkedlist/linkedlist.h b/src/util/linkedlist/linkedlist.h index 8621866..48b13d5 100644 --- a/src/util/linkedlist/linkedlist.h +++ b/src/util/linkedlist/linkedlist.h @@ -8,15 +8,16 @@ #include typedef struct listnode { - struct listnode* next_node; + struct listnode* next; void* data; } listnode; -listnode* list_get_at_index(listnode* list_start, ssize_t index); -ssize_t list_get_length(listnode* list_start); -void list_free(listnode* list_start); - +listnode* lnkdList_GetNth(listnode* head, ssize_t index); +ssize_t lnkdList_GetLength(listnode* head); +void lnkdList_Free(listnode* head); +listnode* lnkdList_FromIntArray(ssize_t length, int* data); +listnode* lnkdList_FromPtrArray(ssize_t length, void** data); #endif //HTTPSERVER_LINKEDLIST_H