summaryrefslogtreecommitdiff
path: root/ccan/btree/test/run-trivial.c
blob: 6992bf61676a0804ca74a6004df97a4c6d41707f (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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* Include the main header first, to test it works */
#include <ccan/btree/btree.h>
/* Include the C files directly. */
#include <ccan/btree/btree.c>
#include <ccan/tap/tap.h>

#include <string.h>

struct test_item {
	int key;
	int value;
};

static btree_search_implement(
	order_by_key,
	struct test_item *,
	,
	a->key == b->key,
	a->key < b->key
)

static int insert_test_item(struct btree *btree, int key, int value)
{
	struct test_item key_item = {key, -101};
	struct test_item *item;
	btree_iterator iter;
	
	if (btree_find_first(btree, &key_item, iter)) {
		/* Don't insert new item, but do update its value. */
		item = iter->item;
		item->value = value;
		return 0;
	}
	
	item = malloc(sizeof(*item));
	item->key = key;
	item->value = value;
	
	btree_insert_at(iter, item);
	
	return 1;
}

static int lookup_test_item(const struct btree *btree, int key)
{
	struct test_item key_item = {key, -102};
	struct test_item *item;
	btree_iterator iter;
	
	if (!btree_find_first(btree, &key_item, iter))
		return -100;
	
	item = iter->item;
	return item->value;
}

static int destroy_test_item(void *item, void *ctx) {
	(void) ctx;
	free(item);
	return 1;
}

struct test_insert_entry {
	int key;
	int value;
	int expected_return;
};

struct test_traverse_entry {
	int key;
	int value;
};

static void print_indent(unsigned int indent) {
	while (indent--)
		fputs("\t", stdout);
}

static void btree_node_trace(struct btree_node *node, unsigned int indent)
{
	unsigned int i;
	for (i=0; i<node->count; i++) {
		if (node->depth)
			btree_node_trace(node->branch[i], indent+1);
		print_indent(indent);
		puts(node->item[i]);
	}
	if (node->depth)
		btree_node_trace(node->branch[node->count], indent+1);
}

static void btree_trace(struct btree *btree)
{
	btree_node_trace(btree->root, 0);
}

static void test_insert(struct btree *btree)
{
	struct test_insert_entry ent[] = {
		{3, 1, 1}, {4, 1, 1}, {5, 9, 1}, {2, 6, 1}, {5, 3, 0}, {5, 8, 0},
		{9, 7, 1}, {9, 3, 0}, {2, 3, 0}, {8, 4, 1}, {6, 2, 1}, {6, 4, 0},
		{3, 3, 0}, {8, 3, 0}, {2, 7, 0}, {9, 5, 0}, {0, 2, 1}, {8, 8, 0},
		{4, 1, 0}, {9, 7, 0}, {1, 6, 1}, {9, 3, 0}, {9, 9, 0}, {3, 7, 0},
		{5, 1, 0}, {0, 5, 0}, {8, 2, 0}, {0, 9, 0}, {7, 4, 1}, {9, 4, 0},
		{4, 5, 0}, {9, 2, 0}
	};
	size_t i, count = sizeof(ent) / sizeof(*ent);
	
	for (i = 0; i < count; i++) {
		int ret = insert_test_item(btree, ent[i].key, ent[i].value);
		ok1(ret == ent[i].expected_return);
	}
}

static void test_find_traverse(struct btree *btree)
{
	struct test_traverse_entry ent[] = {
		{0, 9}, {1, 6}, {2, 7}, {3, 7}, {4, 5},
		{5, 1}, {6, 4}, {7, 4}, {8, 2}, {9, 2}
	};
	size_t i, count = sizeof(ent) / sizeof(*ent);
	btree_iterator iter;
	
	i = 0;
	for (btree_begin(btree, iter); btree_next(iter);) {
		struct test_item *item = iter->item;
		
		if (i >= count) {
			fail("Too many items in btree according to forward traversal");
			break;
		}
		
		ok1(lookup_test_item(btree, item->key) == item->value);
		ok1(item->key == ent[i].key && item->value == ent[i].value);
		
		i++;
	}
	
	if (i != count)
		fail("Not enough items in btree according to forward traversal");
	
	i = count;
	for (btree_end(btree, iter); btree_prev(iter);) {
		struct test_item *item = iter->item;
		
		if (!i--) {
			fail("Too many items in btree according to backward traversal");
			break;
		}
		
		ok1(lookup_test_item(btree, item->key) == item->value);
		ok1(item->key == ent[i].key && item->value == ent[i].value);
	}
	
	if (i != 0)
		fail("Not enough items in btree according to backward traversal");
}

static btree_search_proto order_by_string;

static btree_search_implement(
	order_by_string, //function name
	const char*, //key type
	int c = strcmp(a, b), //setup
	c == 0, // a == b predicate
	c < 0 // a < b predicate
)

//used in the test case to sort the test strings
static int compare_by_string(const void *ap, const void *bp)
{
	const char * const *a = ap;
	const char * const *b = bp;
	return strcmp(*a, *b);
}

static void test_traverse(struct btree *btree, const char *sorted[], size_t count)
{
	btree_iterator iter, iter2;
	size_t i;
	
	i = 0;
	for (btree_begin(btree, iter); btree_next(iter);) {
		if (i >= count) {
			fail("Too many items in btree according to forward traversal");
			break;
		}
		
		ok1(iter->item == sorted[i]);
		
		btree_find_first(btree, sorted[i], iter2);
		ok1(iter2->item == sorted[i]);
		
		i++;
	}
	
	if (i != count)
		fail("Not enough items in btree according to forward traversal");
	
	i = count;
	for (btree_end(btree, iter); btree_prev(iter);) {
		if (!i--) {
			fail("Too many items in btree according to backward traversal");
			break;
		}
		
		ok1(iter->item == sorted[i]);
		
		btree_find_first(btree, sorted[i], iter2);
		ok1(iter2->item == sorted[i]);
	}
	
	if (i != 0)
		fail("Not enough items in btree according to backward traversal");
}

#if 0
//(tries to) remove the key from both the btree and the test array.  Returns 1 on success, 0 on failure.
//success is when an item is removed from the btree and the array, or when none is removed from either
//failure is when an item is removed from the btree but not the array or vice versa
//after removing, it tries removing again to make sure that removal returns 0
static int test_remove(struct btree *btree, struct btree_item items[], size_t *count, const char *key)
{
	size_t i;
	
	for (i = *count; i--;) {
		if (!strcmp(items[i].key, key)) {
			//item found in array
			memmove(&items[i], &items[i+1], (*count-(i+1))*sizeof(*items));
			(*count)--;
			
			//puts("----------");
			//btree_trace(btree);
			
			//removal should succeed, as the key should be there
			//this is not a contradiction; the test is performed twice
			return btree_remove(btree, key) && !btree_remove(btree, key);
		}
	}
	
	//removal should fail, as the key should not be there
	//this is not redundant; the test is performed twice
	return !btree_remove(btree, key) && !btree_remove(btree, key);
}
#endif

static void test_search_implement(void)
{
	struct btree *btree = btree_new(order_by_string);
	size_t i;
	
	const char *unsorted[] = {
		"md4",
		"isaac",
		"noerr",
		"talloc_link",
		"asearch",
		"tap",
		"crcsync",
		"wwviaudio",
		"array_size",
		"alignof",
		"str",
		"read_write_all",
		"grab_file",
		"out",
		"daemonize",
		"array",
		"crc",
		"str_talloc",
		"build_assert",
		"talloc",
		"alloc",
		"endian",
		"btree",
		"typesafe_cb",
		"check_type",
		"list",
		"ciniparser",
		"ilog",
		"ccan_tokenizer",
		"tdb",
		"block_pool",
		"sparse_bsearch",
		"container_of",
		"stringmap",
		"hash",
		"short_types",
		"ogg_to_pcm",
		"antithread",
	};
	size_t count = sizeof(unsorted) / sizeof(*unsorted);
	const char *sorted[count];
	
	memcpy(sorted, unsorted, sizeof(sorted));
	qsort(sorted, count, sizeof(*sorted), compare_by_string);
	
	for (i=0; i<count; i++) {
		btree_iterator iter;
		
		if (btree_find_first(btree, unsorted[i], iter))
			fail("btree_insert thinks the test array has duplicates, but it doesn't");
		else
			btree_insert_at(iter, unsorted[i]);
	}
	btree_trace(btree);
	
	test_traverse(btree, sorted, count);
	
	/*
	//try removing items that should be in the tree
	ok1(test_remove(btree, sorted, &count, "btree"));
	ok1(test_remove(btree, sorted, &count, "ccan_tokenizer"));
	ok1(test_remove(btree, sorted, &count, "endian"));
	ok1(test_remove(btree, sorted, &count, "md4"));
	ok1(test_remove(btree, sorted, &count, "asearch"));
	ok1(test_remove(btree, sorted, &count, "alloc"));
	ok1(test_remove(btree, sorted, &count, "build_assert"));
	ok1(test_remove(btree, sorted, &count, "typesafe_cb"));
	ok1(test_remove(btree, sorted, &count, "sparse_bsearch"));
	ok1(test_remove(btree, sorted, &count, "stringmap"));
	
	//try removing items that should not be in the tree
	ok1(test_remove(btree, sorted, &count, "java"));
	ok1(test_remove(btree, sorted, &count, "openoffice"));
	ok1(test_remove(btree, sorted, &count, "firefox"));
	ok1(test_remove(btree, sorted, &count, "linux"));
	ok1(test_remove(btree, sorted, &count, ""));
	
	//test the traversal again to make sure things are okay
	test_traverse(btree, sorted, count);
	
	//remove the rest of the items, then make sure tree is empty
	while (count)
		ok1(test_remove(btree, sorted, &count, sorted[count-1].key));
	ok1(btree->root == NULL);
	*/
	
	btree_delete(btree);
}

int main(void)
{
	struct btree *btree;
	
	plan_tests(224);
	
	btree = btree_new(order_by_key);
	btree->destroy = destroy_test_item;
	test_insert(btree);
	test_find_traverse(btree);
	btree_delete(btree);
	
	test_search_implement();
	
	return exit_status();
}