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
|
/*
db_hash tests
Copyright (C) Amitay Isaacs 2015
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "replace.h"
#include <assert.h>
#include "common/db_hash.c"
static int record_parser(uint8_t *keybuf, size_t keylen,
uint8_t *databuf, size_t datalen,
void *private_data)
{
int *count = (int *)private_data;
(*count) += 1;
return 0;
}
static void do_test(enum db_hash_type type)
{
struct db_hash_context *dh = NULL;
TALLOC_CTX *mem_ctx = talloc_new(NULL);
uint8_t key[] = "This is a long key";
uint8_t value[] = "This is a long value";
int ret;
int count = 0;
ret = db_hash_insert(dh, key, sizeof(key), value, sizeof(value));
assert(ret == EINVAL);
ret = db_hash_add(dh, key, sizeof(key), value, sizeof(value));
assert(ret == EINVAL);
ret = db_hash_exists(dh, key, sizeof(key));
assert(ret == EINVAL);
ret = db_hash_delete(dh, key, sizeof(key));
assert(ret == EINVAL);
ret = db_hash_init(mem_ctx, "foobar", 1024, type, &dh);
assert(ret == 0);
ret = db_hash_insert(dh, key, sizeof(key), value, sizeof(value));
assert(ret == 0);
ret = db_hash_exists(dh, key, sizeof(key));
assert(ret == 0);
ret = db_hash_fetch(dh, key, sizeof(key), NULL, NULL);
assert(ret == EINVAL);
ret = db_hash_fetch(dh, key, sizeof(key), record_parser, &count);
assert(ret == 0);
assert(count == 1);
ret = db_hash_insert(dh, key, sizeof(key), value, sizeof(value));
assert(ret == EEXIST);
ret = db_hash_delete(dh, key, sizeof(key));
assert(ret == 0);
ret = db_hash_exists(dh, key, sizeof(key));
assert(ret == ENOENT);
ret = db_hash_delete(dh, key, sizeof(key));
assert(ret == ENOENT);
ret = db_hash_add(dh, key, sizeof(key), key, sizeof(key));
assert(ret == 0);
ret = db_hash_add(dh, key, sizeof(key), value, sizeof(value));
assert(ret == 0);
talloc_free(dh);
ret = talloc_get_size(mem_ctx);
assert(ret == 0);
talloc_free(mem_ctx);
}
static void do_traverse_test(enum db_hash_type type)
{
struct db_hash_context *dh = NULL;
TALLOC_CTX *mem_ctx = talloc_new(NULL);
char key[] = "keyXXXX";
char value[] = "This is some test value";
int count, ret, i;
ret = db_hash_traverse(dh, NULL, NULL, &count);
assert(ret == EINVAL);
ret = db_hash_init(mem_ctx, "foobar", 1024, type, &dh);
assert(ret == 0);
for (i=0; i<2000; i++) {
sprintf(key, "key%04d", i);
ret = db_hash_insert(dh, (uint8_t *)key, sizeof(key),
(uint8_t *)value, sizeof(value));
assert(ret == 0);
}
ret = db_hash_traverse(dh, NULL, NULL, &count);
assert(ret == 0);
assert(count == 2000);
ret = db_hash_traverse(dh, record_parser, &count, NULL);
assert(ret == 0);
assert(count == 4000);
talloc_free(dh);
talloc_free(mem_ctx);
}
int main(void)
{
do_test(DB_HASH_SIMPLE);
do_test(DB_HASH_COMPLEX);
do_traverse_test(DB_HASH_SIMPLE);
do_traverse_test(DB_HASH_COMPLEX);
return 0;
}
|