]> git.ozlabs.org Git - ccan/blob - junkcode/rusty@rustcorp.com.au-ntdb/test/run-25-hashoverload.c
ccan/ntdb: demote to junkcode.
[ccan] / junkcode / rusty@rustcorp.com.au-ntdb / test / run-25-hashoverload.c
1 #include "ntdb-source.h"
2 #include "tap-interface.h"
3 #include "logging.h"
4 #include "helprun-external-agent.h"
5
6 #define OVERLOAD 100
7
8 static uint32_t badhash(const void *key, size_t len, uint32_t seed, void *priv)
9 {
10         return 0;
11 }
12
13 static int trav(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf, void *p)
14 {
15         if (p)
16                 return ntdb_delete(ntdb, key);
17         return 0;
18 }
19
20 int main(int argc, char *argv[])
21 {
22         unsigned int i, j;
23         struct ntdb_context *ntdb;
24         NTDB_DATA key = { (unsigned char *)&j, sizeof(j) };
25         NTDB_DATA dbuf = { (unsigned char *)&j, sizeof(j) };
26         union ntdb_attribute hattr = { .hash = { .base = { NTDB_ATTRIBUTE_HASH },
27                                                 .fn = badhash } };
28         int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
29                         NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
30                         NTDB_NOMMAP|NTDB_CONVERT,
31         };
32
33         hattr.base.next = &tap_log_attr;
34
35         plan_tests(sizeof(flags) / sizeof(flags[0]) * (7 * OVERLOAD + 11) + 1);
36         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
37                 NTDB_DATA d = { NULL, 0 }; /* Bogus GCC warning */
38
39                 ntdb = ntdb_open("run-25-hashoverload.ntdb",
40                                  flags[i]|MAYBE_NOSYNC,
41                                  O_RDWR|O_CREAT|O_TRUNC, 0600, &hattr);
42                 ok1(ntdb);
43                 if (!ntdb)
44                         continue;
45
46                 /* Overload a bucket. */
47                 for (j = 0; j < OVERLOAD; j++) {
48                         ok1(ntdb_store(ntdb, key, dbuf, NTDB_INSERT) == 0);
49                 }
50                 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
51
52                 /* Check we can find them all. */
53                 for (j = 0; j < OVERLOAD; j++) {
54                         ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
55                         ok1(d.dsize == sizeof(j));
56                         ok1(d.dptr != NULL);
57                         ok1(d.dptr && memcmp(d.dptr, &j, d.dsize) == 0);
58                         free(d.dptr);
59                 }
60
61                 /* Traverse through them. */
62                 ok1(ntdb_traverse(ntdb, trav, NULL) == OVERLOAD);
63
64                 /* Delete the first 99. */
65                 for (j = 0; j < OVERLOAD-1; j++)
66                         ok1(ntdb_delete(ntdb, key) == 0);
67
68                 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
69
70                 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
71                 ok1(d.dsize == sizeof(j));
72                 ok1(d.dptr != NULL);
73                 ok1(d.dptr && memcmp(d.dptr, &j, d.dsize) == 0);
74                 free(d.dptr);
75
76                 /* Traverse through them. */
77                 ok1(ntdb_traverse(ntdb, trav, NULL) == 1);
78
79                 /* Re-add */
80                 for (j = 0; j < OVERLOAD-1; j++) {
81                         ok1(ntdb_store(ntdb, key, dbuf, NTDB_INSERT) == 0);
82                 }
83                 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
84
85                 /* Now try deleting as we go. */
86                 ok1(ntdb_traverse(ntdb, trav, trav) == OVERLOAD);
87                 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
88                 ok1(ntdb_traverse(ntdb, trav, NULL) == 0);
89                 ntdb_close(ntdb);
90         }
91
92         ok1(tap_log_messages == 0);
93         return exit_status();
94 }