X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftalloc%2Ftest%2Frun-test_steal.c;fp=ccan%2Ftalloc%2Ftest%2Frun-test_steal.c;h=346f0917a45b8ab4dff0c2e70d9c5b2613f1f306;hp=0000000000000000000000000000000000000000;hb=87aba1fa2abc28ff06f82e30cde3fa3d991e8e18;hpb=f0002cb9e4f6f403a25ad50252c06694439900f0 diff --git a/ccan/talloc/test/run-test_steal.c b/ccan/talloc/test/run-test_steal.c new file mode 100644 index 00000000..346f0917 --- /dev/null +++ b/ccan/talloc/test/run-test_steal.c @@ -0,0 +1,141 @@ +/* + Unix SMB/CIFS implementation. + + local testing of talloc routines. + + Copyright (C) Andrew Tridgell 2004 + Converted to ccan tests by Rusty Russell 2008 + + ** NOTE! The following LGPL license applies to the talloc + ** library. This does NOT imply that all of Samba is released + ** under the LGPL + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include +#include +#include +#include +#include + +#define torture_assert(test, expr, str) \ + ok(expr, "%s [\n%s: Expression %s failed: %s\n]\n", \ + test, __location__, #expr, str) + +#define torture_assert_str_equal(test, arg1, arg2, desc) \ + ok(strcmp(arg1, arg2) == 0, \ + "%s [\n%s: Expected %s, got %s: %s\n]\n", \ + test, __location__, arg1, arg2, desc) + +#define CHECK_SIZE(test, ptr, tsize) \ + ok(talloc_total_size(ptr) == (tsize), \ + "%s [\nwrong '%s' tree size: got %u expected %u\n]\n", \ + test, #ptr, \ + (unsigned)talloc_total_size(ptr), \ + (unsigned)tsize) + +#define CHECK_BLOCKS(test, ptr, tblocks) \ + ok(talloc_total_blocks(ptr) == (tblocks), \ + "%s [\nwrong '%s' tree blocks: got %u expected %u\n]\n", \ + test, #ptr, \ + (unsigned)talloc_total_blocks(ptr), \ + (unsigned)tblocks) + +#define CHECK_PARENT(test, ptr, parent) \ + ok(talloc_parent(ptr) == (parent), \ + "%s [\n'%s' has wrong parent: got %p expected %p\n]\n", \ + test, #ptr, \ + talloc_parent(ptr), \ + (parent)) + +struct torture_context; + +/* + test steal +*/ +static bool test_steal(const struct torture_context *ctx) +{ + void *root, *p1, *p2; + bool ret = false; + + root = talloc_new(ctx); + if (!root) + goto out; + + p1 = talloc_array(root, char, 10); + if (!p1) + goto out; + CHECK_SIZE("steal", p1, 10); + + p2 = talloc_realloc(root, NULL, char, 20); + if (!p2) + goto out; + CHECK_SIZE("steal", p1, 10); + CHECK_SIZE("steal", root, 30); + + torture_assert("steal", talloc_steal(p1, NULL) == NULL, + "failed: stealing NULL should give NULL\n"); + + torture_assert("steal", talloc_steal(p1, p1) == p1, + "failed: stealing to ourselves is a nop\n"); + CHECK_BLOCKS("steal", root, 3); + CHECK_SIZE("steal", root, 30); + + talloc_steal(NULL, p1); + talloc_steal(NULL, p2); + CHECK_BLOCKS("steal", root, 1); + CHECK_SIZE("steal", root, 0); + + talloc_free(p1); + talloc_steal(root, p2); + CHECK_BLOCKS("steal", root, 2); + CHECK_SIZE("steal", root, 20); + + talloc_free(p2); + + CHECK_BLOCKS("steal", root, 1); + CHECK_SIZE("steal", root, 0); + + talloc_free(root); + root = NULL; + + p1 = talloc_size(NULL, 3); + if (!p1) + goto out; + CHECK_SIZE("steal", NULL, 3); + talloc_free(p1); + ret = true; + +out: + talloc_free(root); + return ret; +} + +int main(int argc, char *argv[]) +{ + plan_tests(16); + failtest_init(argc, argv); + + talloc_enable_null_tracking(); + if (null_context) { + ok1(test_steal(NULL)); + /* This closes the leak, but don't free any other leaks! */ + ok1(!talloc_chunk_from_ptr(null_context)->child); + talloc_disable_null_tracking(); + } + failtest_exit(exit_status()); +} +