diff --git a/winpr/libwinpr/utils/collections/ArrayList.c b/winpr/libwinpr/utils/collections/ArrayList.c index b29f0d5be..0d246f6ce 100644 --- a/winpr/libwinpr/utils/collections/ArrayList.c +++ b/winpr/libwinpr/utils/collections/ArrayList.c @@ -148,7 +148,7 @@ void ArrayList_Shift(wArrayList* arrayList, int index, int count) } else if (count < 0) { - MoveMemory(&arrayList->array[index + count], &arrayList->array[index], (arrayList->size - index) * sizeof(void*)); + MoveMemory(&arrayList->array[index], &arrayList->array[index - count], (arrayList->size + count) * sizeof(void*)); arrayList->size += count; } } diff --git a/winpr/libwinpr/utils/test/TestArrayList.c b/winpr/libwinpr/utils/test/TestArrayList.c index 59a8e7e72..db0d41ec7 100644 --- a/winpr/libwinpr/utils/test/TestArrayList.c +++ b/winpr/libwinpr/utils/test/TestArrayList.c @@ -7,11 +7,13 @@ int TestArrayList(int argc, char* argv[]) { int index; int count; + size_t val; wArrayList* arrayList; + const int elemsToInsert = 10; arrayList = ArrayList_New(TRUE); - for (index = 0; index < 10; index++) + for (index = 0; index < elemsToInsert; index++) { ArrayList_Add(arrayList, (void*) (size_t) index); } @@ -43,6 +45,26 @@ int TestArrayList(int argc, char* argv[]) if (index != 6) return -1; + for (index = 0; index < elemsToInsert; index++) { + val = (size_t)ArrayList_GetItem(arrayList, 0); + ArrayList_RemoveAt(arrayList, 0); + if (val != index) + { + printf("ArrayList: shifted %d entries, expected value %d, got %d\n", index, index, val); + return -1; + } + } + + index = ArrayList_IndexOf(arrayList, (void*) (size_t) elemsToInsert, -1, -1); + printf("ArrayList index: %d\n", index); + if (index != -1) + return -1; + + count = ArrayList_Count(arrayList); + printf("ArrayList count: %d\n", count); + if (count != 0) + return -1; + ArrayList_Clear(arrayList); ArrayList_Free(arrayList);