Sun, 03 Oct 2021 18:51:44 +0200
add tests for high level functions
test/test_list.c | file | annotate | diff | comparison | revisions |
--- a/test/test_list.c Sun Oct 03 18:37:13 2021 +0200 +++ b/test/test_list.c Sun Oct 03 18:51:44 2021 +0200 @@ -174,6 +174,161 @@ CU_ASSERT_TRUE(cxTestingAllocatorVerify()) } +void test_hl_linked_list_add(void) { + cxTestingAllocatorReset(); + + int data; + CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); + + data = 5; + CU_ASSERT_EQUAL(cxTypedListAdd(int, list, &data), 0) + data = 47; + CU_ASSERT_EQUAL(cxTypedListAdd(int, list, &data), 0) + data = 13; + CU_ASSERT_EQUAL(cxTypedListAdd(int, list, &data), 0) + + CU_ASSERT_EQUAL(list->size, 3) + CU_ASSERT_TRUE(list->capacity >= list->size) + + CU_ASSERT_EQUAL(*cxTypedListAt(int, list, 0), 5) + CU_ASSERT_EQUAL(*cxTypedListAt(int, list, 1), 47) + CU_ASSERT_EQUAL(*cxTypedListAt(int, list, 2), 13) + + cxLinkedListDestroy(list); + CU_ASSERT_TRUE(cxTestingAllocatorVerify()) +} + +void test_hl_linked_list_last(void) { + cxTestingAllocatorReset(); + + int data; + CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); + + CU_ASSERT_PTR_NULL(cxTypedListLast(int, list)) + + data = 5; + CU_ASSERT_EQUAL(cxTypedListAdd(int, list, &data), 0) + CU_ASSERT_EQUAL(*cxTypedListLast(int, list), 5) + + data = 47; + CU_ASSERT_EQUAL(cxTypedListAdd(int, list, &data), 0) + CU_ASSERT_EQUAL(*cxTypedListLast(int, list), 47) + + cxLinkedListDestroy(list); + CU_ASSERT_TRUE(cxTestingAllocatorVerify()) +} + +void test_hl_linked_list_insert(void) { + cxTestingAllocatorReset(); + + int data; + CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); + + data = 5; + CU_ASSERT_NOT_EQUAL(cxTypedListInsert(int, list, 1, &data), 0) + CU_ASSERT_EQUAL(list->size, 0) + CU_ASSERT_EQUAL(cxTypedListInsert(int, list, 0, &data), 0) + CU_ASSERT_EQUAL(list->size, 1) + data = 47; + CU_ASSERT_EQUAL(cxTypedListInsert(int, list, 0, &data), 0) + CU_ASSERT_EQUAL(list->size, 2) + data = 13; + CU_ASSERT_EQUAL(cxTypedListInsert(int, list, 1, &data), 0) + CU_ASSERT_EQUAL(list->size, 3) + data = 42; + CU_ASSERT_EQUAL(cxTypedListInsert(int, list, 3, &data), 0) + + CU_ASSERT_EQUAL(list->size, 4) + CU_ASSERT_TRUE(list->capacity >= list->size) + + CU_ASSERT_EQUAL(*cxTypedListAt(int, list, 0), 47) + CU_ASSERT_EQUAL(*cxTypedListAt(int, list, 1), 13) + CU_ASSERT_EQUAL(*cxTypedListAt(int, list, 2), 5) + CU_ASSERT_EQUAL(*cxTypedListAt(int, list, 3), 42) + + cxLinkedListDestroy(list); + CU_ASSERT_TRUE(cxTestingAllocatorVerify()) +} + +void test_hl_linked_list_remove(void) { + cxTestingAllocatorReset(); + + int data; + CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); + + data = 5; + cxTypedListAdd(int, list, &data); + data = 47; + cxTypedListAdd(int, list, &data); + data = 42; + cxTypedListAdd(int, list, &data); + data = 13; + cxTypedListAdd(int, list, &data); + + CU_ASSERT_EQUAL(list->size, 4) + CU_ASSERT_TRUE(list->capacity >= list->size) + + CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0) + + CU_ASSERT_EQUAL(cxListRemove(list, 2), 0) + CU_ASSERT_EQUAL(list->size, 3) + CU_ASSERT_TRUE(list->capacity >= list->size) + CU_ASSERT_EQUAL(*cxTypedListAt(int, list, 0), 5) + CU_ASSERT_EQUAL(*cxTypedListAt(int, list, 1), 47) + CU_ASSERT_EQUAL(*cxTypedListAt(int, list, 2), 13) + + CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) + CU_ASSERT_EQUAL(list->size, 2) + CU_ASSERT_TRUE(list->capacity >= list->size) + CU_ASSERT_EQUAL(*cxTypedListAt(int, list, 0), 47) + CU_ASSERT_EQUAL(*cxTypedListAt(int, list, 1), 13) + + CU_ASSERT_EQUAL(cxListRemove(list, 1), 0) + CU_ASSERT_EQUAL(list->size, 1) + CU_ASSERT_TRUE(list->capacity >= list->size) + CU_ASSERT_EQUAL(*cxTypedListAt(int, list, 0), 47) + + CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) + CU_ASSERT_EQUAL(list->size, 0) + CU_ASSERT_TRUE(list->capacity >= list->size) + + CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0) + + cxLinkedListDestroy(list); + CU_ASSERT_TRUE(cxTestingAllocatorVerify()) +} + +void test_hl_linked_list_find(void) { + cxTestingAllocatorReset(); + + int data, criteria; + CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); + + data = 5; + cxTypedListAdd(int, list, &data); + data = 47; + cxTypedListAdd(int, list, &data); + data = 13; + cxTypedListAdd(int, list, &data); + + CU_ASSERT_EQUAL(list->size, 3) + CU_ASSERT_TRUE(list->capacity >= list->size) + + criteria = 5; + CU_ASSERT_EQUAL(cxTypedListFind(int, list, &criteria), 0) + criteria = 47; + CU_ASSERT_EQUAL(cxTypedListFind(int, list, &criteria), 1) + criteria = 13; + CU_ASSERT_EQUAL(cxTypedListFind(int, list, &criteria), 2) + criteria = 9000; + CU_ASSERT_EQUAL(cxTypedListFind(int, list, &criteria), 3) + criteria = -5; + CU_ASSERT_EQUAL(cxTypedListFind(int, list, &criteria), 3) + + cxLinkedListDestroy(list); + CU_ASSERT_TRUE(cxTestingAllocatorVerify()) +} + int main() { CU_pSuite suite = NULL; @@ -181,21 +336,20 @@ return CU_get_error(); } - suite = CU_add_suite("low level linked list suite", NULL, NULL); + suite = CU_add_suite("low level linked list", NULL, NULL); cu_add_test(suite, test_linked_list_at); cu_add_test(suite, test_linked_list_add); cu_add_test(suite, test_linked_list_last); - suite = CU_add_suite("high level linked list suite", NULL, NULL); + suite = CU_add_suite("high level linked list", NULL, NULL); cu_add_test(suite, test_hl_linked_list_create); - /* cu_add_test(suite, test_hl_linked_list_add); cu_add_test(suite, test_hl_linked_list_last); cu_add_test(suite, test_hl_linked_list_insert); cu_add_test(suite, test_hl_linked_list_remove); - cu_add_test(suite, test_hl_linked_list_find);*/ + cu_add_test(suite, test_hl_linked_list_find); CU_basic_set_mode(UCX_CU_BRM);