-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathht_test.cc
125 lines (107 loc) · 2.6 KB
/
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
#include <zda/ht.h>
#include <gtest/gtest.h>
typedef struct int_entry {
int key;
zda_ht_node_t node;
} int_entry_t;
static zda_inline size_t int_entry_hash(int i) noexcept { return i; }
static zda_inline zda_bool int_entry_equal(int i, int j) noexcept { return i == j; }
static void int_entry_print(zda_ht_node_t *node) noexcept
{
auto entry = zda_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_ht_search(
ht_search_int_entry,
int,
int_entry_t,
int_entry_get_key,
int_entry_hash,
int_entry_equal
)
zda_def_ht_insert_check(
ht_insert_check_int_entry,
int,
int_entry_t,
int_entry_get_key,
int_entry_hash,
int_entry_equal
)
zda_def_ht_insert_commit(ht_insert_commit_int_entry, int_entry_t)
zda_def_ht_remove(
ht_remove_int_entry,
int,
int_entry_t,
int_entry_get_key,
int_entry_hash,
int_entry_equal
)
static void prepare_ht(zda_ht_t *ht, size_t n)
{
for (int i = 0; i < n; ++i) {
zda_ht_commit_ctx_t commit_ctx;
int_entry *p_dup;
zda_ht_insert_check_inplace(
ht,
i,
int_entry_t,
int_entry_get_key,
int_entry_hash,
int_entry_equal,
commit_ctx,
p_dup
);
ASSERT_TRUE(!p_dup);
int_entry_t *entry = (int_entry_t *)malloc(sizeof(int_entry_t));
entry->key = i;
zda_ht_insert_commit_inplace(ht, commit_ctx, &(entry->node));
}
}
TEST(ht_test, insert)
{
zda_ht_t ht;
zda_ht_init(&ht);
prepare_ht(&ht, 10);
auto first = zda_ht_get_first(&ht);
for (; !zda_ht_iter_is_terminator(&first); zda_ht_iter_inc(&first)) {
auto entry = zda_ht_iter2entry(first, int_entry);
printf("entry: %d\n", entry->key);
}
zda_ht_print_layout(&ht, int_entry_print);
zda_ht_destroy_inplace(&ht, int_entry_t, free);
}
TEST(ht_test, search)
{
zda_ht_t ht;
zda_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_ht_destroy_inplace(&ht, int_entry_t, free);
}
TEST(ht_test, remove)
{
zda_ht_t ht;
zda_ht_init(&ht);
prepare_ht(&ht, 100);
for (int i = 0; i < 100; ++i) {
int_entry_t *result;
zda_ht_remove_inplace(
&ht,
i,
int_entry_t,
int_entry_get_key,
int_entry_hash,
int_entry_equal,
result
);
ASSERT_TRUE(result);
free(result);
zda_ht_print_layout(&ht, int_entry_print);
}
zda_ht_destroy_inplace(&ht, int_entry_t, free);
}