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