]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-firstkey-nextkey.c
tdb2: fix gcc -O3 warnings on test/layout.c
[ccan] / ccan / tdb2 / test / run-firstkey-nextkey.c
1 #include <ccan/tdb2/tdb.c>
2 #include <ccan/tdb2/free.c>
3 #include <ccan/tdb2/lock.c>
4 #include <ccan/tdb2/io.c>
5 #include <ccan/tdb2/hash.c>
6 #include <ccan/tdb2/check.c>
7 #include <ccan/tdb2/traverse.c>
8 #include <ccan/tap/tap.h>
9 #include "logging.h"
10
11 #define NUM_RECORDS 1000
12
13 static bool store_records(struct tdb_context *tdb)
14 {
15         int i;
16         struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
17         struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
18
19         for (i = 0; i < NUM_RECORDS; i++)
20                 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
21                         return false;
22         return true;
23 }
24
25 struct trav_data {
26         unsigned int records[NUM_RECORDS];
27         unsigned int calls;
28 };
29
30 static int trav(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, void *p)
31 {
32         struct trav_data *td = p;
33         int val;
34
35         memcpy(&val, dbuf.dptr, dbuf.dsize);
36         td->records[td->calls++] = val;
37         return 0;
38 }
39
40 int main(int argc, char *argv[])
41 {
42         unsigned int i, j;
43         int num;
44         struct trav_data td;
45         TDB_DATA k, k2;
46         struct tdb_context *tdb;
47         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
48                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
49                         TDB_NOMMAP|TDB_CONVERT };
50
51         plan_tests(sizeof(flags) / sizeof(flags[0])
52                    * (NUM_RECORDS*4 + (NUM_RECORDS-1)*2 + 20) + 1);
53         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
54                 tdb = tdb_open("run-traverse.tdb", flags[i],
55                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
56                 ok1(tdb);
57                 if (!tdb)
58                         continue;
59
60                 ok1(tdb_firstkey(tdb).dptr == NULL);
61                 ok1(tdb_error(tdb) == TDB_SUCCESS);
62
63                 /* One entry... */
64                 k.dptr = (unsigned char *)&num;
65                 k.dsize = sizeof(num);
66                 num = 0;
67                 ok1(tdb_store(tdb, k, k, TDB_INSERT) == 0);
68                 k = tdb_firstkey(tdb);
69                 ok1(k.dsize == sizeof(num));
70                 ok1(memcmp(k.dptr, &num, sizeof(num)) == 0);
71                 k2 = tdb_nextkey(tdb, k);
72                 ok1(k2.dsize == 0 && k2.dptr == NULL);
73                 free(k.dptr);
74
75                 /* Two entries. */
76                 k.dptr = (unsigned char *)&num;
77                 k.dsize = sizeof(num);
78                 num = 1;
79                 ok1(tdb_store(tdb, k, k, TDB_INSERT) == 0);
80                 k = tdb_firstkey(tdb);
81                 ok1(k.dsize == sizeof(num));
82                 memcpy(&num, k.dptr, sizeof(num));
83                 ok1(num == 0 || num == 1);
84                 k2 = tdb_nextkey(tdb, k);
85                 ok1(k2.dsize == sizeof(j));
86                 free(k.dptr);
87                 memcpy(&j, k2.dptr, sizeof(j));
88                 ok1(j == 0 || j == 1);
89                 ok1(j != num);
90                 k = tdb_nextkey(tdb, k2);
91                 ok1(k.dsize == 0 && k.dptr == NULL);
92                 free(k2.dptr);
93
94                 /* Clean up. */
95                 k.dptr = (unsigned char *)&num;
96                 k.dsize = sizeof(num);
97                 num = 0;
98                 ok1(tdb_delete(tdb, k) == 0);
99                 num = 1;
100                 ok1(tdb_delete(tdb, k) == 0);
101
102                 /* Now lots of records. */
103                 ok1(store_records(tdb));
104                 td.calls = 0;
105
106                 num = tdb_traverse_read(tdb, trav, &td);
107                 ok1(num == NUM_RECORDS);
108                 ok1(td.calls == NUM_RECORDS);
109
110                 /* Simple loop should match tdb_traverse_read */
111                 for (j = 0, k = tdb_firstkey(tdb); j < td.calls; j++) {
112                         int val;
113
114                         ok1(k.dsize == sizeof(val));
115                         memcpy(&val, k.dptr, k.dsize);
116                         ok1(td.records[j] == val);
117                         k2 = tdb_nextkey(tdb, k);
118                         free(k.dptr);
119                         k = k2;
120                 }
121
122                 /* But arbitary orderings should work too. */
123                 for (j = td.calls-1; j > 0; j--) {
124                         k.dptr = (unsigned char *)&td.records[j-1];
125                         k.dsize = sizeof(td.records[j-1]);
126                         k = tdb_nextkey(tdb, k);
127                         ok1(k.dsize == sizeof(td.records[j]));
128                         ok1(memcmp(k.dptr, &td.records[j], k.dsize) == 0);
129                         free(k.dptr);
130                 }
131
132                 /* Even delete should work. */
133                 for (j = 0, k = tdb_firstkey(tdb); k.dptr; j++) {
134                         ok1(k.dsize == 4);
135                         ok1(tdb_delete(tdb, k) == 0);
136                         k2 = tdb_nextkey(tdb, k);
137                         free(k.dptr);
138                         k = k2;
139                 }
140
141                 diag("delete using first/nextkey gave %u of %u records",
142                      j, NUM_RECORDS);
143                 ok1(j == NUM_RECORDS);
144                 tdb_close(tdb);
145         }
146
147         ok1(tap_log_messages == 0);
148         return exit_status();
149 }