-
Notifications
You must be signed in to change notification settings - Fork 0
/
avl_ht_test.cc
138 lines (119 loc) · 2.96 KB
/
avl_ht_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
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
#include <zda/avl_ht.h>
#include <stdio.h>
#include <gtest/gtest.h>
typedef struct int_entry {
int key;
zda_avl_ht_node_t node;
} int_entry_t;
static zda_inline size_t int_entry_hash(int i) noexcept { return i ^ ~i; }
static zda_inline zda_bool int_entry_equal(int i, int j) noexcept { return i == j; }
static zda_inline int int_entry_cmp(int i, int j) noexcept { return i - j; }
static void int_entry_print(zda_avl_ht_node_t *node) noexcept
{
auto entry = zda_avl_ht_entry(node, int_entry);
printf("%d", entry->key);
}
static zda_inline int int_entry_get_key(int_entry_t *entry) noexcept { return entry->key; }
zda_def_avl_ht_destroy(ht_destroy_int_entry, int_entry_t, free)
zda_def_avl_ht_insert_check(
ht_insert_check_int_entry,
int,
int_entry_t,
int_entry_get_key,
int_entry_hash,
int_entry_cmp
)
zda_def_avl_ht_search(
ht_search_int_entry,
int,
int_entry_t,
int_entry_get_key,
int_entry_hash,
int_entry_cmp
)
zda_def_avl_ht_remove(
ht_remove_int_entry,
int,
int_entry_t,
int_entry_get_key,
int_entry_hash,
int_entry_cmp
)
zda_def_avl_ht_insert_entry(
ht_insert_int_entry,
int_entry_t,
int_entry_get_key,
int_entry_hash,
int_entry_cmp
)
static void prepare_ht(zda_avl_ht_t *ht, size_t n)
{
for (int i = 0; i < n; ++i) {
zda_avl_ht_commit_ctx_t commit_ctx;
int_entry *p_dup;
zda_avl_ht_insert_check_inplace(
ht,
i,
int_entry_t,
int_entry_get_key,
int_entry_hash,
int_entry_cmp,
commit_ctx,
p_dup
);
ASSERT_TRUE(!p_dup);
int_entry_t *entry = (int_entry_t *)malloc(sizeof(int_entry_t));
entry->key = i;
zda_avl_ht_insert_commit(ht, &commit_ctx, &(entry->node));
zda_avl_ht_print_layout(ht, int_entry_print);
}
}
TEST(ht_test, insert)
{
zda_avl_ht_t ht;
zda_avl_ht_init(&ht);
prepare_ht(&ht, 10);
auto first = zda_avl_ht_get_first(&ht);
for (; !zda_avl_ht_iter_is_terminator(&first); zda_avl_ht_iter_inc(&first)) {
auto entry = zda_avl_ht_iter2entry(first, int_entry_t);
}
zda_avl_ht_destroy_inplace(&ht, int_entry_t, free);
}
TEST(ht_test, search)
{
zda_avl_ht_t ht;
zda_avl_ht_init(&ht);
prepare_ht(&ht, 100);
for (int i = 0; i < 100; ++i) {
auto entry = ht_search_int_entry(&ht, i);
ASSERT_TRUE(entry);
EXPECT_EQ(entry->key, i);
}
zda_avl_ht_destroy_inplace(&ht, int_entry_t, free);
}
TEST(ht_test, remove)
{
zda_avl_ht_t ht;
zda_avl_ht_init(&ht);
prepare_ht(&ht, 100);
for (int i = 0; i < 100; ++i) {
int_entry_t *result;
ASSERT_TRUE(ht_search_int_entry(&ht, i));
#if 1
zda_avl_ht_remove_inplace(
&ht,
i,
int_entry_t,
int_entry_get_key,
int_entry_hash,
int_entry_cmp,
result
);
#else
result = ht_remove_int_entry(&ht, i);
#endif
ASSERT_TRUE(result) << i;
free(result);
}
zda_avl_ht_destroy_inplace(&ht, int_entry_t, free);
}