This commit is contained in:
Rens Pastoor
2025-05-27 22:41:46 +02:00
parent d141296aea
commit 11b391b8a1
416 changed files with 25232 additions and 0 deletions

View File

@@ -0,0 +1,278 @@
/* **********************************************
* generic linked list in plain c
* author: Freddy Hurkmans
* date: dec 20, 2014
* **********************************************/
/* ******************************************************************************
* This list is designed using object oriented ideas, but is implemented in c.
*
* The idea is: you construct a list for a certain data structure (say: mystruct):
* list_admin* mylist = construct_list(sizeof(mystruct))
* mylist contains private data for this list implementation. You need not worry
* about it, only give it as first parameter to each list method.
*
* This means you can have multiple lists at the same time, just make sure you
* give the right list_admin structure to the list function.
* When you're done with your list, just call the destructor: destruct_list(&mylist)
* The destructor automatically deletes remaining list elements and the list administration.
*
* As this list implementation keeps its own administration in list_admin, you need
* not worry about next pointers and such. Your structure doesn't even need pointers
* to itself, you only need to worry about data. A valid struct could thus be:
* typedef struct
* {
* int nr;
* char text[100];
* } mystruct;
*
* Adding data to the list is done using list_add_* methods, such as list_add_tail,
* which adds data to the end of the list:
* mystruct data = {10, "hello world!"};
* int result = list_add_tail(mylist, &data);
* You don't have to provide the size of your struct, you've already done that in
* the destructor. If result < 0 list_add_* has failed (either a parameter problem
* or malloc didn't give memory).
*
* You can get a pointer to your data using list_get_*, e.g. get the 2nd element:
* mystruct* dataptr = list_get_element(admin, 1);
* or get the last element:
* mystruct* dataptr = list_get_last(admin);
* If dataptr is not NULL it points to the correct data, else the operation failed.
*
* Searching for data in your list can be done with list_index_of*. list_index_of
* compares the data in each list item to the given data. If they are the same, it
* returns the index. If you want to search for an element with a certain value for
* nr or text (see mystruct) you can implement your own compare function and use
* list_index_of_special (check memcmp for required return values):
* int mycompare(void* p1, void* p2, size_t size)
* {
* // this example doesn't need size!
* mystruct* org = (mystruct*)p1;
* int* nr = (int*)p2;
* return org->nr - nr; // return 0 if they are the same
* }
* Say you want to search for an element that has nr 10:
* int nr = 10;
* int index = list_index_of_special(mylist, &nr, mycompare);
* As noted earlier: mycompare must have the same prototype as memcmp. In fact:
* list_index_of is a shortcut for list_index_of_special(mylist, data, memcmp).
*
* Finally you can delete items with list_delete_*. They should work rather
* straight forward. If they succeed they return 0. You don't have to delete
* all items before destructing your list.
*
* ******************************************************************************/
#include <string.h> /* for memcmp and memcpy */
#include "list.h"
static size_t data_size(const list_admin* admin);
static list_head* get_guaranteed_list_head_of_element_n(list_admin* admin, size_t index);
static list_head* get_list_head_of_element_n(list_admin* admin, size_t index);
static void remove_first_element(list_admin* admin);
static int remove_non_first_element(list_admin* admin, size_t index);
/* **********************************************
* Constructor / destructor
* **********************************************/
list_admin* construct_list(size_t element_size)
{
list_admin* admin = malloc(sizeof(*admin));
if (admin != NULL)
{
admin->head = NULL;
admin->element_size = element_size;
admin->nr_elements = 0;
}
return admin;
}
void destruct_list(list_admin** admin)
{
if ((admin != NULL) && (*admin != NULL))
{
while ((*admin)->head != NULL)
{
list_head* temp = (*admin)->head;
(*admin)->head = (*admin)->head->next;
free(temp);
temp = NULL;
}
free(*admin);
*admin = NULL;
}
}
/* **********************************************
* Public functions
* **********************************************/
int list_get_nr_elements(list_admin* admin)
{
int nr_elements = -1;
if (admin != NULL)
{
nr_elements = admin->nr_elements;
}
return nr_elements;
}
int list_add_tail(list_admin* admin, const void* data)
{
int result = -1;
if ((admin != NULL) && (data != NULL))
{
list_head* new = malloc(data_size(admin));
if (new != NULL)
{
result = 0;
new->next = NULL;
memcpy(new+1, data, admin->element_size);
if (admin->head == NULL)
{
admin->head = new;
}
else
{
list_head* temp = get_guaranteed_list_head_of_element_n(admin, admin->nr_elements-1);
temp->next = new;
}
admin->nr_elements++;
}
}
return result;
}
void* list_get_element(list_admin* admin, size_t index)
{
list_head* item = get_list_head_of_element_n(admin, index);
if(item != NULL)
{
item++; // user data is just beyond list_head, thus we must increase the pointer
}
return item;
}
void* list_get_last(list_admin* admin)
{
list_head* item = NULL;
if (admin != NULL)
{
item = list_get_element(admin, admin->nr_elements-1);
}
return item;
}
int list_index_of(list_admin* admin, const void* data)
{
return list_index_of_special(admin, data, memcmp);
}
int list_index_of_special(list_admin* admin, const void* data, CompareFuncPtr compare)
{
int index = -1;
if ((admin != NULL) && (data != NULL) && (compare != NULL))
{
list_head* temp = admin->head;
index = 0;
while ((temp != NULL) && (compare(temp+1, data, admin->element_size) != 0))
{
temp = temp->next;
index++;
}
if (temp == NULL)
{
index = -1;
}
}
return index;
}
int list_delete_item(list_admin* admin, size_t index)
{
int result = -1;
if ((admin != NULL) && (admin->head != NULL) && (index < admin->nr_elements))
{
if (index == 0)
{
result = 0;
remove_first_element(admin);
}
else
{
result = remove_non_first_element(admin, index);
}
}
return result;
}
int list_delete_last(list_admin* admin)
{
int result = -1;
if (admin != NULL)
{
result = list_delete_item(admin, admin->nr_elements - 1);
}
return result;
}
/* **********************************************
* Private functions
* **********************************************/
static size_t data_size(const list_admin* admin)
{
return admin->element_size + sizeof(list_head);
}
static list_head* get_guaranteed_list_head_of_element_n(list_admin* admin, size_t index)
{
list_head* item = admin->head;
for (size_t i = 0; (i < index) && (item != NULL); i++)
{
item = item->next;
}
return item;
}
static list_head* get_list_head_of_element_n(list_admin* admin, size_t index)
{
list_head* item = NULL;
if ((admin != NULL) && (index < admin->nr_elements))
{
item = get_guaranteed_list_head_of_element_n(admin, index);
}
return item;
}
static void remove_first_element(list_admin* admin)
{
list_head* temp = admin->head;
admin->head = admin->head->next;
free(temp);
temp = NULL;
admin->nr_elements--;
}
static int remove_non_first_element(list_admin* admin, size_t index)
{
int result = -1;
if (index > 0)
{
list_head* temp = get_list_head_of_element_n(admin, index-1);
if ((temp != NULL) && (temp->next != NULL)) // to remove element n, element n-1 and n must exist
{
result = 0;
list_head* to_delete = temp->next;
temp->next = to_delete->next;
free(to_delete);
to_delete = NULL;
admin->nr_elements--;
}
}
return result;
}

