]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-tdb1-endian.c
tdb2: add TDB_ATTRIBUTE_TDB1_HASHSIZE
[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(13);
18         tdb = tdb1_open("run-endian.tdb",
19                         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(tdb1_store(tdb, key, data, TDB_MODIFY) < 0);
29         ok1(tdb_error(tdb) == TDB_ERR_NOEXIST);
30         ok1(tdb1_store(tdb, key, data, TDB_INSERT) == 0);
31         ok1(tdb1_store(tdb, key, data, TDB_INSERT) < 0);
32         ok1(tdb_error(tdb) == TDB_ERR_EXISTS);
33         ok1(tdb1_store(tdb, key, data, TDB_MODIFY) == 0);
34
35         data = tdb1_fetch(tdb, key);
36         ok1(data.dsize == strlen("world"));
37         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
38         free(data.dptr);
39
40         key.dsize++;
41         data = tdb1_fetch(tdb, key);
42         ok1(data.dptr == NULL);
43         tdb1_close(tdb);
44
45         /* Reopen: should read it */
46         tdb = tdb1_open("run-endian.tdb", 0, O_RDWR, 0, NULL);
47         ok1(tdb);
48
49         key.dsize = strlen("hi");
50         key.dptr = (void *)"hi";
51         data = tdb1_fetch(tdb, key);
52         ok1(data.dsize == strlen("world"));
53         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
54         free(data.dptr);
55         tdb1_close(tdb);
56
57         return exit_status();
58 }