]> git.ozlabs.org Git - ccan/blob - ccan/ntdb/test/api-20-alloc-attr.c
ntdb: don't overlap with test filenames.
[ccan] / ccan / ntdb / test / api-20-alloc-attr.c
1 #include "config.h"
2 #include "../ntdb.h"
3 #include "../private.h"
4 #include "tap-interface.h"
5 #include <ccan/hash/hash.h>
6 #include <assert.h>
7
8 #include "logging.h"
9 #include "helpapi-external-agent.h"
10
11 static const struct ntdb_context *curr_ntdb;
12 static const struct ntdb_file *curr_file;
13
14 static int owner_null_count,
15         owner_weird_count, alloc_count, free_count, expand_count;
16
17 static void *test_alloc(const void *owner, size_t len, void *priv_data)
18 {
19         void *ret;
20
21         if (!owner) {
22                 owner_null_count++;
23         } else if (owner != curr_ntdb && owner != curr_file) {
24                 owner_weird_count++;
25         }
26
27         alloc_count++;
28         ret = malloc(len);
29
30         /* The first time, this is the current ntdb, next is
31          * for the file struct. */
32         if (!owner) {
33                 if (!curr_ntdb) {
34                         curr_ntdb = ret;
35                 } else if (!curr_file) {
36                         curr_file = ret;
37                 }
38         }
39         assert(priv_data == &owner_weird_count);
40         return ret;
41 }
42
43 static void *test_expand(void *old, size_t newlen, void *priv_data)
44 {
45         expand_count++;
46
47         assert(priv_data == &owner_weird_count);
48         return realloc(old, newlen);
49 }
50
51 static void test_free(void *old, void *priv_data)
52 {
53         assert(priv_data == &owner_weird_count);
54         if (old) {
55                 free_count++;
56         }
57         free(old);
58 }
59
60 int main(int argc, char *argv[])
61 {
62         unsigned int i, j;
63         union ntdb_attribute alloc_attr;
64         struct ntdb_context *ntdb;
65         int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
66                         NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
67                         NTDB_NOMMAP|NTDB_CONVERT };
68         NTDB_DATA key = { (unsigned char *)&j, sizeof(j) };
69         NTDB_DATA data = { (unsigned char *)&j, sizeof(j) };
70
71         alloc_attr.base.next = &tap_log_attr;
72         alloc_attr.base.attr = NTDB_ATTRIBUTE_ALLOCATOR;
73
74         alloc_attr.alloc.alloc = test_alloc;
75         alloc_attr.alloc.expand = test_expand;
76         alloc_attr.alloc.free = test_free;
77         alloc_attr.alloc.priv_data = &owner_weird_count;
78
79         plan_tests(sizeof(flags) / sizeof(flags[0]) * (1 + 700 * 3 + 4) + 1);
80
81         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
82                 curr_ntdb = NULL;
83                 curr_file = NULL;
84                 ntdb = ntdb_open("run-20-alloc-attr.ntdb", flags[i]|MAYBE_NOSYNC,
85                                O_RDWR|O_CREAT|O_TRUNC, 0600, &alloc_attr);
86                 ok1(ntdb);
87                 if (!ntdb)
88                         continue;
89
90                 for (j = 0; j < 700; j++) {
91                         NTDB_DATA d = { NULL, 0 }; /* Bogus GCC warning */
92                         ok1(ntdb_store(ntdb, key, data, NTDB_REPLACE) == 0);
93                         ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
94                         ok1(ntdb_deq(d, data));
95                         test_free(d.dptr, &owner_weird_count);
96                 }
97                 ntdb_close(ntdb);
98
99                 ok1(owner_null_count == 2+i*2);
100                 ok1(owner_weird_count == 0);
101                 ok1(alloc_count == free_count);
102                 ok1(expand_count != 0);
103         }
104
105         ok1(tap_log_messages == 0);
106         return exit_status();
107 }