]> git.ozlabs.org Git - ccan/blob - ccan/talloc/test/run-test_talloc_ptrtype.c
tdb2: make internal coalesce() function return length coalesced.
[ccan] / ccan / talloc / test / run-test_talloc_ptrtype.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    local testing of talloc routines.
5
6    Copyright (C) Andrew Tridgell 2004
7    Converted to ccan tests by Rusty Russell 2008
8    
9      ** NOTE! The following LGPL license applies to the talloc
10      ** library. This does NOT imply that all of Samba is released
11      ** under the LGPL
12    
13    This library is free software; you can redistribute it and/or
14    modify it under the terms of the GNU Lesser General Public
15    License as published by the Free Software Foundation; either
16    version 2 of the License, or (at your option) any later version.
17
18    This library is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21    Lesser General Public License for more details.
22
23    You should have received a copy of the GNU Lesser General Public
24    License along with this library; if not, write to the Free Software
25    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 */
27
28 #include <ccan/failtest/failtest_override.h>
29 #include <ccan/talloc/talloc.c>
30 #include <stdbool.h>
31 #include <ccan/tap/tap.h>
32 #include <ccan/failtest/failtest.h>
33
34 #define torture_assert(test, expr, str)                                 \
35         ok(expr, "%s [\n%s: Expression %s failed: %s\n]\n",             \
36            test, __location__, #expr, str)
37
38 #define torture_assert_str_equal(test, arg1, arg2, desc)        \
39         ok(strcmp(arg1, arg2) == 0,                             \
40            "%s [\n%s: Expected %s, got %s: %s\n]\n",            \
41            test, __location__, arg1, arg2, desc)
42
43 #define CHECK_SIZE(test, ptr, tsize)                                    \
44         ok(talloc_total_size(ptr) == (tsize),                           \
45            "%s [\nwrong '%s' tree size: got %u  expected %u\n]\n",      \
46            test, #ptr,                                                  \
47            (unsigned)talloc_total_size(ptr),                            \
48            (unsigned)tsize)
49
50 #define CHECK_BLOCKS(test, ptr, tblocks)                                \
51         ok(talloc_total_blocks(ptr) == (tblocks),                       \
52            "%s [\nwrong '%s' tree blocks: got %u  expected %u\n]\n",    \
53            test, #ptr,                                                  \
54            (unsigned)talloc_total_blocks(ptr),                          \
55            (unsigned)tblocks)
56
57 #define CHECK_PARENT(test, ptr, parent)                                 \
58         ok(talloc_parent(ptr) == (parent),                              \
59            "%s [\n'%s' has wrong parent: got %p  expected %p\n]\n",     \
60            test, #ptr,                                                  \
61            talloc_parent(ptr),                                          \
62            (parent))
63
64 struct torture_context;
65
66 static bool test_talloc_ptrtype(const struct torture_context *ctx)
67 {
68         void *top = talloc_new(ctx);
69         struct struct1 {
70                 int foo;
71                 int bar;
72         } *s1, *s2, **s3, ***s4;
73         const char *location1;
74         const char *location2;
75         const char *location3;
76         const char *location4;
77         bool ret = false;
78
79         if (!top)
80                 goto out;
81
82         s1 = talloc_ptrtype(top, s1);location1 = __location__;
83         if (!s1)
84                 goto out;
85
86         ok1(talloc_get_size(s1) == sizeof(struct struct1));
87
88         ok1(strcmp(location1, talloc_get_name(s1)) == 0);
89
90         s2 = talloc_array_ptrtype(top, s2, 10);location2 = __location__;
91         if (!s2)
92                 goto out;
93
94         ok1(talloc_get_size(s2) == (sizeof(struct struct1) * 10));
95
96         ok1(strcmp(location2, talloc_get_name(s2)) == 0);
97
98         s3 = talloc_array_ptrtype(top, s3, 10);location3 = __location__;
99         if (!s3)
100                 goto out;
101
102         ok1(talloc_get_size(s3) == (sizeof(struct struct1 *) * 10));
103
104         torture_assert_str_equal("ptrtype", location3, talloc_get_name(s3),
105                 "talloc_array_ptrtype() sets the wrong name");
106
107         s4 = talloc_array_ptrtype(top, s4, 10);location4 = __location__;
108         if (!s4)
109                 goto out;
110
111         ok1(talloc_get_size(s4) == (sizeof(struct struct1 **) * 10));
112
113         torture_assert_str_equal("ptrtype", location4, talloc_get_name(s4),
114                 "talloc_array_ptrtype() sets the wrong name");
115         ret = true;
116
117 out:
118         talloc_free(top);
119
120         return ret;
121 }
122
123 int main(int argc, char *argv[])
124 {
125         plan_tests(10);
126         failtest_init(argc, argv);
127
128         talloc_enable_null_tracking();
129         if (null_context) {
130                 ok1(test_talloc_ptrtype(NULL));
131                 /* This closes the leak, but don't free any other leaks! */
132                 ok1(!talloc_chunk_from_ptr(null_context)->child);
133                 talloc_disable_null_tracking();
134         }
135         failtest_exit(exit_status());
136 }
137