📄 testsuite.c
字号:
CHECK_SIZE("realloc", root, 60); CHECK_BLOCKS("realloc", p1, 4); p1 = talloc_realloc_size(NULL, p1, 20); CHECK_SIZE("realloc", p1, 60); talloc_increase_ref_count(p2); torture_assert("realloc", talloc_realloc_size(NULL, p2, 5) == NULL, "failed: talloc_realloc() on a referenced pointer should fail\n"); CHECK_BLOCKS("realloc", p1, 4); talloc_realloc_size(NULL, p2, 0); talloc_realloc_size(NULL, p2, 0); CHECK_BLOCKS("realloc", p1, 3); torture_assert("realloc", talloc_realloc_size(NULL, p1, 0x7fffffff) == NULL, "failed: oversize talloc should fail\n"); talloc_realloc_size(NULL, p1, 0); CHECK_BLOCKS("realloc", root, 1); CHECK_SIZE("realloc", root, 0); talloc_free(root); printf("success: REALLOC\n"); return true;}/* test realloc with a child*/static bool test_realloc_child(void){ void *root; struct el2 { const char *name; } *el2; struct el1 { int count; struct el2 **list, **list2, **list3; } *el1; printf("test: REALLOC WITH CHILD\n"); root = talloc_new(NULL); el1 = talloc(root, struct el1); el1->list = talloc(el1, struct el2 *); el1->list[0] = talloc(el1->list, struct el2); el1->list[0]->name = talloc_strdup(el1->list[0], "testing"); el1->list2 = talloc(el1, struct el2 *); el1->list2[0] = talloc(el1->list2, struct el2); el1->list2[0]->name = talloc_strdup(el1->list2[0], "testing2"); el1->list3 = talloc(el1, struct el2 *); el1->list3[0] = talloc(el1->list3, struct el2); el1->list3[0]->name = talloc_strdup(el1->list3[0], "testing2"); el2 = talloc(el1->list, struct el2); el2 = talloc(el1->list2, struct el2); el2 = talloc(el1->list3, struct el2); el1->list = talloc_realloc(el1, el1->list, struct el2 *, 100); el1->list2 = talloc_realloc(el1, el1->list2, struct el2 *, 200); el1->list3 = talloc_realloc(el1, el1->list3, struct el2 *, 300); talloc_free(root); printf("success: REALLOC WITH CHILD\n"); return true;}/* test type checking*/static bool test_type(void){ void *root; struct el1 { int count; }; struct el2 { int count; }; struct el1 *el1; printf("test: type [\ntalloc type checking\n]\n"); root = talloc_new(NULL); el1 = talloc(root, struct el1); el1->count = 1; torture_assert("type", talloc_get_type(el1, struct el1) == el1, "type check failed on el1\n"); torture_assert("type", talloc_get_type(el1, struct el2) == NULL, "type check failed on el1 with el2\n"); talloc_set_type(el1, struct el2); torture_assert("type", talloc_get_type(el1, struct el2) == (struct el2 *)el1, "type set failed on el1 with el2\n"); talloc_free(root); printf("success: type\n"); return true;}/* test steal*/static bool test_steal(void){ void *root, *p1, *p2; printf("test: steal [\nSTEAL\n]\n"); root = talloc_new(NULL); p1 = talloc_array(root, char, 10); CHECK_SIZE("steal", p1, 10); p2 = talloc_realloc(root, NULL, char, 20); 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); p1 = talloc_size(NULL, 3); talloc_report_full(NULL, stderr); CHECK_SIZE("steal", NULL, 3); talloc_free(p1); printf("success: steal\n"); return true;}/* test move*/static bool test_move(void){ void *root; struct t_move { char *p; int *x; } *t1, *t2; printf("test: move [\nMOVE\n]\n"); root = talloc_new(NULL); t1 = talloc(root, struct t_move); t2 = talloc(root, struct t_move); t1->p = talloc_strdup(t1, "foo"); t1->x = talloc(t1, int); *t1->x = 42; t2->p = talloc_move(t2, &t1->p); t2->x = talloc_move(t2, &t1->x); torture_assert("move", t1->p == NULL && t1->x == NULL && strcmp(t2->p, "foo") == 0 && *t2->x == 42, "talloc move failed"); talloc_free(root); printf("success: move\n"); return true;}/* test talloc_realloc_fn*/static bool test_realloc_fn(void){ void *root, *p1; printf("test: realloc_fn [\ntalloc_realloc_fn\n]\n"); root = talloc_new(NULL); p1 = talloc_realloc_fn(root, NULL, 10); CHECK_BLOCKS("realloc_fn", root, 2); CHECK_SIZE("realloc_fn", root, 10); p1 = talloc_realloc_fn(root, p1, 20); CHECK_BLOCKS("realloc_fn", root, 2); CHECK_SIZE("realloc_fn", root, 20); p1 = talloc_realloc_fn(root, p1, 0); CHECK_BLOCKS("realloc_fn", root, 1); CHECK_SIZE("realloc_fn", root, 0); talloc_free(root); printf("success: realloc_fn\n"); return true;}static bool test_unref_reparent(void){ void *root, *p1, *p2, *c1; printf("test: unref_reparent [\nUNREFERENCE AFTER PARENT FREED\n]\n"); root = talloc_named_const(NULL, 0, "root"); p1 = talloc_named_const(root, 1, "orig parent"); p2 = talloc_named_const(root, 1, "parent by reference"); c1 = talloc_named_const(p1, 1, "child"); talloc_reference(p2, c1); CHECK_PARENT("unref_reparent", c1, p1); talloc_free(p1); CHECK_PARENT("unref_reparent", c1, p2); talloc_unlink(p2, c1); CHECK_SIZE("unref_reparent", root, 1); talloc_free(p2); talloc_free(root); printf("success: unref_reparent\n"); return true;}/* measure the speed of talloc versus malloc*/static bool test_speed(void){ void *ctx = talloc_new(NULL); unsigned count; const int loop = 1000; int i; struct timeval tv; printf("test: speed [\nTALLOC VS MALLOC SPEED\n]\n"); tv = timeval_current(); count = 0; do { void *p1, *p2, *p3; for (i=0;i<loop;i++) { p1 = talloc_size(ctx, loop % 100); p2 = talloc_strdup(p1, "foo bar"); p3 = talloc_size(p1, 300); talloc_free(p1); } count += 3 * loop; } while (timeval_elapsed(&tv) < 5.0); fprintf(stderr, "talloc: %.0f ops/sec\n", count/timeval_elapsed(&tv)); talloc_free(ctx); tv = timeval_current(); count = 0; do { void *p1, *p2, *p3; for (i=0;i<loop;i++) { p1 = malloc(loop % 100); p2 = strdup("foo bar"); p3 = malloc(300); free(p1); free(p2); free(p3); } count += 3 * loop; } while (timeval_elapsed(&tv) < 5.0); fprintf(stderr, "malloc: %.0f ops/sec\n", count/timeval_elapsed(&tv)); printf("success: speed\n"); return true;}static bool test_lifeless(void){ void *top = talloc_new(NULL); char *parent, *child; void *child_owner = talloc_new(NULL); printf("test: lifeless [\nTALLOC_UNLINK LOOP\n]\n"); parent = talloc_strdup(top, "parent"); child = talloc_strdup(parent, "child"); (void)talloc_reference(child, parent); (void)talloc_reference(child_owner, child); talloc_report_full(top, stderr); talloc_unlink(top, parent); talloc_free(child); talloc_report_full(top, stderr); talloc_free(top); talloc_free(child_owner); talloc_free(child); printf("success: lifeless\n"); return true;}static int loop_destructor_count;static int test_loop_destructor(char *ptr){ loop_destructor_count++; return 0;}static bool test_loop(void){ void *top = talloc_new(NULL); char *parent; struct req1 { char *req2, *req3; } *req1; printf("test: loop [\nTALLOC LOOP DESTRUCTION\n]\n"); parent = talloc_strdup(top, "parent"); req1 = talloc(parent, struct req1); req1->req2 = talloc_strdup(req1, "req2"); talloc_set_destructor(req1->req2, test_loop_destructor); req1->req3 = talloc_strdup(req1, "req3"); (void)talloc_reference(req1->req3, req1); talloc_report_full(top, stderr); talloc_free(parent); talloc_report_full(top, stderr); talloc_report_full(NULL, stderr); talloc_free(top); torture_assert("loop", loop_destructor_count == 1, "FAILED TO FIRE LOOP DESTRUCTOR\n"); loop_destructor_count = 0; printf("success: loop\n"); return true;}static int fail_destructor_str(char *ptr){ return -1;}static bool test_free_parent_deny_child(void){ void *top = talloc_new(NULL); char *level1; char *level2; char *level3; printf("test: free_parent_deny_child [\nTALLOC FREE PARENT DENY CHILD\n]\n"); level1 = talloc_strdup(top, "level1"); level2 = talloc_strdup(level1, "level2"); level3 = talloc_strdup(level2, "level3"); talloc_set_destructor(level3, fail_destructor_str); talloc_free(level1); talloc_set_destructor(level3, NULL); CHECK_PARENT("free_parent_deny_child", level3, top); talloc_free(top); printf("success: free_parent_deny_child\n"); return true;}static bool test_talloc_ptrtype(void){ void *top = talloc_new(NULL); struct struct1 { int foo; int bar; } *s1, *s2, **s3, ***s4; const char *location1; const char *location2; const char *location3; const char *location4; printf("test: ptrtype [\nTALLOC PTRTYPE\n]\n"); s1 = talloc_ptrtype(top, s1);location1 = __location__; if (talloc_get_size(s1) != sizeof(struct struct1)) { printf("failure: ptrtype [\n" "talloc_ptrtype() allocated the wrong size %lu (should be %lu)\n" "]\n", (unsigned long)talloc_get_size(s1), (unsigned long)sizeof(struct struct1)); return false; } if (strcmp(location1, talloc_get_name(s1)) != 0) { printf("failure: ptrtype [\n" "talloc_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n", talloc_get_name(s1), location1); return false; } s2 = talloc_array_ptrtype(top, s2, 10);location2 = __location__; if (talloc_get_size(s2) != (sizeof(struct struct1) * 10)) { printf("failure: ptrtype [\n" "talloc_array_ptrtype() allocated the wrong size " "%lu (should be %lu)\n]\n", (unsigned long)talloc_get_size(s2), (unsigned long)(sizeof(struct struct1)*10)); return false; } if (strcmp(location2, talloc_get_name(s2)) != 0) { printf("failure: ptrtype [\n" "talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n]\n", talloc_get_name(s2), location2); return false; } s3 = talloc_array_ptrtype(top, s3, 10);location3 = __location__; if (talloc_get_size(s3) != (sizeof(struct struct1 *) * 10)) { printf("failure: ptrtype [\n" "talloc_array_ptrtype() allocated the wrong size " "%lu (should be %lu)\n]\n", (unsigned long)talloc_get_size(s3), (unsigned long)(sizeof(struct struct1 *)*10)); return false; } torture_assert_str_equal("ptrtype", location3, talloc_get_name(s3), "talloc_array_ptrtype() sets the wrong name"); s4 = talloc_array_ptrtype(top, s4, 10);location4 = __location__; if (talloc_get_size(s4) != (sizeof(struct struct1 **) * 10)) { printf("failure: TALLOC PTRTYPE [\n" "talloc_array_ptrtype() allocated the wrong size " "%lu (should be %lu)\n]\n", (unsigned long)talloc_get_size(s4), (unsigned long)(sizeof(struct struct1 **)*10)); return false; } torture_assert_str_equal("ptrtype", location4, talloc_get_name(s4), "talloc_array_ptrtype() sets the wrong name"); talloc_free(top); printf("success: ptrtype\n"); return true;}static bool test_autofree(void){ void *p; printf("test: autofree [\nTALLOC AUTOFREE CONTEXT\n]\n"); p = talloc_autofree_context(); talloc_free(p); p = talloc_autofree_context(); talloc_free(p); printf("success: autofree\n"); return true;}int main(void){ bool ret = true; talloc_disable_null_tracking(); talloc_enable_null_tracking(); ret &= test_ref1(); ret &= test_ref2(); ret &= test_ref3(); ret &= test_ref4(); ret &= test_unlink1(); ret &= test_misc(); ret &= test_realloc(); ret &= test_realloc_child(); ret &= test_steal(); ret &= test_move(); ret &= test_unref_reparent(); ret &= test_realloc_fn(); ret &= test_type(); ret &= test_lifeless(); ret &= test_loop(); ret &= test_free_parent_deny_child(); ret &= test_talloc_ptrtype(); if (ret) { ret &= test_speed(); } ret &= test_autofree(); if (!ret) return -1; return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -