-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-ll.c
108 lines (73 loc) · 2.9 KB
/
test-ll.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
#include "ll.h"
#include "assertf.h"
int main() {
LL *list = ll_new(NULL, int_compare);
ll_add_i(list, 100);
// test remove first/last value
ll_remove_by_index(list, 0);
assertf(list->count == 0, "list should be empty");
ll_add_i(list, 100);
// test remove first/last element by value
ll_remove_by_value_i(list, 100);
assertf(list->count == 0, "list should be empty");
assertf(list->head == NULL, "head should be null");
assertf(list->tail == NULL, "tail should be null");
for (int i = 1; i <= 20; ++i) {
ll_add_i(list, i);
}
// test remove first element
ll_remove_by_index(list, 0);
// test remove middle element
ll_remove_by_index(list, list->count / 2);
// test remove last element
ll_remove_by_index(list, list->count - 1);
LLIter iter = ll_iter(list);
while (ll_iter_has(&iter)) {
LLIterItem item = ll_iter_consume(&iter);
printf("%02ld %d\n", item.index, *(int*)item.data);
}
printf("---------------------\n");
// test search first digit
int firstDigit = *(int*)ll_find_by_index(list, 0);
int middleDigit = *(int*)ll_find_by_index(list, list->count / 2);
int lastDigit = *(int*)ll_find_by_index(list, list->count - 1);
printf("first digit: %d\n", firstDigit);
printf("middle digit: %d\n", middleDigit);
printf("last digit: %d\n", lastDigit);
// test remove by value first digit
ll_remove_by_value(list, &firstDigit);
// test remove by value middle digit
ll_remove_by_value(list, &middleDigit);
// test remove by value last digit
ll_remove_by_value(list, &lastDigit);
// test flush
ll_iter_flush(&iter);
while (ll_iter_has(&iter)) {
LLIterItem item = ll_iter_consume(&iter);
printf("%02ld %d\n", item.index, *(int*)item.data);
}
ll_free(list);
list = ll_new(NULL, string_compare);
ll_add_s(list, "testing this stuff, yeahh");
ll_add_s(list, "lorem ipsum dolor sit ammet consectur");
LLIter iterator = ll_iter(list);
while (ll_iter_has(&iterator)) {
LLIterItem item = ll_iter_consume(&iterator);
printf("%ld %s\n", item.index, (char*)item.data);
}
ll_remove_by_value(list, "testing this stuff, yeahh");
ll_iter_flush(&iterator);
printf("-----------------------------------------------------------------------------------------------------\n");
while (ll_iter_has(&iterator)) {
LLIterItem item = ll_iter_consume(&iterator);
printf("%ld %s\n", item.index, (char*)item.data);
}
char *text = ll_find_by_value(list, "testing this stuff, yeahh");
assertf(text == NULL, "text should be null not %s", text)
printf("Text found: %s\n", text);
text = ll_find_by_value(list, "lorem ipsum dolor sit ammet consectur");
assertf(text != NULL, "text should not be null");
printf("Text found: %s\n", text);
ll_free(list);
return 0;
}