Fix regression in list module

This commit is contained in:
matt335672 2023-03-06 15:42:33 +00:00
parent a564bbbe5d
commit 8810d46ff1
2 changed files with 11 additions and 7 deletions

View File

@ -205,6 +205,7 @@ list_remove_item(struct list *self, int index)
int int
list_insert_item(struct list *self, int index, tbus item) list_insert_item(struct list *self, int index, tbus item)
{ {
int i;
if (index > self->count) if (index > self->count)
{ {
@ -220,15 +221,13 @@ list_insert_item(struct list *self, int index, tbus item)
return 0; return 0;
} }
// Move all the items above this location up one
for (i = self->count ; i > index ; --i)
{
self->items[i] = self->items[i - 1];
}
self->count++; self->count++;
if (self->count >= 2)
{
unsigned int i;
for (i = (self->count - 2); i >= (unsigned int)index; i--)
{
self->items[i + 1] = self->items[i];
}
}
self->items[index] = item; self->items[index] = item;
return 1; return 1;

View File

@ -43,6 +43,11 @@ START_TEST(test_list__simple)
val = list_get_item(lst, 10); val = list_get_item(lst, 10);
ck_assert_int_eq(val, 10); ck_assert_int_eq(val, 10);
list_insert_item(lst, 0, 99);
ck_assert_int_eq(lst->count, TEST_LIST_SIZE + 1);
val = list_get_item(lst, 10);
ck_assert_int_eq(val, 9);
list_clear(lst); list_clear(lst);
ck_assert_int_eq(lst->count, 0); ck_assert_int_eq(lst->count, 0);
list_delete(lst); list_delete(lst);