-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequential_list.c
231 lines (178 loc) · 7.04 KB
/
sequential_list.c
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "../src/sequential_list/sequential_list.h"
#include "./suite_runner/suite_runner.h"
#include "./test_helpers.h"
#include <stdio.h>
#include <string.h>
void test_create_list() {
List *test_list = create_list();
ASSERT(test_list->qtd == 0, "create_list() initializes an empty list.");
}
void test_appent_to_end() {
List *test_list = create_list();
Student test_student1 = {1, "default", 10.0, 8.9, 9.7};
Student test_student2 = {2, "default", 10.0, 8.9, 9.7};
Student test_student3 = {3, "default", 10.0, 8.9, 9.7};
append_to_end(test_list, test_student1);
append_to_end(test_list, test_student2);
append_to_end(test_list, test_student3);
ASSERT(test_list->data[test_list->qtd - 1].register_number == 3,
"append_to_end() append one instance of Student to the end of the "
"list.");
}
void test_free_list() {
List *test_list = create_list();
Student test_student = {1, "default", 10.0, 8.9, 9.7};
append_to_end(test_list, test_student);
free_list(test_list);
Student test_student2 = {2, "default", 10.0, 8.9, 9.7};
append_to_end(test_list, test_student2);
ASSERT(test_list->qtd == 1 && test_list->data[0].register_number == 2,
"free_list() completely clean the list.");
}
void test_list_length() {
List *test_list = create_list();
Student test_student1 = {1, "default", 10.0, 8.9, 9.7};
Student test_student2 = {2, "default", 10.0, 8.9, 9.7};
Student test_student3 = {3, "default", 10.0, 8.9, 9.7};
append_to_end(test_list, test_student1);
append_to_end(test_list, test_student2);
append_to_end(test_list, test_student3);
ASSERT(list_length(test_list) == 3,
"list_length() return the current length of the list.");
}
void test_is_full() {
List *test_list = create_list();
for (int i = 0; i < 100; i++) {
Student test_student = {i, "default", 10.0, 8.9, 9.7};
append_to_end(test_list, test_student);
}
ASSERT(is_full(test_list), "is_full() return true if the list is full or "
"false if the list still have space.");
}
void test_is_not_full() {
List *test_list = create_list();
for (int i = 0; i < 99; i++) {
Student test_student = {i, "default", 10.0, 8.9, 9.7};
append_to_end(test_list, test_student);
}
ASSERT(!is_full(test_list),
"test if theres space in the list after 99 inserts.");
}
void test_is_empty() {
ASSERT(is_empty(create_list()), "is_empty() verify if the list is empty.");
}
void test_appent_to_start() {
List *test_list = create_list();
Student test_student1 = {1, "default", 10.0, 8.9, 9.7};
Student test_student2 = {2, "default", 10.0, 8.9, 9.7};
Student test_student3 = {3, "default", 10.0, 8.9, 9.7};
append_to_start(test_list, test_student1);
append_to_start(test_list, test_student2);
append_to_start(test_list, test_student3);
ASSERT(test_list->data[0].register_number == 3,
"append_to_end() append a value at the start of the list, moving "
"the old values to the next memory address.");
}
void test_append_sorting() {
List *test_list = create_list();
Student test_student1 = {1, "default", 10.0, 8.9, 9.7};
Student test_student2 = {22, "default", 10.0, 8.9, 9.7};
Student test_student3 = {3, "default", 10.0, 8.9, 9.7};
append_sorting(test_list, test_student3);
append_sorting(test_list, test_student1);
append_sorting(test_list, test_student2);
ASSERT(test_list->data[2].register_number == 22,
"append_sorting() append the item to list sorting by the register "
"number.");
}
void test_remove_from_start() {
List *test_list = create_list();
Student test_student1 = {1, "default", 10.0, 8.9, 9.7};
Student test_student2 = {22, "default", 10.0, 8.9, 9.7};
Student test_student3 = {3, "default", 10.0, 8.9, 9.7};
append_sorting(test_list, test_student3);
append_sorting(test_list, test_student1);
append_sorting(test_list, test_student2);
remove_from_start(test_list);
ASSERT(test_list->data[0].register_number == 3 && test_list->qtd == 2,
"remove_from_start() remove the first item from the list.");
}
void test_remove_from_end() {
List *test_list = create_list();
Student test_student1 = {1, "default", 10.0, 8.9, 9.7};
Student test_student2 = {22, "default", 10.0, 8.9, 9.7};
Student test_student3 = {3, "default", 10.0, 8.9, 9.7};
append_sorting(test_list, test_student3);
append_sorting(test_list, test_student1);
append_sorting(test_list, test_student2);
remove_from_end(test_list);
ASSERT(test_list->qtd == 2, "remove_from_end() remove the last item from the "
"list, decreasing the amount of items.");
}
void test_remove_from_index() {
List *test_list = create_list();
Student test_student1 = {1, "default", 10.0, 8.9, 9.7};
Student test_student2 = {22, "default", 10.0, 8.9, 9.7};
Student test_student3 = {3, "default", 10.0, 8.9, 9.7};
append_sorting(test_list, test_student3);
append_sorting(test_list, test_student1);
append_sorting(test_list, test_student2);
remove_from_index(test_list, 2);
ASSERT(test_list->data[1].register_number == 3 && test_list->qtd == 2,
"remove_from_index() remove an item from its index.");
}
void test_get_by_index() {
List *test_list = create_list();
Student test_student1 = {1, "default", 10.0, 8.9, 9.7};
append_sorting(test_list, test_student1);
// A instance of Student to get the values from the function.
Student st2 = {};
get_by_index(test_list, 0, &st2);
ASSERT(st2.register_number == 1 && strcmp(st2.name, "default") == 0,
"get_by_index() get data from the list by id.");
}
void test_get_by_register_number() {
List *test_list = create_list();
Student test_student1 = {1, "default", 10.0, 8.9, 9.7};
Student test_student2 = {2, "default", 10.0, 8.9, 9.7};
Student test_student3 = {3, "itworked", 10.0, 8.9, 9.7};
append_to_end(test_list, test_student1);
append_to_end(test_list, test_student2);
append_to_end(test_list, test_student3);
Student st = {};
get_by_register_number(test_list, 3, &st);
ASSERT(st.register_number == 3 && strcmp(st.name, "itworked") == 0,
"get_by_register_number() return data from the list by the register "
"number of the student.");
}
void test_remove_from_end_in_an_empty_list() {
List *test_list = create_list();
ASSERT(remove_from_end(test_list) == -1, "an empty list shoud return -1.");
}
void test_remove_from_an_invalid_list() {
List *test_list = NULL;
ASSERT(remove_from_end(test_list) == 0, "an invalid list should return 0.");
}
int main() {
func tests[] = {
test_create_list,
test_appent_to_end,
test_free_list,
test_list_length,
test_is_full,
test_is_not_full,
test_is_empty,
test_appent_to_start,
test_append_sorting,
test_remove_from_start,
test_remove_from_end,
test_remove_from_index,
test_get_by_index,
test_get_by_register_number,
test_remove_from_end_in_an_empty_list,
test_remove_from_an_invalid_list,
};
int amount_of_tests = sizeof(tests) / sizeof(tests[0]);
run(tests, amount_of_tests, "Sequential Static List");
return 0;
}