Wed, 04 Jan 2012 14:51:54 +0100
added clone and equals to lists
1 /*
2 * tests of list implementation
3 */
5 #include <stdio.h>
6 #include <stdlib.h>
8 #include "ucx/list.h"
9 #include "ucx/dlist.h"
11 struct test1_data {
12 int values[3];
13 int i;
14 };
16 int int_cmp(void* e1, void *e2, void *data) {
17 if (e1 == NULL || e2 == NULL) return (e1 == e2) ? 0 : -1;
19 int *i1 = (int*)e1, *i2 = (int*)e2;
20 int r = (*i1) - (*i2);
21 return (r < 0) ? -1 : (r == 0 ? 0 : 1);
22 }
24 int dlist_tests_foreach(void *v, void *custom) {
25 UcxDlist *dl = (UcxDlist*)v;
26 struct test1_data *tdata = (struct test1_data*)custom;
28 tdata->values[tdata->i] = *(int*)dl->data;
29 tdata->i++;
31 return 0;
32 }
34 int list_tests_foreach(void *v, void *custom) {
35 UcxList *dl = (UcxList*)v;
36 struct test1_data *tdata = (struct test1_data*)custom;
38 tdata->values[tdata->i] = *(int*)dl->data;
39 tdata->i++;
41 return 0;
42 }
44 int dlist_tests() {
45 int r = 0;
46 int v[8];
47 UcxDlist *dl = NULL;
48 // build list 0,1,2,3,4,5,6,7
49 printf(" Test ucx_dlist_append\n");
50 fflush(stdout);
51 for(int i=0;i<8;i++) {
52 v[i] = i;
53 dl = ucx_dlist_append(dl, &v[i]);
54 }
56 printf(" Test ucx_dlist_get\n");
57 fflush(stdout);
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");
62 r--;
63 }
64 if(elm->data == NULL) {
65 fprintf(stderr, "ucx_dlist_get failed: data is NULL\n");
66 r--;
67 }
68 int *data = (int*)elm->data;
69 if(*data != i) {
70 fprintf(stderr, "ucx_dlist_get failed with index %d\n", i);
71 r--;
72 }
73 }
75 printf(" Test ucx_dlist_free\n");
76 fflush(stdout);
77 ucx_dlist_free(dl);
79 dl = NULL;
80 // build list 4,0,4
81 printf(" Test ucx_dlist_prepend\n");
82 dl = ucx_dlist_prepend(dl, &v[0]);
83 dl = ucx_dlist_prepend(dl, &v[4]);
84 dl = ucx_dlist_append(dl, &v[4]);
86 struct test1_data tdata;
87 tdata.i = 0;
88 ucx_dlist_foreach(dl, dlist_tests_foreach, &tdata);
90 if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) {
91 fprintf(stderr, "prepend/append test failed\n");
92 fprintf(stderr, "content: [%d, %d, %d]\n",
93 tdata.values[0], tdata.values[1], tdata.values[2]);
94 r--;
95 }
97 printf(" Test ucx_dlist_equals\n");
98 UcxDlist *dl2 = NULL;
99 dl2 = ucx_dlist_append(dl2, &v[4]);
100 dl2 = ucx_dlist_append(dl2, &v[0]);
101 dl2 = ucx_dlist_append(dl2, &v[4]);
102 if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
103 fprintf(stderr, "ucx_dlist_equals failed (false negative)\n");
104 }
105 dl2->next->data = NULL;
106 if (ucx_dlist_equals(dl, dl2, NULL, NULL)) {
107 fprintf(stderr, "ucx_dlist_equals failed (false positive)\n");
108 }
109 dl2->next->data = &(tdata.values[1]);
110 if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) {
111 fprintf(stderr, "ucx_dlist_equals failed (cmp_func false negative)\n");
112 }
113 if (ucx_dlist_equals(dl, dl2, NULL, NULL)) {
114 fprintf(stderr, "ucx_dlist_equals failed (cmp_func false positive)\n");
115 }
116 ucx_dlist_free(dl2);
118 printf(" Test ucx_dlist_clone\n");
119 dl2 = ucx_dlist_clone(dl, NULL, NULL);
120 if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
121 fprintf(stderr, "ucx_dlist_clone (without copy) failed\n");
122 }
123 ucx_dlist_free(dl2);
125 printf(" TODO: test clone with copy\n");
127 return r;
128 }
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 }
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 }
161 printf(" Test ucx_list_free\n");
162 fflush(stdout);
163 ucx_list_free(dl);
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]);
172 struct test1_data tdata;
173 tdata.i = 0;
174 ucx_list_foreach(dl, list_tests_foreach, &tdata);
176 if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) {
177 fprintf(stderr, "prepend/append test failed\n");
178 fprintf(stderr, "content: [%d, %d, %d]\n",
179 tdata.values[0], tdata.values[1], tdata.values[2]);
180 r--;
181 }
183 printf(" Test ucx_list_equals\n");
184 UcxList *dl2 = NULL;
185 dl2 = ucx_list_append(dl2, &v[4]);
186 dl2 = ucx_list_append(dl2, &v[0]);
187 dl2 = ucx_list_append(dl2, &v[4]);
188 if (!ucx_list_equals(dl, dl2, NULL, NULL)) {
189 fprintf(stderr, "ucx_list_equals failed (false negative)\n");
190 }
191 dl2->next->data = NULL;
192 if (ucx_list_equals(dl, dl2, NULL, NULL)) {
193 fprintf(stderr, "ucx_list_equals failed (false positive)\n");
194 }
195 dl2->next->data = &(tdata.values[1]);
196 if (!ucx_list_equals(dl, dl2, int_cmp, NULL)) {
197 fprintf(stderr, "ucx_list_equals failed (cmp_func false negative)\n");
198 }
199 if (ucx_list_equals(dl, dl2, NULL, NULL)) {
200 fprintf(stderr, "ucx_list_equals failed (cmp_func false positive)\n");
201 }
202 ucx_list_free(dl2);
204 printf(" Test ucx_list_clone\n");
205 dl2 = ucx_list_clone(dl, NULL, NULL);
206 if (!ucx_list_equals(dl, dl2, NULL, NULL)) {
207 fprintf(stderr, "ucx_list_clone (without copy) failed\n");
208 }
209 ucx_list_free(dl2);
211 printf(" TODO: test clone with copy\n");
213 return r;
214 }