Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test-avl.c « tests - git.openwrt.org/project/libubox.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 18ee9b7697c5553d96dfa5cf0504a38c8482fce4 (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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "avl.h"
#include "avl-cmp.h"
#include "utils.h"

#define OUT(fmt, ...) do { \
	fprintf(stdout, "%s: " fmt, __func__, ## __VA_ARGS__); \
} while (0);

struct node {
	struct avl_node avl;
};

static void test_basics()
{
	size_t i;
	struct avl_tree t;
	struct node *temp;
	struct node *elem;
	struct node *last;
	struct node *first;
	const char *vals[] = {
		"zero", "one", "two", "three", "four", "five", "six",
		"seven", "eight", "nine", "ten", "eleven", "twelve"
	};

	avl_init(&t, avl_strcmp, false, NULL);

	OUT("insert: ");
	for (i=0; i<ARRAY_SIZE(vals); i++) {
		struct node *n = malloc(sizeof(struct node));
		n->avl.key = vals[i];

		int r = avl_insert(&t, &n->avl);
		fprintf(stdout, "%d=%s ", r, (char *)n->avl.key);
	}
	fprintf(stdout, "\n");

	OUT("insert duplicate: ");
	for (i=0; i<ARRAY_SIZE(vals); i++) {
		struct node *n = malloc(sizeof(struct node));
		n->avl.key = vals[i];

		int r = avl_insert(&t, &n->avl);
		fprintf(stdout, "%d=%s ", r, (char *)n->avl.key);

		if (r)
			free(n);
	}
	fprintf(stdout, "\n");

	first = avl_first_element(&t, first, avl);
	last = avl_last_element(&t, last, avl);
	OUT("first=%s last=%s\n", (char*)first->avl.key, (char*)last->avl.key);

	OUT("for each element: ");
	avl_for_each_element(&t, elem, avl) {
		fprintf(stdout, "%s ", (char*)elem->avl.key);
	}
	fprintf(stdout, "\n");

	OUT("delete 'one' element\n");
	elem = avl_find_element(&t, "one", elem, avl);
	avl_delete(&t, &elem->avl);
	free(elem);

	OUT("for each element reverse: ");
	avl_for_each_element_reverse(&t, elem, avl) {
		fprintf(stdout, "%s ", (char*)elem->avl.key);
	}
	fprintf(stdout, "\n");

	OUT("delete all elements\n");
	avl_for_each_element_safe(&t, elem, avl, temp) {
		avl_delete(&t, &elem->avl);
		free(elem);
	}
}

int main()
{
	test_basics();
	return 0;
}