-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathteststak.c
More file actions
78 lines (68 loc) · 1.72 KB
/
teststak.c
File metadata and controls
78 lines (68 loc) · 1.72 KB
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
/* Test Stack */
#include <stdio.h>
#include <setjmp.h>
static jmp_buf buf;
int recurse1(unsigned int x) {
unsigned int space[200];
if (x == 100) {
printf("Level 100 reached\n");
return 100;
}
if (x > 100) {
printf("ERROR: Over Level 100!!\n");
return 100;
}
space[x] = recurse1(x + 1);
if (space[x] != x + 1) {
printf("ERROR: Mismatched return!!\n");
}
return x;
}
int recurse2(unsigned int x) {
unsigned int space[200];
if (x == 100) {
printf("Level 100 reached\n");
longjmp(buf, 5);
return 100;
}
if (x > 100) {
printf("ERROR: Over Level 100!!\n");
longjmp(buf, 5);
return 100;
}
space[50] = x + 1;
recurse2(space[50]);
printf("ERROR: longjmp must have failed!!\n");
return x;
}
int recurse3(unsigned int x) {
unsigned int space[200];
if (x == 100) {
printf("Level 100 reached\n");
exit(0);
return 100;
}
if (x > 100) {
printf("ERROR: Over Level 100!!\n");
exit(5);
return 100;
}
space[50] = x + 1;
recurse3(space[50]);
printf("ERROR: exit() must have failed!!\n");
return x;
}
int main(int argc, char *argv[]) {
int r;
printf("GCCLIB Stack Test\n");
recurse1(0);
printf("GCCLIB Stack Test - Done\n");
printf("GCCLIB Stack Test (with longjmp)\n");
if (!(r = setjmp(buf))) recurse2(0);
if (r != 5) printf("ERROR: set/longjmp did not return expected rc!!\n");
printf("GCCLIB Stack Test (with longjmp) - Done\n");
printf("GCCLIB Stack Test (with exit)\n");
recurse3(0);
printf("ERROR - GCCLIB Stack Test (with exit) - should not get here\n");
return 0;
}