summaryrefslogtreecommitdiff
path: root/tests/tree.c
blob: 7676ab492886c8e012590133bea72d2daab56a26 (plain) (blame)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "../src/tree.c"

#include "../src/testing.h"



static int
test_tree_new(void) {
	int rc = -1;

	const struct Tree *t = NULL;

	test_start("tree_new()");
	{
		testing("simple allocation");

		const size_t size = sizeof(long long);
		if (tree_new(size, &t)) {
			logerr("tree_new()");
			goto out;
		}

		assert(t->value_size == size);

		tree_free(&t);

		test_ok();
	}

	rc = 0;
out:
	if (t != NULL) {
		tree_free(&t);
	}
	return rc;
}

static int
test_tree_free(void) {
	int rc = -1;

	const struct Tree *t = NULL;

	test_start("tree_free()");
	{
		testing("*t becomes NULL again after tree_free(&t)");


		assert(t == NULL);
		if (tree_new(1U, &t)) {
			logerr("tree_new()");
			goto out;
		}

		assert(t != NULL);
		tree_free(&t);
		assert(t == NULL);

		test_ok();
	}

	rc = 0;
out:
	if (t != NULL) {
		tree_free(&t);
	}
	return rc;
}

static void
test_tree_contains(void) {

	const size_t value_size = sizeof(int);

	/*
		ASCII representation of the tree:

		         12
		      __/  \__
		     /        \
		    8          18
		   / \        /
		  5   11    17
		 /
		4

	**/

	// Values that don't exist on the tree
	const int n0  = 0;
	const int n1  = 1;
	const int n2  = 2;
	const int n3  = 3;
	const int n6  = 6;
	const int n7  = 7;
	const int n9  = 9;
	const int n10 = 10;
	const int n13 = 13;
	const int n14 = 14;
	const int n15 = 15;
	const int n16 = 16;
	const int n19 = 19;

	// Values that DO exist on the tree
	const int y12 = 12;
	const int y8  = 8;
	const int y18 = 18;
	const int y5  = 5;
	const int y11 = 11;
	const int y17 = 17;
	const int y4  = 4;

	const struct Tree *const t = &(struct Tree) {
		.value      = &y12,
		.value_size = value_size,
		.height     = 4U,
		.left       = &(struct Tree) {
			.value      = &y8,
			.value_size = value_size,
			.left       = &(struct Tree) {
				.value      = &y5,
				.value_size = value_size,
				.left       = &(struct Tree) {
					.value      = &y4,
					.value_size = value_size,
					.left       = NULL,
					.right      = NULL,
					.height     = 1U
				},
				.right      = NULL,
				.height     = 2U,
			},
			.right      = &(struct Tree) {
				.value      = &y11,
				.value_size = value_size,
				.left       = NULL,
				.right      = NULL,
				.height     = 1U,
			},
			.height     = 3U,
		},
		.right      = &(struct Tree) {
			.value      = &y18,
			.value_size = value_size,
			.left       = &(struct Tree) {
				.value      = &y17,
				.value_size = value_size,
				.left       = NULL,
				.right      = NULL,
				.height     = 1U,
			},
			.right      = NULL,
			.height     = 2U,
		},
	};

	assert(!tree_contains(t, &n0));
	assert(!tree_contains(t, &n1));
	assert(!tree_contains(t, &n2));
	assert(!tree_contains(t, &n3));
	assert( tree_contains(t, &y4));
	assert( tree_contains(t, &y5));
	assert(!tree_contains(t, &n6));
	assert(!tree_contains(t, &n7));
	assert( tree_contains(t, &y8));
	assert(!tree_contains(t, &n9));
	assert(!tree_contains(t, &n10));
	assert( tree_contains(t, &y11));
	assert( tree_contains(t, &y12));
	assert(!tree_contains(t, &n13));
	assert(!tree_contains(t, &n14));
	assert(!tree_contains(t, &n15));
	assert(!tree_contains(t, &n16));
	assert( tree_contains(t, &y17));
	assert( tree_contains(t, &y18));
	assert(!tree_contains(t, &n19));
}

int
main(void) {
	int rc = -1;

	if (test_tree_new()) {
		logerr("test_tree_new()");
		goto out;
	}

	if (test_tree_free()) {
		logerr("test_tree_free()");
		goto out;
	}

	test_tree_contains();

	rc = 0;
out:
	return !!rc;
}