summaryrefslogtreecommitdiff
path: root/mcs/tests/test-154.cs
blob: 8098cfd5bcabaa322a1a92d3491e5722859ee6c7 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
using System;
using System.Collections;

public class X
{
	public static int Main ()
	{
		// This is a compilation-only test.
		return 0;
	}

	// All code paths throw an exception, no need to set out parameters.
	public static void test1 (out float f)
	{
		throw new NotSupportedException ();
	}

	// The while loop breaks but does not return, so this is ok.
	public static void test2 (int a, out float f)
	{
		while (a > 0) {
			if (a == 5)
				continue;

			Console.WriteLine (a);
		}

		f = 8.53F;
	}

	// a has been assigned in all code paths which do not return.
	public static void test3 (long[] b, int c)
	{
		ICollection a;
		if (b == null)
			throw new ArgumentException ();
		else
			a = (ICollection) b;

		Console.WriteLine (a);
	}

	// Forward goto, it's ok to set f after the target label.
	public static int test4 (int b, out float f)
	{
		long a;

		Console.WriteLine ("Hello World");

		a = 5;

		goto World;

	World:
		Console.WriteLine (a);

		f = 8.53F;

		return 0;
	}

	// try { ... } catch { ... } finally { ... } block
	static public int test5 (out float f, long d)
	{
                int a;
		long b = 8;

		try {
			f = 8.53F;

			if (d == 500)
				return 9;

			a = 5;
		} catch (NotSupportedException e) {
			a = 9;
		} catch (Exception e) {
			return 9;
		} finally {
			f = 9.234F;
		}

		return a;
        }

	// Passing out parameter to method invocation
	static public int test6 (out float f)
	{
		return test5 (out f, 50);
	}

	// Loop-variable of foreach() and for() loop.
	static public long test7 (int[] a, int stop)
	{
		long b = 0;
		foreach (int i in a)
			b += i;

		for (int i = 1; i < stop; i++)
			b *= i;

		return b;
	}

	// Initializing locals in initialize or test of for block
	static public long test8 (int stop)
	{
		int i;
		long b;
		for (i = 1; (b = stop) > 3; i++) {
			stop--;
			b += i;
		}
		return b;
	}

	// Initializing locals in test of while block
	static public long test9 (int stop)
	{
		long b;
		while ((b = stop) > 3) {
			stop--;
			b += stop;
		}
		return b;
	}

	// Return in subblock
	public static void test10 (int a, out float f)
	{
		if (a == 5) {
			f = 8.53F;
			return;
		}

		f = 9.0F;
	}

	// Switch block
	public static long test11 (int a)
	{
		long b;

		switch (a) {
		case 5:
			b = 1;
			break;

		case 9:
			b = 3;
			break;

		default:
			return 9;
		}

		return b;
	}

	// Try block which rethrows exception.
	public static void test12 (out float f)
	{
		try {
			f = 9.0F;
		} catch {
			throw new NotSupportedException ();
		}
	}

	// Return in subblock.
	public static void test13 (int a, out float f)
	{
		do {
			if (a == 8) {
				f = 8.5F;
				return;
			}
		} while (false);

		f = 1.3F;
		return;
	}

	// Switch block with goto case / goto default.
	public static long test14 (int a, out float f)
	{
		long b;

		switch (a) {
		case 1:
			goto case 2;

		case 2:
			f = 9.53F;
			return 9;

		case 3:
			goto default;

		default:
			b = 10;
			break;
		}

		f = 10.0F;

		return b;
	}

	// Forward goto, it's ok to set f before the jump.
	public static int test15 (int b, out float f)
	{
		long a;

		Console.WriteLine ("Hello World");

		a = 5;
		f = 8.53F;

		goto World;

	World:
		Console.WriteLine (a);

		return 0;
	}

	// `continue' breaks unless we're a loop block.
	public static void test16 ()
	{
                int value;

                for (int i = 0; i < 5; ++i) {
                        if (i == 0) {
                                continue;
                        } else if (i == 1) {
                                value = 2;
                        } else {
                                value = 0;
                        }
                        if (value > 0)
                                return;
                }
	}

	// `continue' in a nested if.
	public static void test17 ()
	{
		 int value;
		 long charCount = 9;
		 long testit = 5;

		 while (charCount > 0) {
			 --charCount;

			 if (testit == 8) {
				 if (testit == 9)
					 throw new Exception ();

				 continue;
			 } else {
				 value = 0;
			 }

			 Console.WriteLine (value);
		 }
	}

	// `out' parameter assigned after conditional exception.
	static void test18 (int a, out int f)
	{
		try {
			if (a == 5)
				throw new Exception ();

			f = 9;
		} catch (IndexOutOfRangeException) {
			throw new FormatException ();
		}
	}

