Fixing bug in head_list
This commit is contained in:
@@ -95,17 +95,23 @@ void for_each_list(list_t *list, listIterator iterator)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void head_list(list_t *list, void *element, bool removeFromList)
|
void head_list(list_t *list, void **element, bool removeFromList)
|
||||||
{
|
{
|
||||||
assert(list->head != NULL);
|
assert(list->head != NULL);
|
||||||
|
|
||||||
listNode *node = list->head;
|
listNode *node = list->head;
|
||||||
memcpy(element, node->data, list->elementSize);
|
|
||||||
|
*element = (void *)malloc(list->elementSize);
|
||||||
|
memcpy(*element, node->data, list->elementSize);
|
||||||
|
|
||||||
if(removeFromList) {
|
if(removeFromList) {
|
||||||
list->head = node->next;
|
list->head = node->next;
|
||||||
list->logicalLength--;
|
list->logicalLength--;
|
||||||
|
|
||||||
|
if (list->freeFn) {
|
||||||
|
list->freeFn(node->data);
|
||||||
|
}
|
||||||
|
|
||||||
free(node->data);
|
free(node->data);
|
||||||
free(node);
|
free(node);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ void for_each_list(list_t *list, listIterator iterator);
|
|||||||
/**
|
/**
|
||||||
@brief Returns the head of the list (optionally removing it at the same time).
|
@brief Returns the head of the list (optionally removing it at the same time).
|
||||||
*/
|
*/
|
||||||
void head_list(list_t *list, void *element, bool removeFromList);
|
void head_list(list_t *list, void **element, bool removeFromList);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@brief Returns the tail of the list.
|
@brief Returns the tail of the list.
|
||||||
|
|||||||
@@ -88,4 +88,31 @@ BOOST_AUTO_TEST_CASE(test_string_list) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(test_head_list) {
|
||||||
|
int i, numNames = 5;
|
||||||
|
const char *names[] = { "David", "Kevin", "Michael", "Craig", "Jimi" };
|
||||||
|
|
||||||
|
list_t *list = NULL;
|
||||||
|
|
||||||
|
list = create_list(sizeof(char *), free_string);
|
||||||
|
|
||||||
|
char *name;
|
||||||
|
for (i = 0; i < numNames; i++) {
|
||||||
|
name = _strdup(names[i]);
|
||||||
|
append_list(list, &name);
|
||||||
|
}
|
||||||
|
BOOST_CHECK(size_list(list) == 5);
|
||||||
|
|
||||||
|
|
||||||
|
char *temp = NULL;
|
||||||
|
head_list(list, &(void *)temp, true);
|
||||||
|
name = *(char **)temp;
|
||||||
|
|
||||||
|
BOOST_CHECK(size_list(list) == 4);
|
||||||
|
|
||||||
|
free(temp);
|
||||||
|
delete_list(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|||||||
Reference in New Issue
Block a user