Fix Linked Lists, Fix testing for linked lists
This commit is contained in:
parent
2880a91032
commit
bd242c933d
|
|
@ -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})
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
//
|
||||
// Created by franchioping on 6/26/24.
|
||||
//
|
||||
|
||||
#include "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
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
@ -5,7 +5,8 @@
|
|||
#include <check.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "linked_list.h"
|
||||
#include <mcheck.h>
|
||||
#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());
|
||||
|
|
|
|||
|
|
@ -3,36 +3,99 @@
|
|||
//
|
||||
|
||||
#include <malloc.h>
|
||||
#include <assert.h>
|
||||
#include <memory.h>
|
||||
#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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,15 +8,16 @@
|
|||
#include <sys/types.h>
|
||||
|
||||
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
|
||||
|
|
|
|||
Loading…
Reference in New Issue