aboutsummaryrefslogtreecommitdiff
path: root/lib/fstree/test/fstree_sort.c
blob: 7bce367edaa869cdda029a0638a3dcd880d89da8 (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
99
100
101
102
/* 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 "common.h"
#include "util/test.h"

static tree_node_t *mkentry(fstree_t *fs, const char *name)
{
	dir_entry_t *ent = dir_entry_create(name);
	tree_node_t *out;

	TEST_NOT_NULL(ent);
	ent->mode = S_IFBLK | 0600;
	ent->rdev = 1337;

	out = fstree_add_generic(fs, ent, NULL);
	free(ent);
	TEST_NOT_NULL(out);

	return out;
}

int main(int argc, char **argv)
{
	tree_node_t *a, *b, *c, *d;
	fstree_defaults_t fsd;
	fstree_t fs;
	int ret;
	(void)argc; (void)argv;

	/* in order */
	TEST_ASSERT(parse_fstree_defaults(&fsd, NULL) == 0);
	ret = fstree_init(&fs, &fsd);
	TEST_EQUAL_I(ret, 0);

	a = mkentry(&fs, "a");
	TEST_NOT_NULL(a);
	TEST_ASSERT(fs.root->data.children == a);
	TEST_NULL(a->next);

	b = mkentry(&fs, "b");
	TEST_NOT_NULL(a);
	TEST_ASSERT(fs.root->data.children == a);
	TEST_ASSERT(a->next == b);
	TEST_NULL(b->next);

	c = mkentry(&fs, "c");
	TEST_NOT_NULL(c);
	TEST_ASSERT(fs.root->data.children == a);
	TEST_ASSERT(a->next == b);
	TEST_ASSERT(b->next == c);
	TEST_NULL(c->next);

	d = mkentry(&fs, "d");
	TEST_NOT_NULL(d);
	TEST_ASSERT(fs.root->data.children == a);
	TEST_ASSERT(a->next == b);
	TEST_ASSERT(b->next == c);
	TEST_ASSERT(c->next == d);
	TEST_NULL(d->next);

	fstree_cleanup(&fs);

	/* out-of-order */
	ret = fstree_init(&fs, &fsd);
	TEST_EQUAL_I(ret, 0);

	d = mkentry(&fs, "d");
	TEST_NOT_NULL(d);
	TEST_ASSERT(fs.root->data.children == d);
	TEST_NULL(d->next);

	c = mkentry(&fs, "c");
	TEST_NOT_NULL(c);
	TEST_ASSERT(fs.root->data.children == c);
	TEST_ASSERT(c->next == d);
	TEST_NULL(d->next);

	b = mkentry(&fs, "b");
	TEST_NOT_NULL(b);
	TEST_ASSERT(fs.root->data.children == b);
	TEST_ASSERT(b->next == c);
	TEST_ASSERT(c->next == d);
	TEST_NULL(d->next);

	a = mkentry(&fs, "a");
	TEST_NOT_NULL(a);
	TEST_ASSERT(fs.root->data.children == a);
	TEST_ASSERT(a->next == b);
	TEST_ASSERT(b->next == c);
	TEST_ASSERT(c->next == d);
	TEST_NULL(d->next);

	fstree_cleanup(&fs);
	return EXIT_SUCCESS;
}