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