]> git.ozlabs.org Git - ccan/blob - ccan/talloc/test/run-test_steal.c
talloc: spelling fix.
[ccan] / ccan / talloc / test / run-test_steal.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 /*
67   test steal
68 */
69 static bool test_steal(const struct torture_context *ctx)
70 {
71         void *root, *p1, *p2;
72         bool ret = false;
73
74         root = talloc_new(ctx);
75         if (!root)
76                 goto out;
77
78         p1 = talloc_array(root, char, 10);
79         if (!p1)
80                 goto out;
81         CHECK_SIZE("steal", p1, 10);
82
83         p2 = talloc_realloc(root, NULL, char, 20);
84         if (!p2)
85                 goto out;
86         CHECK_SIZE("steal", p1, 10);
87         CHECK_SIZE("steal", root, 30);
88
89         torture_assert("steal", talloc_steal(p1, NULL) == NULL,
90                 "failed: stealing NULL should give NULL\n");
91
92         torture_assert("steal", talloc_steal(p1, p1) == p1,
93                 "failed: stealing to ourselves is a nop\n");
94         CHECK_BLOCKS("steal", root, 3);
95         CHECK_SIZE("steal", root, 30);
96
97         talloc_steal(NULL, p1);
98         talloc_steal(NULL, p2);
99         CHECK_BLOCKS("steal", root, 1);
100         CHECK_SIZE("steal", root, 0);
101
102         talloc_free(p1);
103         talloc_steal(root, p2);
104         CHECK_BLOCKS("steal", root, 2);
105         CHECK_SIZE("steal", root, 20);
106         
107         talloc_free(p2);
108
109         CHECK_BLOCKS("steal", root, 1);
110         CHECK_SIZE("steal", root, 0);
111
112         talloc_free(root);
113         root = NULL;
114
115         p1 = talloc_size(NULL, 3);
116         if (!p1)
117                 goto out;
118         CHECK_SIZE("steal", NULL, 3);
119         talloc_free(p1);
120         ret = true;
121
122 out:
123         talloc_free(root);
124         return ret;
125 }
126
127 int main(int argc, char *argv[])
128 {
129         plan_tests(16);
130         failtest_init(argc, argv);
131
132         talloc_enable_null_tracking();
133         if (null_context) {
134                 ok1(test_steal(NULL));
135                 /* This closes the leak, but don't free any other leaks! */
136                 ok1(!talloc_chunk_from_ptr(null_context)->child);
137                 talloc_disable_null_tracking();
138         }
139         failtest_exit(exit_status());
140 }
141