]> git.ozlabs.org Git - ccan/blob - ccan/foreach/test/run-not-on-stack.c
tdb2: cleanups for tdbtorture, add more debugging and -S flag.
[ccan] / ccan / foreach / test / run-not-on-stack.c
1 #include <ccan/foreach/foreach.h>
2 #include <ccan/tap/tap.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <ccan/foreach/foreach.c>
7
8 static int global_i;
9 static void *allocs[1000];
10 static unsigned int num_allocs;
11
12 static void iterate(unsigned int depth, bool done_global)
13 {
14         int *i, expecti;
15         const char *expectp[] = { "hello", "there" };
16         const char **p;
17         int stack_i;
18
19         if (depth == 4)
20                 return;
21
22         if (!done_global) {
23                 expecti = 0;
24                 foreach_int(global_i, 0, 1) {
25                         ok1(global_i == expecti);
26                         expecti++;
27                         if (global_i == 0)
28                                 iterate(depth + 1, true);
29                 }
30                 ok1(expecti == 2);
31         }
32
33         i = allocs[num_allocs++] = malloc(sizeof(*i));
34         expecti = 0;
35         foreach_int(*i, 0, 1) {
36                 ok1(*i == expecti);
37                 expecti++;
38                 if (*i == 0)
39                         iterate(depth + 1, done_global);
40         }
41         ok1(expecti == 2);
42
43         p = allocs[num_allocs++] = malloc(sizeof(*p));
44         expecti = 0;
45         foreach_ptr(*p, "hello", "there") {
46                 ok1(strcmp(expectp[expecti], *p) == 0);
47                 expecti++;
48                 if (expecti == 1)
49                         iterate(depth + 1, done_global);
50         }
51         ok1(expecti == 2);
52         ok1(*p == NULL);
53
54         expecti = 0;
55         foreach_int(stack_i, 0, 1) {
56                 ok1(stack_i == expecti);
57                 expecti++;
58                 if (stack_i == 0)
59                         iterate(depth + 1, done_global);
60         }
61         ok1(expecti == 2);
62 }
63
64 int main(void)
65 {
66         unsigned int i;
67         plan_tests(861);
68
69         iterate(0, false);
70
71         ok1(num_allocs < 1000);
72         for (i = 0; i < num_allocs; i++)
73                 free(allocs[i]);
74
75         return exit_status();
76 }