aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/test_goto.sh
blob: 1e28a51d6c1e5ca7d12aeac6ea14baf6ede2476e (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
touch expected
test_diff <<'EOF'
int main() {
    goto end;
    return 1;
end:
    return 0;
}
EOF

cat <<'EOF' > expected
1
2
3
EOF
test_diff <<'EOF'
int printf(const char*, ...);

int main() {
    int i = 0;
loop:
    i++;
    printf("%d\n", i);
    if (i < 3)
        goto loop;
    return 0;
}
EOF

cat < /dev/null > expected
test_diff <<'EOF'
int main() {
    goto skip;
    int x = 5;
skip:
    return 0;
}
EOF

cat <<'EOF' > expected
start
middle
end
EOF
test_diff <<'EOF'
int printf(const char*, ...);

int main() {
    printf("start\n");
    goto middle;
first:
    printf("first\n");
    goto end;
middle:
    printf("middle\n");
    goto end;
last:
    printf("last\n");
end:
    printf("end\n");
    return 0;
}
EOF

cat <<'EOF' > expected
before
after
EOF
test_diff <<'EOF'
int printf(const char*, ...);

int main() {
    printf("before\n");
    {
        {
            goto out;
            printf("inside\n");
        }
        printf("middle\n");
    }
out:
    printf("after\n");
    return 0;
}
EOF

cat <<'EOF' > expected
x is 5
x is 10
EOF
test_diff <<'EOF'
int printf(const char*, ...);

int main() {
    int x = 5;
    if (x == 5) {
        printf("x is 5\n");
        goto next;
    }
    printf("x is not 5\n");
next:
    x = 10;
    printf("x is %d\n", x);
    return 0;
}
EOF

cat <<'EOF' > expected
case 2
done
EOF
test_diff <<'EOF'
int printf(const char*, ...);

int main() {
    int x = 2;
    switch (x) {
    case 1:
        printf("case 1\n");
        break;
    case 2:
        printf("case 2\n");
        goto done;
    case 3:
        printf("case 3\n");
        break;
    }
    printf("after switch\n");
done:
    printf("done\n");
    return 0;
}
EOF

# cat <<'EOF' > expected
# error: use of undeclared label 'undefined'
# EOF
# test_compile_error <<'EOF'
# int main() {
#     goto undefined;
#     return 0;
# }
# EOF

# cat <<'EOF' > expected
# error: redefinition of label 'duplicate'
# EOF
# test_compile_error <<'EOF'
# int main() {
# duplicate:
#     ;
# duplicate:
#     return 0;
# }
# EOF

# cat <<'EOF' > expected
# error: label at end of compound statement
# EOF
# test_compile_error <<'EOF'
# int main() {
#     {
#         goto end;
#     end:
#     }
#     return 0;
# }
# EOF