summaryrefslogtreecommitdiff
path: root/tests/tests2/115_bound_setjmp.c
blob: c402144f97caab742209dff05e51591499b9688f (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdio.h>
#include <string.h>
#include <setjmp.h>

#define TST   int i, a[2], b[2];                \
              for (i = 0; i < 2; i++) a[i] = 0; \
              for (i = 0; i < 2; i++) b[i] = 0

static jmp_buf jmp;

static void tst1 (void)
{
    TST;
    longjmp(jmp, 1);
}

static void tst2(void)
{
    jmp_buf jmp;

    setjmp (jmp);
    TST;
    tst1();
}

static void tst3 (jmp_buf loc)
{
    TST;
    longjmp(loc, 1);
}

static void tst4(jmp_buf loc)
{
    jmp_buf jmp;

    setjmp (jmp);
    TST;
    tst3(loc);
}

static void tst (void)
{
    jmp_buf loc;
    static int cnt;

    cnt = 0;
    if (setjmp (jmp) == 0) {
        TST;
        tst2();
    }
    else {
        cnt++;
    }
    if (setjmp (loc) == 0) {
        TST;
        tst4(loc);
    }
    else {
        cnt++;
    }
    if (cnt != 2)
        printf ("incorrect cnt %d\n", cnt);
}

static jmp_buf buf1;
static jmp_buf buf2;
static int *p;
static int n_x = 6;
static int g_counter;

static void stack (void)
{
    static int counter;
    static int way_point1;
    static int way_point2;

    counter = 0;
    way_point1 = 3;
    way_point2 = 2;
    g_counter = 0;
    if (setjmp (buf1) != 101) {
        int a[n_x];
        g_counter++;
        p = &a[0];
        if (g_counter < 5)
            longjmp (buf1, 2);
        else if (g_counter == 5)
            longjmp (buf1, 101);
        else {
            setjmp (buf2); 
            longjmp (buf1, 101);
        }
    }
    
    way_point1--;

    if (counter == 0) {
        counter++;
        {
            int a[n_x];
            g_counter++;
            p = &a[0];
            if (g_counter < 5)
                longjmp (buf1, 2);
            else if (g_counter == 5)
                longjmp (buf1, 101);
            else {
                setjmp (buf2);
                longjmp (buf1, 101);
            }
        }
    }

    way_point2--;

    if (counter == 1) {
        counter++;
        longjmp (buf2, 2);
    }

    if (!(way_point1 == 0 && way_point2 == 0 &&
          g_counter == 6 && counter == 2))
        printf ("Failed %d %d %d %d\n",
                way_point1, way_point2, g_counter, counter);
}

static jmp_buf env;
static int last_value;

static void jump (int val)
{
    longjmp (env, val);
}

static void check (void)
{
    int value;

    last_value = -1;
    value = setjmp (env);
    if (value != last_value + 1) {
        printf ("incorrect value %d %d\n",
                value, last_value + 1);
        return;
    }
    last_value = value;
    switch (value) {
    case 0:
        jump (0);
    default:
        if (value < 10)
          jump (value + 1);
    }
}

int
main (void)
{
    int i;

    for (i = 0; i < 10;  i++) {
        tst();
        stack();
        check();
    }
    return 0;
}