]> git.ozlabs.org Git - ccan/blob - junkcode/rusty@rustcorp.com.au-ntdb/test/api-firstkey-nextkey.c
ccan/ntdb: demote to junkcode.
[ccan] / junkcode / rusty@rustcorp.com.au-ntdb / test / api-firstkey-nextkey.c
1 #include "config.h"
2 #include "../ntdb.h"
3 #include "../private.h"
4 #include "tap-interface.h"
5 #include "logging.h"
6 #include "helpapi-external-agent.h"
7
8 #define NUM_RECORDS 1000
9
10 static bool store_records(struct ntdb_context *ntdb)
11 {
12         int i;
13         NTDB_DATA key = { (unsigned char *)&i, sizeof(i) };
14         NTDB_DATA data = { (unsigned char *)&i, sizeof(i) };
15
16         for (i = 0; i < NUM_RECORDS; i++)
17                 if (ntdb_store(ntdb, key, data, NTDB_REPLACE) != 0)
18                         return false;
19         return true;
20 }
21
22 struct trav_data {
23         unsigned int records[NUM_RECORDS];
24         unsigned int calls;
25 };
26
27 static int trav(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf, void *p)
28 {
29         struct trav_data *td = p;
30         int val;
31
32         memcpy(&val, dbuf.dptr, dbuf.dsize);
33         td->records[td->calls++] = val;
34         return 0;
35 }
36
37 /* Since ntdb_nextkey frees dptr, we need to clone it. */
38 static NTDB_DATA dup_key(NTDB_DATA key)
39 {
40         void *p = malloc(key.dsize);
41         memcpy(p, key.dptr, key.dsize);
42         key.dptr = p;
43         return key;
44 }
45
46 int main(int argc, char *argv[])
47 {
48         unsigned int i, j;
49         int num;
50         struct trav_data td;
51         NTDB_DATA k;
52         struct ntdb_context *ntdb;
53         union ntdb_attribute seed_attr;
54         enum NTDB_ERROR ecode;
55         int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
56                         NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
57                         NTDB_NOMMAP|NTDB_CONVERT };
58
59         seed_attr.base.attr = NTDB_ATTRIBUTE_SEED;
60         seed_attr.base.next = &tap_log_attr;
61         seed_attr.seed.seed = 6334326220117065685ULL;
62
63         plan_tests(sizeof(flags) / sizeof(flags[0])
64                    * (NUM_RECORDS*6 + (NUM_RECORDS-1)*3 + 22) + 1);
65         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
66                 ntdb = ntdb_open("api-firstkey-nextkey.ntdb",
67                                  flags[i]|MAYBE_NOSYNC,
68                                  O_RDWR|O_CREAT|O_TRUNC, 0600,
69                                  &seed_attr);
70                 ok1(ntdb);
71                 if (!ntdb)
72                         continue;
73
74                 ok1(ntdb_firstkey(ntdb, &k) == NTDB_ERR_NOEXIST);
75
76                 /* One entry... */
77                 k.dptr = (unsigned char *)&num;
78                 k.dsize = sizeof(num);
79                 num = 0;
80                 ok1(ntdb_store(ntdb, k, k, NTDB_INSERT) == 0);
81                 ok1(ntdb_firstkey(ntdb, &k) == NTDB_SUCCESS);
82                 ok1(k.dsize == sizeof(num));
83                 ok1(memcmp(k.dptr, &num, sizeof(num)) == 0);
84                 ok1(ntdb_nextkey(ntdb, &k) == NTDB_ERR_NOEXIST);
85
86                 /* Two entries. */
87                 k.dptr = (unsigned char *)&num;
88                 k.dsize = sizeof(num);
89                 num = 1;
90                 ok1(ntdb_store(ntdb, k, k, NTDB_INSERT) == 0);
91                 ok1(ntdb_firstkey(ntdb, &k) == NTDB_SUCCESS);
92                 ok1(k.dsize == sizeof(num));
93                 memcpy(&num, k.dptr, sizeof(num));
94                 ok1(num == 0 || num == 1);
95                 ok1(ntdb_nextkey(ntdb, &k) == NTDB_SUCCESS);
96                 ok1(k.dsize == sizeof(j));
97                 memcpy(&j, k.dptr, sizeof(j));
98                 ok1(j == 0 || j == 1);
99                 ok1(j != num);
100                 ok1(ntdb_nextkey(ntdb, &k) == NTDB_ERR_NOEXIST);
101
102                 /* Clean up. */
103                 k.dptr = (unsigned char *)&num;
104                 k.dsize = sizeof(num);
105                 num = 0;
106                 ok1(ntdb_delete(ntdb, k) == 0);
107                 num = 1;
108                 ok1(ntdb_delete(ntdb, k) == 0);
109
110                 /* Now lots of records. */
111                 ok1(store_records(ntdb));
112                 td.calls = 0;
113
114                 num = ntdb_traverse(ntdb, trav, &td);
115                 ok1(num == NUM_RECORDS);
116                 ok1(td.calls == NUM_RECORDS);
117
118                 /* Simple loop should match ntdb_traverse */
119                 for (j = 0, ecode = ntdb_firstkey(ntdb, &k); j < td.calls; j++) {
120                         int val;
121
122                         ok1(ecode == NTDB_SUCCESS);
123                         ok1(k.dsize == sizeof(val));
124                         memcpy(&val, k.dptr, k.dsize);
125                         ok1(td.records[j] == val);
126                         ecode = ntdb_nextkey(ntdb, &k);
127                 }
128
129                 /* But arbitrary orderings should work too. */
130                 for (j = td.calls-1; j > 0; j--) {
131                         k.dptr = (unsigned char *)&td.records[j-1];
132                         k.dsize = sizeof(td.records[j-1]);
133                         k = dup_key(k);
134                         ok1(ntdb_nextkey(ntdb, &k) == NTDB_SUCCESS);
135                         ok1(k.dsize == sizeof(td.records[j]));
136                         ok1(memcmp(k.dptr, &td.records[j], k.dsize) == 0);
137                         free(k.dptr);
138                 }
139
140                 /* Even delete should work. */
141                 for (j = 0, ecode = ntdb_firstkey(ntdb, &k);
142                      ecode != NTDB_ERR_NOEXIST;
143                      j++) {
144                         ok1(ecode == NTDB_SUCCESS);
145                         ok1(k.dsize == 4);
146                         ok1(ntdb_delete(ntdb, k) == 0);
147                         ecode = ntdb_nextkey(ntdb, &k);
148                 }
149
150                 diag("delete using first/nextkey gave %u of %u records",
151                      j, NUM_RECORDS);
152                 ok1(j == NUM_RECORDS);
153                 ntdb_close(ntdb);
154         }
155
156         ok1(tap_log_messages == 0);
157         return exit_status();
158 }