Sat, 31 Dec 2011 22:41:16 +0100
fixed mpool compiler warnings
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 | ||
16 | int list_tests_foreach1(void *v, void *custom) { | |
17 | UcxDlist *dl = (UcxDlist*)v; | |
18 | struct test1_data *tdata = (struct test1_data*)custom; | |
19 | ||
20 | tdata->values[tdata->i] = *(int*)dl->data; | |
21 | tdata->i++; | |
22 | } | |
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 | 33 | int r = 0; |
34 | int v[8]; | |
35 | UcxDlist *dl = NULL; | |
36 | // build list 0,1,2,3,4,5,6,7 | |
11 | 37 | printf(" Test ucx_dlist_append\n"); |
38 | fflush(stdout); | |
9 | 39 | for(int i=0;i<8;i++) { |
40 | v[i] = i; | |
41 | dl = ucx_dlist_append(dl, &v[i]); | |
42 | } | |
43 | ||
11 | 44 | printf(" Test ucx_dlist_get\n"); |
45 | fflush(stdout); | |
9 | 46 | for(int i=0;i<8;i++) { |
47 | UcxDlist *elm = ucx_dlist_get(dl, i); | |
48 | if(elm == NULL) { | |
49 | fprintf(stderr, "ucx_dlist_get failed: element is NULL\n"); | |
11 | 50 | r--; |
9 | 51 | } |
52 | if(elm->data == NULL) { | |
53 | fprintf(stderr, "ucx_dlist_get failed: data is NULL\n"); | |
11 | 54 | r--; |
9 | 55 | } |
56 | int *data = (int*)elm->data; | |
57 | if(*data != i) { | |
58 | fprintf(stderr, "ucx_dlist_get failed with index %d\n", i); | |
11 | 59 | r--; |
9 | 60 | } |
61 | } | |
62 | ||
11 | 63 | printf(" Test ucx_dlist_free\n"); |
64 | fflush(stdout); | |
65 | ucx_dlist_free(dl); | |
66 | ||
9 | 67 | dl = NULL; |
68 | // build list 4,0,4 | |
11 | 69 | printf(" Test ucx_dlist_prepend\n"); |
9 | 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 | 73 | |
74 | struct test1_data tdata; | |
75 | tdata.i = 0; | |
76 | ucx_dlist_foreach(dl, list_tests_foreach1, &tdata); | |
77 | ||
78 | if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) { | |
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 | 82 | } |
83 | ||
84 | return r; | |
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 | } |