-
Notifications
You must be signed in to change notification settings - Fork 0
/
rb_tree_test.cc
110 lines (88 loc) · 2.7 KB
/
rb_tree_test.cc
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
#include "zda/rb_tree.h"
#include <gtest/gtest.h>
typedef struct int_entry {
int key;
zda_rb_node_t node;
} int_entry_t;
int int_entry_cmp(int_entry_t const *entry, int key) { return entry->key - key; }
int int_entry_cmp2(zda_rb_node_t const *node, void const *key)
{
int_entry_t const *entry = zda_rb_entry(node, int_entry_t const);
int const *_key = (int const *)key;
return entry->key - *_key;
}
int print_entry(void const *node)
{
int_entry_t *entry = zda_rb_entry((zda_rb_node_t *)node, int_entry);
return printf("%d", entry->key);
}
zda_def_rb_tree_insert(int_rb_tree_insert, int, int_entry_t)
static zda_inline void prepare_entries(zda_rb_header_t *header, int n)
{
for (int i = 0; i < n; i++) {
int_entry_t *p_dup = ZDA_NULL;
int result = 0;
result = int_rb_tree_insert(header, &i, int_entry_cmp2, &p_dup);
if (result) {
p_dup->key = i;
}
}
}
TEST(rb_tree_test, insert)
{
zda_rb_header_t header;
zda_rb_header_init(&header);
for (int i = 0; i < 100; i++) {
int_entry_t *p_dup = ZDA_NULL;
int result = 0;
result = int_rb_tree_insert(&header, &i, int_entry_cmp2, &p_dup);
if (result) {
p_dup->key = i;
printf("insert ok: %d\n", p_dup->key);
zda_rb_tree_print_tree(&header, print_entry);
puts("");
} else {
printf("Failed to insert: %d\n", p_dup->key);
}
}
auto min_entry = zda_rb_entry(zda_rb_tree_first(&header), int_entry_t);
auto max_entry = zda_rb_entry(zda_rb_tree_last(&header), int_entry_t);
printf("min_val = %d\n", min_entry->key);
printf("max_val = %d\n", max_entry->key);
zda_rb_tree_iterate(&header)
{
int_entry_t *entry = zda_rb_entry(pos, int_entry_t);
printf("val: %d\n", entry->key);
}
zda_rb_tree_destroy_inplace(&header, int_entry_t, free);
}
TEST(rb_tree_test, search)
{
zda_rb_header_t header;
zda_rb_header_init(&header);
const int n = 100;
prepare_entries(&header, n);
for (int i = 0; i < n; i++) {
auto res = zda_rb_tree_search(&header, &i, int_entry_cmp2);
ASSERT_TRUE(res);
auto entry = zda_rb_entry(res, int_entry_t);
EXPECT_EQ(entry->key, i);
}
}
TEST(rb_tree_test, remove)
{
zda_rb_header_t header;
zda_rb_header_init(&header);
const int n = 100;
prepare_entries(&header, n);
printf("The tree before remove: \n");
zda_rb_tree_print_tree(&header, print_entry);
for (int i = 0; i < n; ++i) {
zda_rb_node_t *old_node = zda_rb_tree_remove(&header, &i, int_entry_cmp2);
zda_rb_tree_print_tree(&header, print_entry);
fflush(stdout);
free(zda_rb_entry(old_node, int_entry_t));
}
printf("Remove complete: \n");
zda_rb_tree_print_tree(&header, print_entry);
}