View File

@@ -0,0 +1,52 @@
#pragma once
#include <stdlib.h>
typedef struct list_head
{
struct list_head* next;
} list_head;
typedef struct
{
struct list_head* head;
size_t element_size;
size_t nr_elements;
} list_admin;
typedef int (*CompareFuncPtr)(const void*, const void*, size_t);
// call the constructor before using the list:
// list_admin* mylist = construct_list(sizeof(mystruct));
// if mylist equals NULL, no memory could be allocated. Please don't
// mess with the admin data, this can corrupt your data.
list_admin* construct_list(size_t element_size);
// call the destructor when you're done, pass the list administration
// as referenced parameter (the variable will be set to NULL).
void destruct_list(list_admin** admin);
// Reads the number of elements in your list. Returns -1 in case of
// errors, else the number of elements.
int list_get_nr_elements(list_admin* admin);
// Add data to the end of the list. Returns -1 in case of errors, else 0
int list_add_tail(list_admin* admin, const void* data);
// Returns a pointer to the requested element. Returns NULL in case of
// errors (such as: the element does not exist).
void* list_get_element(list_admin* admin, size_t index);
void* list_get_last(list_admin* admin);
// Returns the index to the first found list element that's equal to data,
// or -1 in case of errors (such as: not found).
int list_index_of(list_admin* admin, const void* data);
// Returns the index to the first found list element for which cmp says it's,
// equal to data, or -1 in case of errors (such as: not found). cmp must behave
// just like memcmp, i.e.: return 0 when the data matches.
int list_index_of_special(list_admin* admin, const void* data, CompareFuncPtr cmp);
// Delete an item in the list. Returns -1 in case of errors, else 0.
int list_delete_item(list_admin* admin, size_t index);
int list_delete_last(list_admin* admin);

