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