-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlstmap.cpp
More file actions
89 lines (77 loc) · 2.45 KB
/
Copy pathlstmap.cpp
File metadata and controls
89 lines (77 loc) · 2.45 KB
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
83
84
85
86
87
88
89
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lstmap.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjurkows <kjurkows@student.42warsaw.pl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/06/20 15:30:51 by kjurkows #+# #+# */
/* Updated: 2026/06/25 17:47:24 by kjurkows ### ########.fr */
/* */
/* ************************************************************************** */
#include "_main.hpp"
#include <stdlib.h>
TEST(lstmap, basic)
{
t_list *list1 = (t_list *)malloc(sizeof(t_list));
t_list *list2 = (t_list *)malloc(sizeof(t_list));
t_list *list3 = (t_list *)malloc(sizeof(t_list));
int i1 = 0;
int i2 = 1;
int i3 = 2;
list1->content = &i1;
list1->next = list2;
list2->content = &i2;
list2->next = list3;
list3->content = &i3;
list3->next = nullptr;
t_list *new_list = ft_lstmap(list1, [](void *content) -> void *
{
int *i = (int *)content;
int *new_i = (int *)malloc(sizeof(int));
*new_i = *i + 10;
return new_i;
}, free);
t_list *current_new = new_list;
for (int i = 0; i < 3; i++)
{
int *value = (int *)current_new->content;
ASSERT_EQ(*value, i + 10);
current_new = current_new->next;
}
ASSERT_EQ(current_new, nullptr);
free(new_list->next->next->content);
free(new_list->next->next);
free(new_list->next->content);
free(new_list->next);
free(new_list->content);
free(new_list);
free(list1);
free(list2);
free(list3);
}
TEST(lstmapDeathTest, nullptr_list)
{
ASSERT_EXIT({
t_list *new_list = ft_lstmap(nullptr, [](void *content) -> void *
{
int *i = (int *)content;
int *new_i = (int *)malloc(sizeof(int));
*new_i = *i + 10;
return new_i;
}, free);
exit(!!new_list);
}, ::testing::ExitedWithCode(0), "");
}
TEST(lstmapDeathTest, nullptr_f)
{
ASSERT_EXIT({
t_list *list = (t_list *)malloc(sizeof(t_list));
int i = 0;
list->content = &i;
list->next = nullptr;
t_list *new_list = ft_lstmap(list, nullptr, free);
free(list);
exit(!!new_list);
}, ::testing::ExitedWithCode(0), "");
}