View File

@@ -0,0 +1,270 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
//#include <malloc.h>
#include <string.h>
#include "resource_detector.h"
#undef malloc
#undef free
#undef main
#undef fopen
#undef fclose
#include "list.h"
#define MAGIC_CODE 0x78563412
#define ADDR(addr,offset) ((addr) + (offset))
#define SET_MAGIC_CODE(addr,offset) *((int*) ADDR(addr,offset)) = MAGIC_CODE
#define MAGIC_CODE_OK(addr,offset) (*((int*) ADDR(addr,offset)) == MAGIC_CODE)
typedef struct
{
char* addr;
unsigned int size;
const char* file;
unsigned int line;
} mem_data;
typedef struct
{
FILE* addr;
const char* file_to_open;
const char* mode;
const char* file;
unsigned int line;
} file_data;
static list_admin* memlist = NULL;
static list_admin* filelist = NULL;
static int memory_compare(const void* meminfo, const void* address, size_t size)
{
mem_data* info = (mem_data*)meminfo;
char* addr = (char*)address;
return info->addr - addr;
}
static int file_compare(const void* fileinfo, const void* address, size_t size)
{
file_data* info = (file_data*)fileinfo;
FILE* addr = (FILE*)address;
return info->addr - addr;
}
static void
invalidate (mem_data* info)
{
char* user_addr;
char* raw_addr;
unsigned int size;
user_addr = info->addr;
size = info->size;
raw_addr = ADDR (user_addr, -sizeof(int));
if (!MAGIC_CODE_OK(user_addr, -sizeof(int)) || !MAGIC_CODE_OK(user_addr, size))
{
fprintf (stderr, "ERROR: addr %p (%s, line %d): out-of-bound access\n", (void*)user_addr, info->file, info->line);
}
memset (raw_addr, 0xff, size + (2 * sizeof(int)));
free (raw_addr);
}
/*
* replacement of malloc
*/
extern void*
xmalloc (unsigned int size, const char* file, unsigned int line)
{
char* raw_addr = malloc (size + (2 * sizeof(int)));
char* user_addr;
mem_data info = {NULL, 0, NULL, 0};
if (raw_addr == NULL)
{
fprintf (stderr, "ERROR: malloc failed for %d bytes (%s, line %d)\n", size, file, line);
return (NULL);
}
else
{
user_addr = ADDR (raw_addr, sizeof (int));
SET_MAGIC_CODE (raw_addr, 0);
SET_MAGIC_CODE (user_addr, size);
info.addr = user_addr;
info.size = size;
info.file = file;
info.line = line;
list_add_tail(memlist, &info);
}
return ((void*) user_addr);
}
/*
* replacement of free
*/
extern void
xfree (void* addr, const char* filename, int linenr)
{
char* user_addr = (char*) addr;
mem_data* info = NULL;
/* check if allocated memory is in our list and retrieve its info */
int index = list_index_of_special(memlist, addr, memory_compare);
info = list_get_element(memlist, index);
if (info == NULL)
{
fprintf (stderr, "ERROR: trying to free memory that was not malloc-ed (addr %p) in %s : %d\n", (void*)user_addr, filename, linenr);
return;
}
invalidate (info);
list_delete_item(memlist, index);
}
static void
print_empty_lines(int n)
{
int i;
for (i = 0; i < n; i++)
{
fprintf(stderr, "\n");
}
}
static void set_colour_red(void)
{
fprintf(stderr, "\033[10;31m");
}
static void set_colour_gray(void)
{
fprintf(stderr, "\033[22;37m");
}
static void
print_line(void)
{
fprintf (stderr, "---------------------------------------------------------\n");
}
static void
print_not_good(void)
{
set_colour_gray();
print_line();
set_colour_red();
fprintf (stderr, " # # ###\n");
fprintf (stderr, " ## # #### ##### #### #### #### ##### ###\n");
fprintf (stderr, " # # # # # # # # # # # # # # ###\n");
fprintf (stderr, " # # # # # # # # # # # # # # \n");
fprintf (stderr, " # # # # # # # ### # # # # # # \n");
fprintf (stderr, " # ## # # # # # # # # # # # ###\n");
fprintf (stderr, " # # #### # #### #### #### ##### ###\n");
set_colour_gray();
print_line();
}
/*
* writes all info of the unallocated memory
*/
static void
resource_detection (void)
{
time_t now = time (NULL);
int forgotten_frees = list_get_nr_elements(memlist);
int nr_files_open = list_get_nr_elements(filelist);
if ((forgotten_frees > 0) || (nr_files_open > 0))
{
print_empty_lines(15);
}
if (forgotten_frees > 0)
{
mem_data* info = NULL;
print_line();
fprintf (stderr, "Memory Leak Summary, generated: %s", ctime (&now));
print_not_good();
set_colour_red();
while((info = list_get_element(memlist, 0)) != NULL)
{
fprintf (stderr, "forgot to free address: %p (%d bytes) that was allocated in: %s on line: %d\n",
(void*)info->addr, info->size, info->file, info->line);
list_delete_item(memlist, 0);
}
set_colour_gray();
print_line();
print_empty_lines(1);
}
if (nr_files_open > 0)
{
file_data* info = NULL;
print_line();
fprintf (stderr, "File Management Summary, generated: %s", ctime (&now));
print_not_good();
set_colour_red();
while((info = list_get_element(filelist, 0)) != NULL)
{
fprintf (stderr, "forgot to close file: %s (ptr %p, mode \"%s\") that was opened from: %s on line: %d\n",
info->file_to_open, (void*)(info->addr), info->mode, info->file, info->line);
list_delete_item(filelist, 0);
}
set_colour_gray();
print_line();
print_empty_lines(1);
}
if ((forgotten_frees == 0) && (nr_files_open == 0))
{
printf ("\nResource checks: OK\n\n");
}
destruct_list(&memlist);
destruct_list(&filelist);
}
extern FILE*
xfopen (const char* file_to_open, const char* mode, const char* filename, int linenr)
{
file_data info = {0};
info.addr = fopen(file_to_open, mode);
if (info.addr != NULL)
{
info.file_to_open = file_to_open;
info.mode = mode;
info.file = filename;
info.line = linenr;
list_add_tail(filelist, &info);
}
return info.addr;
}
extern int
xfclose(FILE* fptr, const char* filename, int linenr)
{
int index = list_index_of_special(filelist, fptr, file_compare);
if (index < 0)
{
fprintf (stderr, "ERROR: trying to close an unopened file in %s on line number %d.\n", filename, linenr);
return -1;
}
list_delete_item(filelist, index);
return (fclose (fptr));
}
extern int
main (int argc, char* argv[])
{
atexit (resource_detection);
memlist = construct_list(sizeof(mem_data));
filelist = construct_list(sizeof(file_data));
return (xmain (argc, argv));
}

