4 Copyright (C) Amitay Isaacs 2015
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "common/db_hash.c"
26 static int record_parser(uint8_t *keybuf
, size_t keylen
,
27 uint8_t *databuf
, size_t datalen
,
30 int *count
= (int *)private_data
;
36 static void do_test(enum db_hash_type type
)
38 struct db_hash_context
*dh
= NULL
;
39 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
40 uint8_t key
[] = "This is a long key";
41 uint8_t value
[] = "This is a long value";
45 ret
= db_hash_insert(dh
, key
, sizeof(key
), value
, sizeof(value
));
46 assert(ret
== EINVAL
);
48 ret
= db_hash_add(dh
, key
, sizeof(key
), value
, sizeof(value
));
49 assert(ret
== EINVAL
);
51 ret
= db_hash_exists(dh
, key
, sizeof(key
));
52 assert(ret
== EINVAL
);
54 ret
= db_hash_delete(dh
, key
, sizeof(key
));
55 assert(ret
== EINVAL
);
57 ret
= db_hash_init(mem_ctx
, "foobar", 1024, type
, &dh
);
60 ret
= db_hash_insert(dh
, key
, sizeof(key
), value
, sizeof(value
));
63 ret
= db_hash_exists(dh
, key
, sizeof(key
));
66 ret
= db_hash_fetch(dh
, key
, sizeof(key
), NULL
, NULL
);
67 assert(ret
== EINVAL
);
69 ret
= db_hash_fetch(dh
, key
, sizeof(key
), record_parser
, &count
);
73 ret
= db_hash_insert(dh
, key
, sizeof(key
), value
, sizeof(value
));
74 assert(ret
== EEXIST
);
76 ret
= db_hash_delete(dh
, key
, sizeof(key
));
79 ret
= db_hash_exists(dh
, key
, sizeof(key
));
80 assert(ret
== ENOENT
);
82 ret
= db_hash_delete(dh
, key
, sizeof(key
));
83 assert(ret
== ENOENT
);
85 ret
= db_hash_add(dh
, key
, sizeof(key
), key
, sizeof(key
));
88 ret
= db_hash_add(dh
, key
, sizeof(key
), value
, sizeof(value
));
92 ret
= talloc_get_size(mem_ctx
);
98 static void do_traverse_test(enum db_hash_type type
)
100 struct db_hash_context
*dh
= NULL
;
101 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
102 char key
[] = "keyXXXX";
103 char value
[] = "This is some test value";
106 ret
= db_hash_traverse(dh
, NULL
, NULL
, &count
);
107 assert(ret
== EINVAL
);
109 ret
= db_hash_init(mem_ctx
, "foobar", 1024, type
, &dh
);
112 for (i
=0; i
<2000; i
++) {
113 sprintf(key
, "key%04d", i
);
114 ret
= db_hash_insert(dh
, (uint8_t *)key
, sizeof(key
),
115 (uint8_t *)value
, sizeof(value
));
119 ret
= db_hash_traverse(dh
, NULL
, NULL
, &count
);
121 assert(count
== 2000);
123 ret
= db_hash_traverse(dh
, record_parser
, &count
, NULL
);
125 assert(count
== 4000);
128 talloc_free(mem_ctx
);
133 do_test(DB_HASH_SIMPLE
);
134 do_test(DB_HASH_COMPLEX
);
135 do_traverse_test(DB_HASH_SIMPLE
);
136 do_traverse_test(DB_HASH_COMPLEX
);