3f1fb5b3e878c0dd81db11607cda28949efb7a33
[ccan] / ccan / tdb2 / test / run-15-append.c
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/tap/tap.h>
8 #include "logging.h"
9
10 #define MAX_SIZE 13100
11 #define SIZE_STEP 131
12
13 int main(int argc, char *argv[])
14 {
15         unsigned int i, j;
16         struct tdb_context *tdb;
17         unsigned char *buffer;
18         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
19                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
20                         TDB_NOMMAP|TDB_CONVERT };
21         struct tdb_data key = { (unsigned char *)"key", 3 };
22         struct tdb_data data;
23
24         buffer = malloc(MAX_SIZE);
25         for (i = 0; i < MAX_SIZE; i++)
26                 buffer[i] = i;
27
28         plan_tests(sizeof(flags) / sizeof(flags[0])
29                    * ((2 + MAX_SIZE/SIZE_STEP * 4) * 2 + 6)
30                    + 1);
31
32         /* Using tdb_store. */
33         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
34                 tdb = tdb_open("run-append.tdb", flags[i],
35                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
36                 ok1(tdb);
37                 if (!tdb)
38                         continue;
39
40                 for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
41                         data.dptr = buffer;
42                         data.dsize = j;
43                         ok1(tdb_store(tdb, key, data, TDB_REPLACE) == 0);
44                         ok1(tdb_check(tdb, NULL, NULL) == 0);
45                         data = tdb_fetch(tdb, key);
46                         ok1(data.dsize == j);
47                         ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
48                         free(data.dptr);
49                 }
50                 ok1(!tdb_has_locks(tdb));
51                 tdb_close(tdb);
52         }
53
54         /* Using tdb_append. */
55         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
56                 size_t prev_len = 0;
57                 tdb = tdb_open("run-append.tdb", flags[i],
58                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
59                 ok1(tdb);
60                 if (!tdb)
61                         continue;
62
63                 for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
64                         data.dptr = buffer + prev_len;
65                         data.dsize = j - prev_len;
66                         ok1(tdb_append(tdb, key, data) == 0);
67                         ok1(tdb_check(tdb, NULL, NULL) == 0);
68                         data = tdb_fetch(tdb, key);
69                         ok1(data.dsize == j);
70                         ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
71                         free(data.dptr);
72                         prev_len = data.dsize;
73                 }
74                 ok1(!tdb_has_locks(tdb));
75                 tdb_close(tdb);
76         }
77
78         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
79                 tdb = tdb_open("run-append.tdb", flags[i],
80                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
81                 ok1(tdb);
82                 if (!tdb)
83                         continue;
84
85                 /* Huge initial store. */
86                 data.dptr = buffer;
87                 data.dsize = MAX_SIZE;
88                 ok1(tdb_append(tdb, key, data) == 0);
89                 ok1(tdb_check(tdb, NULL, NULL) == 0);
90                 data = tdb_fetch(tdb, key);
91                 ok1(data.dsize == MAX_SIZE);
92                 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
93                 free(data.dptr);
94                 ok1(!tdb_has_locks(tdb));
95                 tdb_close(tdb);
96         }
97
98         ok1(tap_log_messages == 0);
99         return exit_status();
100 }