From a748a29c6ba4b5531a06d31485d37ca5fe6e07a7 Mon Sep 17 00:00:00 2001 From: DURAND Malo Date: Fri, 22 Mar 2024 02:09:53 +0100 Subject: [PATCH] adding the array_to_list function, fixing indexes from int to long --- .gitignore | 1 + .vscode/settings.json | 16 ---------------- include/list.h | 32 +++++++++++++++++++++---------- src/array_to_list.c | 25 ++++++++++++++++++++++++ src/list_count.c | 2 +- src/list_index_of.c | 4 ++-- src/list_insert_at.c | 4 ++-- src/list_remove_at.c | 2 +- src/list_slice.c | 14 +++++++------- src/list_sort.c | 2 -- src/list_value_at.c | 2 +- tests/tests_array_to_list.c | 36 +++++++++++++++++++++++++++++++++++ tests/tests_list_index_of.c | 10 +++++----- tests/tests_list_reduce.c | 4 ++-- tests/tests_pop_and_remove.c | 12 ++++++------ tests/tests_push_and_insert.c | 4 ++-- 16 files changed, 113 insertions(+), 57 deletions(-) delete mode 100644 .vscode/settings.json create mode 100644 src/array_to_list.c create mode 100644 tests/tests_array_to_list.c diff --git a/.gitignore b/.gitignore index 9383250..989d714 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ a.out *.log unit_tests liblist.so +.vscode diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 6c33b84..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "files.associations": { - "*.ejs": "html", - "stdlib.h": "c", - "list.h": "c", - "__split_buffer": "c", - "list": "c", - "string": "c", - "vector": "c", - "assert.h": "c", - "_types.h": "c", - "unordered_map": "c", - "stdio.h": "c", - "criterion.h": "c" - } -} \ No newline at end of file diff --git a/include/list.h b/include/list.h index 6166c6d..5c5606d 100644 --- a/include/list.h +++ b/include/list.h @@ -24,7 +24,7 @@ typedef struct s_list_elem { } list_elem_t; typedef struct s_list { - int count; + long count; list_elem_destroy_t destroy; list_elem_t *elems_head; list_elem_t *elems_tail; @@ -36,6 +36,17 @@ list_t *list_new(list_elem_destroy_t destroy); void list_destroy(list_t *list); +/** + * @brief Transforms a generic array to a list + * + * @param[in] array + * @param[in] size The size of the array + * @param[in] destroy [OPT] The destructor for array's elem (may be NULL) + * @return The new list on success, NULL on error + */ +list_t *array_to_list(void *const *const array, long size, + list_elem_destroy_t destroy); + /* Getters */ /** @@ -43,7 +54,7 @@ void list_destroy(list_t *list); * * @param[in] list */ -int list_count(list_t const *list); +long list_count(list_t const *list); /** * @brief This function type will be used to compare two elements @@ -63,7 +74,7 @@ typedef int (*list_equal_cmp_t)(void const *expected, void const *elem); * @param[in] elem * @param[in] cmp the function to compare elements with */ -int list_index_of(list_t const *list, void const *elem, list_equal_cmp_t cmp); +long list_index_of(list_t const *list, void const *elem, list_equal_cmp_t cmp); /** * @brief Returns the element for the given index @@ -72,7 +83,7 @@ int list_index_of(list_t const *list, void const *elem, list_equal_cmp_t cmp); * @param[in] index * @return the element on success, NULL on error (Out-Of-Range) */ -void *list_value_at(list_t const *list, int index); +void *list_value_at(list_t const *list, long index); /* Operators */ @@ -109,9 +120,10 @@ int list_push_front(list_t *list, void *elem); * * @param[in] list * @param[in] elem + * @param[in] index * @return 0 on success, -1 on error (Out-Of-Range) */ -int list_insert_at(list_t *list, void *elem, int index); +int list_insert_at(list_t *list, void *elem, long index); /** * @brief Pushes an element at the back of the list @@ -146,7 +158,7 @@ void *list_pop_front(list_t *list); * @param[in] elem * @return the pop'd element if any, NULL otherwise */ -void *list_remove_at(list_t *list, int index); +void *list_remove_at(list_t *list, long index); /** * @brief Pops an element from the back of the list @@ -201,7 +213,7 @@ int list_reverse_into(list_t const *list, list_t *output); * @param[in] from * @param[in] to (excluded) */ -list_t *list_slice(list_t const *list, int from, int to); +list_t *list_slice(list_t const *list, long from, long to); /** * @brief Slices the current list itself, no allocation of any kind. All @@ -213,7 +225,7 @@ list_t *list_slice(list_t const *list, int from, int to); * @param[in] to (excluded) * @return 0 on success, -1 otherwise, the list is unchanged (Out-Of-Range) */ -int list_slice_itself(list_t *list, int from, int to); +int list_slice_itself(list_t *list, long from, long to); /** * @brief Slices a list from 'from' to 'to' (excluded) into the output list @@ -224,7 +236,7 @@ int list_slice_itself(list_t *list, int from, int to); * @param[out] output * @return 0 on success, -1 otherwise (Out-Of-Range) */ -int list_slice_into(list_t const *list, int from, int to, +int list_slice_into(list_t const *list, long from, long to, list_t *output); /** @@ -360,7 +372,7 @@ void list_filter_into(list_t const *list, list_filter_t filter, * @param[in] list the list that holds the 'elem' at 'index' * @return The new accumulator / last value to store */ -typedef void *(*list_reduce_t)(void const *acc, void const *elem, int index, +typedef void *(*list_reduce_t)(void const *acc, void const *elem, long index, list_t const *list); /** diff --git a/src/array_to_list.c b/src/array_to_list.c new file mode 100644 index 0000000..ceacc00 --- /dev/null +++ b/src/array_to_list.c @@ -0,0 +1,25 @@ +/* +** EPITECH PROJECT, 2024 +** List in C +** File description: +** array_to_list.c +*/ + +#include "list.h" + +list_t *array_to_list(void *const *const array, long size, + list_elem_destroy_t destroy) +{ + list_t *list = NULL; + + if (0 >= size) + return NULL; + list = list_new(destroy); + if (NULL == list) + return NULL; + do { + --size; + list_push_front(list, array[size]); + } while (0 < size); + return list; +} diff --git a/src/list_count.c b/src/list_count.c index 651d01e..139ece6 100644 --- a/src/list_count.c +++ b/src/list_count.c @@ -7,7 +7,7 @@ #include "list.h" -int list_count(list_t const *list) +long list_count(list_t const *list) { return list->count; } diff --git a/src/list_index_of.c b/src/list_index_of.c index 538cfad..3b07745 100644 --- a/src/list_index_of.c +++ b/src/list_index_of.c @@ -7,9 +7,9 @@ #include "list.h" -int list_index_of(list_t const *list, void const *elem, list_equal_cmp_t cmp) +long list_index_of(list_t const *list, void const *elem, list_equal_cmp_t cmp) { - int i = 0; + long i = 0; list_elem_t *e = list->elems_head; while (NULL != e) { diff --git a/src/list_insert_at.c b/src/list_insert_at.c index 8634502..82534fd 100644 --- a/src/list_insert_at.c +++ b/src/list_insert_at.c @@ -20,7 +20,7 @@ static list_elem_t *list_elem_insert(list_elem_t *e, void *elem) return new_elem; } -static int list_insert(list_t *list, void *elem, int index) +static int list_insert(list_t *list, void *elem, long index) { list_elem_t *new_e = NULL; list_elem_t *e = list->elems_head; @@ -38,7 +38,7 @@ static int list_insert(list_t *list, void *elem, int index) return 0; } -int list_insert_at(list_t *list, void *elem, int index) +int list_insert_at(list_t *list, void *elem, long index) { if ((0 > index && 0 == list->count) || index > list->count) return -1; diff --git a/src/list_remove_at.c b/src/list_remove_at.c index 84dd72a..e54c3c1 100644 --- a/src/list_remove_at.c +++ b/src/list_remove_at.c @@ -18,7 +18,7 @@ static void list_elem_remove(list_elem_t *e) free(e); } -void *list_remove_at(list_t *list, int index) +void *list_remove_at(list_t *list, long index) { void *data = NULL; list_elem_t *e = list->elems_head; diff --git a/src/list_slice.c b/src/list_slice.c index 6f34393..9da44d8 100644 --- a/src/list_slice.c +++ b/src/list_slice.c @@ -9,7 +9,7 @@ #include #include -static list_elem_t *move_to(list_elem_t *node, int range) +static list_elem_t *move_to(list_elem_t *node, long range) { while (NULL != node && 0 < range) { node = node->next; @@ -18,7 +18,7 @@ static list_elem_t *move_to(list_elem_t *node, int range) return node; } -static int convert_indexes(list_t const *list, int *from, int *to) +static int convert_indexes(list_t const *list, long *from, long *to) { if (0 > *from) *from += list->count; @@ -29,7 +29,7 @@ static int convert_indexes(list_t const *list, int *from, int *to) return 0; } -int list_slice_into(list_t const *list, int from, int to, list_t *output) +int list_slice_into(list_t const *list, long from, long to, list_t *output) { list_elem_t *head = list->elems_head; @@ -44,7 +44,7 @@ int list_slice_into(list_t const *list, int from, int to, list_t *output) return 0; } -list_t *list_slice(list_t const *list, int from, int to) +list_t *list_slice(list_t const *list, long from, long to) { list_t *sliced = NULL; @@ -60,7 +60,7 @@ list_t *list_slice(list_t const *list, int from, int to) return sliced; } -static void destroy_list_tail(list_t *list, int range) +static void destroy_list_tail(list_t *list, long range) { list_elem_t *tmp = NULL; @@ -75,7 +75,7 @@ static void destroy_list_tail(list_t *list, int range) list->elems_tail->next = NULL; } -static void destroy_list_head(list_t *list, int range) +static void destroy_list_head(list_t *list, long range) { list_elem_t *tmp = NULL; @@ -90,7 +90,7 @@ static void destroy_list_head(list_t *list, int range) list->elems_head->prev = NULL; } -int list_slice_itself(list_t *list, int from, int to) +int list_slice_itself(list_t *list, long from, long to) { if (-1 == convert_indexes(list, &from, &to)) return -1; diff --git a/src/list_sort.c b/src/list_sort.c index cfa8666..2fb386e 100644 --- a/src/list_sort.c +++ b/src/list_sort.c @@ -72,9 +72,7 @@ static void quick_sort(list_t *list, int low, int high, list_sort_t sort) if (low < high) { p = partition(list, low, high, sort); quick_sort(list, low, p - 1, sort); - list_print(list); quick_sort(list, p + 1, high, sort); - list_print(list); } } diff --git a/src/list_value_at.c b/src/list_value_at.c index 902919d..33aabcf 100644 --- a/src/list_value_at.c +++ b/src/list_value_at.c @@ -7,7 +7,7 @@ #include "list.h" -void *list_value_at(list_t const *list, int index) +void *list_value_at(list_t const *list, long index) { list_elem_t *elem = list->elems_head; diff --git a/tests/tests_array_to_list.c b/tests/tests_array_to_list.c new file mode 100644 index 0000000..54270d9 --- /dev/null +++ b/tests/tests_array_to_list.c @@ -0,0 +1,36 @@ +/* +** EPITECH PROJECT, 2024 +** Tests of List in C +** File description: +** tests_array_to_list.c +*/ + +#include "list.h" +#include +#include + +Test(array_to_list, test_impl) +{ + void *array[] = { (void *) 1, (void *) 2, (void *) 3, (void *) 4, (void *) 5 }; + long size = 5; + list_t expected = { 0 }; + list_t *list = array_to_list(array, size, NULL); + + cr_assert(eq(int, 0, list_push_back(&expected, (void *) 1))); + cr_assert(eq(int, 0, list_push_back(&expected, (void *) 2))); + cr_assert(eq(int, 0, list_push_back(&expected, (void *) 3))); + cr_assert(eq(int, 0, list_push_back(&expected, (void *) 4))); + cr_assert(eq(int, 0, list_push_back(&expected, (void *) 5))); + cr_assert(ne(ptr, NULL, list)); + cr_assert(eq(int, 1, list_equal(&expected, list, NULL))); + list_destroy(list); + list_clear(&expected); +} + +Test(array_to_list, test_bad_inputs) +{ + void *array[] = { (void *) 1, (void *) 2, (void *) 3, (void *) 4, (void *) 5 }; + + cr_assert(eq(ptr, NULL, array_to_list(array, 0, NULL))); + cr_assert(eq(ptr, NULL, array_to_list(array, -1, NULL))); +} diff --git a/tests/tests_list_index_of.c b/tests/tests_list_index_of.c index 0af946e..7280d13 100644 --- a/tests/tests_list_index_of.c +++ b/tests/tests_list_index_of.c @@ -25,10 +25,10 @@ Test(list_index_of, test_impl) cr_assert(eq(int, 0, list_insert_at(list, "This is my tail 2-1 !", -1))); cr_assert(eq(int, 0, list_insert_at(list, "This is my tail 2 !", 2))); cr_assert(eq(int, 4, list_count(list))); - cr_assert(eq(int, 0, list_index_of(list, "This is my head !", my_strcmp))); - cr_assert(eq(int, 1, list_index_of(list, "This is my tail !", my_strcmp))); - cr_assert(eq(int, 2, list_index_of(list, "This is my tail 2 !", my_strcmp))); - cr_assert(eq(int, 3, list_index_of(list, "This is my tail 2-1 !", my_strcmp))); - cr_assert(eq(int, -1, list_index_of(list, "Invalid Element", my_strcmp))); + cr_assert(eq(long, 0, list_index_of(list, "This is my head !", my_strcmp))); + cr_assert(eq(long, 1, list_index_of(list, "This is my tail !", my_strcmp))); + cr_assert(eq(long, 2, list_index_of(list, "This is my tail 2 !", my_strcmp))); + cr_assert(eq(long, 3, list_index_of(list, "This is my tail 2-1 !", my_strcmp))); + cr_assert(eq(long, -1, list_index_of(list, "Invalid Element", my_strcmp))); list_destroy(list); } diff --git a/tests/tests_list_reduce.c b/tests/tests_list_reduce.c index a328438..b58caa1 100644 --- a/tests/tests_list_reduce.c +++ b/tests/tests_list_reduce.c @@ -13,7 +13,7 @@ #include "list.h" static void *reduce(void const *acc, void const *current, - int index __attribute__((unused)), + long index __attribute__((unused)), list_t const *list __attribute__((unused))) { unsigned long long a = (unsigned long long) acc; @@ -22,7 +22,7 @@ static void *reduce(void const *acc, void const *current, } static void *reduce_str(void const *acc, void const *current, - int index __attribute__((unused)), + long index __attribute__((unused)), list_t const *list __attribute__((unused))) { char const *a = acc; diff --git a/tests/tests_pop_and_remove.c b/tests/tests_pop_and_remove.c index 182f42a..e24888f 100644 --- a/tests/tests_pop_and_remove.c +++ b/tests/tests_pop_and_remove.c @@ -19,11 +19,11 @@ Test(list_pop_back, test_impl) cr_assert(eq(int, 0, list_push_back(list, strdup("Hello, World !")))); cr_assert(eq(int, 0, list_push_back(list, strdup("This is my tail !")))); - cr_assert(eq(int, 2, list_count(list))); + cr_assert(eq(long, 2, list_count(list))); s2 = list_pop_back(list); - cr_assert(eq(int, 1, list_count(list))); + cr_assert(eq(long, 1, list_count(list))); s1 = list_pop_back(list); - cr_assert(eq(int, 0, list_count(list))); + cr_assert(eq(long, 0, list_count(list))); cr_assert(eq(str, "Hello, World !", s1)); cr_assert(eq(str, "This is my tail !", s2)); free(s2); @@ -39,11 +39,11 @@ Test(list_pop_front, test_impl) cr_assert(eq(int, 0, list_push_back(list, strdup("This is my head !")))); cr_assert(eq(int, 0, list_push_back(list, strdup("Hello, World !")))); - cr_assert(eq(int, 2, list_count(list))); + cr_assert(eq(long, 2, list_count(list))); s1 = list_pop_front(list); - cr_assert(eq(int, 1, list_count(list))); + cr_assert(eq(long, 1, list_count(list))); s2 = list_pop_front(list); - cr_assert(eq(int, 0, list_count(list))); + cr_assert(eq(long, 0, list_count(list))); cr_assert(eq(str, "This is my head !", s1)); cr_assert(eq(str, "Hello, World !", s2)); free(s2); diff --git a/tests/tests_push_and_insert.c b/tests/tests_push_and_insert.c index 48dc43f..9d7c5b2 100644 --- a/tests/tests_push_and_insert.c +++ b/tests/tests_push_and_insert.c @@ -22,7 +22,7 @@ Test(list_push_back, test_impl) cr_assert(eq(int, 0, list_push_back(list, strdup("Hello, World !")))); cr_assert(eq(int, 0, list_push_back(list, strdup("This is my tail !")))); - cr_assert(eq(int, 2, list_count(list))); + cr_assert(eq(long, 2, list_count(list))); cr_assert(eq(str, "Hello, World !", list->elems_head->elem)); cr_assert(eq(str, "This is my tail !", list->elems_tail->elem)); list_destroy(list); @@ -34,7 +34,7 @@ Test(list_push_front, test_impl) cr_assert(eq(int, 0, list_push_front(list, strdup("Hello, World !")))); cr_assert(eq(int, 0, list_push_front(list, strdup("This is my head !")))); - cr_assert(eq(int, 2, list_count(list))); + cr_assert(eq(long, 2, list_count(list))); cr_assert(eq(str, "This is my head !", list->elems_head->elem)); cr_assert(eq(str, "Hello, World !", list->elems_tail->elem)); list_destroy(list);