s4:dsdb: allocate DSDB_CONTROL_DBCHECK_FIX_DUPLICATE_LINKS oid
[Samba.git] / ctdb / tests / src / db_hash_test.c
blob1f93743e0007ceff67faa8af6f8b290d71e62efa
1 /*
2 db_hash tests
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/>.
20 #include "replace.h"
22 #include <assert.h>
24 #include "common/db_hash.c"
26 static int record_parser(uint8_t *keybuf, size_t keylen,
27 uint8_t *databuf, size_t datalen,
28 void *private_data)
30 int *count = (int *)private_data;
32 (*count) += 1;
33 return 0;
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";
42 int ret;
43 int count = 0;
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);
58 assert(ret == 0);
60 ret = db_hash_insert(dh, key, sizeof(key), value, sizeof(value));
61 assert(ret == 0);
63 ret = db_hash_exists(dh, key, sizeof(key));
64 assert(ret == 0);
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);
70 assert(ret == 0);
71 assert(count == 1);
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));
77 assert(ret == 0);
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));
86 assert(ret == 0);
88 ret = db_hash_add(dh, key, sizeof(key), value, sizeof(value));
89 assert(ret == 0);
91 talloc_free(dh);
92 ret = talloc_get_size(mem_ctx);
93 assert(ret == 0);
95 talloc_free(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";
104 int count, ret, i;
106 ret = db_hash_traverse(dh, NULL, NULL, &count);
107 assert(ret == EINVAL);
109 ret = db_hash_init(mem_ctx, "foobar", 1024, type, &dh);
110 assert(ret == 0);
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));
116 assert(ret == 0);
119 ret = db_hash_traverse(dh, NULL, NULL, &count);
120 assert(ret == 0);
121 assert(count == 2000);
123 ret = db_hash_traverse(dh, record_parser, &count, NULL);
124 assert(ret == 0);
125 assert(count == 4000);
127 talloc_free(dh);
128 talloc_free(mem_ctx);
131 int main(void)
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);
137 return 0;