summaryrefslogtreecommitdiff
path: root/ccan/foreach/test/run-not-on-stack.c
blob: 20895ecc9a61a074d6f25d7382dd5dcfe9453799 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <ccan/foreach/foreach.h>
#include <ccan/tap/tap.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ccan/foreach/foreach.c>

static int global_i;
static void *allocs[1000];
static unsigned int num_allocs;

static void iterate(unsigned int depth, bool done_global)
{
	int *i, expecti;
	const char *expectp[] = { "hello", "there" };
	const char **p;
	int stack_i;

	if (depth == 4)
		return;

	if (!done_global) {
		expecti = 0;
		foreach_int(global_i, 0, 1) {
			ok1(global_i == expecti);
			expecti++;
			if (global_i == 0)
				iterate(depth + 1, true);
		}
		ok1(expecti == 2);
	}

	i = allocs[num_allocs++] = malloc(sizeof(*i));
	expecti = 0;
	foreach_int(*i, 0, 1) {
		ok1(*i == expecti);
		expecti++;
		if (*i == 0)
			iterate(depth + 1, done_global);
	}
	ok1(expecti == 2);

	p = allocs[num_allocs++] = malloc(sizeof(*p));
	expecti = 0;
	foreach_ptr(*p, "hello", "there") {
		ok1(strcmp(expectp[expecti], *p) == 0);
		expecti++;
		if (expecti == 1)
			iterate(depth + 1, done_global);
	}
	ok1(expecti == 2);
	ok1(*p == NULL);

	expecti = 0;
	foreach_int(stack_i, 0, 1) {
		ok1(stack_i == expecti);
		expecti++;
		if (stack_i == 0)
			iterate(depth + 1, done_global);
	}
	ok1(expecti == 2);
}

int main(void)
{
	unsigned int i;
	plan_tests(861);

	iterate(0, false);

	ok1(num_allocs < 1000);
	for (i = 0; i < num_allocs; i++)
		free(allocs[i]);

	return exit_status();
}