Wed, 04 Jan 2012 14:51:54 +0100
added clone and equals to lists
9 | 1 | /* |
2 | * tests of list implementation | |
3 | */ | |
4 | ||
5 | #include <stdio.h> | |
6 | #include <stdlib.h> | |
7 | ||
8 | #include "ucx/list.h" | |
9 | #include "ucx/dlist.h" | |
10 | ||
11 | struct test1_data { | |
12 | int values[3]; | |
13 | int i; | |
14 | }; | |
15 | ||
18
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
16 | int int_cmp(void* e1, void *e2, void *data) { |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
17 | if (e1 == NULL || e2 == NULL) return (e1 == e2) ? 0 : -1; |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
18 | |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
19 | int *i1 = (int*)e1, *i2 = (int*)e2; |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
20 | int r = (*i1) - (*i2); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
21 | return (r < 0) ? -1 : (r == 0 ? 0 : 1); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
22 | } |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
23 | |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
24 | int dlist_tests_foreach(void *v, void *custom) { |
9 | 25 | UcxDlist *dl = (UcxDlist*)v; |
26 | struct test1_data *tdata = (struct test1_data*)custom; | |
27 | ||
28 | tdata->values[tdata->i] = *(int*)dl->data; | |
29 | tdata->i++; | |
18
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
30 | |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
31 | return 0; |
9 | 32 | } |
33 | ||
18
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
34 | int list_tests_foreach(void *v, void *custom) { |
11 | 35 | UcxList *dl = (UcxList*)v; |
36 | struct test1_data *tdata = (struct test1_data*)custom; | |
37 | ||
38 | tdata->values[tdata->i] = *(int*)dl->data; | |
39 | tdata->i++; | |
18
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
40 | |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
41 | return 0; |
11 | 42 | } |
43 | ||
44 | int dlist_tests() { | |
9 | 45 | int r = 0; |
46 | int v[8]; | |
47 | UcxDlist *dl = NULL; | |
48 | // build list 0,1,2,3,4,5,6,7 | |
11 | 49 | printf(" Test ucx_dlist_append\n"); |
50 | fflush(stdout); | |
9 | 51 | for(int i=0;i<8;i++) { |
52 | v[i] = i; | |
53 | dl = ucx_dlist_append(dl, &v[i]); | |
54 | } | |
55 | ||
11 | 56 | printf(" Test ucx_dlist_get\n"); |
57 | fflush(stdout); | |
9 | 58 | for(int i=0;i<8;i++) { |
59 | UcxDlist *elm = ucx_dlist_get(dl, i); | |
60 | if(elm == NULL) { | |
61 | fprintf(stderr, "ucx_dlist_get failed: element is NULL\n"); | |
11 | 62 | r--; |
9 | 63 | } |
64 | if(elm->data == NULL) { | |
65 | fprintf(stderr, "ucx_dlist_get failed: data is NULL\n"); | |
11 | 66 | r--; |
9 | 67 | } |
68 | int *data = (int*)elm->data; | |
69 | if(*data != i) { | |
70 | fprintf(stderr, "ucx_dlist_get failed with index %d\n", i); | |
11 | 71 | r--; |
9 | 72 | } |
73 | } | |
74 | ||
11 | 75 | printf(" Test ucx_dlist_free\n"); |
76 | fflush(stdout); | |
77 | ucx_dlist_free(dl); | |
78 | ||
9 | 79 | dl = NULL; |
80 | // build list 4,0,4 | |
11 | 81 | printf(" Test ucx_dlist_prepend\n"); |
9 | 82 | dl = ucx_dlist_prepend(dl, &v[0]); |
11 | 83 | dl = ucx_dlist_prepend(dl, &v[4]); |
84 | dl = ucx_dlist_append(dl, &v[4]); | |
9 | 85 | |
86 | struct test1_data tdata; | |
87 | tdata.i = 0; | |
18
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
88 | ucx_dlist_foreach(dl, dlist_tests_foreach, &tdata); |
9 | 89 | |
90 | if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) { | |
91 | fprintf(stderr, "prepend/append test failed\n"); | |
18
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
92 | fprintf(stderr, "content: [%d, %d, %d]\n", |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
93 | tdata.values[0], tdata.values[1], tdata.values[2]); |
11 | 94 | r--; |
9 | 95 | } |
96 | ||
18
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
97 | printf(" Test ucx_dlist_equals\n"); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
98 | UcxDlist *dl2 = NULL; |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
99 | dl2 = ucx_dlist_append(dl2, &v[4]); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
100 | dl2 = ucx_dlist_append(dl2, &v[0]); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
101 | dl2 = ucx_dlist_append(dl2, &v[4]); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
102 | if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
103 | fprintf(stderr, "ucx_dlist_equals failed (false negative)\n"); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
104 | } |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
105 | dl2->next->data = NULL; |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
106 | if (ucx_dlist_equals(dl, dl2, NULL, NULL)) { |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
107 | fprintf(stderr, "ucx_dlist_equals failed (false positive)\n"); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
108 | } |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
109 | dl2->next->data = &(tdata.values[1]); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
110 | if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) { |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
111 | fprintf(stderr, "ucx_dlist_equals failed (cmp_func false negative)\n"); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
112 | } |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
113 | if (ucx_dlist_equals(dl, dl2, NULL, NULL)) { |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
114 | fprintf(stderr, "ucx_dlist_equals failed (cmp_func false positive)\n"); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
115 | } |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
116 | ucx_dlist_free(dl2); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
117 | |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
118 | printf(" Test ucx_dlist_clone\n"); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
119 | dl2 = ucx_dlist_clone(dl, NULL, NULL); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
120 | if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
121 | fprintf(stderr, "ucx_dlist_clone (without copy) failed\n"); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
122 | } |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
123 | ucx_dlist_free(dl2); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
124 | |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
125 | printf(" TODO: test clone with copy\n"); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
126 | |
9 | 127 | return r; |
128 | } | |
11 | 129 | |
130 | int list_tests() { | |
131 | int r = 0; | |
132 | int v[8]; | |
133 | UcxList *dl = NULL; | |
134 | // build list 0,1,2,3,4,5,6,7 | |
135 | printf(" Test ucx_list_append\n"); | |
136 | fflush(stdout); | |
137 | for(int i=0;i<8;i++) { | |
138 | v[i] = i; | |
139 | dl = ucx_list_append(dl, &v[i]); | |
140 | } | |
141 | ||
142 | printf(" Test ucx_list_get\n"); | |
143 | fflush(stdout); | |
144 | for(int i=0;i<8;i++) { | |
145 | UcxList *elm = ucx_list_get(dl, i); | |
146 | if(elm == NULL) { | |
147 | fprintf(stderr, "ucx_list_get failed: element is NULL\n"); | |
148 | r--; | |
149 | } | |
150 | if(elm->data == NULL) { | |
151 | fprintf(stderr, "ucx_list_get failed: data is NULL\n"); | |
152 | r--; | |
153 | } | |
154 | int *data = (int*)elm->data; | |
155 | if(*data != i) { | |
156 | fprintf(stderr, "ucx_list_get failed with index %d\n", i); | |
157 | r--; | |
158 | } | |
159 | } | |
160 | ||
161 | printf(" Test ucx_list_free\n"); | |
162 | fflush(stdout); | |
163 | ucx_list_free(dl); | |
164 | ||
165 | dl = NULL; | |
166 | // build list 4,0,4 | |
167 | printf(" Test ucx_list_prepend\n"); | |
168 | dl = ucx_list_prepend(dl, &v[0]); | |
169 | dl = ucx_list_prepend(dl, &v[4]); | |
170 | dl = ucx_list_append(dl, &v[4]); | |
171 | ||
172 | struct test1_data tdata; | |
173 | tdata.i = 0; | |
18
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
174 | ucx_list_foreach(dl, list_tests_foreach, &tdata); |
11 | 175 | |
176 | if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) { | |
177 | fprintf(stderr, "prepend/append test failed\n"); | |
18
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
178 | fprintf(stderr, "content: [%d, %d, %d]\n", |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
179 | tdata.values[0], tdata.values[1], tdata.values[2]); |
11 | 180 | r--; |
181 | } | |
18
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
182 | |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
183 | printf(" Test ucx_list_equals\n"); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
184 | UcxList *dl2 = NULL; |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
185 | dl2 = ucx_list_append(dl2, &v[4]); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
186 | dl2 = ucx_list_append(dl2, &v[0]); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
187 | dl2 = ucx_list_append(dl2, &v[4]); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
188 | if (!ucx_list_equals(dl, dl2, NULL, NULL)) { |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
189 | fprintf(stderr, "ucx_list_equals failed (false negative)\n"); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
190 | } |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
191 | dl2->next->data = NULL; |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
192 | if (ucx_list_equals(dl, dl2, NULL, NULL)) { |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
193 | fprintf(stderr, "ucx_list_equals failed (false positive)\n"); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
194 | } |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
195 | dl2->next->data = &(tdata.values[1]); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
196 | if (!ucx_list_equals(dl, dl2, int_cmp, NULL)) { |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
197 | fprintf(stderr, "ucx_list_equals failed (cmp_func false negative)\n"); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
198 | } |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
199 | if (ucx_list_equals(dl, dl2, NULL, NULL)) { |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
200 | fprintf(stderr, "ucx_list_equals failed (cmp_func false positive)\n"); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
201 | } |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
202 | ucx_list_free(dl2); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
203 | |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
204 | printf(" Test ucx_list_clone\n"); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
205 | dl2 = ucx_list_clone(dl, NULL, NULL); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
206 | if (!ucx_list_equals(dl, dl2, NULL, NULL)) { |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
207 | fprintf(stderr, "ucx_list_clone (without copy) failed\n"); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
208 | } |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
209 | ucx_list_free(dl2); |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
210 | |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
211 | printf(" TODO: test clone with copy\n"); |
11 | 212 | |
213 | return r; | |
214 | } |