Fix Linked Lists, Fix testing for linked lists

This commit is contained in:
franchioping 2024-06-27 00:37:56 +01:00
parent 2880a91032
commit bd242c933d
10 changed files with 207 additions and 115 deletions

View File

@ -13,13 +13,17 @@ set(COMMON_SOURCES src/request/handle_client.c
src/request/headers.c src/request/headers.c
src/request/headers.h src/request/headers.h
src/util/linkedlist/linkedlist.c 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 set(TEST_SOURCES src/test/test_main.c
src/test/linked_list.c src/test/test_linked_list.c
src/test/linked_list.h) src/test/test_linked_list.h)
add_executable(HttpServer ${COMMON_SOURCES} ${TARGET_SOURCES}) add_executable(HttpServer ${COMMON_SOURCES} ${TARGET_SOURCES})

5
src/http/message.c Normal file
View File

@ -0,0 +1,5 @@
//
// Created by franchioping on 6/26/24.
//
#include "message.h"

15
src/http/message.h Normal file
View File

@ -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

View File

@ -1,75 +0,0 @@
//
// Created by franchioping on 6/25/24.
//
#include "linked_list.h"
#include <check.h>
#include <malloc.h>
#include "src/util/linkedlist/linkedlist.h"
#include <mcheck.h>
#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;
}

View File

@ -1,12 +0,0 @@
//
// Created by franchioping on 6/25/24.
//
#ifndef HTTPSERVER_LINKED_LIST_H
#define HTTPSERVER_LINKED_LIST_H
#include <check.h>
Suite *linked_list_suite(void);
#endif //HTTPSERVER_LINKED_LIST_H

View File

@ -0,0 +1,75 @@
//
// Created by franchioping on 6/25/24.
//
#include "test_linked_list.h"
#include <check.h>
#include <malloc.h>
#include "src/util/linkedlist/linkedlist.h"
#include <mcheck.h>
#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;
}

View File

@ -0,0 +1,12 @@
//
// Created by franchioping on 6/25/24.
//
#ifndef HTTPSERVER_TEST_LINKED_LIST_H
#define HTTPSERVER_TEST_LINKED_LIST_H
#include <check.h>
Suite *linked_list_suite(void);
#endif //HTTPSERVER_TEST_LINKED_LIST_H

View File

@ -5,7 +5,8 @@
#include <check.h> #include <check.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include "linked_list.h" #include <mcheck.h>
#include "test_linked_list.h"
int run_suite(Suite* suite){ int run_suite(Suite* suite){
@ -17,7 +18,10 @@ int run_suite(Suite* suite){
return no_failed; return no_failed;
} }
void no_op(enum mcheck_status status) {}
int main(void) { int main(void) {
mcheck(&no_op);
int no_failed = 0; int no_failed = 0;
no_failed += run_suite(linked_list_suite()); no_failed += run_suite(linked_list_suite());

View File

@ -3,36 +3,99 @@
// //
#include <malloc.h> #include <malloc.h>
#include <assert.h>
#include <memory.h>
#include "linkedlist.h" #include "linkedlist.h"
listnode* lnkdList_NewNode(){
listnode* list_get_at_index(listnode* list_start, ssize_t index){ listnode* node = (listnode*) malloc(sizeof(listnode));
listnode* node = list_start; memset(node, 0, sizeof(listnode));
for(int i = 0; i < index; i++){
node = node->next_node;
}
return node; return node;
} }
ssize_t list_get_length(listnode* list_start){
ssize_t length = 1; listnode* lnkdList_FromPtrArray(const ssize_t length, void** data){
while(list_start->next_node){
length++; listnode *start = lnkdList_NewNode();
list_start = list_start->next_node; 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){ listnode* lnkdList_FromIntArray(const ssize_t length, int* data){
if(list_start == NULL)
return; listnode *start = lnkdList_NewNode();
listnode* head = list_start; start->data = &data[0];
listnode * tmp;
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) while (head != NULL)
{ {
tmp = head; tmp = head;
head = head->next_node; head = head->next;
free(tmp); free(tmp);
} }
} }

View File

@ -8,15 +8,16 @@
#include <sys/types.h> #include <sys/types.h>
typedef struct listnode { typedef struct listnode {
struct listnode* next_node; struct listnode* next;
void* data; void* data;
} listnode; } listnode;
listnode* list_get_at_index(listnode* list_start, ssize_t index); listnode* lnkdList_GetNth(listnode* head, ssize_t index);
ssize_t list_get_length(listnode* list_start); ssize_t lnkdList_GetLength(listnode* head);
void list_free(listnode* list_start); 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 #endif //HTTPSERVER_LINKEDLIST_H