-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic-list-test.c
136 lines (109 loc) · 2.58 KB
/
generic-list-test.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "generic-list.h"
typedef void (*PfCbPrintElement)(const ElementAddr elemaddr);
void
print_list(List list, PfCbPrintElement printfn)
{
Position pos;
if ( list_is_empty(list) ) {
fprintf(stdout, "Empty list\n");
return ;
}
for ( pos = list_get_first(list);
!list_is_end(pos);
pos = list_next(pos)
) {
printfn(list_retrieve(pos));
}
printf("\n");
}
int
int_equal(ElementAddr left, ElementAddr right)
{
if ( *(int *)left == *(int *)right )
return 1;
return 0;
}
void
print_int(ElementAddr elemaddr)
{
printf("%d ", *(int *)elemaddr);
}
void
stringfree(ElementAddr elemaddr)
{
free(*(void **)elemaddr);
}
int
str_equal(ElementAddr left, ElementAddr right)
{
if ( strcmp((char *)(*(void **)left), (char *)(*(void **)right)) == 0 )
return 1;
return 0;
}
void
print_str(ElementAddr elemaddr)
{
printf("%s ", (char *)(*(void **)elemaddr));
}
int main(int argc, char **argv)
{
List int_list, str_list;
Position pos;
int i;
char *names[] = {
"C", "C++", "Jave", "C#", "Python",
"PHP", "Basic", "Objective C", "Matlab", "Golang"
};
/* test int list */
printf("Test int list\n");
int_list = list_create(sizeof(int), NULL);
pos = list_get_header(int_list);
for ( i = 0; i < 10; ++i ) {
list_insert(int_list, pos, &i);
print_list(int_list, print_int);
pos = list_next(pos);
}
for ( i = 0; i < 10; i += 2 ) {
int delval;
int is_found;
is_found = list_delete(int_list, &i, int_equal, &delval);
if ( is_found )
printf("Delete: %d\n", delval );
}
for ( i = 0; i < 10; ++i)
if ( (i % 2 == 0) == (list_find(int_list, &i, int_equal) != NULL) )
printf("Find fails\n");
printf("Finished deletions\n");
print_list(int_list, print_int);
list_dispose(int_list);
/* test string list */
printf("Test string list\n");
str_list = list_create(sizeof(char *), stringfree);
pos = list_get_header(str_list);
for ( i = 0; i < sizeof(names) / sizeof(names[0]); ++i) {
char *copy = strdup(names[i]);
list_insert(str_list, pos, ©);
print_list(str_list, print_str);
pos = list_next(pos);
}
for ( i = 0; i < sizeof(names) / sizeof(names[0]); i += 2) {
char *delstr;
int is_found;
is_found = list_delete(str_list, &names[i], str_equal, &delstr);
if ( is_found ) {
printf("Delete: %s\n", delstr);
free(delstr);
}
}
for ( i = 0; i < 10; ++i ) {
if ( (i % 2 == 0) == (list_find(str_list, &names[i], str_equal) != NULL))
printf("Find fails\n");
}
printf("Finished deletions\n");
print_list(str_list, print_str);
list_dispose(str_list);
return 0;
}