summaryrefslogtreecommitdiff
path: root/tests/fstree_sort.c
blob: 28aa74e9031c3389c3e27c92d32df5de6e10b688 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include "config.h"

#include "fstree.h"

#include <stdlib.h>
#include <string.h>
#include <assert.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(&fs, NULL, "a", 1, NULL, &sb);
	b = fstree_mknode(&fs, NULL, "b", 1, NULL, &sb);
	c = fstree_mknode(&fs, NULL, "c", 1, NULL, &sb);
	d = fstree_mknode(&fs, NULL, "d", 1, NULL, &sb);
	assert(a != NULL && b != NULL && c != NULL && d != NULL);

	/* empty list */
	assert(tree_node_list_sort(NULL) == NULL);

	/* single element */
	assert(tree_node_list_sort(a) == a);
	assert(a->next == NULL);

	/* two elements, reverse order */
	b->next = a;
	assert(tree_node_list_sort(b) == a);
	assert(a->next == b);
	assert(b->next == NULL);

	/* two elements, sorted order */
	assert(tree_node_list_sort(a) == a);
	assert(a->next == b);
	assert(b->next == NULL);

	/* three elements, reverse order */
	c->next = b;
	b->next = a;
	a->next = NULL;

	assert(tree_node_list_sort(c) == a);
	assert(a->next == b);
	assert(b->next == c);
	assert(c->next == NULL);

	/* three elements, ordered */
	assert(tree_node_list_sort(a) == a);
	assert(a->next == b);
	assert(b->next == c);
	assert(c->next == NULL);

	/* four elements, reverse order */
	d->next = c;
	c->next = b;
	b->next = a;
	a->next = NULL;

	assert(tree_node_list_sort(d) == a);
	assert(a->next == b);
	assert(b->next == c);
	assert(c->next == d);
	assert(d->next == NULL);

	/* four elements, sorted order */
	assert(tree_node_list_sort(a) == a);
	assert(a->next == b);
	assert(b->next == c);
	assert(c->next == d);
	assert(d->next == NULL);

	/* force merge sort to go through LRLR pattern */
	b->next = a;
	a->next = d;
	d->next = c;
	c->next = NULL;

	assert(tree_node_list_sort(b) == a);
	assert(a->next == b);
	assert(b->next == c);
	assert(c->next == d);
	assert(d->next == NULL);

	/* cleanup and done */
	free(a);
	free(b);
	free(c);
	free(d);
	return EXIT_SUCCESS;
}