1 /* |
1 /* |
2 * tests of list implementation |
2 * tests of list implementation |
3 */ |
3 */ |
4 |
4 |
5 #include <stdio.h> |
5 #include "list_tests.h" |
6 #include <stdlib.h> |
|
7 |
6 |
8 #include "ucx/list.h" |
7 UCX_TEST_BEGIN(test_ucx_list_append) { |
9 #include "ucx/dlist.h" |
8 UcxList *list = ucx_list_append(NULL, "Hello"); |
10 |
9 |
11 struct foreach_testdata { |
10 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") |
12 int values[3]; |
11 |
13 int i; |
12 list = ucx_list_append(list, " World!"); |
14 }; |
13 |
15 |
14 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") |
16 void* int_cpy(void* source, void* data) { |
15 UCX_TEST_ASSERT(list->next->next == NULL, "failed") |
17 void *dest = malloc(sizeof(int)); |
16 |
18 if (dest != NULL) { |
17 ucx_list_free(list); |
19 *((int*)dest) = *((int*)source); |
18 |
20 } |
19 UCX_TEST_END |
21 return dest; |
|
22 } |
20 } |
23 |
21 |
24 int int_cmp(void* e1, void *e2, void *data) { |
22 UCX_TEST_BEGIN(test_ucx_list_prepend) { |
25 if (e1 == NULL || e2 == NULL) return (e1 == e2) ? 0 : -1; |
23 UcxList *list = ucx_list_prepend(NULL, " World!"); |
26 |
24 list = ucx_list_prepend(list, "Hello"); |
27 int *i1 = (int*)e1, *i2 = (int*)e2; |
25 |
28 int r = (*i1) - (*i2); |
26 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") |
29 return (r < 0) ? -1 : (r == 0 ? 0 : 1); |
27 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") |
|
28 UCX_TEST_ASSERT(list->next->next == NULL, "failed") |
|
29 |
|
30 ucx_list_free(list); |
|
31 |
|
32 UCX_TEST_END |
30 } |
33 } |
31 |
34 |
32 int dlist_tests_foreach(void *v, void *custom) { |
35 UCX_TEST_BEGIN(test_ucx_list_equals) { |
33 UcxDlist *dl = (UcxDlist*)v; |
36 UcxList *list = ucx_list_append(NULL, "Hello"); |
34 struct foreach_testdata *tdata = (struct foreach_testdata*)custom; |
37 list = ucx_list_append(list, " World!"); |
35 |
38 UcxList *list2 = ucx_list_prepend(NULL, " World!"); |
36 tdata->values[tdata->i] = *(int*)dl->data; |
39 list2 = ucx_list_prepend(list2, "Hello"); |
37 tdata->i++; |
40 UcxList *list3 = ucx_list_prepend(NULL, " Welt!"); |
38 |
41 list3 = ucx_list_prepend(list3, "Hallo"); |
39 return 0; |
42 |
|
43 UCX_TEST_ASSERT(ucx_list_equals(list, list2, cmp_string, NULL), "failed") |
|
44 UCX_TEST_ASSERT(!ucx_list_equals(list, list3, cmp_string, NULL), "failed") |
|
45 |
|
46 ucx_list_free(list3); |
|
47 ucx_list_free(list2); |
|
48 ucx_list_free(list); |
|
49 |
|
50 UCX_TEST_END |
40 } |
51 } |
41 |
52 |
42 int dlist_free_content(void *v, void *custom) { |
53 UCX_TEST_BEGIN(test_ucx_list_concat) { |
43 UcxDlist *dl = (UcxDlist*)v; |
54 UcxList *list = ucx_list_append(NULL, "Hello"); |
44 free(dl->data); |
55 UcxList *list2 = ucx_list_prepend(NULL, " World!"); |
45 return 0; |
56 |
|
57 list = ucx_list_concat(list, list2); |
|
58 |
|
59 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") |
|
60 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") |
|
61 UCX_TEST_ASSERT(list->next->next == NULL, "failed") |
|
62 |
|
63 ucx_list_free(list2); |
|
64 ucx_list_free(list); |
|
65 |
|
66 UCX_TEST_END |
46 } |
67 } |
47 |
68 |
48 int dlist_tests() { |
69 UCX_TEST_BEGIN(test_ucx_list_size) { |
49 int r = 0; |
70 UcxList *list = ucx_list_append(NULL, "This "); |
50 int v[8]; |
71 list = ucx_list_append(list, "list "); |
51 UcxDlist *dl = NULL; |
72 list = ucx_list_append(list, "has "); |
52 // build list 0,1,2,3,4,5,6,7 |
73 list = ucx_list_append(list, "size "); |
53 printf(" Test ucx_dlist_append\n"); |
74 list = ucx_list_append(list, "5!"); |
54 fflush(stdout); |
75 |
55 for(int i=0;i<8;i++) { |
76 UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed"); |
56 v[i] = i; |
77 |
57 dl = ucx_dlist_append(dl, &v[i]); |
78 ucx_list_free(list); |
58 } |
79 |
59 |
80 UCX_TEST_END |
60 printf(" Test ucx_dlist_get\n"); |
|
61 fflush(stdout); |
|
62 for(int i=0;i<8;i++) { |
|
63 UcxDlist *elm = ucx_dlist_get(dl, i); |
|
64 if(elm == NULL) { |
|
65 fprintf(stderr, "ucx_dlist_get failed: element is NULL\n"); |
|
66 r--; |
|
67 } else if(elm->data == NULL) { |
|
68 fprintf(stderr, "ucx_dlist_get failed: data is NULL\n"); |
|
69 r--; |
|
70 } else { |
|
71 int *data = (int*)elm->data; |
|
72 if(*data != i) { |
|
73 fprintf(stderr, "ucx_dlist_get failed with index %d\n", i); |
|
74 r--; |
|
75 } |
|
76 } |
|
77 } |
|
78 |
|
79 printf(" Test ucx_dlist_free\n"); |
|
80 fflush(stdout); |
|
81 ucx_dlist_free(dl); |
|
82 |
|
83 dl = NULL; |
|
84 // build list 4,0,4 |
|
85 printf(" Test ucx_dlist_prepend\n"); |
|
86 dl = ucx_dlist_prepend(dl, &v[0]); |
|
87 dl = ucx_dlist_prepend(dl, &v[4]); |
|
88 dl = ucx_dlist_append(dl, &v[4]); |
|
89 |
|
90 struct foreach_testdata tdata; |
|
91 tdata.i = 0; |
|
92 ucx_dlist_foreach(dl, dlist_tests_foreach, &tdata); |
|
93 |
|
94 if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) { |
|
95 fprintf(stderr, "prepend/append test failed\n"); |
|
96 fprintf(stderr, "content: [%d, %d, %d]\n", |
|
97 tdata.values[0], tdata.values[1], tdata.values[2]); |
|
98 r--; |
|
99 } |
|
100 |
|
101 printf(" Test ucx_dlist_equals\n"); |
|
102 UcxDlist *dl2 = NULL; |
|
103 dl2 = ucx_dlist_append(dl2, &v[4]); |
|
104 dl2 = ucx_dlist_append(dl2, &v[0]); |
|
105 dl2 = ucx_dlist_append(dl2, &v[4]); |
|
106 if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { |
|
107 fprintf(stderr, "ucx_dlist_equals failed (false negative)\n"); |
|
108 r--; |
|
109 } |
|
110 dl2->next->data = NULL; |
|
111 if (ucx_dlist_equals(dl, dl2, NULL, NULL)) { |
|
112 fprintf(stderr, "ucx_dlist_equals failed (false positive)\n"); |
|
113 r--; |
|
114 } |
|
115 dl2->next->data = &(tdata.values[1]); |
|
116 if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) { |
|
117 fprintf(stderr, "ucx_dlist_equals failed (cmp_func false negative)\n"); |
|
118 r--; |
|
119 } |
|
120 if (ucx_dlist_equals(dl, dl2, NULL, NULL)) { |
|
121 fprintf(stderr, "ucx_dlist_equals failed (cmp_func false positive)\n"); |
|
122 r--; |
|
123 } |
|
124 ucx_dlist_free(dl2); |
|
125 |
|
126 printf(" Test ucx_dlist_clone\n"); |
|
127 dl2 = ucx_dlist_clone(dl, NULL, NULL); |
|
128 if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { |
|
129 fprintf(stderr, "ucx_dlist_clone (without copy) failed\n"); |
|
130 r--; |
|
131 } |
|
132 ucx_dlist_free(dl2); |
|
133 |
|
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); |
|
147 |
|
148 ucx_dlist_free(dl); |
|
149 |
|
150 dl = NULL; |
|
151 printf(" Test ucx_dlist_remove\n"); |
|
152 dl = ucx_dlist_append(dl, &v[4]); |
|
153 dl = ucx_dlist_append(dl, &v[0]); |
|
154 dl = ucx_dlist_append(dl, &v[3]); |
|
155 dl = ucx_dlist_remove(dl, dl->next); |
|
156 if (ucx_dlist_size(dl) == 2) { |
|
157 if ((*((int*)(dl->data)) != 4) || (*((int*)(dl->next->data)) != 3)) { |
|
158 fprintf(stderr, "ucx_dlist_remove failed (wrong data)\n"); |
|
159 r--; |
|
160 } |
|
161 } else { |
|
162 fprintf(stderr, "ucx_dlist_remove failed (wrong size)\n"); |
|
163 r--; |
|
164 } |
|
165 dl = ucx_dlist_remove(dl, dl); |
|
166 if (ucx_dlist_size(dl) == 1) { |
|
167 if ((*((int*)(dl->data)) != 3)) { |
|
168 fprintf(stderr, "ucx_dlist_remove first failed (wrong data)\n"); |
|
169 r--; |
|
170 } |
|
171 } else { |
|
172 fprintf(stderr, "ucx_dlist_remove first failed (wrong size)\n"); |
|
173 r--; |
|
174 } |
|
175 |
|
176 return r; |
|
177 } |
81 } |
178 |
82 |
|
83 UCX_TEST_BEGIN(test_ucx_list_last) { |
|
84 UcxList *list = ucx_list_append(NULL, "Find "); |
|
85 list = ucx_list_append(list, "the "); |
|
86 list = ucx_list_append(list, "last!"); |
|
87 |
|
88 char* last = (char*) (ucx_list_last(list)->data); |
|
89 |
|
90 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed"); |
|
91 |
|
92 ucx_list_free(list); |
|
93 |
|
94 UCX_TEST_END |
|
95 } |
|
96 |
|
97 UCX_TEST_BEGIN(test_ucx_list_get) { |
|
98 UcxList *list = ucx_list_append(NULL, "Find "); |
|
99 list = ucx_list_append(list, "the "); |
|
100 list = ucx_list_append(list, "mid!"); |
|
101 |
|
102 char* mid = (char*) (ucx_list_get(list, 1)->data); |
|
103 |
|
104 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed"); |
|
105 |
|
106 ucx_list_free(list); |
|
107 |
|
108 UCX_TEST_END |
|
109 } |
|
110 |
|
111 UCX_TEST_BEGIN(test_ucx_list_remove) { |
|
112 UcxList *list = ucx_list_append(NULL, "Hello"); |
|
113 list = ucx_list_append(list, " fucking"); |
|
114 list = ucx_list_append(list, " World!"); |
|
115 |
|
116 list = ucx_list_remove(list, ucx_list_get(list, 1)); |
|
117 |
|
118 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") |
|
119 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") |
|
120 UCX_TEST_ASSERT(list->next->next == NULL, "failed") |
|
121 |
|
122 ucx_list_free(list); |
|
123 |
|
124 UCX_TEST_END |
|
125 } |
|
126 |
|
127 UCX_TEST_BEGIN(test_ucx_list_clone) { |
|
128 |
|
129 char *hello = (char*)malloc(6); |
|
130 char *world = (char*)malloc(8); |
|
131 |
|
132 memcpy(hello, "Hello", 6); |
|
133 memcpy(world, " World!", 8); |
|
134 |
|
135 UcxList *list = ucx_list_append(NULL, hello); |
|
136 list = ucx_list_append(list, world); |
|
137 |
|
138 UcxList *copy = ucx_list_clone(list, copy_string, NULL); |
|
139 |
|
140 UCX_TEST_ASSERT(ucx_list_equals(list, copy, cmp_string, NULL), "failed") |
|
141 UCX_TEST_ASSERT(hello != copy->data, "first element is no copy") |
|
142 UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy") |
|
143 |
|
144 free(copy->next->data); |
|
145 free(copy->data); |
|
146 |
|
147 free(world); |
|
148 free(hello); |
|
149 free(list); |
|
150 free(copy); |
|
151 |
|
152 UCX_TEST_END |
|
153 } |