From ae4e3d46ef5887fcafe0ec0d41aec5cb147bcf24 Mon Sep 17 00:00:00 2001 From: Alessandro Iezzi Date: Wed, 30 Nov 2022 22:07:48 +0100 Subject: Add test folder --- list.c | 104 ------------------------------------------------------------ list.h | 46 --------------------------- src/list.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/list.h | 46 +++++++++++++++++++++++++++ src/main.c | 45 -------------------------- test/main.c | 45 ++++++++++++++++++++++++++ 6 files changed, 195 insertions(+), 195 deletions(-) delete mode 100644 list.c delete mode 100644 list.h create mode 100644 src/list.c create mode 100644 src/list.h delete mode 100644 src/main.c create mode 100644 test/main.c diff --git a/list.c b/list.c deleted file mode 100644 index 3b3eb63..0000000 --- a/list.c +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (C) 2022 Alessandro Iezzi - * - * This file is part of clist. - * - * clist is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * clist is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with clist. If not, see . - */ - -#include -#include /* memcpy */ -#include "list.h" - -list_t clist_create() -{ - list_t list; - - list.first = NULL; - list.last = NULL; - list.size = 0; - - return list; -} - -void clist_add(list_t *list, - void *data, - int size_of) -{ - struct list_item_t *item = malloc(sizeof(struct list_item_t)); - - void *dest = malloc(size_of); /* allocates memory like data parameter */ - memcpy(dest, data, size_of); /* copies data inside dest */ - item->data = dest; - item->next = NULL; - - if (list->first == NULL) - list->first = item; - - if (list->last != NULL) - list->last->next = item; - - list->last = item; - list->size ++; -} - -void clist_add_all(list_t *dest, - list_t *other) -{ - dest->last->next = other->first; - dest->size += other->size; -} - -void clist_remove(list_t *list, - list_item_t *item) -{ - list_item_t *curr = list->first->next; - list_item_t *prev = list->first; - - if (list->first == item) { - free(list->first); - list->first = curr; - } else { - while (curr != NULL) { - if (curr == item) { - prev->next = curr->next; - free(curr); - return; - } - prev = curr; - curr = curr->next; - } - } -} - -iterator_t clist_iterator(list_t *list) -{ - struct iterator_t *iterator = malloc(sizeof(struct iterator_t)); - iterator->current = list->first; - - return *iterator; -} - -int iterator_has_next(iterator_t *iterator) -{ - return iterator->current != NULL; -} - -list_item_t *iterator_next(iterator_t *iterator) -{ - struct list_item_t *current = iterator->current; - iterator->current = iterator->current->next; - - return current; -} diff --git a/list.h b/list.h deleted file mode 100644 index dccfa62..0000000 --- a/list.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (C) 2022 Alessandro Iezzi - * - * This file is part of clist. - * - * clist is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * clist is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with clist. If not, see . - */ - -#ifndef _LIST_H_ -#define _LIST_H_ - -typedef struct list_item_t { - void *data; - struct list_item_t *next; -} list_item_t; - -typedef struct list_t { - struct list_item_t *first; - struct list_item_t *last; - int size; -} list_t; - -typedef struct iterator_t { - struct list_item_t *current; -} iterator_t; - -list_t clist_create (void); -void clist_add (list_t *, void *, int); -void clist_add_all (list_t *, list_t *); -void clist_remove (list_t *, list_item_t *); -iterator_t clist_iterator (list_t *); -int clist_iterator_has_next(iterator_t *); -list_item_t *iterator_next (iterator_t *); - -#endif diff --git a/src/list.c b/src/list.c new file mode 100644 index 0000000..3b3eb63 --- /dev/null +++ b/src/list.c @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2022 Alessandro Iezzi + * + * This file is part of clist. + * + * clist is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * clist is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with clist. If not, see . + */ + +#include +#include /* memcpy */ +#include "list.h" + +list_t clist_create() +{ + list_t list; + + list.first = NULL; + list.last = NULL; + list.size = 0; + + return list; +} + +void clist_add(list_t *list, + void *data, + int size_of) +{ + struct list_item_t *item = malloc(sizeof(struct list_item_t)); + + void *dest = malloc(size_of); /* allocates memory like data parameter */ + memcpy(dest, data, size_of); /* copies data inside dest */ + item->data = dest; + item->next = NULL; + + if (list->first == NULL) + list->first = item; + + if (list->last != NULL) + list->last->next = item; + + list->last = item; + list->size ++; +} + +void clist_add_all(list_t *dest, + list_t *other) +{ + dest->last->next = other->first; + dest->size += other->size; +} + +void clist_remove(list_t *list, + list_item_t *item) +{ + list_item_t *curr = list->first->next; + list_item_t *prev = list->first; + + if (list->first == item) { + free(list->first); + list->first = curr; + } else { + while (curr != NULL) { + if (curr == item) { + prev->next = curr->next; + free(curr); + return; + } + prev = curr; + curr = curr->next; + } + } +} + +iterator_t clist_iterator(list_t *list) +{ + struct iterator_t *iterator = malloc(sizeof(struct iterator_t)); + iterator->current = list->first; + + return *iterator; +} + +int iterator_has_next(iterator_t *iterator) +{ + return iterator->current != NULL; +} + +list_item_t *iterator_next(iterator_t *iterator) +{ + struct list_item_t *current = iterator->current; + iterator->current = iterator->current->next; + + return current; +} diff --git a/src/list.h b/src/list.h new file mode 100644 index 0000000..dccfa62 --- /dev/null +++ b/src/list.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2022 Alessandro Iezzi + * + * This file is part of clist. + * + * clist is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * clist is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with clist. If not, see . + */ + +#ifndef _LIST_H_ +#define _LIST_H_ + +typedef struct list_item_t { + void *data; + struct list_item_t *next; +} list_item_t; + +typedef struct list_t { + struct list_item_t *first; + struct list_item_t *last; + int size; +} list_t; + +typedef struct iterator_t { + struct list_item_t *current; +} iterator_t; + +list_t clist_create (void); +void clist_add (list_t *, void *, int); +void clist_add_all (list_t *, list_t *); +void clist_remove (list_t *, list_item_t *); +iterator_t clist_iterator (list_t *); +int clist_iterator_has_next(iterator_t *); +list_item_t *iterator_next (iterator_t *); + +#endif diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 9ad5067..0000000 --- a/src/main.c +++ /dev/null @@ -1,45 +0,0 @@ -#include -#include -#include -#include "list.h" - -int main(int argc, char** argv) -{ - int i; - list_item_t *current, *r; - char buffer[4]; - char *dest = malloc(sizeof(char) * 4); - - iterator_t it; - - list_t list1 = clist_create(); - list_t list2 = clist_create(); - - for (i = 0; i < 2; i++) { - itoa(i, buffer, 10); - strcpy(dest, "i"); - strcat(dest, buffer); - clist_add(&list1, dest, sizeof(int)); - } - - for (i = 0; i < 5; i++) { - itoa(i, buffer, 10); - strcpy(dest, "j"); - strcat(dest, buffer); - clist_add(&list2, dest, sizeof(int)); - if (i == 3) r = list2.last; - } - - clist_remove(&list2, r); - clist_add_all(&list1, &list2); - - printf("read from list with iterator:\n"); - - it = clist_iterator(&list1); - while(iterator_has_next(&it)) { - current = iterator_next(&it); - printf("%s\n", (char *) current->data); - } - - return 0; -} diff --git a/test/main.c b/test/main.c new file mode 100644 index 0000000..9ad5067 --- /dev/null +++ b/test/main.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include "list.h" + +int main(int argc, char** argv) +{ + int i; + list_item_t *current, *r; + char buffer[4]; + char *dest = malloc(sizeof(char) * 4); + + iterator_t it; + + list_t list1 = clist_create(); + list_t list2 = clist_create(); + + for (i = 0; i < 2; i++) { + itoa(i, buffer, 10); + strcpy(dest, "i"); + strcat(dest, buffer); + clist_add(&list1, dest, sizeof(int)); + } + + for (i = 0; i < 5; i++) { + itoa(i, buffer, 10); + strcpy(dest, "j"); + strcat(dest, buffer); + clist_add(&list2, dest, sizeof(int)); + if (i == 3) r = list2.last; + } + + clist_remove(&list2, r); + clist_add_all(&list1, &list2); + + printf("read from list with iterator:\n"); + + it = clist_iterator(&list1); + while(iterator_has_next(&it)) { + current = iterator_next(&it); + printf("%s\n", (char *) current->data); + } + + return 0; +} -- cgit v1.2.3