ntdb: next-generation trivial key-value database
[ccan] / ccan / ntdb / test / run-tdb_foreach.c
1 #include "ntdb-source.h"
2 #include "tap-interface.h"
3 #include "logging.h"
4
5 static int drop_count(struct ntdb_context *ntdb, unsigned int *count)
6 {
7         if (--(*count) == 0)
8                 return 1;
9         return 0;
10 }
11
12 static int set_found(struct ntdb_context *ntdb, bool found[3])
13 {
14         unsigned int idx;
15
16         if (strcmp(ntdb_name(ntdb), "run-ntdb_foreach0.ntdb") == 0)
17                 idx = 0;
18         else if (strcmp(ntdb_name(ntdb), "run-ntdb_foreach1.ntdb") == 0)
19                 idx = 1;
20         else if (strcmp(ntdb_name(ntdb), "run-ntdb_foreach2.ntdb") == 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 ntdb_context *ntdb0, *ntdb1, *ntdb;
36         int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
37                         NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
38
39         plan_tests(sizeof(flags) / sizeof(flags[0]) * 8);
40         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
41                 ntdb0 = ntdb_open("run-ntdb_foreach0.ntdb",
42                                   flags[i]|MAYBE_NOSYNC,
43                                   O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
44                 ntdb1 = ntdb_open("run-ntdb_foreach1.ntdb",
45                                   flags[i]|MAYBE_NOSYNC,
46                                   O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
47                 ntdb = ntdb_open("run-ntdb_foreach2.ntdb",
48                                  flags[i]|MAYBE_NOSYNC,
49                                  O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
50
51                 memset(found, 0, sizeof(found));
52                 ntdb_foreach(set_found, found);
53                 ok1(found[0] && found[1] && found[2]);
54
55                 /* Test premature iteration termination */
56                 count = 1;
57                 ntdb_foreach(drop_count, &count);
58                 ok1(count == 0);
59
60                 ntdb_close(ntdb1);
61                 memset(found, 0, sizeof(found));
62                 ntdb_foreach(set_found, found);
63                 ok1(found[0] && !found[1] && found[2]);
64
65                 ntdb_close(ntdb);
66                 memset(found, 0, sizeof(found));
67                 ntdb_foreach(set_found, found);
68                 ok1(found[0] && !found[1] && !found[2]);
69
70                 ntdb1 = ntdb_open("run-ntdb_foreach1.ntdb",
71                                   flags[i]|MAYBE_NOSYNC,
72                                   O_RDWR, 0600, &tap_log_attr);
73                 memset(found, 0, sizeof(found));
74                 ntdb_foreach(set_found, found);
75                 ok1(found[0] && found[1] && !found[2]);
76
77                 ntdb_close(ntdb0);
78                 memset(found, 0, sizeof(found));
79                 ntdb_foreach(set_found, found);
80                 ok1(!found[0] && found[1] && !found[2]);
81
82                 ntdb_close(ntdb1);
83                 memset(found, 0, sizeof(found));
84                 ntdb_foreach(set_found, found);
85                 ok1(!found[0] && !found[1] && !found[2]);
86                 ok1(tap_log_messages == 0);
87         }
88
89         return exit_status();
90 }