1 #include <ccan/tdb2/tdb.c>
2 #include <ccan/tdb2/free.c>
3 #include <ccan/tdb2/lock.c>
4 #include <ccan/tdb2/io.c>
5 #include <ccan/tdb2/hash.c>
6 #include <ccan/tdb2/check.c>
7 #include <ccan/tdb2/traverse.c>
8 #include <ccan/tap/tap.h>
11 #define NUM_RECORDS 1000
13 static bool store_records(struct tdb_context *tdb)
16 struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
17 struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
19 for (i = 0; i < NUM_RECORDS; i++)
20 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
26 unsigned int records[NUM_RECORDS];
30 static int trav(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, void *p)
32 struct trav_data *td = p;
35 memcpy(&val, dbuf.dptr, dbuf.dsize);
36 td->records[td->calls++] = val;
40 int main(int argc, char *argv[])
46 struct tdb_context *tdb;
47 int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
48 TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
49 TDB_NOMMAP|TDB_CONVERT };
51 plan_tests(sizeof(flags) / sizeof(flags[0])
52 * (NUM_RECORDS*4 + (NUM_RECORDS-1)*2 + 20) + 1);
53 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
54 tdb = tdb_open("run-traverse.tdb", flags[i],
55 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
60 ok1(tdb_firstkey(tdb).dptr == NULL);
61 ok1(tdb_error(tdb) == TDB_SUCCESS);
64 k.dptr = (unsigned char *)#
65 k.dsize = sizeof(num);
67 ok1(tdb_store(tdb, k, k, TDB_INSERT) == 0);
68 k = tdb_firstkey(tdb);
69 ok1(k.dsize == sizeof(num));
70 ok1(memcmp(k.dptr, &num, sizeof(num)) == 0);
71 k2 = tdb_nextkey(tdb, k);
72 ok1(k2.dsize == 0 && k2.dptr == NULL);
76 k.dptr = (unsigned char *)#
77 k.dsize = sizeof(num);
79 ok1(tdb_store(tdb, k, k, TDB_INSERT) == 0);
80 k = tdb_firstkey(tdb);
81 ok1(k.dsize == sizeof(num));
82 memcpy(&num, k.dptr, sizeof(num));
83 ok1(num == 0 || num == 1);
84 k2 = tdb_nextkey(tdb, k);
85 ok1(k2.dsize == sizeof(j));
87 memcpy(&j, k2.dptr, sizeof(j));
88 ok1(j == 0 || j == 1);
90 k = tdb_nextkey(tdb, k2);
91 ok1(k.dsize == 0 && k.dptr == NULL);
95 k.dptr = (unsigned char *)#
96 k.dsize = sizeof(num);
98 ok1(tdb_delete(tdb, k) == 0);
100 ok1(tdb_delete(tdb, k) == 0);
102 /* Now lots of records. */
103 ok1(store_records(tdb));
106 num = tdb_traverse_read(tdb, trav, &td);
107 ok1(num == NUM_RECORDS);
108 ok1(td.calls == NUM_RECORDS);
110 /* Simple loop should match tdb_traverse_read */
111 for (j = 0, k = tdb_firstkey(tdb); j < td.calls; j++) {
114 ok1(k.dsize == sizeof(val));
115 memcpy(&val, k.dptr, k.dsize);
116 ok1(td.records[j] == val);
117 k2 = tdb_nextkey(tdb, k);
122 /* But arbitary orderings should work too. */
123 for (j = td.calls-1; j > 0; j--) {
124 k.dptr = (unsigned char *)&td.records[j-1];
125 k.dsize = sizeof(td.records[j-1]);
126 k = tdb_nextkey(tdb, k);
127 ok1(k.dsize == sizeof(td.records[j]));
128 ok1(memcmp(k.dptr, &td.records[j], k.dsize) == 0);
132 /* Even delete should work. */
133 for (j = 0, k = tdb_firstkey(tdb); k.dptr; j++) {
135 ok1(tdb_delete(tdb, k) == 0);
136 k2 = tdb_nextkey(tdb, k);
141 diag("delete using first/nextkey gave %u of %u records",
143 ok1(j == NUM_RECORDS);
147 ok1(tap_log_messages == 0);
148 return exit_status();