1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
#include "libft_test.h"
TEST_GROUP(ft_lstmap);
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);
}
|