]> git.ozlabs.org Git - ccan/blob - ccan/talloc/test/run-set_allocator.c
ttxml: removed cruft from tests
[ccan] / ccan / talloc / test / run-set_allocator.c
1 #include <ccan/failtest/failtest_override.h>
2 #include <ccan/talloc/talloc.c>
3 #include <stdbool.h>
4 #include <ccan/tap/tap.h>
5 #include <ccan/failtest/failtest.h>
6
7 static unsigned my_malloc_count, my_free_count, my_realloc_count;
8
9 static void *my_malloc(size_t size)
10 {
11         my_malloc_count++;
12         return malloc(size);
13 }
14
15 static void my_free(void *ptr)
16 {
17         my_free_count++;
18         free(ptr);
19 }
20
21 static void *my_realloc(void *ptr, size_t size)
22 {
23         my_realloc_count++;
24         ok1(ptr);
25         ok1(size);
26         return realloc(ptr, size);
27 }
28
29 int main(int argc, char *argv[])
30 {
31         int *p1, *p2;
32
33         plan_tests(14);
34         failtest_init(argc, argv);
35         talloc_set_allocator(my_malloc, my_free, my_realloc);
36         p1 = talloc_array(NULL, int, 10);
37         if (!p1)
38                 failtest_exit(exit_status());
39         ok1(my_malloc_count == 1);
40         ok1(my_free_count == 0);
41         ok1(my_realloc_count == 0);
42
43         p2 = talloc_realloc(NULL, p1, int, 10000);
44         if (!p2) {
45                 talloc_free(p1);
46                 failtest_exit(exit_status());
47         }
48         p1 = p2;
49         ok1(my_malloc_count == 1);
50         ok1(my_free_count == 0);
51         ok1(my_realloc_count == 1);
52
53         p2 = talloc(p1, int);
54         if (!p2) {
55                 talloc_free(p1);
56                 failtest_exit(exit_status());
57         }
58         ok1(my_malloc_count == 2);
59         ok1(my_free_count == 0);
60         ok1(my_realloc_count == 1);
61
62         talloc_free(p1);
63         ok1(my_malloc_count == 2);
64         ok1(my_free_count == 2);
65         ok1(my_realloc_count == 1);
66
67         failtest_exit(exit_status());
68 }