tdb2: test: import tdb1's tests.
[ccan] / ccan / tdb2 / test / run-tdb1-endian.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include <stdlib.h>
4 #include <err.h>
5 #include "tdb1-logging.h"
6
7 int main(int argc, char *argv[])
8 {
9         struct tdb1_context *tdb;
10         TDB1_DATA key, data;
11
12         plan_tests(13);
13         tdb = tdb1_open_ex("run-endian.tdb", 1024,
14                           TDB1_CLEAR_IF_FIRST|TDB1_CONVERT,
15                           O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
16
17         ok1(tdb);
18         key.dsize = strlen("hi");
19         key.dptr = (void *)"hi";
20         data.dsize = strlen("world");
21         data.dptr = (void *)"world";
22
23         ok1(tdb1_store(tdb, key, data, TDB1_MODIFY) < 0);
24         ok1(tdb1_error(tdb) == TDB1_ERR_NOEXIST);
25         ok1(tdb1_store(tdb, key, data, TDB1_INSERT) == 0);
26         ok1(tdb1_store(tdb, key, data, TDB1_INSERT) < 0);
27         ok1(tdb1_error(tdb) == TDB1_ERR_EXISTS);
28         ok1(tdb1_store(tdb, key, data, TDB1_MODIFY) == 0);
29
30         data = tdb1_fetch(tdb, key);
31         ok1(data.dsize == strlen("world"));
32         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
33         free(data.dptr);
34
35         key.dsize++;
36         data = tdb1_fetch(tdb, key);
37         ok1(data.dptr == NULL);
38         tdb1_close(tdb);
39
40         /* Reopen: should read it */
41         tdb = tdb1_open_ex("run-endian.tdb", 1024, 0, O_RDWR, 0,
42                           &taplogctx, NULL);
43         ok1(tdb);
44
45         key.dsize = strlen("hi");
46         key.dptr = (void *)"hi";
47         data = tdb1_fetch(tdb, key);
48         ok1(data.dsize == strlen("world"));
49         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
50         free(data.dptr);
51         tdb1_close(tdb);
52
53         return exit_status();
54 }