2 Unix SMB/CIFS implementation.
4 local testing of talloc routines.
6 Copyright (C) Andrew Tridgell 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #define CHECK_BLOCKS(ptr, tblocks) do { \
26 if (talloc_total_blocks(ptr) != (tblocks)) { \
27 printf("(%d) failed: wrong '%s' tree size: got %u expected %u\n", \
29 (unsigned)talloc_total_blocks(ptr), \
31 talloc_report_full(ptr, stdout); \
39 static BOOL test_ref1(void)
41 void *root, *p1, *p2, *ref, *r1;
43 printf("TESTING SINGLE REFERENCE FREE\n");
45 root = talloc_named_const(NULL, 0, "root");
46 p1 = talloc_named_const(root, 1, "p1");
47 p2 = talloc_named_const(p1, 1, "p2");
48 talloc_named_const(p1, 1, "x1");
49 talloc_named_const(p1, 2, "x2");
50 talloc_named_const(p1, 3, "x3");
52 r1 = talloc_named_const(root, 1, "r1");
53 ref = talloc_reference(r1, p2);
54 talloc_report_full(root, stdout);
60 printf("Freeing p2\n");
62 talloc_report_full(root, stdout);
68 printf("Freeing p1\n");
70 talloc_report_full(root, stdout);
74 printf("Freeing r1\n");
76 talloc_report_full(NULL, stdout);
78 CHECK_BLOCKS(root, 1);
80 if (talloc_total_size(root) != 0) {
81 printf("failed: non-zero total size\n");
93 static BOOL test_ref2(void)
95 void *root, *p1, *p2, *ref, *r1;
97 printf("TESTING DOUBLE REFERENCE FREE\n");
99 root = talloc_named_const(NULL, 0, "root");
100 p1 = talloc_named_const(root, 1, "p1");
101 talloc_named_const(p1, 1, "x1");
102 talloc_named_const(p1, 1, "x2");
103 talloc_named_const(p1, 1, "x3");
104 p2 = talloc_named_const(p1, 1, "p2");
106 r1 = talloc_named_const(root, 1, "r1");
107 ref = talloc_reference(r1, p2);
108 talloc_report_full(root, stdout);
114 printf("Freeing ref\n");
116 talloc_report_full(root, stdout);
122 printf("Freeing p2\n");
124 talloc_report_full(root, stdout);
129 printf("Freeing p1\n");
131 talloc_report_full(root, stdout);
135 printf("Freeing r1\n");
137 talloc_report_full(root, stdout);
139 if (talloc_total_size(root) != 0) {
140 printf("failed: non-zero total size\n");
152 static BOOL test_ref3(void)
154 void *root, *p1, *p2, *ref, *r1;
156 printf("TESTING PARENT REFERENCE FREE\n");
158 root = talloc_named_const(NULL, 0, "root");
159 p1 = talloc_named_const(root, 1, "p1");
160 p2 = talloc_named_const(root, 1, "p2");
161 r1 = talloc_named_const(p1, 1, "r1");
162 ref = talloc_reference(p2, r1);
163 talloc_report_full(root, stdout);
169 printf("Freeing p1\n");
171 talloc_report_full(root, stdout);
176 printf("Freeing p2\n");
178 talloc_report_full(root, stdout);
180 if (talloc_total_size(root) != 0) {
181 printf("failed: non-zero total size\n");
193 static BOOL test_ref4(void)
195 void *root, *p1, *p2, *ref, *r1;
197 printf("TESTING REFERRER REFERENCE FREE\n");
199 root = talloc_named_const(NULL, 0, "root");
200 p1 = talloc_named_const(root, 1, "p1");
201 talloc_named_const(p1, 1, "x1");
202 talloc_named_const(p1, 1, "x2");
203 talloc_named_const(p1, 1, "x3");
204 p2 = talloc_named_const(p1, 1, "p2");
206 r1 = talloc_named_const(root, 1, "r1");
207 ref = talloc_reference(r1, p2);
208 talloc_report_full(root, stdout);
214 printf("Freeing r1\n");
216 talloc_report_full(root, stdout);
221 printf("Freeing p2\n");
223 talloc_report_full(root, stdout);
227 printf("Freeing p1\n");
229 talloc_report_full(root, stdout);
231 if (talloc_total_size(root) != 0) {
232 printf("failed: non-zero total size\n");
245 static BOOL test_unref1(void)
247 void *root, *p1, *p2, *ref, *r1;
249 printf("TESTING UNREFERENCE\n");
251 root = talloc_named_const(NULL, 0, "root");
252 p1 = talloc_named_const(root, 1, "p1");
253 talloc_named_const(p1, 1, "x1");
254 talloc_named_const(p1, 1, "x2");
255 talloc_named_const(p1, 1, "x3");
256 p2 = talloc_named_const(p1, 1, "p2");
258 r1 = talloc_named_const(p1, 1, "r1");
259 ref = talloc_reference(r1, p2);
260 talloc_report_full(root, stdout);
266 printf("Unreferencing r1\n");
267 talloc_unreference(r1, p2);
268 talloc_report_full(root, stdout);
274 printf("Freeing p1\n");
276 talloc_report_full(root, stdout);
278 if (talloc_total_size(root) != 0) {
279 printf("failed: non-zero total size\n");
289 measure the speed of talloc versus malloc
291 static BOOL test_speed(void)
293 void *ctx = talloc(NULL, 0);
296 printf("MEASURING TALLOC VS MALLOC SPEED\n");
302 p1 = talloc(ctx, count);
303 p2 = talloc_strdup(p1, "foo bar");
304 p3 = talloc(p1, 300);
307 } while (end_timer() < 5.0);
309 printf("talloc: %.0f ops/sec\n", count/end_timer());
318 p2 = strdup("foo bar");
324 } while (end_timer() < 5.0);
326 printf("malloc: %.0f ops/sec\n", count/end_timer());
332 BOOL torture_local_talloc(int dummy)
342 ret &= test_unref1();