lib/talloc/testsuite.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003 
00004    local testing of talloc routines.
00005 
00006    Copyright (C) Andrew Tridgell 2004
00007    
00008      ** NOTE! The following LGPL license applies to the talloc
00009      ** library. This does NOT imply that all of Samba is released
00010      ** under the LGPL
00011    
00012    This library is free software; you can redistribute it and/or
00013    modify it under the terms of the GNU Lesser General Public
00014    License as published by the Free Software Foundation; either
00015    version 2 of the License, or (at your option) any later version.
00016 
00017    This library is distributed in the hope that it will be useful,
00018    but WITHOUT ANY WARRANTY; without even the implied warranty of
00019    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020    Lesser General Public License for more details.
00021 
00022    You should have received a copy of the GNU Lesser General Public
00023    License along with this library; if not, write to the Free Software
00024    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 */
00026 
00027 #include "replace.h"
00028 #include "system/time.h"
00029 #include "talloc.h"
00030 
00031 static struct timeval timeval_current(void)
00032 {
00033         struct timeval tv;
00034         gettimeofday(&tv, NULL);
00035         return tv;
00036 }
00037 
00038 static double timeval_elapsed(struct timeval *tv)
00039 {
00040         struct timeval tv2 = timeval_current();
00041         return (tv2.tv_sec - tv->tv_sec) + 
00042                (tv2.tv_usec - tv->tv_usec)*1.0e-6;
00043 }
00044 
00045 #define torture_assert(test, expr, str) if (!(expr)) { \
00046         printf("failure: %s [\n%s: Expression %s failed: %s\n]\n", \
00047                 test, __location__, #expr, str); \
00048         return false; \
00049 }
00050 
00051 #define torture_assert_str_equal(test, arg1, arg2, desc) \
00052         if (strcmp(arg1, arg2)) { \
00053                 printf("failure: %s [\n%s: Expected %s, got %s: %s\n]\n", \
00054                    test, __location__, arg1, arg2, desc); \
00055                 return false; \
00056         }
00057 
00058 #if _SAMBA_BUILD_==3
00059 #ifdef malloc
00060 #undef malloc
00061 #endif
00062 #ifdef strdup
00063 #undef strdup
00064 #endif
00065 #endif
00066 
00067 #define CHECK_SIZE(test, ptr, tsize) do { \
00068         if (talloc_total_size(ptr) != (tsize)) { \
00069                 printf("failed: %s [\nwrong '%s' tree size: got %u  expected %u\n]\n", \
00070                        test, #ptr, \
00071                        (unsigned)talloc_total_size(ptr), \
00072                        (unsigned)tsize); \
00073                 talloc_report_full(ptr, stdout); \
00074                 return false; \
00075         } \
00076 } while (0)
00077 
00078 #define CHECK_BLOCKS(test, ptr, tblocks) do { \
00079         if (talloc_total_blocks(ptr) != (tblocks)) { \
00080                 printf("failed: %s [\nwrong '%s' tree blocks: got %u  expected %u\n]\n", \
00081                        test, #ptr, \
00082                        (unsigned)talloc_total_blocks(ptr), \
00083                        (unsigned)tblocks); \
00084                 talloc_report_full(ptr, stdout); \
00085                 return false; \
00086         } \
00087 } while (0)
00088 
00089 #define CHECK_PARENT(test, ptr, parent) do { \
00090         if (talloc_parent(ptr) != (parent)) { \
00091                 printf("failed: %s [\n'%s' has wrong parent: got %p  expected %p\n]\n", \
00092                        test, #ptr, \
00093                        talloc_parent(ptr), \
00094                        (parent)); \
00095                 talloc_report_full(ptr, stdout); \
00096                 talloc_report_full(parent, stdout); \
00097                 talloc_report_full(NULL, stdout); \
00098                 return false; \
00099         } \
00100 } while (0)
00101 
00102 
00103 /*
00104   test references 
00105 */
00106 static bool test_ref1(void)
00107 {
00108         void *root, *p1, *p2, *ref, *r1;
00109 
00110         printf("test: ref1 [\nSINGLE REFERENCE FREE\n]\n");
00111 
00112         root = talloc_named_const(NULL, 0, "root");
00113         p1 = talloc_named_const(root, 1, "p1");
00114         p2 = talloc_named_const(p1, 1, "p2");
00115         talloc_named_const(p1, 1, "x1");
00116         talloc_named_const(p1, 2, "x2");
00117         talloc_named_const(p1, 3, "x3");
00118 
00119         r1 = talloc_named_const(root, 1, "r1"); 
00120         ref = talloc_reference(r1, p2);
00121         talloc_report_full(root, stderr);
00122 
00123         CHECK_BLOCKS("ref1", p1, 5);
00124         CHECK_BLOCKS("ref1", p2, 1);
00125         CHECK_BLOCKS("ref1", r1, 2);
00126 
00127         fprintf(stderr, "Freeing p2\n");
00128         talloc_free(p2);
00129         talloc_report_full(root, stderr);
00130 
00131         CHECK_BLOCKS("ref1", p1, 5);
00132         CHECK_BLOCKS("ref1", p2, 1);
00133         CHECK_BLOCKS("ref1", r1, 1);
00134 
00135         fprintf(stderr, "Freeing p1\n");
00136         talloc_free(p1);
00137         talloc_report_full(root, stderr);
00138 
00139         CHECK_BLOCKS("ref1", r1, 1);
00140 
00141         fprintf(stderr, "Freeing r1\n");
00142         talloc_free(r1);
00143         talloc_report_full(NULL, stderr);
00144 
00145         fprintf(stderr, "Testing NULL\n");
00146         if (talloc_reference(root, NULL)) {
00147                 return false;
00148         }
00149 
00150         CHECK_BLOCKS("ref1", root, 1);
00151 
00152         CHECK_SIZE("ref1", root, 0);
00153 
00154         talloc_free(root);
00155         printf("success: ref1\n");
00156         return true;
00157 }
00158 
00159 /*
00160   test references 
00161 */
00162 static bool test_ref2(void)
00163 {
00164         void *root, *p1, *p2, *ref, *r1;
00165 
00166         printf("test: ref2 [\nDOUBLE REFERENCE FREE\n]\n");
00167         root = talloc_named_const(NULL, 0, "root");
00168         p1 = talloc_named_const(root, 1, "p1");
00169         talloc_named_const(p1, 1, "x1");
00170         talloc_named_const(p1, 1, "x2");
00171         talloc_named_const(p1, 1, "x3");
00172         p2 = talloc_named_const(p1, 1, "p2");
00173 
00174         r1 = talloc_named_const(root, 1, "r1"); 
00175         ref = talloc_reference(r1, p2);
00176         talloc_report_full(root, stderr);
00177 
00178         CHECK_BLOCKS("ref2", p1, 5);
00179         CHECK_BLOCKS("ref2", p2, 1);
00180         CHECK_BLOCKS("ref2", r1, 2);
00181 
00182         fprintf(stderr, "Freeing ref\n");
00183         talloc_free(ref);
00184         talloc_report_full(root, stderr);
00185 
00186         CHECK_BLOCKS("ref2", p1, 5);
00187         CHECK_BLOCKS("ref2", p2, 1);
00188         CHECK_BLOCKS("ref2", r1, 1);
00189 
00190         fprintf(stderr, "Freeing p2\n");
00191         talloc_free(p2);
00192         talloc_report_full(root, stderr);
00193 
00194         CHECK_BLOCKS("ref2", p1, 4);
00195         CHECK_BLOCKS("ref2", r1, 1);
00196 
00197         fprintf(stderr, "Freeing p1\n");
00198         talloc_free(p1);
00199         talloc_report_full(root, stderr);
00200 
00201         CHECK_BLOCKS("ref2", r1, 1);
00202 
00203         fprintf(stderr, "Freeing r1\n");
00204         talloc_free(r1);
00205         talloc_report_full(root, stderr);
00206 
00207         CHECK_SIZE("ref2", root, 0);
00208 
00209         talloc_free(root);
00210         printf("success: ref2\n");
00211         return true;
00212 }
00213 
00214 /*
00215   test references 
00216 */
00217 static bool test_ref3(void)
00218 {
00219         void *root, *p1, *p2, *ref, *r1;
00220 
00221         printf("test: ref3 [\nPARENT REFERENCE FREE\n]\n");
00222 
00223         root = talloc_named_const(NULL, 0, "root");
00224         p1 = talloc_named_const(root, 1, "p1");
00225         p2 = talloc_named_const(root, 1, "p2");
00226         r1 = talloc_named_const(p1, 1, "r1");
00227         ref = talloc_reference(p2, r1);
00228         talloc_report_full(root, stderr);
00229 
00230         CHECK_BLOCKS("ref3", p1, 2);
00231         CHECK_BLOCKS("ref3", p2, 2);
00232         CHECK_BLOCKS("ref3", r1, 1);
00233 
00234         fprintf(stderr, "Freeing p1\n");
00235         talloc_free(p1);
00236         talloc_report_full(root, stderr);
00237 
00238         CHECK_BLOCKS("ref3", p2, 2);
00239         CHECK_BLOCKS("ref3", r1, 1);
00240 
00241         fprintf(stderr, "Freeing p2\n");
00242         talloc_free(p2);
00243         talloc_report_full(root, stderr);
00244 
00245         CHECK_SIZE("ref3", root, 0);
00246 
00247         talloc_free(root);
00248 
00249         printf("success: ref3\n");
00250         return true;
00251 }
00252 
00253 /*
00254   test references 
00255 */
00256 static bool test_ref4(void)
00257 {
00258         void *root, *p1, *p2, *ref, *r1;
00259 
00260         printf("test: ref4 [\nREFERRER REFERENCE FREE\n]\n");
00261 
00262         root = talloc_named_const(NULL, 0, "root");
00263         p1 = talloc_named_const(root, 1, "p1");
00264         talloc_named_const(p1, 1, "x1");
00265         talloc_named_const(p1, 1, "x2");
00266         talloc_named_const(p1, 1, "x3");
00267         p2 = talloc_named_const(p1, 1, "p2");
00268 
00269         r1 = talloc_named_const(root, 1, "r1"); 
00270         ref = talloc_reference(r1, p2);
00271         talloc_report_full(root, stderr);
00272 
00273         CHECK_BLOCKS("ref4", p1, 5);
00274         CHECK_BLOCKS("ref4", p2, 1);
00275         CHECK_BLOCKS("ref4", r1, 2);
00276 
00277         fprintf(stderr, "Freeing r1\n");
00278         talloc_free(r1);
00279         talloc_report_full(root, stderr);
00280 
00281         CHECK_BLOCKS("ref4", p1, 5);
00282         CHECK_BLOCKS("ref4", p2, 1);
00283 
00284         fprintf(stderr, "Freeing p2\n");
00285         talloc_free(p2);
00286         talloc_report_full(root, stderr);
00287 
00288         CHECK_BLOCKS("ref4", p1, 4);
00289 
00290         fprintf(stderr, "Freeing p1\n");
00291         talloc_free(p1);
00292         talloc_report_full(root, stderr);
00293 
00294         CHECK_SIZE("ref4", root, 0);
00295 
00296         talloc_free(root);
00297 
00298         printf("success: ref4\n");
00299         return true;
00300 }
00301 
00302 
00303 /*
00304   test references 
00305 */
00306 static bool test_unlink1(void)
00307 {
00308         void *root, *p1, *p2, *ref, *r1;
00309 
00310         printf("test: unlink [\nUNLINK\n]\n");
00311 
00312         root = talloc_named_const(NULL, 0, "root");
00313         p1 = talloc_named_const(root, 1, "p1");
00314         talloc_named_const(p1, 1, "x1");
00315         talloc_named_const(p1, 1, "x2");
00316         talloc_named_const(p1, 1, "x3");
00317         p2 = talloc_named_const(p1, 1, "p2");
00318 
00319         r1 = talloc_named_const(p1, 1, "r1");   
00320         ref = talloc_reference(r1, p2);
00321         talloc_report_full(root, stderr);
00322 
00323         CHECK_BLOCKS("unlink", p1, 7);
00324         CHECK_BLOCKS("unlink", p2, 1);
00325         CHECK_BLOCKS("unlink", r1, 2);
00326 
00327         fprintf(stderr, "Unreferencing r1\n");
00328         talloc_unlink(r1, p2);
00329         talloc_report_full(root, stderr);
00330 
00331         CHECK_BLOCKS("unlink", p1, 6);
00332         CHECK_BLOCKS("unlink", p2, 1);
00333         CHECK_BLOCKS("unlink", r1, 1);
00334 
00335         fprintf(stderr, "Freeing p1\n");
00336         talloc_free(p1);
00337         talloc_report_full(root, stderr);
00338 
00339         CHECK_SIZE("unlink", root, 0);
00340 
00341         talloc_free(root);
00342 
00343         printf("success: unlink\n");
00344         return true;
00345 }
00346 
00347 static int fail_destructor(void *ptr)
00348 {
00349         return -1;
00350 }
00351 
00352 /*
00353   miscellaneous tests to try to get a higher test coverage percentage
00354 */
00355 static bool test_misc(void)
00356 {
00357         void *root, *p1;
00358         char *p2;
00359         double *d;
00360         const char *name;
00361 
00362         printf("test: misc [\nMISCELLANEOUS\n]\n");
00363 
00364         root = talloc_new(NULL);
00365 
00366         p1 = talloc_size(root, 0x7fffffff);
00367         torture_assert("misc", !p1, "failed: large talloc allowed\n");
00368 
00369         p1 = talloc_strdup(root, "foo");
00370         talloc_increase_ref_count(p1);
00371         talloc_increase_ref_count(p1);
00372         talloc_increase_ref_count(p1);
00373         CHECK_BLOCKS("misc", p1, 1);
00374         CHECK_BLOCKS("misc", root, 2);
00375         talloc_free(p1);
00376         CHECK_BLOCKS("misc", p1, 1);
00377         CHECK_BLOCKS("misc", root, 2);
00378         talloc_unlink(NULL, p1);
00379         CHECK_BLOCKS("misc", p1, 1);
00380         CHECK_BLOCKS("misc", root, 2);
00381         p2 = talloc_strdup(p1, "foo");
00382         torture_assert("misc", talloc_unlink(root, p2) == -1,
00383                                    "failed: talloc_unlink() of non-reference context should return -1\n");
00384         torture_assert("misc", talloc_unlink(p1, p2) == 0,
00385                 "failed: talloc_unlink() of parent should succeed\n");
00386         talloc_free(p1);
00387         CHECK_BLOCKS("misc", p1, 1);
00388         CHECK_BLOCKS("misc", root, 2);
00389 
00390         name = talloc_set_name(p1, "my name is %s", "foo");
00391         torture_assert_str_equal("misc", talloc_get_name(p1), "my name is foo",
00392                 "failed: wrong name after talloc_set_name(my name is foo)");
00393         CHECK_BLOCKS("misc", p1, 2);
00394         CHECK_BLOCKS("misc", root, 3);
00395 
00396         talloc_set_name_const(p1, NULL);
00397         torture_assert_str_equal ("misc", talloc_get_name(p1), "UNNAMED",
00398                 "failed: wrong name after talloc_set_name(NULL)");
00399         CHECK_BLOCKS("misc", p1, 2);
00400         CHECK_BLOCKS("misc", root, 3);
00401 
00402         torture_assert("misc", talloc_free(NULL) == -1, 
00403                                    "talloc_free(NULL) should give -1\n");
00404 
00405         talloc_set_destructor(p1, fail_destructor);
00406         torture_assert("misc", talloc_free(p1) == -1, 
00407                 "Failed destructor should cause talloc_free to fail\n");
00408         talloc_set_destructor(p1, NULL);
00409 
00410         talloc_report(root, stderr);
00411 
00412 
00413         p2 = (char *)talloc_zero_size(p1, 20);
00414         torture_assert("misc", p2[19] == 0, "Failed to give zero memory\n");
00415         talloc_free(p2);
00416 
00417         torture_assert("misc", talloc_strdup(root, NULL) == NULL,
00418                 "failed: strdup on NULL should give NULL\n");
00419 
00420         p2 = talloc_strndup(p1, "foo", 2);
00421         torture_assert("misc", strcmp("fo", p2) == 0, 
00422                                    "strndup doesn't work\n");
00423         p2 = talloc_asprintf_append(p2, "o%c", 'd');
00424         torture_assert("misc", strcmp("food", p2) == 0, 
00425                                    "talloc_asprintf_append doesn't work\n");
00426         CHECK_BLOCKS("misc", p2, 1);
00427         CHECK_BLOCKS("misc", p1, 3);
00428 
00429         p2 = talloc_asprintf_append(NULL, "hello %s", "world");
00430         torture_assert("misc", strcmp("hello world", p2) == 0,
00431                 "talloc_asprintf_append doesn't work\n");
00432         CHECK_BLOCKS("misc", p2, 1);
00433         CHECK_BLOCKS("misc", p1, 3);
00434         talloc_free(p2);
00435 
00436         d = talloc_array(p1, double, 0x20000000);
00437         torture_assert("misc", !d, "failed: integer overflow not detected\n");
00438 
00439         d = talloc_realloc(p1, d, double, 0x20000000);
00440         torture_assert("misc", !d, "failed: integer overflow not detected\n");
00441 
00442         talloc_free(p1);
00443         CHECK_BLOCKS("misc", root, 1);
00444 
00445         p1 = talloc_named(root, 100, "%d bytes", 100);
00446         CHECK_BLOCKS("misc", p1, 2);
00447         CHECK_BLOCKS("misc", root, 3);
00448         talloc_unlink(root, p1);
00449 
00450         p1 = talloc_init("%d bytes", 200);
00451         p2 = talloc_asprintf(p1, "my test '%s'", "string");
00452         torture_assert_str_equal("misc", p2, "my test 'string'",
00453                 "failed: talloc_asprintf(\"my test '%%s'\", \"string\") gave: \"%s\"");
00454         CHECK_BLOCKS("misc", p1, 3);
00455         CHECK_SIZE("misc", p2, 17);
00456         CHECK_BLOCKS("misc", root, 1);
00457         talloc_unlink(NULL, p1);
00458 
00459         p1 = talloc_named_const(root, 10, "p1");
00460         p2 = (char *)talloc_named_const(root, 20, "p2");
00461         (void)talloc_reference(p1, p2);
00462         talloc_report_full(root, stderr);
00463         talloc_unlink(root, p2);
00464         talloc_report_full(root, stderr);
00465         CHECK_BLOCKS("misc", p2, 1);
00466         CHECK_BLOCKS("misc", p1, 2);
00467         CHECK_BLOCKS("misc", root, 3);
00468         talloc_unlink(p1, p2);
00469         talloc_unlink(root, p1);
00470 
00471         p1 = talloc_named_const(root, 10, "p1");
00472         p2 = (char *)talloc_named_const(root, 20, "p2");
00473         (void)talloc_reference(NULL, p2);
00474         talloc_report_full(root, stderr);
00475         talloc_unlink(root, p2);
00476         talloc_report_full(root, stderr);
00477         CHECK_BLOCKS("misc", p2, 1);
00478         CHECK_BLOCKS("misc", p1, 1);
00479         CHECK_BLOCKS("misc", root, 2);
00480         talloc_unlink(NULL, p2);
00481         talloc_unlink(root, p1);
00482 
00483         /* Test that talloc_unlink is a no-op */
00484 
00485         torture_assert("misc", talloc_unlink(root, NULL) == -1,
00486                 "failed: talloc_unlink(root, NULL) == -1\n");
00487 
00488         talloc_report(root, stderr);
00489         talloc_report(NULL, stderr);
00490 
00491         CHECK_SIZE("misc", root, 0);
00492 
00493         talloc_free(root);
00494 
00495         CHECK_SIZE("misc", NULL, 0);
00496 
00497         talloc_enable_leak_report();
00498         talloc_enable_leak_report_full();
00499 
00500         printf("success: misc\n");
00501 
00502         return true;
00503 }
00504 
00505 
00506 /*
00507   test realloc
00508 */
00509 static bool test_realloc(void)
00510 {
00511         void *root, *p1, *p2;
00512 
00513         printf("test: realloc [\nREALLOC\n]\n");
00514 
00515         root = talloc_new(NULL);
00516 
00517         p1 = talloc_size(root, 10);
00518         CHECK_SIZE("realloc", p1, 10);
00519 
00520         p1 = talloc_realloc_size(NULL, p1, 20);
00521         CHECK_SIZE("realloc", p1, 20);
00522 
00523         talloc_new(p1);
00524 
00525         p2 = talloc_realloc_size(p1, NULL, 30);
00526 
00527         talloc_new(p1);
00528 
00529         p2 = talloc_realloc_size(p1, p2, 40);
00530 
00531         CHECK_SIZE("realloc", p2, 40);
00532         CHECK_SIZE("realloc", root, 60);
00533         CHECK_BLOCKS("realloc", p1, 4);
00534 
00535         p1 = talloc_realloc_size(NULL, p1, 20);
00536         CHECK_SIZE("realloc", p1, 60);
00537 
00538         talloc_increase_ref_count(p2);
00539         torture_assert("realloc", talloc_realloc_size(NULL, p2, 5) == NULL,
00540                 "failed: talloc_realloc() on a referenced pointer should fail\n");
00541         CHECK_BLOCKS("realloc", p1, 4);
00542 
00543         talloc_realloc_size(NULL, p2, 0);
00544         talloc_realloc_size(NULL, p2, 0);
00545         CHECK_BLOCKS("realloc", p1, 3);
00546 
00547         torture_assert("realloc", talloc_realloc_size(NULL, p1, 0x7fffffff) == NULL,
00548                 "failed: oversize talloc should fail\n");
00549 
00550         talloc_realloc_size(NULL, p1, 0);
00551 
00552         CHECK_BLOCKS("realloc", root, 1);
00553         CHECK_SIZE("realloc", root, 0);
00554 
00555         talloc_free(root);
00556 
00557         printf("success: REALLOC\n");
00558 
00559         return true;
00560 }
00561 
00562 /*
00563   test realloc with a child
00564 */
00565 static bool test_realloc_child(void)
00566 {
00567         void *root;
00568         struct el2 {
00569                 const char *name;
00570         } *el2; 
00571         struct el1 {
00572                 int count;
00573                 struct el2 **list, **list2, **list3;
00574         } *el1;
00575 
00576         printf("test: REALLOC WITH CHILD\n");
00577 
00578         root = talloc_new(NULL);
00579 
00580         el1 = talloc(root, struct el1);
00581         el1->list = talloc(el1, struct el2 *);
00582         el1->list[0] = talloc(el1->list, struct el2);
00583         el1->list[0]->name = talloc_strdup(el1->list[0], "testing");
00584 
00585         el1->list2 = talloc(el1, struct el2 *);
00586         el1->list2[0] = talloc(el1->list2, struct el2);
00587         el1->list2[0]->name = talloc_strdup(el1->list2[0], "testing2");
00588 
00589         el1->list3 = talloc(el1, struct el2 *);
00590         el1->list3[0] = talloc(el1->list3, struct el2);
00591         el1->list3[0]->name = talloc_strdup(el1->list3[0], "testing2");
00592         
00593         el2 = talloc(el1->list, struct el2);
00594         el2 = talloc(el1->list2, struct el2);
00595         el2 = talloc(el1->list3, struct el2);
00596 
00597         el1->list = talloc_realloc(el1, el1->list, struct el2 *, 100);
00598         el1->list2 = talloc_realloc(el1, el1->list2, struct el2 *, 200);
00599         el1->list3 = talloc_realloc(el1, el1->list3, struct el2 *, 300);
00600 
00601         talloc_free(root);
00602 
00603         printf("success: REALLOC WITH CHILD\n");
00604         return true;
00605 }
00606 
00607 /*
00608   test type checking
00609 */
00610 static bool test_type(void)
00611 {
00612         void *root;
00613         struct el1 {
00614                 int count;
00615         };
00616         struct el2 {
00617                 int count;
00618         };
00619         struct el1 *el1;
00620 
00621         printf("test: type [\ntalloc type checking\n]\n");
00622 
00623         root = talloc_new(NULL);
00624 
00625         el1 = talloc(root, struct el1);
00626 
00627         el1->count = 1;
00628 
00629         torture_assert("type", talloc_get_type(el1, struct el1) == el1,
00630                 "type check failed on el1\n");
00631         torture_assert("type", talloc_get_type(el1, struct el2) == NULL,
00632                 "type check failed on el1 with el2\n");
00633         talloc_set_type(el1, struct el2);
00634         torture_assert("type", talloc_get_type(el1, struct el2) == (struct el2 *)el1,
00635                 "type set failed on el1 with el2\n");
00636 
00637         talloc_free(root);
00638 
00639         printf("success: type\n");
00640         return true;
00641 }
00642 
00643 /*
00644   test steal
00645 */
00646 static bool test_steal(void)
00647 {
00648         void *root, *p1, *p2;
00649 
00650         printf("test: steal [\nSTEAL\n]\n");
00651 
00652         root = talloc_new(NULL);
00653 
00654         p1 = talloc_array(root, char, 10);
00655         CHECK_SIZE("steal", p1, 10);
00656 
00657         p2 = talloc_realloc(root, NULL, char, 20);
00658         CHECK_SIZE("steal", p1, 10);
00659         CHECK_SIZE("steal", root, 30);
00660 
00661         torture_assert("steal", talloc_steal(p1, NULL) == NULL,
00662                 "failed: stealing NULL should give NULL\n");
00663 
00664         torture_assert("steal", talloc_steal(p1, p1) == p1,
00665                 "failed: stealing to ourselves is a nop\n");
00666         CHECK_BLOCKS("steal", root, 3);
00667         CHECK_SIZE("steal", root, 30);
00668 
00669         talloc_steal(NULL, p1);
00670         talloc_steal(NULL, p2);
00671         CHECK_BLOCKS("steal", root, 1);
00672         CHECK_SIZE("steal", root, 0);
00673 
00674         talloc_free(p1);
00675         talloc_steal(root, p2);
00676         CHECK_BLOCKS("steal", root, 2);
00677         CHECK_SIZE("steal", root, 20);
00678         
00679         talloc_free(p2);
00680 
00681         CHECK_BLOCKS("steal", root, 1);
00682         CHECK_SIZE("steal", root, 0);
00683 
00684         talloc_free(root);
00685 
00686         p1 = talloc_size(NULL, 3);
00687         talloc_report_full(NULL, stderr);
00688         CHECK_SIZE("steal", NULL, 3);
00689         talloc_free(p1);
00690 
00691         printf("success: steal\n");
00692         return true;
00693 }
00694 
00695 /*
00696   test move
00697 */
00698 static bool test_move(void)
00699 {
00700         void *root;
00701         struct t_move {
00702                 char *p;
00703                 int *x;
00704         } *t1, *t2;
00705 
00706         printf("test: move [\nMOVE\n]\n");
00707 
00708         root = talloc_new(NULL);
00709 
00710         t1 = talloc(root, struct t_move);
00711         t2 = talloc(root, struct t_move);
00712         t1->p = talloc_strdup(t1, "foo");
00713         t1->x = talloc(t1, int);
00714         *t1->x = 42;
00715 
00716         t2->p = talloc_move(t2, &t1->p);
00717         t2->x = talloc_move(t2, &t1->x);
00718         torture_assert("move", t1->p == NULL && t1->x == NULL &&
00719             strcmp(t2->p, "foo") == 0 && *t2->x == 42,
00720                 "talloc move failed");
00721 
00722         talloc_free(root);
00723 
00724         printf("success: move\n");
00725 
00726         return true;
00727 }
00728 
00729 /*
00730   test talloc_realloc_fn
00731 */
00732 static bool test_realloc_fn(void)
00733 {
00734         void *root, *p1;
00735 
00736         printf("test: realloc_fn [\ntalloc_realloc_fn\n]\n");
00737 
00738         root = talloc_new(NULL);
00739 
00740         p1 = talloc_realloc_fn(root, NULL, 10);
00741         CHECK_BLOCKS("realloc_fn", root, 2);
00742         CHECK_SIZE("realloc_fn", root, 10);
00743         p1 = talloc_realloc_fn(root, p1, 20);
00744         CHECK_BLOCKS("realloc_fn", root, 2);
00745         CHECK_SIZE("realloc_fn", root, 20);
00746         p1 = talloc_realloc_fn(root, p1, 0);
00747         CHECK_BLOCKS("realloc_fn", root, 1);
00748         CHECK_SIZE("realloc_fn", root, 0);
00749 
00750         talloc_free(root);
00751 
00752         printf("success: realloc_fn\n");
00753         return true;
00754 }
00755 
00756 
00757 static bool test_unref_reparent(void)
00758 {
00759         void *root, *p1, *p2, *c1;
00760 
00761         printf("test: unref_reparent [\nUNREFERENCE AFTER PARENT FREED\n]\n");
00762 
00763         root = talloc_named_const(NULL, 0, "root");
00764         p1 = talloc_named_const(root, 1, "orig parent");
00765         p2 = talloc_named_const(root, 1, "parent by reference");
00766 
00767         c1 = talloc_named_const(p1, 1, "child");
00768         talloc_reference(p2, c1);
00769 
00770         CHECK_PARENT("unref_reparent", c1, p1);
00771 
00772         talloc_free(p1);
00773 
00774         CHECK_PARENT("unref_reparent", c1, p2);
00775 
00776         talloc_unlink(p2, c1);
00777 
00778         CHECK_SIZE("unref_reparent", root, 1);
00779 
00780         talloc_free(p2);
00781         talloc_free(root);
00782 
00783         printf("success: unref_reparent\n");
00784         return true;
00785 }
00786 
00787 /*
00788   measure the speed of talloc versus malloc
00789 */
00790 static bool test_speed(void)
00791 {
00792         void *ctx = talloc_new(NULL);
00793         unsigned count;
00794         const int loop = 1000;
00795         int i;
00796         struct timeval tv;
00797 
00798         printf("test: speed [\nTALLOC VS MALLOC SPEED\n]\n");
00799 
00800         tv = timeval_current();
00801         count = 0;
00802         do {
00803                 void *p1, *p2, *p3;
00804                 for (i=0;i<loop;i++) {
00805                         p1 = talloc_size(ctx, loop % 100);
00806                         p2 = talloc_strdup(p1, "foo bar");
00807                         p3 = talloc_size(p1, 300);
00808                         talloc_free(p1);
00809                 }
00810                 count += 3 * loop;
00811         } while (timeval_elapsed(&tv) < 5.0);
00812 
00813         fprintf(stderr, "talloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
00814 
00815         talloc_free(ctx);
00816 
00817         tv = timeval_current();
00818         count = 0;
00819         do {
00820                 void *p1, *p2, *p3;
00821                 for (i=0;i<loop;i++) {
00822                         p1 = malloc(loop % 100);
00823                         p2 = strdup("foo bar");
00824                         p3 = malloc(300);
00825                         free(p1);
00826                         free(p2);
00827                         free(p3);
00828                 }
00829                 count += 3 * loop;
00830         } while (timeval_elapsed(&tv) < 5.0);
00831         fprintf(stderr, "malloc: %.0f ops/sec\n", count/timeval_elapsed(&tv));
00832 
00833         printf("success: speed\n");
00834 
00835         return true;
00836 }
00837 
00838 static bool test_lifeless(void)
00839 {
00840         void *top = talloc_new(NULL);
00841         char *parent, *child; 
00842         void *child_owner = talloc_new(NULL);
00843 
00844         printf("test: lifeless [\nTALLOC_UNLINK LOOP\n]\n");
00845 
00846         parent = talloc_strdup(top, "parent");
00847         child = talloc_strdup(parent, "child");  
00848         (void)talloc_reference(child, parent);
00849         (void)talloc_reference(child_owner, child); 
00850         talloc_report_full(top, stderr);
00851         talloc_unlink(top, parent);
00852         talloc_free(child);
00853         talloc_report_full(top, stderr);
00854         talloc_free(top);
00855         talloc_free(child_owner);
00856         talloc_free(child);
00857 
00858         printf("success: lifeless\n");
00859         return true;
00860 }
00861 
00862 static int loop_destructor_count;
00863 
00864 static int test_loop_destructor(char *ptr)
00865 {
00866         loop_destructor_count++;
00867         return 0;
00868 }
00869 
00870 static bool test_loop(void)
00871 {
00872         void *top = talloc_new(NULL);
00873         char *parent;
00874         struct req1 {
00875                 char *req2, *req3;
00876         } *req1;
00877 
00878         printf("test: loop [\nTALLOC LOOP DESTRUCTION\n]\n");
00879 
00880         parent = talloc_strdup(top, "parent");
00881         req1 = talloc(parent, struct req1);
00882         req1->req2 = talloc_strdup(req1, "req2");  
00883         talloc_set_destructor(req1->req2, test_loop_destructor);
00884         req1->req3 = talloc_strdup(req1, "req3");
00885         (void)talloc_reference(req1->req3, req1);
00886         talloc_report_full(top, stderr);
00887         talloc_free(parent);
00888         talloc_report_full(top, stderr);
00889         talloc_report_full(NULL, stderr);
00890         talloc_free(top);
00891 
00892         torture_assert("loop", loop_destructor_count == 1, 
00893                                    "FAILED TO FIRE LOOP DESTRUCTOR\n");
00894         loop_destructor_count = 0;
00895 
00896         printf("success: loop\n");
00897         return true;
00898 }
00899 
00900 static int fail_destructor_str(char *ptr)
00901 {
00902         return -1;
00903 }
00904 
00905 static bool test_free_parent_deny_child(void)
00906 {
00907         void *top = talloc_new(NULL);
00908         char *level1;
00909         char *level2;
00910         char *level3;
00911 
00912         printf("test: free_parent_deny_child [\nTALLOC FREE PARENT DENY CHILD\n]\n");
00913 
00914         level1 = talloc_strdup(top, "level1");
00915         level2 = talloc_strdup(level1, "level2");
00916         level3 = talloc_strdup(level2, "level3");
00917 
00918         talloc_set_destructor(level3, fail_destructor_str);
00919         talloc_free(level1);
00920         talloc_set_destructor(level3, NULL);
00921 
00922         CHECK_PARENT("free_parent_deny_child", level3, top);
00923 
00924         talloc_free(top);
00925 
00926         printf("success: free_parent_deny_child\n");
00927         return true;
00928 }
00929 
00930 static bool test_talloc_ptrtype(void)
00931 {
00932         void *top = talloc_new(NULL);
00933         struct struct1 {
00934                 int foo;
00935                 int bar;
00936         } *s1, *s2, **s3, ***s4;
00937         const char *location1;
00938         const char *location2;
00939         const char *location3;
00940         const char *location4;
00941 
00942         printf("test: ptrtype [\nTALLOC PTRTYPE\n]\n");
00943 
00944         s1 = talloc_ptrtype(top, s1);location1 = __location__;
00945 
00946         if (talloc_get_size(s1) != sizeof(struct struct1)) {
00947                 printf("failure: ptrtype [\n"
00948                   "talloc_ptrtype() allocated the wrong size %lu (should be %lu)\n"
00949                   "]\n", (unsigned long)talloc_get_size(s1),
00950                            (unsigned long)sizeof(struct struct1));
00951                 return false;
00952         }
00953 
00954         if (strcmp(location1, talloc_get_name(s1)) != 0) {
00955                 printf("failure: ptrtype [\n"
00956                   "talloc_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
00957                         talloc_get_name(s1), location1);
00958                 return false;
00959         }
00960 
00961         s2 = talloc_array_ptrtype(top, s2, 10);location2 = __location__;
00962 
00963         if (talloc_get_size(s2) != (sizeof(struct struct1) * 10)) {
00964                 printf("failure: ptrtype [\n"
00965                            "talloc_array_ptrtype() allocated the wrong size "
00966                        "%lu (should be %lu)\n]\n",
00967                         (unsigned long)talloc_get_size(s2),
00968                     (unsigned long)(sizeof(struct struct1)*10));
00969                 return false;
00970         }
00971 
00972         if (strcmp(location2, talloc_get_name(s2)) != 0) {
00973                 printf("failure: ptrtype [\n"
00974                 "talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n",
00975                         talloc_get_name(s2), location2);
00976                 return false;
00977         }
00978 
00979         s3 = talloc_array_ptrtype(top, s3, 10);location3 = __location__;
00980 
00981         if (talloc_get_size(s3) != (sizeof(struct struct1 *) * 10)) {
00982                 printf("failure: ptrtype [\n"
00983                            "talloc_array_ptrtype() allocated the wrong size "
00984                        "%lu (should be %lu)\n]\n",
00985                            (unsigned long)talloc_get_size(s3),
00986                        (unsigned long)(sizeof(struct struct1 *)*10));
00987                 return false;
00988         }
00989 
00990         torture_assert_str_equal("ptrtype", location3, talloc_get_name(s3),
00991                 "talloc_array_ptrtype() sets the wrong name");
00992 
00993         s4 = talloc_array_ptrtype(top, s4, 10);location4 = __location__;
00994 
00995         if (talloc_get_size(s4) != (sizeof(struct struct1 **) * 10)) {
00996                 printf("failure: TALLOC PTRTYPE [\n"
00997                       "talloc_array_ptrtype() allocated the wrong size "
00998                        "%lu (should be %lu)\n]\n",
00999                            (unsigned long)talloc_get_size(s4),
01000                        (unsigned long)(sizeof(struct struct1 **)*10));
01001                 return false;
01002         }
01003 
01004         torture_assert_str_equal("ptrtype", location4, talloc_get_name(s4),
01005                 "talloc_array_ptrtype() sets the wrong name");
01006 
01007         talloc_free(top);
01008 
01009         printf("success: ptrtype\n");
01010         return true;
01011 }
01012 
01013 static bool test_autofree(void)
01014 {
01015         void *p;
01016         printf("test: autofree [\nTALLOC AUTOFREE CONTEXT\n]\n");
01017 
01018         p = talloc_autofree_context();
01019         talloc_free(p);
01020 
01021         p = talloc_autofree_context();
01022         talloc_free(p);
01023 
01024         printf("success: autofree\n");
01025         return true;
01026 }
01027 
01028 int main(void)
01029 {
01030         bool ret = true;
01031 
01032         talloc_disable_null_tracking();
01033         talloc_enable_null_tracking();
01034 
01035         ret &= test_ref1();
01036         ret &= test_ref2();
01037         ret &= test_ref3();
01038         ret &= test_ref4();
01039         ret &= test_unlink1(); 
01040         ret &= test_misc();
01041         ret &= test_realloc();
01042         ret &= test_realloc_child(); 
01043         ret &= test_steal(); 
01044         ret &= test_move(); 
01045         ret &= test_unref_reparent();
01046         ret &= test_realloc_fn(); 
01047         ret &= test_type();
01048         ret &= test_lifeless(); 
01049         ret &= test_loop();
01050         ret &= test_free_parent_deny_child(); 
01051         ret &= test_talloc_ptrtype();
01052 
01053         if (ret) {
01054                 ret &= test_speed();
01055         }
01056         ret &= test_autofree();
01057 
01058         if (!ret)
01059                 return -1;
01060         return 0;
01061 }

Sambaに対してSat Aug 29 21:22:59 2009に生成されました。  doxygen 1.4.7