tdb2: unify tdb1_store into tdb_store
[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 "logging.h"
6
7 int main(int argc, char *argv[])
8 {
9         struct tdb_context *tdb;
10         TDB_DATA key, data;
11         union tdb_attribute hsize;
12
13         hsize.base.attr = TDB_ATTRIBUTE_TDB1_HASHSIZE;
14         hsize.base.next = &tap_log_attr;
15         hsize.tdb1_hashsize.hsize = 1024;
16
17         plan_tests(11);
18         tdb = tdb_open("run-endian.tdb1",
19                        TDB_VERSION1|TDB_CONVERT,
20                        O_CREAT|O_TRUNC|O_RDWR, 0600, &hsize);
21
22         ok1(tdb);
23         key.dsize = strlen("hi");
24         key.dptr = (void *)"hi";
25         data.dsize = strlen("world");
26         data.dptr = (void *)"world";
27
28         ok1(tdb_store(tdb, key, data, TDB_MODIFY) == TDB_ERR_NOEXIST);
29         ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_SUCCESS);
30         ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_ERR_EXISTS);
31         ok1(tdb_store(tdb, key, data, TDB_MODIFY) == TDB_SUCCESS);
32
33         data = tdb1_fetch(tdb, key);
34         ok1(data.dsize == strlen("world"));
35         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
36         free(data.dptr);
37
38         key.dsize++;
39         data = tdb1_fetch(tdb, key);
40         ok1(data.dptr == NULL);
41         tdb_close(tdb);
42
43         /* Reopen: should read it */
44         tdb = tdb_open("run-endian.tdb1", 0, O_RDWR, 0, NULL);
45         ok1(tdb);
46
47         key.dsize = strlen("hi");
48         key.dptr = (void *)"hi";
49         data = tdb1_fetch(tdb, key);
50         ok1(data.dsize == strlen("world"));
51         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
52         free(data.dptr);
53         tdb_close(tdb);
54
55         return exit_status();
56 }