Wed, 11 Jan 2012 12:19:48 +0100
Removed linked list from tests (assume that they are correct if the dlist tests are correct)
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 dlist_tests() {
35 int r = 0;
36 int v[8];
37 UcxDlist *dl = NULL;
38 // build list 0,1,2,3,4,5,6,7
39 printf(" Test ucx_dlist_append\n");
40 fflush(stdout);
41 for(int i=0;i<8;i++) {
42 v[i] = i;
43 dl = ucx_dlist_append(dl, &v[i]);
44 }
46 printf(" Test ucx_dlist_get\n");
47 fflush(stdout);
48 for(int i=0;i<8;i++) {
49 UcxDlist *elm = ucx_dlist_get(dl, i);
50 if(elm == NULL) {
51 fprintf(stderr, "ucx_dlist_get failed: element is NULL\n");
52 r--;
53 }
54 if(elm->data == NULL) {
55 fprintf(stderr, "ucx_dlist_get failed: data is NULL\n");
56 r--;
57 }
58 int *data = (int*)elm->data;
59 if(*data != i) {
60 fprintf(stderr, "ucx_dlist_get failed with index %d\n", i);
61 r--;
62 }
63 }
65 printf(" Test ucx_dlist_free\n");
66 fflush(stdout);
67 ucx_dlist_free(dl);
69 dl = NULL;
70 // build list 4,0,4
71 printf(" Test ucx_dlist_prepend\n");
72 dl = ucx_dlist_prepend(dl, &v[0]);
73 dl = ucx_dlist_prepend(dl, &v[4]);
74 dl = ucx_dlist_append(dl, &v[4]);
76 struct test1_data tdata;
77 tdata.i = 0;
78 ucx_dlist_foreach(dl, dlist_tests_foreach, &tdata);
80 if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) {
81 fprintf(stderr, "prepend/append test failed\n");
82 fprintf(stderr, "content: [%d, %d, %d]\n",
83 tdata.values[0], tdata.values[1], tdata.values[2]);
84 r--;
85 }
87 printf(" Test ucx_dlist_equals\n");
88 UcxDlist *dl2 = NULL;
89 dl2 = ucx_dlist_append(dl2, &v[4]);
90 dl2 = ucx_dlist_append(dl2, &v[0]);
91 dl2 = ucx_dlist_append(dl2, &v[4]);
92 if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
93 fprintf(stderr, "ucx_dlist_equals failed (false negative)\n");
94 r--;
95 }
96 dl2->next->data = NULL;
97 if (ucx_dlist_equals(dl, dl2, NULL, NULL)) {
98 fprintf(stderr, "ucx_dlist_equals failed (false positive)\n");
99 r--;
100 }
101 dl2->next->data = &(tdata.values[1]);
102 if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) {
103 fprintf(stderr, "ucx_dlist_equals failed (cmp_func false negative)\n");
104 r--;
105 }
106 if (ucx_dlist_equals(dl, dl2, NULL, NULL)) {
107 fprintf(stderr, "ucx_dlist_equals failed (cmp_func false positive)\n");
108 r--;
109 }
110 ucx_dlist_free(dl2);
112 printf(" Test ucx_dlist_clone\n");
113 dl2 = ucx_dlist_clone(dl, NULL, NULL);
114 if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
115 fprintf(stderr, "ucx_dlist_clone (without copy) failed\n");
116 r--;
117 }
118 ucx_dlist_free(dl2);
120 printf(" TODO: test clone with copy\n");
122 return r;
123 }