2002-08-05 Martin Baulig <martin@gnome.org>
[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 }