changeset 760:1cb9454ec569

Add some simple tests for linked lists.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 05 Feb 2023 04:00:50 +0200
parents 618c7fa3a4f8
children 2263dd13cf2d
files tests.c
diffstat 1 files changed, 115 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/tests.c	Sun Feb 05 02:16:22 2023 +0200
+++ b/tests.c	Sun Feb 05 04:00:50 2023 +0200
@@ -490,7 +490,7 @@
 }
 
 
-void test_config(void)
+void test_config_file(void)
 {
     static const char *filename = "cfg.temp";
     test_ctx ctx;
@@ -573,6 +573,112 @@
 }
 
 
+bool test_linked_list_length(th_llist_t *list, const size_t n)
+{
+    test_ctx ctx;
+    bool ok = th_llist_length(list) == n;
+
+    test_start(&ctx, "Test list length matching");
+    test_result(&ctx, ok);
+    test_end(&ctx);
+
+    return ok;
+}
+
+
+bool test_linked_list_validity(th_llist_t *list, const size_t nstart, const ssize_t ndelta)
+{
+    test_ctx ctx;
+    th_llist_t *node;
+    size_t n;
+    bool ok = true;
+
+    for (n = nstart, node = list; node != NULL; node = node->next, n += ndelta)
+    {
+        if ((size_t) node->data != n)
+        {
+            ok = false;
+            break;
+        }
+    }
+
+    test_start(&ctx, "Test list data contents");
+    test_result_msg(&ctx, ok, "failure at index #%d", n);
+    test_end(&ctx);
+
+    return ok;
+}
+
+
+int test_llist_sortfunc(const th_llist_t *lnode, const th_llist_t *rnode, void *userdata)
+{
+    (void) userdata;
+    return lnode->data <= rnode->data;
+}
+
+
+void test_linked_lists(void)
+{
+    th_llist_t *list = NULL, *node;
+    size_t nnodes = 128, n;
+
+    tprint(2, "Creating linked list\n");
+    for (n = 0; n < nnodes; n++)
+    {
+        if ((node = th_llist_append(&list, (void *) n)) == NULL)
+        {
+            THERR("Error allocating memory.\n");
+            goto out;
+        }
+    }
+
+    // Test
+    test_linked_list_length(list, nnodes);
+    test_linked_list_validity(list, 0, 1);
+
+    // Reverse
+    th_llist_reverse(&list);
+
+    test_linked_list_length(list, nnodes);
+    test_linked_list_validity(list, nnodes - 1, -1);
+
+    // Sort
+    th_llist_mergesort(&list, test_llist_sortfunc, NULL);
+
+    // Test count
+    test_linked_list_length(list, nnodes);
+    test_linked_list_validity(list, 0, 1);
+
+    // Prepend
+    tprint(2, "Prepend node to list\n");
+    for (size_t k = 0; k < 16; k++)
+    {
+        if ((node = th_llist_prepend(&list, (void *) n)) == NULL)
+        {
+            THERR("Error allocating memory.\n");
+            goto out;
+        }
+        nnodes++;
+        n++;
+    }
+
+    // Test count
+    test_linked_list_length(list, nnodes);
+
+    // Sort
+    th_llist_mergesort(&list, test_llist_sortfunc, NULL);
+    test_linked_list_validity(list, 0, 1);
+
+    // Delete
+    th_llist_delete_node(&list, list);
+    test_linked_list_length(list, nnodes - 1);
+    test_linked_list_validity(list, 1, 1);
+
+out:
+    th_llist_free(list);
+}
+
+
 #ifdef TH_EXPERIMENTAL_REGEX
 
 typedef struct
@@ -1056,10 +1162,17 @@
     //
     if (test_set_start("Configuration file handling"))
     {
-        test_config();
+        test_config_file();
     }
 
     //
+    // Linked lists
+    //
+    if (test_set_start("Linked lists"))
+    {
+        test_linked_lists();
+    }
+    //
     // String functions
     //
     if (test_set_start("String functions"))