]> pd.if.org Git - lice/blob - tests/control.c
autocommit for files dated 2014-11-17 20:13:34
[lice] / tests / control.c
1 // control flow
2
3 int if1(void) {
4     if (1) {
5         return 1;
6     }
7     return 0;
8 }
9
10 int if2(void) {
11     if (0) {
12         return 0;
13     }
14     return 2;
15 }
16
17 int if3(void) {
18     if (1) {
19         return 3;
20     } else {
21         return 0;
22     }
23     return 0;
24 }
25
26 int if4(void) {
27     if (0) {
28         return 0;
29     } else {
30         return 4;
31     }
32     return 0;
33 }
34
35 int if5(void) {
36     if (1) return 5;
37     return 0;
38 }
39
40 int if6(void) {
41     if (0) return 0;
42     return 6;
43 }
44
45 int if7(void) {
46     if (1) return 7;
47     else return 0;
48     return 0;
49 }
50
51 int if8(void) {
52     if (0) return 0;
53     else return 8;
54     return 0;
55 }
56
57 int if9(void) {
58     // 0.5 evaluates true
59     if (0.5) return 9;
60     else return 0;
61 }
62
63 int main(void) {
64     int i;
65     int j;
66
67     expecti(if1(), 1);
68     expecti(if2(), 2);
69     expecti(if3(), 3);
70     expecti(if4(), 4);
71     expecti(if5(), 5);
72     expecti(if6(), 6);
73     expecti(if7(), 7);
74     expecti(if8(), 8);
75     expecti(if9(), 9);
76
77     // for control
78     j = 0;
79     for (i = 0; i < 5; i++)
80         j = j + i;
81     expecti(j, 10);
82
83     j = 0;
84     for (i = 0; i < 5; i++) {
85         j = j + i;
86     }
87     expecti(j, 10);
88
89     j = 0;
90     for (i = 0; i < 100; i++) {
91         if (i < 5)
92             continue;
93         if (i == 9)
94             break;
95         j += i;
96     }
97     expecti(j, 5 + 6 + 7 + 8);
98
99     i = 0;
100     for (; 0.5;) {
101         i = 1337;
102         break;
103     }
104     expecti(i, 1337);
105
106     // while control
107     i = 0;
108     j = 0;
109     while (i <= 100)
110         j = j + i++;
111     expecti(j, 5050);
112
113     i = 0;
114     j = 1;
115     while (i <= 100)
116         j = j + i++;
117     expecti(j, 5051);
118
119     i = 0;
120     j = 0;
121     while (i < 10) {
122         if (i++ < 5)
123             continue;
124         j += i;
125         if (i == 9)
126             break;
127     }
128     expecti(j, 6 + 7 + 8 + 9);
129
130     i = 0;
131     while (0.5) {
132         i = 1337;
133         break;
134     }
135     expecti(i, 1337);
136
137     // do control
138     i = 0;
139     j = 0;
140     do {
141         j = j + i++;
142     } while (i <= 100);
143     expecti(j, 5050);
144
145     i = 0;
146     do { i = 1337; } while (0);
147     expecti(i, 1337);
148
149     i = 0;
150     j = 0;
151     do {
152         if (i++ < 5)
153             continue;
154         j += i;
155         if (i == 9)
156             break;
157     } while (i < 10);
158     expecti(j, 6 + 7 + 8 + 9);
159     expecti(i, 9);
160
161     i = 0;
162     do { i++; break; } while (0.5);
163     expecti(i, 1);
164
165     // switch control
166     i = 0;
167     switch (1 + 2) {
168         case 0:
169             expecti(0, 1);
170         case 3:
171             i = 3;
172             break;
173         case 1:
174             expecti(0, 1);
175     }
176     expecti(i, 3);
177
178     i = 0;
179     switch(1) {
180         case 0: i++;
181         case 1: i++;
182         case 2: i++;
183         case 3: i++;
184     }
185     expecti(i, 3);
186
187     i = 0;
188     switch (1024) {
189         case 0: i++;
190         default:
191             i = 128;
192     }
193     expecti(i, 128);
194
195     i = 0;
196     switch (1024) {
197         case 0: i++;
198     }
199     expecti(i, 0);
200
201     i = 1024;
202     switch (3) {
203         i++;
204     }
205
206     switch (1337) {
207         case 1 ... 100:
208             expecti(1, 0);
209         case 101:
210             expecti(1, 0);
211         case 102 ... 1337:
212             break;
213         default:
214             expecti(1, 0);
215     }
216
217     expecti(i, 1024);
218
219     i = 0;
220     j = 1024;
221     switch (j % 8) {
222         case 0: do { i++;
223         case 7:      i++;
224         case 6:      i++;
225         case 5:      i++;
226         case 4:      i++;
227         case 3:      i++;
228         case 2:      i++;
229         case 1:      i++;
230                 } while ((j -= 8) > 0);
231     }
232     expecti(i, 1024);
233
234     // goto control
235     j = 0;
236     goto A;
237     j = 5;
238 A:  expecti(j, 0);
239     i = 0;
240     j = 0;
241 B:  if (i > 10)
242         goto C;
243     j += i++;
244     goto B;
245 C:  if (i > 11)
246         goto D;
247     expecti(j, 55);
248     i++;
249     goto B;
250 D:
251     expecti(i, 12);
252     expecti(j, 55);
253
254     // logical or control flow
255     expecti(0 || 3, 1);
256     expecti(5 || 0, 1);
257     expecti(0 || 0, 0);
258
259     // empty expressions
260     for (;;)
261         break;
262
263     for (i = 0; i < 50; i++)
264         ;
265
266     i = 0;
267     while (i++ < 50)
268         ;
269
270     i = 0;
271     do 1; while (i++ < 50);
272     i = 0;
273     do; while (i++ < 50);
274
275     switch (1)
276         ;
277
278     i = ((0.5) ? 1 : 0);
279     expecti(i, 1);
280
281     // computed goto
282     void *Q[] = { &&__a, &&__b, &&__c, &&__d };
283     int _ = 0;
284     goto *Q[0];
285     _ = 100;
286
287 __a:
288     if (_ == 5)
289         return 0;
290     expecti(_, 0);
291     _ = 1;
292     goto *Q[2];
293     _ = 2;
294
295 __b:
296     expecti(_, -1);
297     _ = 5;
298     goto *Q[3];
299     _ = 0;
300
301 __c:
302     expecti(_, 1);
303     _ = -1;
304     goto *Q[1];
305     _ = 3;
306
307 __d:
308     expecti(_, 5);
309     goto **Q;
310
311
312     return 1; // error
313 }