dd4f7d40eda615d315505502eed80a8683ade180
[ccan] / ccan / tdb2 / test / run-simple-fetch.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/check.c>
6 #include <ccan/tap/tap.h>
7 #include "logging.h"
8
9 int main(int argc, char *argv[])
10 {
11         unsigned int i;
12         struct tdb_context *tdb;
13         int flags[] = { TDB_INTERNAL, TDB_DEFAULT,
14                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT };
15         struct tdb_data key = { (unsigned char *)"key", 3 };
16         struct tdb_data data = { (unsigned char *)"data", 4 };
17
18         plan_tests(sizeof(flags) / sizeof(flags[0]) * 8 + 1);
19         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
20                 tdb = tdb_open("run-simple-fetch.tdb", flags[i],
21                                O_RDWR|O_CREAT|O_TRUNC, 0600, NULL);
22                 tdb->log = tap_log_fn;
23                 ok1(tdb);
24                 if (tdb) {
25                         struct tdb_data d;
26
27                         /* fetch should fail. */
28                         d = tdb_fetch(tdb, key);
29                         ok1(d.dptr == NULL);
30                         ok1(tdb_error(tdb) == TDB_ERR_NOEXIST);
31                         ok1(tdb_check(tdb, NULL, NULL) == 0);
32                         /* Insert should succeed. */
33                         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
34                         ok1(tdb_check(tdb, NULL, NULL) == 0);
35                         /* Fetch should now work. */
36                         d = tdb_fetch(tdb, key);
37                         ok1(data_equal(d, data));
38                         ok1(tdb_check(tdb, NULL, NULL) == 0);
39                         tdb_close(tdb);
40                 }
41         }
42         ok1(tap_log_messages == 0);
43         return exit_status();
44 }