View File

@@ -0,0 +1,18 @@
#ifndef RESOURCE_DETECTOR_H
#define RESOURCE_DETECTOR_H
#include <stdio.h>
#define main xmain
#define malloc(size) xmalloc ((size), (__FILE__), (__LINE__))
#define free(addr) xfree ((addr), __FILE__, __LINE__)
#define fopen(name,mode) xfopen ((name),(mode), __FILE__, __LINE__)
#define fclose(fptr) xfclose ((fptr), __FILE__, __LINE__)
extern int xmain(int argc, char* argv[]);
extern void* xmalloc(unsigned int size, const char* file, unsigned int line);
extern void xfree(void* addr, const char* filename, int linenr);
extern FILE* xfopen(const char* file_to_open, const char* mode, const char* filename, int linenr);
extern int xfclose(FILE* fptr, const char* filename, int linenr);
#endif

View File

@@ -0,0 +1,28 @@
UNITY_FOLDER=../Unity
TEST_INC_DIRS=$(INC_DIRS) -I$(UNITY_FOLDER)
TEST_FILES=$(UNITY_FOLDER)/unity.c \
list_test.c \
list.c
HEADER_FILES=*.h
TEST = list_test
CC=gcc
SYMBOLS=-Wall -Werror -pedantic -O0 -ggdb -std=c99
TEST_SYMBOLS=$(SYMBOLS) -DTEST
.PHONY: clean test
all: $(TEST)
$(TEST): Makefile $(TEST_FILES) $(HEADER_FILES)
$(CC) $(TEST_INC_DIRS) $(TEST_SYMBOLS) $(TEST_FILES) -o $(TEST)
clean:
rm -f list $(TEST)
test: $(TEST)
@valgrind ./$(TEST)

