New test for endianness.
[ccan] / ccan / tdb / test / run-endian.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(13);
21         tdb = tdb_open("/tmp/test.tdb", 1024, TDB_CLEAR_IF_FIRST|TDB_CONVERT,
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         /* Reopen: should read it */
48         tdb = tdb_open("/tmp/test.tdb", 1024, 0, O_RDWR, 0);
49         ok1(tdb);
50         
51         key.dsize = strlen("hi");
52         key.dptr = (void *)"hi";
53         data = tdb_fetch(tdb, key);
54         ok1(data.dsize == strlen("world"));
55         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
56         free(data.dptr);
57         tdb_close(tdb);
58
59         return exit_status();
60 }