aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-02-16 04:51:41 +0100
committerCharles <sircharlesaze@gmail.com>2020-02-16 04:51:41 +0100
commit01b4cc91d1596cf94d709a627ed8ad64bc1e285d (patch)
tree8b15a7758247ed4d4a084bccefedbf426ddfd305
parentbf4dc8e5c1dbb8149f0cab473f73b5e9bac24ae0 (diff)
downloadlibft-01b4cc91d1596cf94d709a627ed8ad64bc1e285d.tar.gz
libft-01b4cc91d1596cf94d709a627ed8ad64bc1e285d.tar.bz2
libft-01b4cc91d1596cf94d709a627ed8ad64bc1e285d.zip
Filled lst* tests
-rw-r--r--README.md24
-rw-r--r--include/libft_ht.h6
-rw-r--r--include/libft_lst.h19
-rw-r--r--src/ht/ft_htdelone.c4
-rw-r--r--src/ht/ft_htget.c4
-rw-r--r--src/ht/ft_inter_htkey_cmp.c (renamed from src/ht/ft_inter_htkey_equal.c)8
-rw-r--r--src/lst/ft_lstbsearch.c34
-rw-r--r--src/lst/ft_lstmap.c2
-rw-r--r--src/lst/ft_lstremove_if.c19
-rw-r--r--src/lst/ft_lstreverse.c2
-rw-r--r--src/lst/ft_lstsort.c4
-rw-r--r--src/lst/ft_lstsorted_merge.c5
-rw-r--r--test/Makefile6
-rw-r--r--test/src/lst/test_ft_lstadd_back.c2
-rw-r--r--test/src/lst/test_ft_lstadd_front.c2
-rw-r--r--test/src/lst/test_ft_lstbsearch.c23
-rw-r--r--test/src/lst/test_ft_lstclear.c2
-rw-r--r--test/src/lst/test_ft_lstdelone.c2
-rw-r--r--test/src/lst/test_ft_lstiter.c38
-rw-r--r--test/src/lst/test_ft_lstlast.c15
-rw-r--r--test/src/lst/test_ft_lstmap.c68
-rw-r--r--test/src/lst/test_ft_lstpop_front.c17
-rw-r--r--test/src/lst/test_ft_lstremove_if.c56
-rw-r--r--test/src/lst/test_ft_lstreverse.c31
-rw-r--r--test/src/lst/test_ft_lstreverse_ret.c31
-rw-r--r--test/src/lst/test_ft_lstsize.c2
-rw-r--r--test/src/lst/test_ft_lstsort.c24
-rw-r--r--test/src/lst/test_ft_lstsorted_merge.c26
-rw-r--r--test/src/mem/test_ft_calloc.c2
29 files changed, 414 insertions, 64 deletions
diff --git a/README.md b/README.md
index 271d68e..29959fc 100644
--- a/README.md
+++ b/README.md
@@ -130,20 +130,20 @@ Much like the `.gitignore` file, you can put the files/directory to ignore when
|--------------------|---------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------|--------|
| ft_lstadd_back | `void ft_lstadd_back(t_ftlst **alst, t_ftlst *new)` | add `new` at the start of `alst` | [x] |
| ft_lstadd_front | `void ft_lstadd_front(t_ftlst **alst, t_ftlst *new)` | add `new` at the end of `alst` | [x] |
-| ft_lstbsearch | `t_ftlst *ft_lstbsearch(t_ftlst *lst, t_ftbool (*equal)(void *ref, void *content), void *ref)` | search `ref` in `lst` using binary search | [ ] |
-| ft_lstclear | `void ft_lstclear(t_ftlst **lst, void (*del)(void *))` | free all data with `del`, free all nodes and set `*lst` to NULL | [ ] |
-| ft_lstdelone | `void ft_lstdelone(t_ftlst *lst, void (*del)(void *))` | free `lst` node | [ ] |
-| ft_lstiter | `void ft_lstiter(t_ftlst *lst, void (*f)(void *))` | apply `f` on each node of `lst` | [ ] |
-| ft_lstlast | `t_ftlst *ft_lstlast(t_ftlst *lst)` | return the last node of `lst` | [ ] |
-| ft_lstmap | `t_ftlst *ft_lstmap(t_ftlst *lst, void *(*f)(void *), void (*del)(void *))` | create a new list which is `lst` with all node passed through `f` | [ ] |
+| ft_lstbsearch | `t_ftlst *ft_lstbsearch(t_ftlst *lst, t_ftbool (*equal)(void *ref, void *content), void *ref)` | search `ref` in `lst` using binary search | [x] |
+| ft_lstclear | `void ft_lstclear(t_ftlst **lst, void (*del)(void *))` | free all data with `del`, free all nodes and set `*lst` to NULL | [x] |
+| ft_lstdelone | `void ft_lstdelone(t_ftlst *lst, void (*del)(void *))` | free `lst` node | [x] |
+| ft_lstiter | `void ft_lstiter(t_ftlst *lst, void (*f)(void *))` | apply `f` on each node of `lst` | [x] |
+| ft_lstlast | `t_ftlst *ft_lstlast(t_ftlst *lst)` | return the last node of `lst` | [x] |
+| ft_lstmap | `t_ftlst *ft_lstmap(t_ftlst *lst, void *(*f)(void *), void (*del)(void *))` | create a new list which is `lst` with all node passed through `f` | [x] |
| ft_lstnew | `t_ftlst *ft_lstnew(void const *content)` | create a new node with `content` | [x] |
-| ft_lstpop_front | `void ft_lstpop_front(t_ftlst **lst, void (*del)(void *))` | remove the front of `lst` | [ ] |
-| ft_lstremove_if | `void ft_lstremove_if(t_ftlst **lst, t_ftbool (*equal)(void *ref, void *content), void *ref, void (*del)(void *content))` | remove all element which verify `equal` | [ ] |
-| ft_lstreverse | `void ft_lstreverse(t_ftlst **lst)` | reverse `lst` | [ ] |
-| ft_lstreverse_ret | `t_ftlst *ft_lstreverse_ret(t_ftlst *lst)` | reverse `lst` and return it | [ ] |
+| ft_lstpop_front | `void ft_lstpop_front(t_ftlst **lst, void (*del)(void *))` | remove the front of `lst` | [x] |
+| ft_lstremove_if | `void ft_lstremove_if(t_ftlst **lst, int (*cmp)(const void *ref, const void *content), void *ref, void (*del)(void *content))` | remove all element which verify `equal` | [x] |
+| ft_lstreverse | `void ft_lstreverse(t_ftlst **lst)` | reverse `lst` | [x] |
+| ft_lstreverse_ret | `t_ftlst *ft_lstreverse_ret(t_ftlst *lst)` | reverse `lst` and return it | [x] |
| ft_lstsize | `int ft_lstsize(t_ftlst *lst)` | return the number of node in `lst` | [x] |
-| ft_lstsort | `void ft_lstsort(t_ftlst **begin_list, int (*cmp)(void *, void*))` | sort `begin_list` using mergesort | [ ] |
-| ft_lstsorted_merge | `t_ftlst *ft_lstsorted_merge(t_ftlst *l1, t_ftlst *l2, int (*cmp)(void *, void *))` | merge 2 sorted list and return the merged and still sorted one | [ ] |
+| ft_lstsort | `void ft_lstsort(t_ftlst **begin_list, int (*cmp)(void *, void*))` | sort `begin_list` using mergesort | [x] |
+| ft_lstsorted_merge | `t_ftlst *ft_lstsorted_merge(t_ftlst *l1, t_ftlst *l2, int (*cmp)(void *, void *))` | merge 2 sorted list and return the merged and still sorted one | [x] |
### str
diff --git a/include/libft_ht.h b/include/libft_ht.h
index 8eda59c..0873e7d 100644
--- a/include/libft_ht.h
+++ b/include/libft_ht.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/31 10:36:09 by cacharle #+# #+# */
-/* Updated: 2020/02/14 02:14:57 by cacharle ### ########.fr */
+/* Updated: 2020/02/16 02:23:06 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -50,7 +50,7 @@ t_ftht_content *ft_htcontent_new(char *key, void *value);
*/
void ft_inter_htdelcontent_key(t_ftht_content *content);
-t_ftbool ft_inter_htkey_equal(char *ref_key,
- t_ftht_content *content);
+int ft_inter_htkey_equal(const void *ref_key,
+ const void *content);
#endif
diff --git a/include/libft_lst.h b/include/libft_lst.h
index d092e59..f92067d 100644
--- a/include/libft_lst.h
+++ b/include/libft_lst.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/31 10:36:39 by cacharle #+# #+# */
-/* Updated: 2020/02/10 02:12:26 by cacharle ### ########.fr */
+/* Updated: 2020/02/16 02:15:26 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,6 +15,7 @@
# include <stdlib.h>
# include "libft_types.h"
+# include "libft_algo.h"
typedef struct s_ftlst
{
@@ -22,6 +23,8 @@ typedef struct s_ftlst
struct s_ftlst *next;
} t_ftlst;
+typedef void (*t_ftdel_func)(void *);
+
t_ftlst *ft_lstnew(void const *content);
void ft_lstadd_front(t_ftlst **alst, t_ftlst *new);
int ft_lstsize(t_ftlst *lst);
@@ -31,17 +34,17 @@ void ft_lstdelone(t_ftlst *lst, void (*del)(void *));
void ft_lstclear(t_ftlst **lst, void (*del)(void *));
void ft_lstiter(t_ftlst *lst, void (*f)(void *));
t_ftlst *ft_lstmap(t_ftlst *lst, void *(*f)(void *),
- void (*del)(void *));
+ t_ftdel_func del);
void ft_lstpop_front(t_ftlst **lst, void (*del)(void *));
t_ftlst *ft_lstreverse_ret(t_ftlst *lst);
void ft_lstreverse(t_ftlst **lst);
void ft_lstremove_if(t_ftlst **lst,
- t_ftbool (*equal)(void *ref, void *content), void *ref,
- void (*del)(void *content));
-t_ftlst *ft_lstbsearch(t_ftlst *lst,
- t_ftbool (*equal)(void *ref, void *content), void *ref);
-void ft_lstsort(t_ftlst **begin_list, int (*cmp)(void *, void*));
+ t_ftcompar_func cmp, const void *ref,
+ t_ftdel_func del);
+t_ftlst *ft_lstbsearch(t_ftlst *lst, t_ftcompar_func cmp,
+ const void *ref);
+void ft_lstsort(t_ftlst **begin_list, t_ftcompar_func cmp);
t_ftlst *ft_lstsorted_merge(t_ftlst *l1, t_ftlst *l2,
- int (*cmp)(void *, void *));
+ t_ftcompar_func cmp);
#endif
diff --git a/src/ht/ft_htdelone.c b/src/ht/ft_htdelone.c
index d502bf2..e4ab80f 100644
--- a/src/ht/ft_htdelone.c
+++ b/src/ht/ft_htdelone.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/30 09:27:18 by cacharle #+# #+# */
-/* Updated: 2020/01/31 10:40:40 by cacharle ### ########.fr */
+/* Updated: 2020/02/16 02:20:35 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,6 +16,6 @@
void ft_htdelone(t_ftht *ht, char *key, void (*del)(t_ftht_content*))
{
ft_lstremove_if(ht->entries + ft_hthash(ht, key),
- (t_ftbool (*)(void*, void*))ft_inter_htkey_equal, key,
+ ft_inter_htkey_equal, key,
(void (*)(void*))del);
}
diff --git a/src/ht/ft_htget.c b/src/ht/ft_htget.c
index 02ce4a3..6a9d785 100644
--- a/src/ht/ft_htget.c
+++ b/src/ht/ft_htget.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/30 08:33:21 by cacharle #+# #+# */
-/* Updated: 2020/02/14 02:18:00 by cacharle ### ########.fr */
+/* Updated: 2020/02/16 02:23:23 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -23,6 +23,6 @@ void *ft_htget(t_ftht *ht, char *key)
return (NULL);
digest = ft_hthash(ht, key);
return (((t_ftht_content*)ft_lstbsearch(ht->entries[digest],
- (t_ftbool (*)(void*, void*))ft_inter_htkey_equal,
+ ft_inter_htkey_equal,
key)->content)->value);
}
diff --git a/src/ht/ft_inter_htkey_equal.c b/src/ht/ft_inter_htkey_cmp.c
index 7714c84..c91663d 100644
--- a/src/ht/ft_inter_htkey_equal.c
+++ b/src/ht/ft_inter_htkey_cmp.c
@@ -6,16 +6,16 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/30 09:24:39 by cacharle #+# #+# */
-/* Updated: 2020/01/31 09:51:03 by cacharle ### ########.fr */
+/* Updated: 2020/02/16 02:25:43 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "libft_ht.h"
-t_ftbool ft_inter_htkey_equal(char *ref_key, t_ftht_content *content)
+int ft_inter_htkey_cmp(const void *ref_key, const void *content)
{
if (ref_key == NULL || content == NULL)
- return (FALSE);
- return (ft_strcmp(ref_key, content->key) == 0);
+ return (-1);
+ return (ft_strcmp((char*)ref_key, ((t_ftht_content*)content)->key));
}
diff --git a/src/lst/ft_lstbsearch.c b/src/lst/ft_lstbsearch.c
index 25a7b4c..68dc43b 100644
--- a/src/lst/ft_lstbsearch.c
+++ b/src/lst/ft_lstbsearch.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/30 09:17:51 by cacharle #+# #+# */
-/* Updated: 2020/01/31 10:42:22 by cacharle ### ########.fr */
+/* Updated: 2020/02/16 04:40:16 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -25,7 +25,7 @@ static t_ftlst *st_lstmiddle(t_ftlst *lst, t_ftlst *last)
while (fast != last)
{
fast = fast->next;
- if (fast == NULL)
+ if (fast == last)
break ;
slow = slow->next;
fast = fast->next;
@@ -34,24 +34,32 @@ static t_ftlst *st_lstmiddle(t_ftlst *lst, t_ftlst *last)
}
static t_ftlst *st_lstbsearch_rec(t_ftlst *lst, t_ftlst *last,
- t_ftbool (*equal)(void *ref, void *content), void *ref)
+ t_ftcompar_func cmp, const void *ref)
{
+ int res;
t_ftlst *mid;
- t_ftlst *left;
if (lst == NULL)
return (NULL);
mid = st_lstmiddle(lst, last);
- if ((*equal)(ref, mid->content))
- return (mid);
- left = st_lstbsearch_rec(lst, mid, equal, ref);
- if (left != NULL)
- return (left);
- return (st_lstbsearch_rec(mid, NULL, equal, ref));
+ if (mid == NULL)
+ return NULL;
+ if (mid->next == NULL)
+ {
+ if (cmp(ref, mid->content) == 0)
+ return (mid);
+ return NULL;
+ }
+ res = cmp(ref, mid->next->content);
+ if (res < 0)
+ return (st_lstbsearch_rec(lst, mid, cmp, ref));
+ else if (res > 0)
+ return (st_lstbsearch_rec(mid->next->next, NULL, cmp, ref));
+ return (mid->next);
}
-t_ftlst *ft_lstbsearch(t_ftlst *lst,
- t_ftbool (*equal)(void *ref, void *content), void *ref)
+t_ftlst *ft_lstbsearch(t_ftlst *lst, t_ftcompar_func cmp,
+ const void *ref)
{
- return (st_lstbsearch_rec(lst, NULL, equal, ref));
+ return (st_lstbsearch_rec(lst, NULL, cmp, ref));
}
diff --git a/src/lst/ft_lstmap.c b/src/lst/ft_lstmap.c
index 9039d71..dda15de 100644
--- a/src/lst/ft_lstmap.c
+++ b/src/lst/ft_lstmap.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/09 09:03:57 by cacharle #+# #+# */
-/* Updated: 2019/11/20 04:01:31 by cacharle ### ########.fr */
+/* Updated: 2020/02/15 23:11:42 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/src/lst/ft_lstremove_if.c b/src/lst/ft_lstremove_if.c
index a597c2e..488539e 100644
--- a/src/lst/ft_lstremove_if.c
+++ b/src/lst/ft_lstremove_if.c
@@ -6,28 +6,27 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/30 09:36:49 by cacharle #+# #+# */
-/* Updated: 2020/01/30 09:55:47 by cacharle ### ########.fr */
+/* Updated: 2020/02/16 03:57:07 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "libft_lst.h"
-void ft_lstremove_if(t_ftlst **lst,
- t_ftbool (*equal)(void *ref, void *content), void *ref,
- void (*del)(void *content))
+void ft_lstremove_if(t_ftlst **lst, t_ftcompar_func cmp,
+ const void *ref, t_ftdel_func del)
{
t_ftlst *saved_next;
if (lst == NULL || *lst == NULL)
return ;
- if (!equal(ref, &(*lst)->content))
+ if (cmp((*lst)->content, ref) == 0)
{
- ft_lstremove_if(&(*lst)->next, equal, ref, del);
+ saved_next = (*lst)->next;
+ ft_lstdelone(*lst, del);
+ *lst = saved_next;
+ ft_lstremove_if(lst, cmp, ref, del);
return ;
}
- saved_next = (*lst)->next;
- ft_lstdelone(*lst, del);
- *lst = saved_next;
- ft_lstremove_if(lst, equal, ref, del);
+ ft_lstremove_if(&(*lst)->next, cmp, ref, del);
}
diff --git a/src/lst/ft_lstreverse.c b/src/lst/ft_lstreverse.c
index 3d3fc7b..61c9daf 100644
--- a/src/lst/ft_lstreverse.c
+++ b/src/lst/ft_lstreverse.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/15 12:49:14 by cacharle #+# #+# */
-/* Updated: 2020/01/15 12:51:54 by cacharle ### ########.fr */
+/* Updated: 2020/02/15 22:53:23 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/src/lst/ft_lstsort.c b/src/lst/ft_lstsort.c
index 883604f..e1c9913 100644
--- a/src/lst/ft_lstsort.c
+++ b/src/lst/ft_lstsort.c
@@ -6,13 +6,13 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/10 01:53:55 by cacharle #+# #+# */
-/* Updated: 2020/02/10 02:09:28 by cacharle ### ########.fr */
+/* Updated: 2020/02/16 02:18:05 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft_lst.h"
-void ft_lstsort(t_ftlst **begin_list, int (*cmp)(void *, void*))
+void ft_lstsort(t_ftlst **begin_list, t_ftcompar_func cmp)
{
t_ftlst *fast;
t_ftlst *slow;
diff --git a/src/lst/ft_lstsorted_merge.c b/src/lst/ft_lstsorted_merge.c
index c3d0391..4f5332c 100644
--- a/src/lst/ft_lstsorted_merge.c
+++ b/src/lst/ft_lstsorted_merge.c
@@ -6,14 +6,13 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/10 01:58:52 by cacharle #+# #+# */
-/* Updated: 2020/02/10 02:13:37 by cacharle ### ########.fr */
+/* Updated: 2020/02/16 02:18:11 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft_lst.h"
-t_ftlst *ft_lstsorted_merge(t_ftlst *l1, t_ftlst *l2,
- int (*cmp)(void *, void *))
+t_ftlst *ft_lstsorted_merge(t_ftlst *l1, t_ftlst *l2, t_ftcompar_func cmp)
{
t_ftlst *merged;
diff --git a/test/Makefile b/test/Makefile
index d37ced4..5835c1c 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -6,7 +6,7 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/02/15 04:35:44 by cacharle #+# #+# #
-# Updated: 2020/02/15 04:35:45 by cacharle ### ########.fr #
+# Updated: 2020/02/15 22:46:20 by cacharle ### ########.fr #
# #
# **************************************************************************** #
@@ -40,6 +40,10 @@ run_v: all
@echo "Test: Running"
@./$(NAME) -v
+run_s: all
+ @echo "Test: Running"
+ @./$(NAME) -s
+
$(NAME): libft_all $(OBJ)
@echo "Test: Linking: $@"
@$(CC) -o $@ $(OBJ) $(LDFLAGS)
diff --git a/test/src/lst/test_ft_lstadd_back.c b/test/src/lst/test_ft_lstadd_back.c
index 6ece111..42e749c 100644
--- a/test/src/lst/test_ft_lstadd_back.c
+++ b/test/src/lst/test_ft_lstadd_back.c
@@ -36,4 +36,6 @@ TEST(ft_lstadd_back, basic)
TEST_ASSERT_NOT_NULL(lst->next->next);
TEST_ASSERT_EQUAL_PTR(&c, lst->next->next->content);
TEST_ASSERT_EQUAL(c, *(int*)lst->next->next->content);
+
+ ft_lstclear(&lst, NULL);
}
diff --git a/test/src/lst/test_ft_lstadd_front.c b/test/src/lst/test_ft_lstadd_front.c
index 95137dd..21b7617 100644
--- a/test/src/lst/test_ft_lstadd_front.c
+++ b/test/src/lst/test_ft_lstadd_front.c
@@ -36,4 +36,6 @@ TEST(ft_lstadd_front, basic)
TEST_ASSERT_NOT_NULL(lst->next->next);
TEST_ASSERT_EQUAL_PTR(&a, lst->next->next->content);
TEST_ASSERT_EQUAL(a, *(int*)lst->next->next->content);
+
+ ft_lstclear(&lst, NULL);
}
diff --git a/test/src/lst/test_ft_lstbsearch.c b/test/src/lst/test_ft_lstbsearch.c
index 0f40780..218f0bf 100644
--- a/test/src/lst/test_ft_lstbsearch.c
+++ b/test/src/lst/test_ft_lstbsearch.c
@@ -10,5 +10,28 @@ TEST_TEAR_DOWN(ft_lstbsearch)
TEST(ft_lstbsearch, basic)
{
+ t_ftlst *found = NULL;
+ t_ftlst *lst = NULL;
+ int a = 1;
+ int b = 2;
+ int c = 3;
+ int d = 4;
+ ft_lstadd_front(&lst, ft_lstnew(&c));
+ ft_lstadd_front(&lst, ft_lstnew(&b));
+ ft_lstadd_front(&lst, ft_lstnew(&a));
+ ft_lstadd_front(&lst, ft_lstnew(&a));
+
+ found = ft_lstbsearch(lst, ft_compar_int, &c);
+ TEST_ASSERT_NOT_NULL(found);
+ found = ft_lstbsearch(lst, ft_compar_int, &c);
+ TEST_ASSERT_NOT_NULL(found);
+ found = ft_lstbsearch(lst, ft_compar_int, &b);
+ TEST_ASSERT_NOT_NULL(found);
+ found = ft_lstbsearch(lst, ft_compar_int, &a);
+ TEST_ASSERT_NOT_NULL(found);
+ found = ft_lstbsearch(lst, ft_compar_int, &d);
+ TEST_ASSERT_NULL(found);
+
+ ft_lstclear(&lst, NULL);
}
diff --git a/test/src/lst/test_ft_lstclear.c b/test/src/lst/test_ft_lstclear.c
index 1139f96..acffb17 100644
--- a/test/src/lst/test_ft_lstclear.c
+++ b/test/src/lst/test_ft_lstclear.c
@@ -10,5 +10,5 @@ TEST_TEAR_DOWN(ft_lstclear)
TEST(ft_lstclear, basic)
{
-
+ TEST_PASS(); // how to test free?
}
diff --git a/test/src/lst/test_ft_lstdelone.c b/test/src/lst/test_ft_lstdelone.c
index d033b7b..03c87d5 100644
--- a/test/src/lst/test_ft_lstdelone.c
+++ b/test/src/lst/test_ft_lstdelone.c
@@ -10,5 +10,5 @@ TEST_TEAR_DOWN(ft_lstdelone)
TEST(ft_lstdelone, basic)
{
-
+ TEST_PASS(); // how to test free?
}
diff --git a/test/src/lst/test_ft_lstiter.c b/test/src/lst/test_ft_lstiter.c
index 20a1511..c8bf90d 100644
--- a/test/src/lst/test_ft_lstiter.c
+++ b/test/src/lst/test_ft_lstiter.c
@@ -8,7 +8,45 @@ TEST_SETUP(ft_lstiter)
TEST_TEAR_DOWN(ft_lstiter)
{}
+static void square_iter_func(void *data)
+{
+ int *d = (int*)data;
+
+ *d = *d * *d;
+}
+
TEST(ft_lstiter, basic)
{
+ t_ftlst *lst = NULL;
+ int a = 2;
+ int b = 3;
+ int c = 4;
+ int d = 5;
+
+ ft_lstiter(lst, square_iter_func);
+ TEST_ASSERT_NULL(lst);
+
+ ft_lstadd_front(&lst, ft_lstnew(&a));
+ ft_lstiter(lst, square_iter_func);
+ TEST_ASSERT_EQUAL(4, *(int*)lst->content);
+
+ ft_lstadd_front(&lst, ft_lstnew(&b));
+ ft_lstiter(lst, square_iter_func);
+ TEST_ASSERT_EQUAL(9, *(int*)lst->content);
+ TEST_ASSERT_EQUAL(16, *(int*)lst->next->content);
+
+ ft_lstadd_front(&lst, ft_lstnew(&c));
+ ft_lstiter(lst, square_iter_func);
+ TEST_ASSERT_EQUAL(16, *(int*)lst->content);
+ TEST_ASSERT_EQUAL(81, *(int*)lst->next->content);
+ TEST_ASSERT_EQUAL(256, *(int*)lst->next->next->content);
+
+ ft_lstadd_front(&lst, ft_lstnew(&d));
+ ft_lstiter(lst, square_iter_func);
+ TEST_ASSERT_EQUAL(25, *(int*)lst->content);
+ TEST_ASSERT_EQUAL(256, *(int*)lst->next->content);
+ TEST_ASSERT_EQUAL(81 * 81, *(int*)lst->next->next->content);
+ TEST_ASSERT_EQUAL(256 * 256, *(int*)lst->next->next->next->content);
+ ft_lstclear(&lst, NULL);
}
diff --git a/test/src/lst/test_ft_lstlast.c b/test/src/lst/test_ft_lstlast.c
index f7dffe2..6b44c28 100644
--- a/test/src/lst/test_ft_lstlast.c
+++ b/test/src/lst/test_ft_lstlast.c
@@ -10,5 +10,20 @@ TEST_TEAR_DOWN(ft_lstlast)
TEST(ft_lstlast, basic)
{
+ t_ftlst *lst = NULL;
+ int a = 1;
+ int b = 2;
+ int c = 3;
+ int d = 4;
+ ft_lstadd_front(&lst, ft_lstnew(&a));
+ TEST_ASSERT_EQUAL_PTR(lst, ft_lstlast(lst));
+ ft_lstadd_front(&lst, ft_lstnew(&b));
+ TEST_ASSERT_EQUAL_PTR(lst->next, ft_lstlast(lst));
+ ft_lstadd_front(&lst, ft_lstnew(&c));
+ TEST_ASSERT_EQUAL_PTR(lst->next->next, ft_lstlast(lst));
+ ft_lstadd_front(&lst, ft_lstnew(&d));
+ TEST_ASSERT_EQUAL_PTR(lst->next->next->next, ft_lstlast(lst));
+
+ ft_lstclear(&lst, NULL);
}
diff --git a/test/src/lst/test_ft_lstmap.c b/test/src/lst/test_ft_lstmap.c
index 5252e9e..4abdddc 100644
--- a/test/src/lst/test_ft_lstmap.c
+++ b/test/src/lst/test_ft_lstmap.c
@@ -8,7 +8,75 @@ TEST_SETUP(ft_lstmap)
TEST_TEAR_DOWN(ft_lstmap)
{}
+static void *f_square(void *data)
+{
+ int *d = malloc(sizeof(int));
+
+ *d = *(int*)data;
+ *d = *d * *d;
+ return d;
+}
+
TEST(ft_lstmap, basic)
{
+ t_ftlst *lst = NULL;
+ t_ftlst *mapped = NULL;
+ int a = 2;
+ int b = 3;
+ int c = 4;
+ int d = 5;
+
+ mapped = ft_lstmap(lst, f_square, free);
+ TEST_ASSERT_NULL(mapped);
+
+ ft_lstadd_front(&lst, ft_lstnew(&a));
+ mapped = ft_lstmap(lst, f_square, free);
+ TEST_ASSERT_NOT_NULL(mapped);
+ TEST_ASSERT_NOT_NULL(mapped->content);
+ TEST_ASSERT_EQUAL(4, *(int*)mapped->content);
+ TEST_ASSERT_NULL(mapped->next);
+ ft_lstclear(&mapped, free);
+
+ ft_lstadd_front(&lst, ft_lstnew(&b));
+ mapped = ft_lstmap(lst, f_square, free);
+ TEST_ASSERT_NOT_NULL(mapped);
+ TEST_ASSERT_NOT_NULL(mapped->content);
+ TEST_ASSERT_EQUAL(9, *(int*)mapped->content);
+ TEST_ASSERT_NOT_NULL(mapped->next);
+ TEST_ASSERT_NOT_NULL(mapped->next->content);
+ TEST_ASSERT_EQUAL(4, *(int*)mapped->next->content);
+ TEST_ASSERT_NULL(mapped->next->next);
+ ft_lstclear(&mapped, free);
+
+ ft_lstadd_front(&lst, ft_lstnew(&c));
+ mapped = ft_lstmap(lst, f_square, free);
+ TEST_ASSERT_NOT_NULL(mapped);
+ TEST_ASSERT_NOT_NULL(mapped->content);
+ TEST_ASSERT_EQUAL(16, *(int*)mapped->content);
+ TEST_ASSERT_NOT_NULL(mapped->next);
+ TEST_ASSERT_NOT_NULL(mapped->next->content);
+ TEST_ASSERT_EQUAL(9, *(int*)mapped->next->content);
+ TEST_ASSERT_NOT_NULL(mapped->next->next);
+ TEST_ASSERT_NOT_NULL(mapped->next->next->content);
+ TEST_ASSERT_EQUAL(4, *(int*)mapped->next->next->content);
+ TEST_ASSERT_NULL(mapped->next->next->next);
+ ft_lstclear(&mapped, free);
+
+ ft_lstadd_front(&lst, ft_lstnew(&d));
+ mapped = ft_lstmap(lst, f_square, free);
+ TEST_ASSERT_NOT_NULL(mapped);
+ TEST_ASSERT_NOT_NULL(mapped->content);
+ TEST_ASSERT_EQUAL(25, *(int*)mapped->content);
+ TEST_ASSERT_NOT_NULL(mapped->next);
+ TEST_ASSERT_NOT_NULL(mapped->next->content);
+ TEST_ASSERT_EQUAL(16, *(int*)mapped->next->content);
+ TEST_ASSERT_NOT_NULL(mapped->next->next);
+ TEST_ASSERT_NOT_NULL(mapped->next->next->content);
+ TEST_ASSERT_EQUAL(9, *(int*)mapped->next->next->content);
+ TEST_ASSERT_NOT_NULL(mapped->next->next->next);
+ TEST_ASSERT_NOT_NULL(mapped->next->next->next->content);
+ TEST_ASSERT_EQUAL(4, *(int*)mapped->next->next->next->content);
+ ft_lstclear(&mapped, free);
+ ft_lstclear(&lst, NULL);
}
diff --git a/test/src/lst/test_ft_lstpop_front.c b/test/src/lst/test_ft_lstpop_front.c
index 2c8fc4d..18815d4 100644
--- a/test/src/lst/test_ft_lstpop_front.c
+++ b/