9
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1
|
/*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2
|
* tests of list implementation
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
4
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5
|
#include <stdio.h>
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6
|
#include <stdlib.h>
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8
|
#include "ucx/list.h"
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9
|
#include "ucx/dlist.h"
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11
|
struct test1_data {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12
|
int values[3];
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13
|
int i;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14
|
};
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16
|
int list_tests_foreach1(void *v, void *custom) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17
|
UcxDlist *dl = (UcxDlist*)v;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18
|
struct test1_data *tdata = (struct test1_data*)custom;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20
|
tdata->values[tdata->i] = *(int*)dl->data;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21
|
tdata->i++;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23
|
|
11
|
24
|
int list_tests_foreach2(void *v, void *custom) {
|
|
25
|
UcxList *dl = (UcxList*)v;
|
|
26
|
struct test1_data *tdata = (struct test1_data*)custom;
|
|
27
|
|
|
28
|
tdata->values[tdata->i] = *(int*)dl->data;
|
|
29
|
tdata->i++;
|
|
30
|
}
|
|
31
|
|
|
32
|
int dlist_tests() {
|
9
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
33
|
int r = 0;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34
|
int v[8];
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35
|
UcxDlist *dl = NULL;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36
|
// build list 0,1,2,3,4,5,6,7
|
11
|
37
|
printf(" Test ucx_dlist_append\n");
|
|
38
|
fflush(stdout);
|
9
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39
|
for(int i=0;i<8;i++) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
40
|
v[i] = i;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
41
|
dl = ucx_dlist_append(dl, &v[i]);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
42
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
43
|
|
11
|
44
|
printf(" Test ucx_dlist_get\n");
|
|
45
|
fflush(stdout);
|
9
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
46
|
for(int i=0;i<8;i++) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
47
|
UcxDlist *elm = ucx_dlist_get(dl, i);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48
|
if(elm == NULL) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
49
|
fprintf(stderr, "ucx_dlist_get failed: element is NULL\n");
|
11
|
50
|
r--;
|
9
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
52
|
if(elm->data == NULL) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
53
|
fprintf(stderr, "ucx_dlist_get failed: data is NULL\n");
|
11
|
54
|
r--;
|
9
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
55
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
56
|
int *data = (int*)elm->data;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
57
|
if(*data != i) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
58
|
fprintf(stderr, "ucx_dlist_get failed with index %d\n", i);
|
11
|
59
|
r--;
|
9
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
60
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
61
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
62
|
|
11
|
63
|
printf(" Test ucx_dlist_free\n");
|
|
64
|
fflush(stdout);
|
|
65
|
ucx_dlist_free(dl);
|
|
66
|
|
9
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67
|
dl = NULL;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68
|
// build list 4,0,4
|
11
|
69
|
printf(" Test ucx_dlist_prepend\n");
|
9
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70
|
dl = ucx_dlist_prepend(dl, &v[0]);
|
11
|
71
|
dl = ucx_dlist_prepend(dl, &v[4]);
|
|
72
|
dl = ucx_dlist_append(dl, &v[4]);
|
9
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
73
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
74
|
struct test1_data tdata;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
75
|
tdata.i = 0;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
76
|
ucx_dlist_foreach(dl, list_tests_foreach1, &tdata);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
77
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
78
|
if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
79
|
fprintf(stderr, "prepend/append test failed\n");
|
11
|
80
|
fprintf(stderr, "content: [%d, %d, %d]\n", tdata.values[0], tdata.values[1], tdata.values[2]);
|
|
81
|
r--;
|
9
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
82
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
83
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
84
|
return r;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
85
|
}
|
11
|
86
|
|
|
87
|
int list_tests() {
|
|
88
|
int r = 0;
|
|
89
|
int v[8];
|
|
90
|
UcxList *dl = NULL;
|
|
91
|
// build list 0,1,2,3,4,5,6,7
|
|
92
|
printf(" Test ucx_list_append\n");
|
|
93
|
fflush(stdout);
|
|
94
|
for(int i=0;i<8;i++) {
|
|
95
|
v[i] = i;
|
|
96
|
dl = ucx_list_append(dl, &v[i]);
|
|
97
|
}
|
|
98
|
|
|
99
|
printf(" Test ucx_list_get\n");
|
|
100
|
fflush(stdout);
|
|
101
|
for(int i=0;i<8;i++) {
|
|
102
|
UcxList *elm = ucx_list_get(dl, i);
|
|
103
|
if(elm == NULL) {
|
|
104
|
fprintf(stderr, "ucx_list_get failed: element is NULL\n");
|
|
105
|
r--;
|
|
106
|
}
|
|
107
|
if(elm->data == NULL) {
|
|
108
|
fprintf(stderr, "ucx_list_get failed: data is NULL\n");
|
|
109
|
r--;
|
|
110
|
}
|
|
111
|
int *data = (int*)elm->data;
|
|
112
|
if(*data != i) {
|
|
113
|
fprintf(stderr, "ucx_list_get failed with index %d\n", i);
|
|
114
|
r--;
|
|
115
|
}
|
|
116
|
}
|
|
117
|
|
|
118
|
printf(" Test ucx_list_free\n");
|
|
119
|
fflush(stdout);
|
|
120
|
ucx_list_free(dl);
|
|
121
|
|
|
122
|
dl = NULL;
|
|
123
|
// build list 4,0,4
|
|
124
|
printf(" Test ucx_list_prepend\n");
|
|
125
|
dl = ucx_list_prepend(dl, &v[0]);
|
|
126
|
dl = ucx_list_prepend(dl, &v[4]);
|
|
127
|
dl = ucx_list_append(dl, &v[4]);
|
|
128
|
|
|
129
|
struct test1_data tdata;
|
|
130
|
tdata.i = 0;
|
|
131
|
ucx_list_foreach(dl, list_tests_foreach1, &tdata);
|
|
132
|
|
|
133
|
if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) {
|
|
134
|
fprintf(stderr, "prepend/append test failed\n");
|
|
135
|
fprintf(stderr, "content: [%d, %d, %d]\n", tdata.values[0], tdata.values[1], tdata.values[2]);
|
|
136
|
r--;
|
|
137
|
}
|
|
138
|
|
|
139
|
return r;
|
|
140
|
}
|