2003-08-22 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / tests / test-154.cs
1 using System;
2 using System.Collections;
3
4 public class X
5 {
6         public static int Main ()
7         {
8                 // This is a compilation-only test.
9                 return 0;
10         }
11
12         // All code paths throw an exception, no need to set out parameters.
13         public static void test1 (out float f)
14         {
15                 throw new NotSupportedException ();
16         }
17
18         // The while loop breaks but does not return, so this is ok.
19         public static void test2 (int a, out float f)
20         {
21                 while (a > 0) {
22                         if (a == 5)
23                                 continue;
24
25                         Console.WriteLine (a);
26                 }
27
28                 f = 8.53F;
29         }
30
31         // a has been assigned in all code paths which do not return.
32         public static void test3 (long[] b, int c)
33         {
34                 ICollection a;
35                 if (b == null)
36                         throw new ArgumentException ();
37                 else
38                         a = (ICollection) b;
39
40                 Console.WriteLine (a);
41         }
42
43         // Forward goto, it's ok to set f after the target label.
44         public static int test4 (int b, out float f)
45         {
46                 long a;
47
48                 Console.WriteLine ("Hello World");
49
50                 a = 5;
51
52                 goto World;
53
54         World:
55                 Console.WriteLine (a);
56
57                 f = 8.53F;
58
59                 return 0;
60         }
61
62         // try { ... } catch { ... } finally { ... } block
63         static public int test5 (out float f, long d)
64         {
65                 int a;
66                 long b = 8;
67
68                 try {
69                         f = 8.53F;
70
71                         if (d == 500)
72                                 return 9;
73
74                         a = 5;
75                 } catch (NotSupportedException e) {
76                         a = 9;
77                 } catch (Exception e) {
78                         return 9;
79                 } finally {
80                         f = 9.234F;
81                 }
82
83                 return a;
84         }
85
86         // Passing out parameter to method invocation
87         static public int test6 (out float f)
88         {
89                 return test5 (out f, 50);
90         }
91
92         // Loop-variable of foreach() and for() loop.
93         static public long test7 (int[] a, int stop)
94         {
95                 long b = 0;
96                 foreach (int i in a)
97                         b += i;
98
99                 for (int i = 1; i < stop; i++)
100                         b *= i;
101
102                 return b;
103         }
104
105         // Initializing locals in initialize or test of for block
106         static public long test8 (int stop)
107         {
108                 int i;
109                 long b;
110                 for (i = 1; (b = stop) > 3; i++) {
111                         stop--;
112                         b += i;
113                 }
114                 return b;
115         }
116
117         // Initializing locals in test of while block
118         static public long test9 (int stop)
119         {
120                 long b;
121                 while ((b = stop) > 3) {
122                         stop--;
123                         b += stop;
124                 }
125                 return b;
126         }
127
128         // Return in subblock
129         public static void test10 (int a, out float f)
130         {
131                 if (a == 5) {
132                         f = 8.53F;
133                         return;
134                 }
135
136                 f = 9.0F;
137         }
138
139         // Switch block
140         public static long test11 (int a)
141         {
142                 long b;
143
144                 switch (a) {
145                 case 5:
146                         b = 1;
147                         break;
148
149                 case 9:
150                         b = 3;
151                         break;
152
153                 default:
154                         return 9;
155                 }
156
157                 return b;
158         }
159
160         // Try block which rethrows exception.
161         public static void test12 (out float f)
162         {
163                 try {
164                         f = 9.0F;
165                 } catch {
166                         throw new NotSupportedException ();
167                 }
168         }
169
170         // Return in subblock.
171         public static void test13 (int a, out float f)
172         {
173                 do {
174                         if (a == 8) {
175                                 f = 8.5F;
176                                 return;
177                         }
178                 } while (false);
179
180                 f = 1.3F;
181                 return;
182         }
183
184         // Switch block with goto case / goto default.
185         public static long test14 (int a, out float f)
186         {
187                 long b;
188
189                 switch (a) {
190                 case 1:
191                         goto case 2;
192
193                 case 2:
194                         f = 9.53F;
195                         return 9;
196
197                 case 3:
198                         goto default;
199
200                 default:
201                         b = 10;
202                         break;
203                 }
204
205                 f = 10.0F;
206
207                 return b;
208         }
209
210         // Forward goto, it's ok to set f before the jump.
211         public static int test15 (int b, out float f)
212         {
213                 long a;
214
215                 Console.WriteLine ("Hello World");
216
217                 a = 5;
218                 f = 8.53F;
219
220                 goto World;
221
222         World:
223                 Console.WriteLine (a);
224
225                 return 0;
226         }
227
228         // `continue' breaks unless we're a loop block.
229         public static void test16 ()
230         {
231                 int value;
232
233                 for (int i = 0; i < 5; ++i) {
234                         if (i == 0) {
235                                 continue;
236                         } else if (i == 1) {
237                                 value = 2;
238                         } else {
239                                 value = 0;
240                         }
241                         if (value > 0)
242                                 return;
243                 }
244         }
245
246         // `continue' in a nested if.
247         public static void test17 ()
248         {
249                  int value;
250                  long charCount = 9;
251                  long testit = 5;
252
253                  while (charCount > 0) {
254                          --charCount;
255
256                          if (testit == 8) {
257                                  if (testit == 9)
258                                          throw new Exception ();
259
260                                  continue;
261                          } else {
262                                  value = 0;
263                          }
264
265                          Console.WriteLine (value);
266                  }
267         }
268
269         // `out' parameter assigned after conditional exception.
270         static void test18 (int a, out int f)
271         {
272                 try {
273                         if (a == 5)
274                                 throw new Exception ();
275
276                         f = 9;
277                 } catch (IndexOutOfRangeException) {
278                         throw new FormatException ();
279                 }
280         }
281
282         // code after try/catch block is unreachable. always returns.
283         static int test19 () {
284                 int res;
285                 int a = Environment.NewLine.Length;
286                 int fin = 0;
287
288                 try {
289                         res = 10/a;
290                         throw new NotImplementedException ();
291                 } catch (NotImplementedException e) {
292                         fin = 2;
293                         throw new NotImplementedException ();
294                 } finally {
295                         fin = 1;
296                 }
297                 return res;
298         }
299
300         // from bug #30487.
301         static int test20 () {
302                 try {
303                         return 0;
304                 }
305                 catch (Exception) {
306                         throw;
307                 }
308         }
309
310         // from bug #31546
311         static int test21 () {
312                 int res;
313
314                 try {
315                         res = 4;
316                         return 3;
317                 } catch (DivideByZeroException) {
318                         res = 33;
319                 } finally {
320                         // Do nothing
321                 }
322
323                 return res;
324         }
325
326         // the same, but without the finally block.
327         static int test22 () {
328                 int res;
329
330                 try {
331                         res = 4;
332                         return 3;
333                 } catch (DivideByZeroException) {
334                         res = 33;
335                 }
336
337                 return res;
338         }
339
340         static int test23 (object obj, int a, out bool test) {
341                 if (obj == null)
342                         throw new ArgumentNullException ();
343
344                 if (a == 5) {
345                         test = false;
346                         return 4;
347                 } else {
348                         test = true;
349                         return 5;
350                 }
351         }
352
353         static long test24 (int a) {
354                 long b;
355
356                 switch (a) {
357                 case 0:
358                         return 4;
359                 }
360
361                 if (a > 2) {
362                         if (a == 5)
363                                 b = 4;
364                         else if (a == 6)
365                                 b = 5;
366                         else
367                                 return 7;
368
369                         Console.WriteLine (b);
370                         return b;
371                 }
372
373                 return 4;
374         }
375
376         static long test25 (int a) {
377                 long b, c;
378
379                 try {
380                         b = 5;
381                 } catch (NotSupportedException) {
382                         throw new InvalidOperationException ();
383                 }
384
385                 try {
386                         c = 5;
387                 } catch {
388                         throw new InvalidOperationException ();
389                 }
390
391                 return b + c;
392         }
393
394         //
395         // Tests that the flow analysis is preformed first in the for statement
396         // and later on the `increment' part of the for
397         //
398         static void test26 ()
399         {
400                 int j;
401                 for( int i=0; i<10; i=j ) 
402                         j = i+1;
403         }
404
405         //
406         // Nested infinite loops.  Bug #40670.
407         //
408         static int test27 ()
409         {
410                 while (true) {
411                         break;
412
413                         while (true)
414                                 Console.WriteLine ("Test");
415                 }
416
417                 return 0;
418         }
419
420         //
421         // Bug #41657.
422         //
423         static void test28 (out object value)
424         {
425                 if (true) {
426                         try {
427                                 value = null;
428                                 return;
429                         } catch {
430                         }
431                 }
432                 value = null;
433         }
434
435         //
436         // Bug #47095
437         //
438         static bool method (out int a)
439         {
440                 try {
441                         a = 0;
442                         return true;
443                 } catch (System.Exception) {
444                         a = -1;
445                         return false;
446                 }
447         }
448 }