This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathhashtab.c
150 lines (109 loc) · 3.02 KB
/
hashtab.c
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
/* Copyright 2015 BitPay, Inc.
* Distributed under the MIT/X11 software license, see the accompanying
* file COPYING or http://www.opensource.org/licenses/mit-license.php.
*/
#include "picocoin-config.h"
#include "libtest.h"
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <ccoin/hashtab.h>
#include <ccoin/util.h>
static void test_basics(void)
{
struct bp_hashtab *ht;
ht = bp_hashtab_new_ext(czstr_hash, czstr_equal, free, free);
assert(ht != NULL);
bp_hashtab_ref(ht);
assert(bp_hashtab_size(ht) == 0);
bp_hashtab_put(ht, strdup("name"), strdup("john"));
assert(bp_hashtab_size(ht) == 1);
char *lr = bp_hashtab_get(ht, "name");
assert(lr != NULL);
assert(strcmp(lr, "john") == 0);
void *ret_key = NULL;
void *ret_value = NULL;
bool rc = bp_hashtab_get_ext(ht, "name", &ret_key, &ret_value);
assert(rc == true);
assert(ret_key != NULL);
assert(ret_value != NULL);
assert(strcmp(ret_key, "name") == 0);
assert(strcmp(ret_value, "john") == 0);
rc = bp_hashtab_get_ext(ht, "name", NULL, NULL);
assert(rc == true);
void *dummy = bp_hashtab_get(ht, "dummy");
assert(dummy == NULL);
assert(bp_hashtab_size(ht) == 1);
// overwrite existing entry
bp_hashtab_put(ht, strdup("name"), strdup("bob"));
rc = bp_hashtab_get_ext(ht, "name", &ret_key, &ret_value);
assert(rc == true);
assert(ret_key != NULL);
assert(ret_value != NULL);
assert(strcmp(ret_key, "name") == 0);
assert(strcmp(ret_value, "bob") == 0);
assert(bp_hashtab_size(ht) == 1);
// test deletion
rc = bp_hashtab_del(ht, "does-not-exist");
assert(rc == false);
assert(bp_hashtab_size(ht) == 1);
rc = bp_hashtab_del(ht, "name");
assert(rc == true);
assert(bp_hashtab_size(ht) == 0);
rc = bp_hashtab_del(ht, "name");
assert(rc == false);
assert(bp_hashtab_size(ht) == 0);
rc = bp_hashtab_clear(ht);
assert(rc == true);
assert(bp_hashtab_size(ht) == 0);
bp_hashtab_unref(ht);
bp_hashtab_unref(ht);
}
struct iter_info {
unsigned int count;
};
static void test_generate_iter(void *key_, void *val_, void *priv)
{
char *key = key_;
char *value = val_;
struct iter_info *ii = priv;
ii->count++;
(void) key;
(void) value;
}
static void test_generate(void)
{
struct bp_hashtab *ht;
ht = bp_hashtab_new_ext(czstr_hash, czstr_equal, free, free);
assert(ht != NULL);
const int n_values = 100000;
unsigned int i;
char s[32];
for (i = 0; i < n_values; i++) {
sprintf(s, "%d", i);
char *key = strdup(s);
char *value = strdup(s);
bool rc = bp_hashtab_put(ht, key, value);
assert(rc == true);
}
assert(bp_hashtab_size(ht) == n_values);
for (i = 0; i < n_values; i++) {
sprintf(s, "%d", i);
char *value = bp_hashtab_get(ht, s);
assert(value != NULL);
assert(strcmp(s, value) == 0);
}
assert(bp_hashtab_size(ht) == n_values);
void *dummy = bp_hashtab_get(ht, "dummy");
assert(dummy == NULL);
struct iter_info ii = {};
bp_hashtab_iter(ht, test_generate_iter, &ii);
assert(ii.count == n_values);
bp_hashtab_unref(ht);
}
int main (int argc, char *argv[])
{
test_basics();
test_generate();
return 0;
}