]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-tdb_foreach.c
htable: fix bug where first entry has hash of 0 or 1.
[ccan] / ccan / tdb2 / test / run-tdb_foreach.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include "logging.h"
4
5 static int drop_count(struct tdb_context *tdb, unsigned int *count)
6 {
7         if (--(*count) == 0)
8                 return 1;
9         return 0;
10 }
11
12 static int set_found(struct tdb_context *tdb, bool found[3])
13 {
14         unsigned int idx;
15
16         if (strcmp(tdb_name(tdb), "run-tdb_foreach0.tdb") == 0)
17                 idx = 0;
18         else if (strcmp(tdb_name(tdb), "run-tdb_foreach1.tdb") == 0)
19                 idx = 1;
20         else if (strcmp(tdb_name(tdb), "run-tdb_foreach2.tdb") == 0)
21                 idx = 2;
22         else
23                 abort();
24
25         if (found[idx])
26                 abort();
27         found[idx] = true;
28         return 0;
29 }
30
31 int main(int argc, char *argv[])
32 {
33         unsigned int i, count;
34         bool found[3];
35         struct tdb_context *tdb0, *tdb1, *tdb2;
36         int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
37                         TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
38
39         plan_tests(sizeof(flags) / sizeof(flags[0]) * 8);
40         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
41                 tdb0 = tdb_open("run-tdb_foreach0.tdb", flags[i],
42                                 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
43                 tdb1 = tdb_open("run-tdb_foreach1.tdb", flags[i],
44                                 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
45                 tdb2 = tdb_open("run-tdb_foreach2.tdb", flags[i],
46                                 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
47
48                 memset(found, 0, sizeof(found));
49                 tdb_foreach(set_found, found);
50                 ok1(found[0] && found[1] && found[2]);
51
52                 /* Test premature iteration termination */
53                 count = 1;
54                 tdb_foreach(drop_count, &count);
55                 ok1(count == 0);
56
57                 tdb_close(tdb1);
58                 memset(found, 0, sizeof(found));
59                 tdb_foreach(set_found, found);
60                 ok1(found[0] && !found[1] && found[2]);
61
62                 tdb_close(tdb2);
63                 memset(found, 0, sizeof(found));
64                 tdb_foreach(set_found, found);
65                 ok1(found[0] && !found[1] && !found[2]);
66
67                 tdb1 = tdb_open("run-tdb_foreach1.tdb", flags[i],
68                                 O_RDWR, 0600, &tap_log_attr);
69                 memset(found, 0, sizeof(found));
70                 tdb_foreach(set_found, found);
71                 ok1(found[0] && found[1] && !found[2]);
72
73                 tdb_close(tdb0);
74                 memset(found, 0, sizeof(found));
75                 tdb_foreach(set_found, found);
76                 ok1(!found[0] && found[1] && !found[2]);
77
78                 tdb_close(tdb1);
79                 memset(found, 0, sizeof(found));
80                 tdb_foreach(set_found, found);
81                 ok1(!found[0] && !found[1] && !found[2]);
82                 ok1(tap_log_messages == 0);
83         }
84
85         return exit_status();
86 }