aboutsummaryrefslogtreecommitdiff
path: root/tests/gen_inode_table.c
blob: 190f39320b367fe0bb49f4b550daa53b0fa96fd7 (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
103
104
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * gen_inode_table.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"

#include "fstree.h"

#include <stdlib.h>
#include <assert.h>
#include <string.h>

static tree_node_t *gen_node(tree_node_t *parent, const char *name)
{
	struct stat sb;

	memset(&sb, 0, sizeof(sb));
	sb.st_mode = S_IFDIR | 0755;

	return fstree_mknode(parent, name, strlen(name), NULL, &sb);
}

static void check_children_before_root(tree_node_t *root)
{
	tree_node_t *n;

	for (n = root->data.dir->children; n != NULL; n = n->next)
		assert(n->inode_num < root->inode_num);

	for (n = root->data.dir->children; n != NULL; n = n->next)
		check_children_before_root(n);
}

static void check_children_continuous(tree_node_t *root)
{
	tree_node_t *n;

	for (n = root->data.dir->children; n != NULL; n = n->next) {
		if (n->next != NULL) {
			assert(n->next->inode_num == (n->inode_num + 1));
		}
	}

	for (n = root->data.dir->children; n != NULL; n = n->next)
		check_children_continuous(n);
}

int main(void)
{
	tree_node_t *a, *b, *c;
	unsigned int i;
	fstree_t fs;

	// inode table for the empty tree
	assert(fstree_init(&fs, 0, NULL) == 0);
	assert(fstree_gen_inode_table(&fs) == 0);
	assert(fs.inode_tbl_size == 3);
	assert(fs.root->inode_num == 2);
	assert(fs.inode_table[0] == NULL);
	assert(fs.inode_table[1] == NULL);
	assert(fs.inode_table[2] == fs.root);
	fstree_cleanup(&fs);

	// tree with 2 levels under root, fan out 3
	assert(fstree_init(&fs, 0, NULL) == 0);

	a = gen_node(fs.root, "a");
	b = gen_node(fs.root, "b");
	c = gen_node(fs.root, "c");
	assert(a != NULL);
	assert(b != NULL);
	assert(c != NULL);

	assert(gen_node(a, "a_a") != NULL);
	assert(gen_node(a, "a_b") != NULL);
	assert(gen_node(a, "a_c") != NULL);

	assert(gen_node(b, "b_a") != NULL);
	assert(gen_node(b, "b_b") != NULL);
	assert(gen_node(b, "b_c") != NULL);

	assert(gen_node(c, "c_a") != NULL);
	assert(gen_node(c, "c_b") != NULL);
	assert(gen_node(c, "c_c") != NULL);

	assert(fstree_gen_inode_table(&fs) == 0);

	assert(fs.inode_tbl_size == (13 + 2));
	assert(fs.inode_table[0] == NULL);
	assert(fs.inode_table[1] == NULL);

	for (i = 2; i < (13 + 2); ++i) {
		assert(fs.inode_table[i] != NULL);
		assert(fs.inode_table[i]->inode_num == i);
	}

	check_children_before_root(fs.root);
	check_children_continuous(fs.root);

	fstree_cleanup(&fs);
	return EXIT_SUCCESS;
}