Fix bug which occurred when removing the last node from a list (#64)

This commit is contained in:
Bailey Thompson
2020-08-07 01:40:05 -04:00
committed by GitHub
parent eb4f8fb1ae
commit 163fdb32ce
2 changed files with 27 additions and 2 deletions

View File

@@ -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) {

View File

@@ -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();
}