test/list_tests.c

changeset 24
e04822101291
parent 22
76cdd8209f1f
child 27
22644e2572bc
equal deleted inserted replaced
23:ccc294fafb9b 24:e04822101291
6 #include <stdlib.h> 6 #include <stdlib.h>
7 7
8 #include "ucx/list.h" 8 #include "ucx/list.h"
9 #include "ucx/dlist.h" 9 #include "ucx/dlist.h"
10 10
11 struct test1_data { 11 struct foreach_testdata {
12 int values[3]; 12 int values[3];
13 int i; 13 int i;
14 }; 14 };
15 15
16 void* int_cpy(void* source, void* data) {
17 void *dest = malloc(sizeof(int));
18 if (dest != NULL) {
19 *((int*)dest) = *((int*)source);
20 }
21 return dest;
22 }
23
16 int int_cmp(void* e1, void *e2, void *data) { 24 int int_cmp(void* e1, void *e2, void *data) {
17 if (e1 == NULL || e2 == NULL) return (e1 == e2) ? 0 : -1; 25 if (e1 == NULL || e2 == NULL) return (e1 == e2) ? 0 : -1;
18 26
19 int *i1 = (int*)e1, *i2 = (int*)e2; 27 int *i1 = (int*)e1, *i2 = (int*)e2;
20 int r = (*i1) - (*i2); 28 int r = (*i1) - (*i2);
21 return (r < 0) ? -1 : (r == 0 ? 0 : 1); 29 return (r < 0) ? -1 : (r == 0 ? 0 : 1);
22 } 30 }
23 31
24 int dlist_tests_foreach(void *v, void *custom) { 32 int dlist_tests_foreach(void *v, void *custom) {
25 UcxDlist *dl = (UcxDlist*)v; 33 UcxDlist *dl = (UcxDlist*)v;
26 struct test1_data *tdata = (struct test1_data*)custom; 34 struct foreach_testdata *tdata = (struct foreach_testdata*)custom;
27 35
28 tdata->values[tdata->i] = *(int*)dl->data; 36 tdata->values[tdata->i] = *(int*)dl->data;
29 tdata->i++; 37 tdata->i++;
30 38
39 return 0;
40 }
41
42 int dlist_free_content(void *v, void *custom) {
43 UcxDlist *dl = (UcxDlist*)v;
44 free(dl->data);
31 return 0; 45 return 0;
32 } 46 }
33 47
34 int dlist_tests() { 48 int dlist_tests() {
35 int r = 0; 49 int r = 0;
48 for(int i=0;i<8;i++) { 62 for(int i=0;i<8;i++) {
49 UcxDlist *elm = ucx_dlist_get(dl, i); 63 UcxDlist *elm = ucx_dlist_get(dl, i);
50 if(elm == NULL) { 64 if(elm == NULL) {
51 fprintf(stderr, "ucx_dlist_get failed: element is NULL\n"); 65 fprintf(stderr, "ucx_dlist_get failed: element is NULL\n");
52 r--; 66 r--;
53 } 67 } else if(elm->data == NULL) {
54 if(elm->data == NULL) {
55 fprintf(stderr, "ucx_dlist_get failed: data is NULL\n"); 68 fprintf(stderr, "ucx_dlist_get failed: data is NULL\n");
56 r--; 69 r--;
57 } 70 } else {
58 int *data = (int*)elm->data; 71 int *data = (int*)elm->data;
59 if(*data != i) { 72 if(*data != i) {
60 fprintf(stderr, "ucx_dlist_get failed with index %d\n", i); 73 fprintf(stderr, "ucx_dlist_get failed with index %d\n", i);
61 r--; 74 r--;
75 }
62 } 76 }
63 } 77 }
64 78
65 printf(" Test ucx_dlist_free\n"); 79 printf(" Test ucx_dlist_free\n");
66 fflush(stdout); 80 fflush(stdout);
71 printf(" Test ucx_dlist_prepend\n"); 85 printf(" Test ucx_dlist_prepend\n");
72 dl = ucx_dlist_prepend(dl, &v[0]); 86 dl = ucx_dlist_prepend(dl, &v[0]);
73 dl = ucx_dlist_prepend(dl, &v[4]); 87 dl = ucx_dlist_prepend(dl, &v[4]);
74 dl = ucx_dlist_append(dl, &v[4]); 88 dl = ucx_dlist_append(dl, &v[4]);
75 89
76 struct test1_data tdata; 90 struct foreach_testdata tdata;
77 tdata.i = 0; 91 tdata.i = 0;
78 ucx_dlist_foreach(dl, dlist_tests_foreach, &tdata); 92 ucx_dlist_foreach(dl, dlist_tests_foreach, &tdata);
79 93
80 if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) { 94 if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) {
81 fprintf(stderr, "prepend/append test failed\n"); 95 fprintf(stderr, "prepend/append test failed\n");
106 if (ucx_dlist_equals(dl, dl2, NULL, NULL)) { 120 if (ucx_dlist_equals(dl, dl2, NULL, NULL)) {
107 fprintf(stderr, "ucx_dlist_equals failed (cmp_func false positive)\n"); 121 fprintf(stderr, "ucx_dlist_equals failed (cmp_func false positive)\n");
108 r--; 122 r--;
109 } 123 }
110 ucx_dlist_free(dl2); 124 ucx_dlist_free(dl2);
111 125
112 printf(" Test ucx_dlist_clone\n"); 126 printf(" Test ucx_dlist_clone\n");
113 dl2 = ucx_dlist_clone(dl, NULL, NULL); 127 dl2 = ucx_dlist_clone(dl, NULL, NULL);
114 if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { 128 if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
115 fprintf(stderr, "ucx_dlist_clone (without copy) failed\n"); 129 fprintf(stderr, "ucx_dlist_clone (without copy) failed\n");
116 r--; 130 r--;
117 } 131 }
118 ucx_dlist_free(dl2); 132 ucx_dlist_free(dl2);
119 133
120 printf(" TODO: test clone with copy\n"); 134 printf(" Test ucx_dlist_clone with copy\n");
135 dl2 = ucx_dlist_clone(dl, int_cpy, NULL);
136 if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
137 if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) {
138 fprintf(stderr, "ucx_dlist_clone (with copy) failed (compare)\n");
139 r--;
140 }
141 } else {
142 fprintf(stderr, "ucx_dlist_clone (with copy) failed (identity)\n");
143 r--;
144 }
145 ucx_dlist_foreach(dl, dlist_free_content, NULL);
146 ucx_dlist_free(dl2);
121 147
122 ucx_dlist_free(dl); 148 ucx_dlist_free(dl);
123 149
124 dl = NULL; 150 dl = NULL;
125 printf(" Test ucx_dlist_remove\n"); 151 printf(" Test ucx_dlist_remove\n");

mercurial