2010-05-31 Jb Evain <jbevain@novell.com>
[mcs.git] / tests / test-154.cs
blob8098cfd5bcabaa322a1a92d3491e5722859ee6c7
1 using System;
2 using System.Collections;
4 public class X
6 public static int Main ()
8 // This is a compilation-only test.
9 return 0;
12 // All code paths throw an exception, no need to set out parameters.
13 public static void test1 (out float f)
15 throw new NotSupportedException ();
18 // The while loop breaks but does not return, so this is ok.
19 public static void test2 (int a, out float f)
21 while (a > 0) {
22 if (a == 5)
23 continue;
25 Console.WriteLine (a);
28 f = 8.53F;
31 // a has been assigned in all code paths which do not return.
32 public static void test3 (long[] b, int c)
34 ICollection a;
35 if (b == null)
36 throw new ArgumentException ();
37 else
38 a = (ICollection) b;
40 Console.WriteLine (a);
43 // Forward goto, it's ok to set f after the target label.
44 public static int test4 (int b, out float f)
46 long a;
48 Console.WriteLine ("Hello World");
50 a = 5;
52 goto World;
54 World:
55 Console.WriteLine (a);
57 f = 8.53F;
59 return 0;
62 // try { ... } catch { ... } finally { ... } block
63 static public int test5 (out float f, long d)
65 int a;
66 long b = 8;
68 try {
69 f = 8.53F;
71 if (d == 500)
72 return 9;
74 a = 5;
75 } catch (NotSupportedException e) {
76 a = 9;
77 } catch (Exception e) {
78 return 9;
79 } finally {
80 f = 9.234F;
83 return a;
86 // Passing out parameter to method invocation
87 static public int test6 (out float f)
89 return test5 (out f, 50);
92 // Loop-variable of foreach() and for() loop.
93 static public long test7 (int[] a, int stop)
95 long b = 0;
96 foreach (int i in a)
97 b += i;
99 for (int i = 1; i < stop; i++)
100 b *= i;
102 return b;
105 // Initializing locals in initialize or test of for block
106 static public long test8 (int stop)
108 int i;
109 long b;
110 for (i = 1; (b = stop) > 3; i++) {
111 stop--;
112 b += i;
114 return b;
117 // Initializing locals in test of while block
118 static public long test9 (int stop)
120 long b;
121 while ((b = stop) > 3) {
122 stop--;
123 b += stop;
125 return b;
128 // Return in subblock
129 public static void test10 (int a, out float f)
131 if (a == 5) {
132 f = 8.53F;
133 return;
136 f = 9.0F;
139 // Switch block
140 public static long test11 (int a)
142 long b;
144 switch (a) {
145 case 5:
146 b = 1;
147 break;
149 case 9:
150 b = 3;
151 break;
153 default:
154 return 9;
157 return b;
160 // Try block which rethrows exception.
161 public static void test12 (out float f)
163 try {
164 f = 9.0F;
165 } catch {
166 throw new NotSupportedException ();
170 // Return in subblock.
171 public static void test13 (int a, out float f)
173 do {
174 if (a == 8) {
175 f = 8.5F;
176 return;
178 } while (false);
180 f = 1.3F;
181 return;
184 // Switch block with goto case / goto default.
185 public static long test14 (int a, out float f)
187 long b;
189 switch (a) {
190 case 1:
191 goto case 2;
193 case 2:
194 f = 9.53F;
195 return 9;
197 case 3:
198 goto default;
200 default:
201 b = 10;
202 break;
205 f = 10.0F;
207 return b;
210 // Forward goto, it's ok to set f before the jump.
211 public static int test15 (int b, out float f)
213 long a;
215 Console.WriteLine ("Hello World");
217 a = 5;
218 f = 8.53F;
220 goto World;
222 World:
223 Console.WriteLine (a);
225 return 0;
228 // `continue' breaks unless we're a loop block.
229 public static void test16 ()
231 int value;
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;
241 if (value > 0)
242 return;
246 // `continue' in a nested if.
247 public static void test17 ()
249 int value;
250 long charCount = 9;
251 long testit = 5;
253 while (charCount > 0) {
254 --charCount;
256 if (testit == 8) {
257 if (testit == 9)
258 throw new Exception ();
260 continue;
261 } else {
262 value = 0;
265 Console.WriteLine (value);
269 // `out' parameter assigned after conditional exception.
270 static void test18 (int a, out int f)
272 try {
273 if (a == 5)
274 throw new Exception ();
276 f = 9;
277 } catch (IndexOutOfRangeException) {
278 throw new FormatException ();
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;
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;
297 return fin;
300 // from bug #30487.
301 static int test20 () {
302 try {
303 return 0;
305 catch (Exception) {
306 throw;
310 // from bug #31546
311 static int test21 () {
312 int res;
314 try {
315 res = 4;
316 return 3;
317 } catch (DivideByZeroException) {
318 res = 33;
319 } finally {
320 // Do nothing
323 return res;
326 // the same, but without the finally block.
327 static int test22 () {
328 int res;
330 try {
331 res = 4;
332 return 3;
333 } catch (DivideByZeroException) {
334 res = 33;
337 return res;
340 static int test23 (object obj, int a, out bool test) {
341 if (obj == null)
342 throw new ArgumentNullException ();
344 if (a == 5) {
345 test = false;
346 return 4;
347 } else {
348 test = true;
349 return 5;
353 static long test24 (int a) {
354 long b;
356 switch (a) {
357 case 0:
358 return 4;
361 if (a > 2) {
362 if (a == 5)
363 b = 4;
364 else if (a == 6)
365 b = 5;
366 else
367 return 7;
369 Console.WriteLine (b);
370 return b;
373 return 4;
376 static long test25 (int a) {
377 long b, c;
379 try {
380 b = 5;
381 } catch (NotSupportedException) {
382 throw new InvalidOperationException ();
385 try {
386 c = 5;
387 } catch {
388 throw new InvalidOperationException ();
391 return b + c;
395 // Tests that the flow analysis is preformed first in the for statement
396 // and later on the `increment' part of the for
398 static void test26 ()
400 int j;
401 for( int i=0; i<10; i=j )
402 j = i+1;
406 // Nested infinite loops. Bug #40670.
408 static int test27 ()
410 while (true) {
411 break;
413 while (true)
414 Console.WriteLine ("Test");
417 return 0;
421 // Bug #41657.
423 static void test28 (out object value)
425 if (true) {
426 try {
427 value = null;
428 return;
429 } catch {
432 value = null;
436 // Bug #47095
438 static bool test29 (out int a)
440 try {
441 a = 0;
442 return true;
443 } catch (System.Exception) {
444 a = -1;
445 return false;
450 // Bug #46949
452 public string test30 (out string outparam)
454 try {
455 if (true) {
456 outparam = "";
457 return "";
459 } catch {
462 outparam = null;
463 return null;
467 // Bug #49153
469 public string test31 (int blah)
471 switch(blah) {
472 case 1: return("foo"); break;
473 case 2: return("bar"); break;
474 case 3: return("baz"); break;
476 default:
477 throw new ArgumentException ("Value 0x"+blah.ToString ("x4")+" is not supported.");
482 // Bug #49359
484 public void test32 ()
486 while (true) {
487 System.Threading.Thread.Sleep (1);
490 Console.WriteLine ("Hello");
494 // Bug 49602
496 public int test33 ()
498 int i = 0;
499 return 0;
500 if (i == 0)
501 return 0;
505 // Bug 48962
507 public void test34 ()
509 int y, x = 3;
510 if (x > 3) {
511 y = 3;
512 goto end;
514 return;
515 end:
516 x = y;
520 // Bug 46640
522 public static void test35 (int a, bool test)
524 switch (a) {
525 case 3:
526 if (test)
527 break;
528 return;
529 default:
530 return;
535 // Bug 52625
537 public static void test36 ()
539 string myVar;
540 int counter = 0;
542 while (true)
544 if (counter < 3)
545 counter++;
546 else {
547 myVar = "assigned";
548 break;
551 Console.WriteLine (myVar);
555 // Bug 58322
557 public static void test37 ()
559 int x = 0;
560 int y = 0;
561 switch (x) {
562 case 0:
563 switch (y) {
564 case 0:
565 goto k_0;
566 default:
567 throw new Exception ();
571 k_0:
576 // Bug 59429
578 public static int test38 ()
580 return 0;
581 foo:
585 static int test40 (int stop)
587 int service;
589 int pos = 0;
590 do {
591 service = 1;
592 break;
593 } while (pos < stop);
595 return service;