test/list_tests.c

changeset 18
69636f81db31
parent 11
4f6082f99bd7
child 19
cdd7a3173249
equal deleted inserted replaced
17:2e7050c3a18e 18:69636f81db31
11 struct test1_data { 11 struct test1_data {
12 int values[3]; 12 int values[3];
13 int i; 13 int i;
14 }; 14 };
15 15
16 int list_tests_foreach1(void *v, void *custom) { 16 int int_cmp(void* e1, void *e2, void *data) {
17 if (e1 == NULL || e2 == NULL) return (e1 == e2) ? 0 : -1;
18
19 int *i1 = (int*)e1, *i2 = (int*)e2;
20 int r = (*i1) - (*i2);
21 return (r < 0) ? -1 : (r == 0 ? 0 : 1);
22 }
23
24 int dlist_tests_foreach(void *v, void *custom) {
17 UcxDlist *dl = (UcxDlist*)v; 25 UcxDlist *dl = (UcxDlist*)v;
18 struct test1_data *tdata = (struct test1_data*)custom; 26 struct test1_data *tdata = (struct test1_data*)custom;
19 27
20 tdata->values[tdata->i] = *(int*)dl->data; 28 tdata->values[tdata->i] = *(int*)dl->data;
21 tdata->i++; 29 tdata->i++;
22 } 30
23 31 return 0;
24 int list_tests_foreach2(void *v, void *custom) { 32 }
33
34 int list_tests_foreach(void *v, void *custom) {
25 UcxList *dl = (UcxList*)v; 35 UcxList *dl = (UcxList*)v;
26 struct test1_data *tdata = (struct test1_data*)custom; 36 struct test1_data *tdata = (struct test1_data*)custom;
27 37
28 tdata->values[tdata->i] = *(int*)dl->data; 38 tdata->values[tdata->i] = *(int*)dl->data;
29 tdata->i++; 39 tdata->i++;
40
41 return 0;
30 } 42 }
31 43
32 int dlist_tests() { 44 int dlist_tests() {
33 int r = 0; 45 int r = 0;
34 int v[8]; 46 int v[8];
71 dl = ucx_dlist_prepend(dl, &v[4]); 83 dl = ucx_dlist_prepend(dl, &v[4]);
72 dl = ucx_dlist_append(dl, &v[4]); 84 dl = ucx_dlist_append(dl, &v[4]);
73 85
74 struct test1_data tdata; 86 struct test1_data tdata;
75 tdata.i = 0; 87 tdata.i = 0;
76 ucx_dlist_foreach(dl, list_tests_foreach1, &tdata); 88 ucx_dlist_foreach(dl, dlist_tests_foreach, &tdata);
77 89
78 if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) { 90 if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) {
79 fprintf(stderr, "prepend/append test failed\n"); 91 fprintf(stderr, "prepend/append test failed\n");
80 fprintf(stderr, "content: [%d, %d, %d]\n", tdata.values[0], tdata.values[1], tdata.values[2]); 92 fprintf(stderr, "content: [%d, %d, %d]\n",
93 tdata.values[0], tdata.values[1], tdata.values[2]);
81 r--; 94 r--;
82 } 95 }
96
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);
117
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);
124
125 printf(" TODO: test clone with copy\n");
83 126
84 return r; 127 return r;
85 } 128 }
86 129
87 int list_tests() { 130 int list_tests() {
126 dl = ucx_list_prepend(dl, &v[4]); 169 dl = ucx_list_prepend(dl, &v[4]);
127 dl = ucx_list_append(dl, &v[4]); 170 dl = ucx_list_append(dl, &v[4]);
128 171
129 struct test1_data tdata; 172 struct test1_data tdata;
130 tdata.i = 0; 173 tdata.i = 0;
131 ucx_list_foreach(dl, list_tests_foreach1, &tdata); 174 ucx_list_foreach(dl, list_tests_foreach, &tdata);
132 175
133 if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) { 176 if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) {
134 fprintf(stderr, "prepend/append test failed\n"); 177 fprintf(stderr, "prepend/append test failed\n");
135 fprintf(stderr, "content: [%d, %d, %d]\n", tdata.values[0], tdata.values[1], tdata.values[2]); 178 fprintf(stderr, "content: [%d, %d, %d]\n",
179 tdata.values[0], tdata.values[1], tdata.values[2]);
136 r--; 180 r--;
137 } 181 }
182
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);
203
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);
210
211 printf(" TODO: test clone with copy\n");
138 212
139 return r; 213 return r;
140 } 214 }

mercurial