tdb2: feature support.
[ccan] / ccan / tdb2 / test / run-check-callback.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/tdb2/transaction.c>
9 #include <ccan/tap/tap.h>
10 #include "logging.h"
11
12 #define NUM_RECORDS 1000
13
14 static bool store_records(struct tdb_context *tdb)
15 {
16         int i;
17         struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
18         struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
19
20         for (i = 0; i < NUM_RECORDS; i++)
21                 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
22                         return false;
23         return true;
24 }
25
26 static enum TDB_ERROR check(struct tdb_data key,
27                             struct tdb_data data,
28                             bool *array)
29 {
30         int val;
31
32         if (key.dsize != sizeof(val)) {
33                 diag("Wrong key size: %u\n", key.dsize);
34                 return TDB_ERR_CORRUPT;
35         }
36
37         if (key.dsize != data.dsize
38             || memcmp(key.dptr, data.dptr, sizeof(val)) != 0) {
39                 diag("Key and data differ\n");
40                 return TDB_ERR_CORRUPT;
41         }
42
43         memcpy(&val, key.dptr, sizeof(val));
44         if (val >= NUM_RECORDS || val < 0) {
45                 diag("check value %i\n", val);
46                 return TDB_ERR_CORRUPT;
47         }
48
49         if (array[val]) {
50                 diag("Value %i already seen\n", val);
51                 return TDB_ERR_CORRUPT;
52         }
53
54         array[val] = true;
55         return TDB_SUCCESS;
56 }
57
58 int main(int argc, char *argv[])
59 {
60         unsigned int i, j;
61         struct tdb_context *tdb;
62         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
63                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
64                         TDB_NOMMAP|TDB_CONVERT };
65
66         plan_tests(sizeof(flags) / sizeof(flags[0]) * 4 + 1);
67         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
68                 bool array[NUM_RECORDS];
69
70                 tdb = tdb_open("run-check-callback.tdb", flags[i],
71                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
72                 ok1(tdb);
73                 if (!tdb)
74                         continue;
75
76                 ok1(store_records(tdb));
77                 for (j = 0; j < NUM_RECORDS; j++)
78                         array[j] = false;
79                 ok1(tdb_check(tdb, check, array) == TDB_SUCCESS);
80                 for (j = 0; j < NUM_RECORDS; j++)
81                         if (!array[j])
82                                 break;
83                 ok1(j == NUM_RECORDS);
84                 tdb_close(tdb);
85         }
86
87         ok1(tap_log_messages == 0);
88         return exit_status();
89 }