ccanize tdb further, and add simple test.
[ccan] / ccan / tdb / test / run.c
1 #define _XOPEN_SOURCE 500
2 #include "tdb/tdb.h"
3 #include "tdb/io.c"
4 #include "tdb/tdb.c"
5 #include "tdb/lock.c"
6 #include "tdb/freelist.c"
7 #include "tdb/traverse.c"
8 #include "tdb/transaction.c"
9 #include "tdb/error.c"
10 #include "tdb/open.c"
11 #include "tap/tap.h"
12 #include <stdlib.h>
13 #include <err.h>
14
15 int main(int argc, char *argv[])
16 {
17         struct tdb_context *tdb;
18         TDB_DATA key, data;
19
20         plan_tests(10);
21         tdb = tdb_open("/tmp/test.tdb", 1024, TDB_CLEAR_IF_FIRST,
22                        O_CREAT|O_TRUNC|O_RDWR, 0600);
23
24         ok1(tdb);
25         key.dsize = strlen("hi");
26         key.dptr = (void *)"hi";
27         data.dsize = strlen("world");
28         data.dptr = (void *)"world";
29
30         ok1(tdb_store(tdb, key, data, TDB_MODIFY) < 0);
31         ok1(tdb_error(tdb) == TDB_ERR_NOEXIST);
32         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
33         ok1(tdb_store(tdb, key, data, TDB_INSERT) < 0);
34         ok1(tdb_error(tdb) == TDB_ERR_EXISTS);
35         ok1(tdb_store(tdb, key, data, TDB_MODIFY) == 0);
36
37         data = tdb_fetch(tdb, key);
38         ok1(data.dsize == strlen("world"));
39         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
40         free(data.dptr);
41
42         key.dsize++;
43         data = tdb_fetch(tdb, key);
44         ok1(data.dptr == NULL);
45         tdb_close(tdb);
46
47         return exit_status();
48 }