test/list_tests.c

changeset 123
7fb0f74517c5
parent 122
540d99722f1f
child 134
4d320dc3a7af
equal deleted inserted replaced
122:540d99722f1f 123:7fb0f74517c5
97 ucx_list_free(list); 97 ucx_list_free(list);
98 } 98 }
99 99
100 UCX_TEST_IMPLEMENT(test_ucx_list_size) { 100 UCX_TEST_IMPLEMENT(test_ucx_list_size) {
101 UcxList *list = ucx_list_append(NULL, (void*)"This "); 101 UcxList *list = ucx_list_append(NULL, (void*)"This ");
102 UCX_TEST_BEGIN
103 list = ucx_list_append(list, (void*)"list "); 102 list = ucx_list_append(list, (void*)"list ");
104 list = ucx_list_append(list, (void*)"has "); 103 list = ucx_list_append(list, (void*)"has ");
105 list = ucx_list_append(list, (void*)"size "); 104 list = ucx_list_append(list, (void*)"size ");
106 list = ucx_list_append(list, (void*)"5!"); 105 list = ucx_list_append(list, (void*)"5!");
107 106
107 UCX_TEST_BEGIN
108
108 UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed"); 109 UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
109 110
110 UCX_TEST_END 111 UCX_TEST_END
111 ucx_list_free(list); 112 ucx_list_free(list);
112 } 113 }
113 114
114 UCX_TEST_IMPLEMENT(test_ucx_list_first) { 115 UCX_TEST_IMPLEMENT(test_ucx_list_first) {
115 UcxList *list = ucx_list_append(NULL, (void*)"Find "); 116 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
116 UCX_TEST_BEGIN
117 list = ucx_list_append(list, (void*)"the "); 117 list = ucx_list_append(list, (void*)"the ");
118 list = ucx_list_append(list, (void*)"first!"); 118 list = ucx_list_append(list, (void*)"first!");
119 119
120 UCX_TEST_BEGIN
121
120 const char* first = (const char*) (ucx_list_first(list)->data); 122 const char* first = (const char*) (ucx_list_first(list)->data);
121 123
122 UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed"); 124 UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
123 125
124 UCX_TEST_END 126 UCX_TEST_END
125 ucx_list_free(list); 127 ucx_list_free(list);
126 } 128 }
127 129
128 UCX_TEST_IMPLEMENT(test_ucx_list_last) { 130 UCX_TEST_IMPLEMENT(test_ucx_list_last) {
129 UcxList *list = ucx_list_append(NULL, (void*)"Find "); 131 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
130 UCX_TEST_BEGIN
131 list = ucx_list_append(list, (void*)"the "); 132 list = ucx_list_append(list, (void*)"the ");
132 list = ucx_list_append(list, (void*)"last!"); 133 list = ucx_list_append(list, (void*)"last!");
133 134
135 UCX_TEST_BEGIN
136
134 const char* last = (const char*) (ucx_list_last(list)->data); 137 const char* last = (const char*) (ucx_list_last(list)->data);
135 138
136 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed"); 139 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
137 140
138 UCX_TEST_END 141 UCX_TEST_END
139 ucx_list_free(list); 142 ucx_list_free(list);
140 } 143 }
141 144
142 UCX_TEST_IMPLEMENT(test_ucx_list_get) { 145 UCX_TEST_IMPLEMENT(test_ucx_list_get) {
143 UcxList *list = ucx_list_append(NULL, (void*)"Find "); 146 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
144 UCX_TEST_BEGIN
145 list = ucx_list_append(list, (void*)"the "); 147 list = ucx_list_append(list, (void*)"the ");
146 list = ucx_list_append(list, (void*)"mid!"); 148 list = ucx_list_append(list, (void*)"mid!");
147 149
150 UCX_TEST_BEGIN
151
148 const char* mid = (const char*) (ucx_list_get(list, 1)->data); 152 const char* mid = (const char*) (ucx_list_get(list, 1)->data);
149 153
150 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed"); 154 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
151 155
152 UCX_TEST_END 156 UCX_TEST_END
153 ucx_list_free(list); 157 ucx_list_free(list);
158 }
159
160 UCX_TEST_IMPLEMENT(test_ucx_list_indexof) {
161 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
162 list = ucx_list_append(list, (void*)"the ");
163 list = ucx_list_append(list, (void*)"mid!");
164
165 UCX_TEST_BEGIN
166
167 UCX_TEST_ASSERT(ucx_list_indexof(list, list) == 0, "failed");
168 UCX_TEST_ASSERT(ucx_list_indexof(list, list->next) == 1, "failed");
169 UCX_TEST_ASSERT(ucx_list_indexof(list, ucx_list_get(list, 2)) == 2,
170 "failed");
171
172 UcxList *otherlist = ucx_list_append(NULL, (void*) "foobar");
173 UCX_TEST_ASSERT(ucx_list_indexof(list, otherlist) == -1, "failed");
174 ucx_list_free(otherlist);
175
176 UCX_TEST_END
177 ucx_list_free(list);
178 }
179
180 UCX_TEST_IMPLEMENT(test_ucx_list_find) {
181 UcxList *l = ucx_list_append(NULL, (void*)"find ");
182 l = ucx_list_append(l, (void*)"some ");
183 l = ucx_list_append(l, (void*)"string!");
184
185 UCX_TEST_BEGIN
186
187 UCX_TEST_ASSERT(ucx_list_find(l,(void*)"some ",ucx_strcmp,NULL) == 1,
188 "doesn't find string");
189 UCX_TEST_ASSERT(ucx_list_find(l,(void*)"a",ucx_strcmp,NULL) == -1,
190 "finds non-existing string");
191
192 UCX_TEST_END
193 ucx_list_free(l);
154 } 194 }
155 195
156 UCX_TEST_IMPLEMENT(test_ucx_list_contains) { 196 UCX_TEST_IMPLEMENT(test_ucx_list_contains) {
157 UcxList *l = ucx_list_append(NULL, (void*)"Contains "); 197 UcxList *l = ucx_list_append(NULL, (void*)"Contains ");
158 UCX_TEST_BEGIN
159 l = ucx_list_append(l, (void*)"a "); 198 l = ucx_list_append(l, (void*)"a ");
160 l = ucx_list_append(l, (void*)"string!"); 199 l = ucx_list_append(l, (void*)"string!");
161 200
162 UCX_TEST_ASSERT(ucx_list_contains(l,(void*)"a ",ucx_strcmp,NULL),"failed"); 201 UCX_TEST_BEGIN
163 UCX_TEST_ASSERT(!ucx_list_contains(l,(void*)"a",ucx_strcmp,NULL),"failed"); 202
203 UCX_TEST_ASSERT(ucx_list_contains(l,(void*)"a ",ucx_strcmp,NULL),
204 "false negative");
205 UCX_TEST_ASSERT(!ucx_list_contains(l,(void*)"a",ucx_strcmp,NULL),
206 "false positive");
164 207
165 UCX_TEST_END 208 UCX_TEST_END
166 ucx_list_free(l); 209 ucx_list_free(l);
167 } 210 }
168 211
169 UCX_TEST_IMPLEMENT(test_ucx_list_remove) { 212 UCX_TEST_IMPLEMENT(test_ucx_list_remove) {
170 UcxList *list = ucx_list_append(NULL, (void*)"Hello"); 213 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
171 UCX_TEST_BEGIN
172 list = ucx_list_append(list, (void*)" fucking"); 214 list = ucx_list_append(list, (void*)" fucking");
173 list = ucx_list_append(list, (void*)" World!"); 215 list = ucx_list_append(list, (void*)" World!");
216
217 UCX_TEST_BEGIN
174 218
175 list = ucx_list_remove(list, ucx_list_get(list, 1)); 219 list = ucx_list_remove(list, ucx_list_get(list, 1));
176 220
177 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, 221 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
178 "failed"); 222 "failed");

mercurial