108 lines
4.4 KiB
C
108 lines
4.4 KiB
C
// Copyright (jacekpoz 2024). Licensed under the EUPL-1.2 or later.
|
|
|
|
#include <test.h>
|
|
#include <ptk_list.h>
|
|
|
|
PTK_LIST_DEFINE(uint32_t);
|
|
|
|
TEST_START()
|
|
TEST("create list", {
|
|
size_t size = 5;
|
|
PTK_LIST(uint32_t) list = PTK_LIST_NEW(uint32_t, size);
|
|
|
|
TEST_ASSERT(list.allocated == size, "incorrect list allocation");
|
|
TEST_ASSERT(list.size == 0, "list isn't empty after allocation");
|
|
});
|
|
|
|
TEST("add elements without growing", {
|
|
size_t size = 5;
|
|
PTK_LIST(uint32_t) list = PTK_LIST_NEW(uint32_t, size);
|
|
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){21});
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){37});
|
|
|
|
TEST_ASSERT(list.size == 2, "size doesn't match number of elements added");
|
|
TEST_ASSERT(list.allocated == size, "needlessly grew list");
|
|
|
|
TEST_ASSERT(list.data[0] == 21, "first element doesn't match");
|
|
TEST_ASSERT(list.data[1] == 37, "second element doesn't match");
|
|
});
|
|
|
|
TEST("add elements and grow", {
|
|
size_t size = 1;
|
|
PTK_LIST(uint32_t) list = PTK_LIST_NEW(uint32_t, size);
|
|
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){21});
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){37});
|
|
|
|
TEST_ASSERT(list.allocated == size * 2, "(1st grow) didn't grow size by a factor of 2");
|
|
TEST_ASSERT(list.data[1] == 37, "(1st grow) element added in grown space doesn't match");
|
|
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){2137});
|
|
|
|
TEST_ASSERT(list.allocated == size * 4, "(2nd grow) didn't grow size by a factor of 2");
|
|
TEST_ASSERT(list.data[2] == 2137, "(2nd grow) element added in grown space doesn't match");
|
|
});
|
|
|
|
TEST("add multiple elements", {
|
|
size_t size = 1;
|
|
PTK_LIST(uint32_t) list = PTK_LIST_NEW(uint32_t, size);
|
|
|
|
PTK_LIST_ADD_ALL(uint32_t, list, { 21, 37, 2137, 31, 27, 7312 });
|
|
TEST_ASSERT(list.size == 6, "size doesn't match number of elements added");
|
|
|
|
TEST_ASSERT(list.data[0] == 21, "first element doesn't match");
|
|
TEST_ASSERT(list.data[5] == 7312, "last element doesn't match");
|
|
});
|
|
|
|
TEST("remove elements", {
|
|
size_t size = 1;
|
|
PTK_LIST(uint32_t) list = PTK_LIST_NEW(uint32_t, size);
|
|
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){21});
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){37});
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){2137});
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){31});
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){27});
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){7312});
|
|
|
|
PTK_LIST_REMOVE(uint32_t, list, (uint32_t){2137});
|
|
TEST_ASSERT(list.size == 5, "size doesn't match after removing");
|
|
TEST_ASSERT(list.data[2] == 31, "remaining elements not moved to the left");
|
|
TEST_ASSERT(list.data[4] == 7312, "last element moved improperly (check for off-by-ones)");
|
|
});
|
|
|
|
TEST("remove multiple elements", {
|
|
size_t size = 1;
|
|
PTK_LIST(uint32_t) list = PTK_LIST_NEW(uint32_t, size);
|
|
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){21});
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){37});
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){2137});
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){31});
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){27});
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){7312});
|
|
|
|
PTK_LIST_REMOVE_ALL(uint32_t, list, { 2137, 37, 27 });
|
|
TEST_ASSERT(list.size == 3, "size doesn't match after removing");
|
|
TEST_ASSERT(list.data[1] == 31, "remaining elements not moved to the left");
|
|
TEST_ASSERT(list.data[2] == 7312, "last element moved improperly (check for off-by-ones)");
|
|
});
|
|
|
|
TEST("remove elements at index", {
|
|
size_t size = 1;
|
|
PTK_LIST(uint32_t) list = PTK_LIST_NEW(uint32_t, size);
|
|
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){21});
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){37});
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){2137});
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){31});
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){27});
|
|
PTK_LIST_ADD(uint32_t, list, (uint32_t){7312});
|
|
|
|
PTK_LIST_REMOVE_AT(uint32_t, list, 2);
|
|
TEST_ASSERT(list.size == 5, "size doesn't match after removing");
|
|
TEST_ASSERT(list.data[2] == 31, "remaining elements not moved to the left");
|
|
TEST_ASSERT(list.data[4] == 7312, "last element moved improperly (check for off-by-ones)");
|
|
});
|
|
TEST_FINISH()
|