	// code after try/catch block is unreachable. always returns.
        static int test19 () {
                int res;
                int a = Environment.NewLine.Length;
                int fin = 0;

                try {
                        res = 10/a;
                        throw new NotImplementedException ();
                } catch (NotImplementedException e) {
                        fin = 2;
                        throw new NotImplementedException ();
                } finally {
                        fin = 1;
                }
                return fin;
        }

	// from bug #30487.
	static int test20 () {
		try {
			return 0;
		}
		catch (Exception) {
			throw;
		}
	}

	// from bug #31546
	static int test21 () {
		int res;

		try {
			res = 4;
			return 3;
		} catch (DivideByZeroException) {
			res = 33;
		} finally {
			// Do nothing
		}

		return res;
	}

	// the same, but without the finally block.
	static int test22 () {
		int res;

		try {
			res = 4;
			return 3;
		} catch (DivideByZeroException) {
			res = 33;
		}

		return res;
	}

	static int test23 (object obj, int a, out bool test) {
		if (obj == null)
			throw new ArgumentNullException ();

		if (a == 5) {
			test = false;
			return 4;
		} else {
			test = true;
			return 5;
		}
	}

	static long test24 (int a) {
		long b;

		switch (a) {
		case 0:
			return 4;
		}

		if (a > 2) {
			if (a == 5)
				b = 4;
			else if (a == 6)
				b = 5;
			else
				return 7;

			Console.WriteLine (b);
			return b;
		}

		return 4;
	}

	static long test25 (int a) {
		long b, c;

		try {
			b = 5;
		} catch (NotSupportedException) {
			throw new InvalidOperationException ();
		}

		try {
			c = 5;
		} catch {
			throw new InvalidOperationException ();
		}

		return b + c;
	}

	//
	// Tests that the flow analysis is preformed first in the for statement
	// and later on the `increment' part of the for
	//
	static void test26 ()
	{
		int j;
		for( int i=0; i<10; i=j ) 
			j = i+1;
	}

	//
	// Nested infinite loops.  Bug #40670.
	//
	static int test27 ()
	{
		while (true) {
			break;

			while (true)
				Console.WriteLine ("Test");
		}

		return 0;
	}

	//
	// Bug #41657.
	//
	static void test28 (out object value)
	{
		if (true) {
			try {
				value = null;
				return;
			} catch {
			}
		}
		value = null;
	}

	//
	// Bug #47095
	//
	static bool test29 (out int a)
	{
		try {
			a = 0;
			return true;
		} catch (System.Exception) {
			a = -1;
			return false;
		}
	}

	//
	// Bug #46949
	//
	public string test30 (out string outparam)
	{
		try {
			if (true) {
				outparam = "";
				return "";
			}
		} catch {
		}

		outparam = null;
		return null;
	}

	//
	// Bug #49153
	//
	public string test31 (int blah)
	{
		switch(blah) {
		case 1: return("foo"); break;
		case 2: return("bar"); break;
		case 3: return("baz"); break;

		default:
			throw new ArgumentException ("Value 0x"+blah.ToString ("x4")+" is not supported.");
		}
	}

	//
	// Bug #49359
	//
        public void test32 ()
	{
                while (true) {
                        System.Threading.Thread.Sleep (1);
                }

                Console.WriteLine ("Hello");
        }

	//
	// Bug 49602
	//
        public int test33 ()
        {
                int i = 0;
                return 0;
                if (i == 0)
                        return 0;
        }

	//
	// Bug 48962
	//
	public void test34 ()
	{
		int y, x = 3;
		if (x > 3) {
			y = 3;
			goto end;
		}
		return;
        end:
		x = y;
	}

	//
	// Bug 46640
	//
	public static void test35 (int a, bool test)
	{
		switch (a) {
		case 3:
			if (test)
				break;
			return;
		default:
			return;
		}
	}

	//
	// Bug 52625
	//
	public static void test36 ()
	{
		string myVar;
		int counter = 0;

		while (true)
		{
			if (counter < 3)
				counter++;
			else {
				myVar = "assigned";
				break;
			}
		}
		Console.WriteLine (myVar);
	}

	//
	// Bug 58322
	//
	public static void test37 ()
	{
		int x = 0;
		int y = 0;
		switch (x) {
		case 0:
			switch (y) {
			case 0:
				goto k_0;
			default:
				throw new Exception ();
			}
		}

	k_0:
		;
	}

	//
	// Bug 59429
	//
	public static int test38 ()
	{
		return 0;
	foo:
		;
	}

	static int test40 (int stop)
	{
		int service;

		int pos = 0;
		do {
			service = 1;
			break;
		} while (pos < stop);

		return service;
	}
}