mirror of
https://github.com/bkthomps/Containers.git
synced 2025-11-16 04:24:47 +00:00
Fix bug which occurred when removing the last node from a list (#64)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2017-2019 Bailey Thompson
|
||||
* Copyright (c) 2017-2020 Bailey Thompson
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -278,7 +278,10 @@ int list_remove_at(list me, const int index)
|
||||
return -EINVAL;
|
||||
}
|
||||
traverse = list_get_node_at(me, index);
|
||||
if (index == 0) {
|
||||
if (me->item_count == 1) {
|
||||
me->head = NULL;
|
||||
me->tail = NULL;
|
||||
} else if (index == 0) {
|
||||
traverse->next->prev = NULL;
|
||||
me->head = traverse->next;
|
||||
} else if (index == me->item_count - 1) {
|
||||
|
||||
22
tst/list.c
22
tst/list.c
@@ -260,6 +260,27 @@ static void test_add_last_out_of_memory(void)
|
||||
assert(!list_destroy(me));
|
||||
}
|
||||
|
||||
void test_remove_all(void)
|
||||
{
|
||||
int i;
|
||||
list me = list_init(sizeof(int));
|
||||
for (i = 0; i < 100; i++) {
|
||||
list_add_first(me, &i);
|
||||
assert(list_size(me) == i + 1);
|
||||
}
|
||||
for (i = 0; i < 100; i++) {
|
||||
list_remove_first(me);
|
||||
assert(list_size(me) == 100 - i - 1);
|
||||
}
|
||||
assert(list_is_empty(me));
|
||||
for (i = 0; i < 100; i++) {
|
||||
list_add_first(me, &i);
|
||||
assert(list_size(me) == i + 1);
|
||||
}
|
||||
assert(list_size(me) == 100);
|
||||
assert(!list_destroy(me));
|
||||
}
|
||||
|
||||
void test_list(void)
|
||||
{
|
||||
test_invalid_init();
|
||||
@@ -268,4 +289,5 @@ void test_list(void)
|
||||
test_add_first_out_of_memory();
|
||||
test_add_at_out_of_memory();
|
||||
test_add_last_out_of_memory();
|
||||
test_remove_all();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user