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