test/list_tests.c

changeset 162
52dfe5f4ecd7
parent 134
4d320dc3a7af
child 172
7084e8e8433c
equal deleted inserted replaced
161:1031dd910f8e 162:52dfe5f4ecd7
209 ucx_list_free(l); 209 ucx_list_free(l);
210 } 210 }
211 211
212 UCX_TEST(test_ucx_list_remove) { 212 UCX_TEST(test_ucx_list_remove) {
213 UcxList *list = ucx_list_append(NULL, (void*)"Hello"); 213 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
214 list = ucx_list_append(list, (void*)" fucking"); 214 list = ucx_list_append(list, (void*)"fucking");
215 list = ucx_list_append(list, (void*)" World!"); 215 list = ucx_list_append(list, (void*)"World!");
216
217 UcxList *list2 = ucx_list_append(NULL, (void*)"A");
218 list2 = ucx_list_append(list2, (void*)"B");
219 list2 = ucx_list_append(list2, (void*)"C");
220 list2 = ucx_list_append(list2, (void*)"D");
221 list2 = ucx_list_append(list2, (void*)"E");
222 list2 = ucx_list_append(list2, (void*)"F");
223 list2 = ucx_list_append(list2, (void*)"G");
216 224
217 UCX_TEST_BEGIN 225 UCX_TEST_BEGIN
218 226
219 list = ucx_list_remove(list, ucx_list_get(list, 1)); 227 list = ucx_list_remove(list, ucx_list_get(list, 1));
220 228
221 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, 229 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
222 "failed"); 230 "failed");
223 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0, 231 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, "World!", 7) == 0,
224 "failed"); 232 "failed");
225 UCX_TEST_ASSERT(list->next->next == NULL, "failed"); 233 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
234
235 // remove first element: B, C, D, E, F, G
236 list2 = ucx_list_remove(list2, list2);
237
238 UCX_TEST_ASSERT(ucx_list_size(list2) == 6, "list2 has wrong size");
239 UCX_TEST_ASSERT(strncmp((const char*)list2->data, "B", 1) == 0,
240 "wrong first element");
241 UCX_TEST_ASSERT(strncmp((const char*)ucx_list_get(list2, 5)->data, "G", 1)
242 == 0, "wrong last element");
243
244 // remove second element: B, D, E, F, G
245 list2 = ucx_list_remove(list2, list2->next);
246
247 UCX_TEST_ASSERT(ucx_list_size(list2) == 5, "list2 has wrong size");
248 UCX_TEST_ASSERT(strncmp((const char*)list2->next->data, "D", 1) == 0,
249 "wrong second element");
250
251 UcxList *last = ucx_list_get(list2, 4);
252 list2 = ucx_list_remove(list2, last->prev);
253
254 UCX_TEST_ASSERT(ucx_list_size(list2) == 4, "list2 has wrong size");
255 UCX_TEST_ASSERT(strncmp((const char*)last->prev->data, "E", 1) == 0,
256 "wrong element");
257
258 // remove last element: B, D, E, F
259 list2 = ucx_list_remove(list2, last);
260 UCX_TEST_ASSERT(ucx_list_size(list2) == 3, "list2 has wrong size");
261 UCX_TEST_ASSERT(strncmp((const char*)ucx_list_get(list2, 2)->data, "E", 1)
262 == 0, "wrong last element");
263
264 UCX_TEST_ASSERT(strncmp((const char*)list2->data, "B", 1) == 0,
265 "wrong element");
266
267 list2 = ucx_list_remove(list2, list2);
268 UCX_TEST_ASSERT(ucx_list_size(list2) == 2, "list2 has wrong size");
269 list2 = ucx_list_remove(list2, list2);
270 UCX_TEST_ASSERT(ucx_list_size(list2) == 1, "list2 has wrong size");
271 list2 = ucx_list_remove(list2, list2);
272 UCX_TEST_ASSERT(list2 == NULL, "list2 is not null");
226 273
227 UCX_TEST_END 274 UCX_TEST_END
228 ucx_list_free(list); 275 ucx_list_free(list);
229 } 276 }
230 277

mercurial