View File

@@ -0,0 +1,300 @@
#include <string.h> /* for memcmp */
#include "list.h"
#include "unity.h"
// I rather dislike keeping line numbers updated, so I made my own macro to ditch the line number
#define MY_RUN_TEST(func) RUN_TEST(func, 0)
static list_admin* mylist = NULL;
typedef struct
{
int i;
int j;
} test_struct;
// compare function that returns 0 if test_struct.i matches, it ignores .j
static int compare_test_func(const void* p1, const void* p2, size_t size)
{
size = size; // to prevent warnings
const test_struct* data1 = (const test_struct*)p1;
const test_struct* data2 = (const test_struct*)p2;
return data1->i - data2->i;
}
// check n items in list against verify_data
// IMPORTANT: this function must check using list_get_element,
// else the test for that function is no longer useful
static void check_n_list_elements_ok(list_admin* admin, int nr_items, const test_struct* verify_data)
{
for(int i = 0; i < nr_items; i++)
{
test_struct* ptr = list_get_element(admin, i);
TEST_ASSERT_NOT_NULL(ptr);
TEST_ASSERT_EQUAL(0, memcmp(verify_data+i, ptr, sizeof(*ptr)));
}
}
void setUp(void)
{
// This is run before EACH test
mylist = construct_list(sizeof(test_struct));
TEST_ASSERT_NOT_NULL(mylist);
}
void tearDown(void)
{
// This is run after EACH test
destruct_list(&mylist);
TEST_ASSERT_NULL(mylist);
}
static void test_ConstructList(void)
{
TEST_ASSERT_NULL(mylist->head);
TEST_ASSERT_EQUAL(sizeof(test_struct), mylist->element_size);
TEST_ASSERT_EQUAL(0, mylist->nr_elements);
}
static void test_NrElements_parameters(void)
{
TEST_ASSERT_EQUAL(-1, list_get_nr_elements(NULL));
}
static void test_AddData_parameters(void)
{
TEST_ASSERT_EQUAL(-1, list_add_tail(mylist, NULL));
TEST_ASSERT_EQUAL(-1, list_add_tail(NULL, mylist));
}
static void test_AddData(void)
{
test_struct data = {3, 4};
TEST_ASSERT_EQUAL(0, list_add_tail(mylist, &data));
TEST_ASSERT_NOT_NULL(mylist->head);
TEST_ASSERT_EQUAL(1, list_get_nr_elements(mylist));
list_head* head = mylist->head;
test_struct* element = (test_struct*)(head+1);
TEST_ASSERT_EQUAL(data.i, element->i);
TEST_ASSERT_EQUAL(data.j, element->j);
}
static void test_Add2PiecesOfData(void)
{
test_struct data1 = {8, 9};
test_struct data2 = {-3, -4};
TEST_ASSERT_EQUAL(0, list_add_tail(mylist, &data1));
TEST_ASSERT_EQUAL(0, list_add_tail(mylist, &data2));
TEST_ASSERT_NOT_NULL(mylist->head);
TEST_ASSERT_NOT_NULL(mylist->head->next);
TEST_ASSERT_EQUAL(2, list_get_nr_elements(mylist));
list_head* head = mylist->head->next;
test_struct* element = (test_struct*)(head+1);
TEST_ASSERT_EQUAL(data2.i, element->i);
TEST_ASSERT_EQUAL(data2.j, element->j);
}
static void test_GetElement_parameters(void)
{
TEST_ASSERT_NULL(list_get_element(NULL, 0));
}
static void test_GetElement(void)
{
const test_struct data[] = {{8, 9}, {-3, -4}, {21, 56}, {0, 0}};
const int nr_elements = sizeof(data)/sizeof(data[0]);
for(int i = 0; i < nr_elements; i++)
{
TEST_ASSERT_EQUAL(0, list_add_tail(mylist, &(data[i])));
}
check_n_list_elements_ok(mylist, nr_elements, data); // checks using list_get_element
TEST_ASSERT_NULL(list_get_element(mylist, nr_elements));
TEST_ASSERT_NULL(list_get_element(mylist, -1));
}
static void test_GetLast_parameters(void)
{
TEST_ASSERT_NULL(list_get_last(NULL));
}
static void test_GetLast(void)
{
const test_struct data[] = {{8, 9}, {-3, -4}, {21, 56}, {0, 0}};
const int nr_elements = sizeof(data)/sizeof(data[0]);
TEST_ASSERT_NULL(list_get_last(mylist));
for(int i = 0; i < nr_elements; i++)
{
TEST_ASSERT_EQUAL(0, list_add_tail(mylist, &(data[i])));
test_struct* ptr = list_get_last(mylist);
TEST_ASSERT_NOT_NULL(ptr);
TEST_ASSERT_EQUAL(0, memcmp(&(data[i]), ptr, sizeof(*ptr)));
}
}
static void test_IndexOf_parameters(void)
{
TEST_ASSERT_EQUAL(-1, list_index_of(mylist, NULL));
TEST_ASSERT_EQUAL(-1, list_index_of(NULL, mylist));
}
static void test_IndexOf(void)
{
const test_struct real_data[] = {{8, 9}, {-3, -4}, {21, 56}};
const int nr_real_elements = sizeof(real_data)/sizeof(real_data[0]);
const test_struct false_data[] = {{-3, 9}, {8, 56}, {21, -4}, {0, 0}};
const int nr_false_elements = sizeof(false_data)/sizeof(false_data[0]);
for(int i = 0; i < nr_real_elements; i++)
{
TEST_ASSERT_EQUAL(0, list_add_tail(mylist, &(real_data[i])));
}
for(int i = 0; i < nr_real_elements; i++)
{
TEST_ASSERT_EQUAL(i, list_index_of(mylist, &(real_data[i])));
}
for(int i = 0; i < nr_false_elements; i++)
{
TEST_ASSERT_EQUAL(-1, list_index_of(mylist, &(false_data[i])));
}
}
static void test_IndexOfSpecial_parameters(void)
{
TEST_ASSERT_EQUAL(-1, list_index_of_special(mylist, NULL, compare_test_func));
TEST_ASSERT_EQUAL(-1, list_index_of_special(NULL, mylist, compare_test_func));
TEST_ASSERT_EQUAL(-1, list_index_of_special(mylist, mylist, NULL));
}
static void test_IndexOfSpecial(void)
{
const test_struct data[] = {{8, 9}, {-3, -4}, {21, 56}}; // data in list
const test_struct real_data[] = {{8, -4}, {-3, 56}, {21, 9}}; // data to search for (i equals)
const int nr_real_elements = sizeof(real_data)/sizeof(real_data[0]);
const test_struct false_data[] = {{-2, 9}, {9, -4}, {22, 56}, {0, 0}}; // data that doesn't exist
const int nr_false_elements = sizeof(false_data)/sizeof(false_data[0]);
// first make sure our compare function works
for(int i = 0; i < nr_real_elements; i++)
{
TEST_ASSERT_EQUAL(0, compare_test_func(&(data[i]), &(real_data[i]), 0));
for (int j = 0; j < nr_false_elements; j++)
{
TEST_ASSERT_NOT_EQUAL(0, compare_test_func(&(data[i]), &(false_data[j]), 0))
}
}
for(int i = 0; i < nr_real_elements; i++)
{
TEST_ASSERT_EQUAL(0, list_add_tail(mylist, &(data[i])));
}
for(int i = 0; i < nr_real_elements; i++)
{
TEST_ASSERT_EQUAL(i, list_index_of_special(mylist, &(real_data[i]), compare_test_func));
}
for(int i = 0; i < nr_false_elements; i++)
{
TEST_ASSERT_EQUAL(-1, list_index_of_special(mylist, &(false_data[i]), compare_test_func));
}
}
static void test_DeleteItem_parameters(void)
{
TEST_ASSERT_EQUAL(-1, list_delete_item(NULL, 0));
TEST_ASSERT_EQUAL(-1, list_delete_item(mylist, -1));
}
static void test_DeleteItem(void)
{
const test_struct data[] = {{8, 9}, {-3, -4}, {21, 56}, {0, 0}, {12, 12345}};
int nr_elements = sizeof(data)/sizeof(data[0]);
const test_struct data_noFirst[] = {{-3, -4}, {21, 56}, {0, 0}, {12, 12345}};
const test_struct data_noLast[] = {{-3, -4}, {21, 56}, {0, 0}};
const test_struct data_noMiddle[] = {{-3, -4}, {0, 0}};
TEST_ASSERT_EQUAL(-1, list_delete_item(mylist, 0));
for(int i = 0; i < nr_elements; i++)
{
TEST_ASSERT_EQUAL(0, list_add_tail(mylist, &(data[i])));
}
TEST_ASSERT_EQUAL(0, list_delete_item(mylist, 0));
nr_elements--;
TEST_ASSERT_EQUAL(nr_elements, list_get_nr_elements(mylist));
check_n_list_elements_ok(mylist, nr_elements, data_noFirst);
TEST_ASSERT_EQUAL(0, list_delete_item(mylist, nr_elements-1));
nr_elements--;
TEST_ASSERT_EQUAL(nr_elements, list_get_nr_elements(mylist));
check_n_list_elements_ok(mylist, nr_elements, data_noLast);
TEST_ASSERT_EQUAL(0, list_delete_item(mylist, 1));
nr_elements--;
TEST_ASSERT_EQUAL(nr_elements, list_get_nr_elements(mylist));
check_n_list_elements_ok(mylist, nr_elements, data_noMiddle);
}
static void test_DeleteLast_parameters(void)
{
TEST_ASSERT_EQUAL(-1, list_delete_last(NULL));
}
static void test_DeleteLast(void)
{
const test_struct data[] = {{8, 9}, {-3, -4}, {21, 56}, {0, 0}, {12, 12345}};
int nr_elements = sizeof(data)/sizeof(data[0]);
TEST_ASSERT_EQUAL(-1, list_delete_last(mylist));
for(int i = 0; i < nr_elements; i++)
{
TEST_ASSERT_EQUAL(0, list_add_tail(mylist, &(data[i])));
}
for (int i = nr_elements-1; i >= 0; i--)
{
TEST_ASSERT_EQUAL(0, list_delete_last(mylist));
TEST_ASSERT_EQUAL(i, list_get_nr_elements(mylist));
check_n_list_elements_ok(mylist, i, data);
}
TEST_ASSERT_NULL(mylist->head);
TEST_ASSERT_EQUAL(0, list_get_nr_elements(mylist));
}
int main(void)
{
UnityBegin();
MY_RUN_TEST(test_ConstructList);
MY_RUN_TEST(test_NrElements_parameters);
MY_RUN_TEST(test_AddData_parameters);
MY_RUN_TEST(test_AddData);
MY_RUN_TEST(test_Add2PiecesOfData);
MY_RUN_TEST(test_GetElement_parameters);
MY_RUN_TEST(test_GetElement);
MY_RUN_TEST(test_GetLast_parameters);
MY_RUN_TEST(test_GetLast);
MY_RUN_TEST(test_IndexOf_parameters);
MY_RUN_TEST(test_IndexOf);
MY_RUN_TEST(test_IndexOfSpecial_parameters);
MY_RUN_TEST(test_IndexOfSpecial);
MY_RUN_TEST(test_DeleteItem_parameters);
MY_RUN_TEST(test_DeleteItem);
MY_RUN_TEST(test_DeleteLast_parameters);
MY_RUN_TEST(test_DeleteLast);
// MY_RUN_TEST();
// MY_RUN_TEST();
// MY_RUN_TEST();
// MY_RUN_TEST();
return UnityEnd();
}