summaryrefslogtreecommitdiff
path: root/tests/libfstree/fstree_sort.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2021-03-05 15:53:21 +0100
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2021-03-06 22:08:36 +0100
commitb950412ca3a91aa37349cf51ebe98cc84767d448 (patch)
treee3bb062114d019984321a5a21b29818c88c36795 /tests/libfstree/fstree_sort.c
parent3fc6bf24b5cc071fc323f08ece541e37578f6369 (diff)
Cleanup: add some structure to the test directory
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'tests/libfstree/fstree_sort.c')
-rw-r--r--tests/libfstree/fstree_sort.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/tests/libfstree/fstree_sort.c b/tests/libfstree/fstree_sort.c
new file mode 100644
index 0000000..6fc7543
--- /dev/null
+++ b/tests/libfstree/fstree_sort.c
@@ -0,0 +1,101 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * fstree_sort.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "config.h"
+
+#include "fstree.h"
+#include "internal.h"
+#include "../test.h"
+
+int main(void)
+{
+ tree_node_t *a, *b, *c, *d;
+ struct stat sb;
+ fstree_t fs;
+
+ memset(&fs, 0, sizeof(fs));
+ memset(&sb, 0, sizeof(sb));
+ sb.st_mode = S_IFBLK | 0600;
+ sb.st_rdev = 1337;
+
+ a = fstree_mknode(NULL, "a", 1, NULL, &sb);
+ b = fstree_mknode(NULL, "b", 1, NULL, &sb);
+ c = fstree_mknode(NULL, "c", 1, NULL, &sb);
+ d = fstree_mknode(NULL, "d", 1, NULL, &sb);
+ TEST_ASSERT(a != NULL && b != NULL && c != NULL && d != NULL);
+
+ /* empty list */
+ TEST_NULL(tree_node_list_sort(NULL));
+
+ /* single element */
+ TEST_ASSERT(tree_node_list_sort(a) == a);
+ TEST_NULL(a->next);
+
+ /* two elements, reverse order */
+ b->next = a;
+ TEST_ASSERT(tree_node_list_sort(b) == a);
+ TEST_ASSERT(a->next == b);
+ TEST_NULL(b->next);
+
+ /* two elements, sorted order */
+ TEST_ASSERT(tree_node_list_sort(a) == a);
+ TEST_ASSERT(a->next == b);
+ TEST_NULL(b->next);
+
+ /* three elements, reverse order */
+ c->next = b;
+ b->next = a;
+ a->next = NULL;
+
+ TEST_ASSERT(tree_node_list_sort(c) == a);
+ TEST_ASSERT(a->next == b);
+ TEST_ASSERT(b->next == c);
+ TEST_NULL(c->next);
+
+ /* three elements, ordered */
+ TEST_ASSERT(tree_node_list_sort(a) == a);
+ TEST_ASSERT(a->next == b);
+ TEST_ASSERT(b->next == c);
+ TEST_NULL(c->next);
+
+ /* four elements, reverse order */
+ d->next = c;
+ c->next = b;
+ b->next = a;
+ a->next = NULL;
+
+ TEST_ASSERT(tree_node_list_sort(d) == a);
+ TEST_ASSERT(a->next == b);
+ TEST_ASSERT(b->next == c);
+ TEST_ASSERT(c->next == d);
+ TEST_NULL(d->next);
+
+ /* four elements, sorted order */
+ TEST_ASSERT(tree_node_list_sort(a) == a);
+ TEST_ASSERT(a->next == b);
+ TEST_ASSERT(b->next == c);
+ TEST_ASSERT(c->next == d);
+ TEST_NULL(d->next);
+
+ /* force merge sort to go through LRLR pattern */
+ b->next = a;
+ a->next = d;
+ d->next = c;
+ c->next = NULL;
+
+ TEST_ASSERT(tree_node_list_sort(b) == a);
+ TEST_ASSERT(a->next == b);
+ TEST_ASSERT(b->next == c);
+ TEST_ASSERT(c->next == d);
+ TEST_NULL(d->next);
+
+ /* cleanup and done */
+ free(a);
+ free(b);
+ free(c);
+ free(d);
+ return EXIT_SUCCESS;
+}