void test_list_remove_entry(void) { ListEntry *empty_list = NULL; ListEntry *list; ListEntry *entry; list = generate_list(); /* Remove the third entry */ entry = list_nth_entry(list, 2); assert(list_remove_entry(&list, entry) != 0); assert(list_length(list) == 3); check_list_integrity(list); /* Remove the first entry */ entry = list_nth_entry(list, 0); assert(list_remove_entry(&list, entry) != 0); assert(list_length(list) == 2); check_list_integrity(list); /* Try some invalid removes */ /* NULL */ assert(list_remove_entry(&list, NULL) == 0); /* Removing NULL from an empty list */ assert(list_remove_entry(&empty_list, NULL) == 0); list_free(list); /* Test removing an entry when it is the only entry. */ list = NULL; assert(list_append(&list, &variable1) != NULL); assert(list != NULL); assert(list_remove_entry(&list, list) != 0); assert(list == NULL); /* Test removing the last entry */ list = generate_list(); entry = list_nth_entry(list, 3); assert(list_remove_entry(&list, entry) != 0); check_list_integrity(list); list_free(list); }
void test_list_nth_entry(void) { ListEntry *list; ListEntry *entry; list = generate_list(); /* Check all values in the list */ entry = list_nth_entry(list, 0); assert(list_data(entry) == &variable1); entry = list_nth_entry(list, 1); assert(list_data(entry) == &variable2); entry = list_nth_entry(list, 2); assert(list_data(entry) == &variable3); entry = list_nth_entry(list, 3); assert(list_data(entry) == &variable4); /* Check out of range values */ entry = list_nth_entry(list, 4); assert(entry == NULL); entry = list_nth_entry(list, 400); assert(entry == NULL); list_free(list); }
ListValue list_nth_data(ListEntry *list, unsigned int n) { ListEntry *entry; /* Find the specified entry */ entry = list_nth_entry(list, n); /* If out of range, return NULL, otherwise return the data */ if (entry == NULL) { return LIST_NULL; } else { return entry->data; } }