Write singly linked list including test cases
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
project(base64-test)
|
||||
project(linklist-lib-test)
|
||||
|
||||
add_custom_target(test "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}" "-r compact" "-s" DEPENDS ${PROJECT_NAME})
|
||||
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/catch-framework")
|
||||
aux_source_directory("src" TEST_SOURCES)
|
||||
|
||||
add_executable(${PROJECT_NAME} ${TEST_SOURCES})
|
||||
target_link_libraries(${PROJECT_NAME} base64-lib)
|
||||
target_link_libraries(${PROJECT_NAME} linklist-lib)
|
||||
|
||||
|
@@ -2,3 +2,255 @@
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
extern "C" {
|
||||
#include <linklist-lib/singly-linked-list.h>
|
||||
}
|
||||
|
||||
TEST_CASE("Append first element", "[SLL]")
|
||||
{
|
||||
SlList *list = NULL;
|
||||
void *ptr = (void *)0x1234;
|
||||
|
||||
list = sl_list_append(list, ptr);
|
||||
|
||||
REQUIRE(list->data == ptr);
|
||||
REQUIRE(list->next == NULL);
|
||||
|
||||
free(list);
|
||||
}
|
||||
|
||||
TEST_CASE("Append second element", "[SLL]")
|
||||
{
|
||||
SlList *list = NULL;
|
||||
void *ptr = (void *)0x1234;
|
||||
void *ptr2 = (void *)0x585796;
|
||||
|
||||
list = sl_list_append(list, ptr);
|
||||
|
||||
REQUIRE(list->data == ptr);
|
||||
REQUIRE(list->next == NULL);
|
||||
|
||||
list = sl_list_append(list, ptr2);
|
||||
|
||||
REQUIRE(list->data == ptr);
|
||||
REQUIRE(list->next != NULL);
|
||||
REQUIRE(list->next->data == ptr2);
|
||||
|
||||
if (list->next)
|
||||
free(list->next);
|
||||
if (list)
|
||||
free(list);
|
||||
}
|
||||
|
||||
TEST_CASE("Prepend element", "[SLL]")
|
||||
{
|
||||
SlList *list = NULL;
|
||||
void *ptr = (void *)0x12345;
|
||||
|
||||
list = sl_list_prepend(list, ptr);
|
||||
REQUIRE(list != NULL);
|
||||
REQUIRE(list->next == NULL);
|
||||
REQUIRE(list->data == ptr);
|
||||
if (list)
|
||||
free(list);
|
||||
}
|
||||
|
||||
TEST_CASE("Prepend second element", "[SLL]")
|
||||
{
|
||||
SlList *list = NULL;
|
||||
void *ptr = (void *)0x12345;
|
||||
void *ptr2 = (void *)0x1AA45;
|
||||
SlList *list_backup;
|
||||
|
||||
list = sl_list_prepend(list, ptr);
|
||||
REQUIRE(list != NULL);
|
||||
REQUIRE(list->next == NULL);
|
||||
REQUIRE(list->data == ptr);
|
||||
list_backup = list;
|
||||
|
||||
list = sl_list_prepend(list, ptr2);
|
||||
|
||||
REQUIRE(list != NULL);
|
||||
REQUIRE(list->next == list_backup);
|
||||
REQUIRE(list->data == ptr2);
|
||||
|
||||
if (list->next)
|
||||
free(list->next);
|
||||
if (list)
|
||||
free(list);
|
||||
}
|
||||
|
||||
TEST_CASE("Insert element empty list", "[SLL]")
|
||||
{
|
||||
SlList *list = NULL;
|
||||
void *ptr = (void *)0xAABB;
|
||||
|
||||
list = sl_list_insert(list, 0, ptr);
|
||||
REQUIRE(list != NULL);
|
||||
REQUIRE(list->next == NULL);
|
||||
REQUIRE(list->data == ptr);
|
||||
}
|
||||
|
||||
TEST_CASE("Insert element at beginning of list", "[SLL]")
|
||||
{
|
||||
SlList *list = NULL;
|
||||
void *ptr = (char *)0xAABB;
|
||||
void *ptr2 = (void *)0x454;
|
||||
|
||||
list = sl_list_insert(list, 0, ptr);
|
||||
REQUIRE(list != NULL);
|
||||
REQUIRE(list->next == NULL);
|
||||
REQUIRE(list->data == ptr);
|
||||
|
||||
|
||||
list = sl_list_insert(list, 0, ptr2);
|
||||
REQUIRE(list != NULL);
|
||||
REQUIRE(list->next != NULL);
|
||||
REQUIRE(list->data == ptr2);
|
||||
REQUIRE(list->next->data == ptr);
|
||||
REQUIRE(list->next->next == NULL);
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("Insert element at second position of list", "[SLL]")
|
||||
{
|
||||
SlList *list = NULL;
|
||||
void *ptr = (char *)0xAABB;
|
||||
void *ptr2 = (void *)0x454;
|
||||
|
||||
list = sl_list_append(list, ptr);
|
||||
list = sl_list_append(list, ptr);
|
||||
|
||||
list = sl_list_insert(list, 1, ptr2);
|
||||
REQUIRE(list != NULL);
|
||||
REQUIRE(list->next != NULL);
|
||||
REQUIRE(list->data == ptr);
|
||||
REQUIRE(list->next->data == ptr2);
|
||||
REQUIRE(list->data == ptr);
|
||||
REQUIRE(list->next->next != NULL);
|
||||
REQUIRE(list->next->next->data == ptr);
|
||||
REQUIRE(list->next->next->next == NULL);
|
||||
}
|
||||
|
||||
TEST_CASE("Insert element at overflow position of list", "[SLL]")
|
||||
{
|
||||
SlList *list = NULL;
|
||||
void *ptr = (char *)0xAABB;
|
||||
void *ptr2 = (void *)0x454;
|
||||
|
||||
list = sl_list_append(list, ptr);
|
||||
list = sl_list_append(list, ptr);
|
||||
|
||||
list = sl_list_insert(list, 300, ptr2);
|
||||
REQUIRE(list != NULL);
|
||||
REQUIRE(list->next != NULL);
|
||||
REQUIRE(list->next->next != NULL);
|
||||
REQUIRE(list->next->next->data == ptr2);
|
||||
}
|
||||
|
||||
TEST_CASE("Remove 1st element of empty list", "[SLL]")
|
||||
{
|
||||
SlList *list = NULL;
|
||||
|
||||
list = sl_list_remove(list, (void *)0x0);
|
||||
REQUIRE(list == NULL);
|
||||
}
|
||||
|
||||
TEST_CASE("Remove 1st element of list", "[SLL]")
|
||||
{
|
||||
SlList *list = NULL;
|
||||
void *ptr = (char *)0xAAB54B;
|
||||
|
||||
list = sl_list_append(list, ptr);
|
||||
|
||||
list = sl_list_remove(list, (void *)0x0);
|
||||
REQUIRE(list != NULL);
|
||||
|
||||
list = sl_list_remove(list, ptr);
|
||||
REQUIRE(list == NULL);
|
||||
}
|
||||
|
||||
TEST_CASE("Remove 2nd element of list", "[SLL]")
|
||||
{
|
||||
SlList *list = NULL;
|
||||
void *ptr = (char *)0xAAB54B;
|
||||
void *ptr2 = (char *)0xAA23B54B;
|
||||
|
||||
list = sl_list_append(list, ptr);
|
||||
list = sl_list_append(list, ptr2);
|
||||
|
||||
list = sl_list_remove(list, (void *)0x0);
|
||||
REQUIRE(list != NULL);
|
||||
REQUIRE(list->next != NULL);
|
||||
|
||||
list = sl_list_remove(list, ptr2);
|
||||
REQUIRE(list != NULL);
|
||||
REQUIRE(list->next == NULL);
|
||||
}
|
||||
|
||||
TEST_CASE("Remove 4th element of list", "[SLL]")
|
||||
{
|
||||
SlList *list = NULL;
|
||||
void *ptr = (char *)0xAAB54B;
|
||||
void *ptr2 = (char *)0xAA23B54B;
|
||||
|
||||
list = sl_list_append(list, ptr);
|
||||
list = sl_list_append(list, ptr);
|
||||
list = sl_list_append(list, ptr);
|
||||
list = sl_list_append(list, ptr2);
|
||||
|
||||
list = sl_list_remove(list, (void *)0x0);
|
||||
REQUIRE(list != NULL);
|
||||
REQUIRE(list->next != NULL);
|
||||
|
||||
list = sl_list_remove(list, ptr2);
|
||||
REQUIRE(list != NULL);
|
||||
REQUIRE(list->next->next->next == NULL);
|
||||
}
|
||||
|
||||
TEST_CASE("List length", "[SLL]")
|
||||
{
|
||||
SlList *list = NULL;
|
||||
void *ptr = (char *)0xAAB54B;
|
||||
uint32_t len;
|
||||
|
||||
len = sl_list_length(list);
|
||||
REQUIRE(len == 0);
|
||||
|
||||
list = sl_list_append(list, ptr);
|
||||
len = sl_list_length(list);
|
||||
REQUIRE(len == 1);
|
||||
|
||||
list = sl_list_append(list, ptr);
|
||||
len = sl_list_length(list);
|
||||
REQUIRE(len == 2);
|
||||
|
||||
list = sl_list_prepend(list, ptr);
|
||||
len = sl_list_length(list);
|
||||
REQUIRE(len == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("nth element", "[SLL]")
|
||||
{
|
||||
SlList *list = NULL;
|
||||
void *ptr = (char *)0xAAB54B;
|
||||
SlList *nth;
|
||||
|
||||
nth = sl_list_nth(list, 0);
|
||||
REQUIRE(nth == NULL);
|
||||
nth = sl_list_nth(list, 1);
|
||||
REQUIRE(nth == NULL);
|
||||
|
||||
list = sl_list_append(list, ptr);
|
||||
nth = sl_list_nth(list, 0);
|
||||
REQUIRE(nth == list);
|
||||
nth = sl_list_nth(list, 1);
|
||||
REQUIRE(nth == NULL);
|
||||
|
||||
list = sl_list_append(list, ptr);
|
||||
nth = sl_list_nth(list, 0);
|
||||
REQUIRE(nth == list);
|
||||
nth = sl_list_nth(list, 1);
|
||||
REQUIRE(nth == list->next);
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user