ntdb: remove hash table trees.
[Samba/id10ts.git] / lib / ntdb / test / run-traverse.c
blobed95f3360458d169336037b845e98e311885854a
1 #include "ntdb-source.h"
2 #include "tap-interface.h"
3 #include "logging.h"
5 #define NUM_RECORDS 1000
7 /* We use the same seed which we saw a failure on. */
8 static uint32_t fixedhash(const void *key, size_t len, uint32_t seed, void *p)
10 return hash64_stable((const unsigned char *)key, len,
11 *(uint64_t *)p);
14 static bool store_records(struct ntdb_context *ntdb)
16 int i;
17 NTDB_DATA key = { (unsigned char *)&i, sizeof(i) };
18 NTDB_DATA data = { (unsigned char *)&i, sizeof(i) };
20 for (i = 0; i < NUM_RECORDS; i++)
21 if (ntdb_store(ntdb, key, data, NTDB_REPLACE) != 0)
22 return false;
23 return true;
26 struct trav_data {
27 unsigned int calls, call_limit;
28 int low, high;
29 bool mismatch;
30 bool delete;
31 enum NTDB_ERROR delete_error;
34 static int trav(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf,
35 struct trav_data *td)
37 int val;
39 td->calls++;
40 if (key.dsize != sizeof(val) || dbuf.dsize != sizeof(val)
41 || memcmp(key.dptr, dbuf.dptr, key.dsize) != 0) {
42 td->mismatch = true;
43 return -1;
45 memcpy(&val, dbuf.dptr, dbuf.dsize);
46 if (val < td->low)
47 td->low = val;
48 if (val > td->high)
49 td->high = val;
51 if (td->delete) {
52 td->delete_error = ntdb_delete(ntdb, key);
53 if (td->delete_error != NTDB_SUCCESS) {
54 return -1;
58 if (td->calls == td->call_limit)
59 return 1;
60 return 0;
63 struct trav_grow_data {
64 unsigned int calls;
65 unsigned int num_large;
66 bool mismatch;
67 enum NTDB_ERROR error;
70 static int trav_grow(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf,
71 struct trav_grow_data *tgd)
73 int val;
74 unsigned char buffer[128] = { 0 };
76 tgd->calls++;
77 if (key.dsize != sizeof(val) || dbuf.dsize < sizeof(val)
78 || memcmp(key.dptr, dbuf.dptr, key.dsize) != 0) {
79 tgd->mismatch = true;
80 return -1;
83 if (dbuf.dsize > sizeof(val))
84 /* We must have seen this before! */
85 tgd->num_large++;
87 /* Make a big difference to the database. */
88 dbuf.dptr = buffer;
89 dbuf.dsize = sizeof(buffer);
90 tgd->error = ntdb_append(ntdb, key, dbuf);
91 if (tgd->error != NTDB_SUCCESS) {
92 return -1;
94 return 0;
97 int main(int argc, char *argv[])
99 unsigned int i;
100 int num;
101 struct trav_data td;
102 struct trav_grow_data tgd;
103 struct ntdb_context *ntdb;
104 uint64_t seed = 16014841315512641303ULL;
105 int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
106 NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
107 NTDB_NOMMAP|NTDB_CONVERT };
108 union ntdb_attribute hattr = { .hash = { .base = { NTDB_ATTRIBUTE_HASH },
109 .fn = fixedhash,
110 .data = &seed } };
112 hattr.base.next = &tap_log_attr;
114 plan_tests(sizeof(flags) / sizeof(flags[0]) * 32 + 1);
115 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
116 ntdb = ntdb_open("run-traverse.ntdb", flags[i],
117 O_RDWR|O_CREAT|O_TRUNC, 0600, &hattr);
118 ok1(ntdb);
119 if (!ntdb)
120 continue;
122 ok1(ntdb_traverse(ntdb, NULL, NULL) == 0);
124 ok1(store_records(ntdb));
125 num = ntdb_traverse(ntdb, NULL, NULL);
126 ok1(num == NUM_RECORDS);
128 /* Full traverse. */
129 td.calls = 0;
130 td.call_limit = UINT_MAX;
131 td.low = INT_MAX;
132 td.high = INT_MIN;
133 td.mismatch = false;
134 td.delete = false;
136 num = ntdb_traverse(ntdb, trav, &td);
137 ok1(num == NUM_RECORDS);
138 ok1(!td.mismatch);
139 ok1(td.calls == NUM_RECORDS);
140 ok1(td.low == 0);
141 ok1(td.high == NUM_RECORDS-1);
143 /* Short traverse. */
144 td.calls = 0;
145 td.call_limit = NUM_RECORDS / 2;
146 td.low = INT_MAX;
147 td.high = INT_MIN;
148 td.mismatch = false;
149 td.delete = false;
151 num = ntdb_traverse(ntdb, trav, &td);
152 ok1(num == NUM_RECORDS / 2);
153 ok1(!td.mismatch);
154 ok1(td.calls == NUM_RECORDS / 2);
155 ok1(td.low <= NUM_RECORDS / 2);
156 ok1(td.high > NUM_RECORDS / 2);
157 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
158 ok1(tap_log_messages == 0);
160 /* Deleting traverse (delete everything). */
161 td.calls = 0;
162 td.call_limit = UINT_MAX;
163 td.low = INT_MAX;
164 td.high = INT_MIN;
165 td.mismatch = false;
166 td.delete = true;
167 td.delete_error = NTDB_SUCCESS;
168 num = ntdb_traverse(ntdb, trav, &td);
169 ok1(num == NUM_RECORDS);
170 ok1(td.delete_error == NTDB_SUCCESS);
171 ok1(!td.mismatch);
172 ok1(td.calls == NUM_RECORDS);
173 ok1(td.low == 0);
174 ok1(td.high == NUM_RECORDS - 1);
175 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
177 /* Now it's empty! */
178 ok1(ntdb_traverse(ntdb, NULL, NULL) == 0);
180 /* Re-add. */
181 ok1(store_records(ntdb));
182 ok1(ntdb_traverse(ntdb, NULL, NULL) == NUM_RECORDS);
183 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
185 /* Grow. This will cause us to be reshuffled. */
186 tgd.calls = 0;
187 tgd.num_large = 0;
188 tgd.mismatch = false;
189 tgd.error = NTDB_SUCCESS;
190 ok1(ntdb_traverse(ntdb, trav_grow, &tgd) > 1);
191 ok1(tgd.error == 0);
192 ok1(!tgd.mismatch);
193 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
194 ok1(tgd.num_large < tgd.calls);
195 diag("growing db: %u calls, %u repeats",
196 tgd.calls, tgd.num_large);
198 ntdb_close(ntdb);
201 ok1(tap_log_messages == 0);
202 return exit_status();