Merge pull request #665 from andreas-auerswald/master
[mono.git] / mcs / tests / test-anon-124.cs
1 using System;
2 using System.Collections.Generic;
3
4 // Generics mutate tests
5
6 class Disposable<T> : IDisposable
7 {
8         public void Dispose ()
9         {
10         }
11 }
12
13 interface IFoo<TOne, TTwo>
14 {
15 }
16
17 class CA<T>
18 {
19         public struct Nested
20         {
21                 public static readonly T Value;
22                 public readonly T Value2;
23         }
24 }
25
26 class Test
27 {
28         static Func<T[]> For<T> (List<T> list)
29         {
30                 T[] t = new T[2];
31                 return () => {
32                         for (int i = 0; i < t.Length; ++i) {
33                                 t[i] = list[i];
34                         }
35
36                         return t;
37                 };
38         }
39
40         static Func<T> Throw<T> (T t)
41         {
42                 T l = t;
43                 return () => {
44                         throw new ApplicationException (l.ToString ());
45                 };
46         }
47
48         static Func<Type> TypeOf<T> (T t)
49         {
50                 T l = t;
51                 return () => {
52                         l = t;
53                         var e = typeof (Disposable<T>);
54                         e = typeof (Disposable<>);
55                         e = typeof (IFoo<,>);
56                         return typeof (T);
57                 };
58         }
59
60         static Func<T> Do<T> (T t)
61         {
62                 T l = t;
63                 return () => {
64                         T t2;
65                         do {
66                                 t2 = l;
67                         } while (default (T) != null);
68
69                         return t2;
70                 };
71         }
72
73         static Func<T> Lock<T> (T t)
74         {
75                 T l = t;
76                 return () => {
77                         lock (l.GetType ()) {
78                                 l = default (T);
79                                 return l;
80                         }
81                 };
82         }
83
84         static Func<T> Catch<T> (T t)
85         {
86                 T l = t;
87                 return () => {
88                         try {
89                                 throw new ApplicationException (l.ToString ());
90                         } catch {
91                                 return l;
92                         }
93                 };
94         }
95
96         static Func<T> Catch_2<T> (T t) where T : Exception
97         {
98                 T l = t;
99                 return () => {
100                         try {
101                                 throw new NotSupportedException ();
102                         } catch (T e) {
103                                 return l;
104                         } catch {
105                                 throw new ApplicationException ("Should not be reached");
106                         }
107                 };
108         }
109
110         static Func<T> Finally<T> (T t)
111         {
112                 T l = t;
113                 return () => {
114                         try {
115                                 l = Lock (l) ();
116                         } finally {
117                                 l = default (T);
118                         }
119
120                         return l;
121                 };
122         }
123
124         static Func<T> Using<T> (T t)
125         {
126                 T l = t;
127                 using (var d = new Disposable<T> ()) {
128                         return () => {
129                                 return l;
130                         };
131                 }
132         }
133
134         static Func<T> Switch<T> (T t)
135         {
136                 T l = t;
137                 int? i = 0;
138                 return () => {
139                         switch (i) {
140                         default: return l;
141                         }
142                 };
143         }
144
145         static Func<List<T>> ForForeach<T> (T[] t)
146         {
147                 return () => {
148                         foreach (T e in t)
149                                 return new List<T> () { e };
150
151                         throw new ApplicationException ();
152                 };
153         }
154
155         public void ArrayMutate<T> (T[] array)
156         {
157                 int r = 4;
158                 Action<int> anonMeth = delegate (int slc) {
159                         long[] idx = new long[] { 0, 0 };
160                         for (int i = 0; i < r; i++) {
161                                 idx[0] = i;
162                         }
163                 };
164         }
165
166         static Func<T[][]> ArrayMultiMutate<T> (T[][] array)
167         {
168                 return () => {
169                         for (int i = 0; i < 3; i++) {
170                                 array[i][i] = default (T);
171                         }
172
173                         return array;
174                 };
175         }
176
177         static Func<int> ArrayMultiMutate<T> (T[,] array)
178         {
179                 return () => {
180                         return array[0, 0].GetHashCode ();
181                 };
182         }
183
184         static Func<T[]> NestedTypeMutate<T> () where T : IEquatable<T>
185         {
186                 var local = new CA<T>.Nested ();
187                 return () => {
188                         return new[] { CA<T>.Nested.Value, local.Value2 };
189                 };
190         }
191
192         public static int Main ()
193         {
194                 if (For (new List<int> { 5, 10 }) ()[1] != 10)
195                         return 1;
196
197                 var t = Throw (5);
198                 try {
199                         t ();
200                         return 2;
201                 } catch (ApplicationException) {
202                 }
203
204                 var t3 = Do ("rr");
205                 if (t3 () != "rr")
206                         return 3;
207
208                 var t4 = Lock ('f');
209                 if (t4 () != '\0')
210                         return 4;
211
212                 var t5 = Catch (3);
213                 if (t5 () != 3)
214                         return 5;
215
216                 var ex = new NotSupportedException ();
217                 var t5_2 = Catch_2 (ex);
218                 if (t5_2 () != ex)
219                         return 52;
220
221                 var t6 = Finally (5);
222                 if (t6 () != 0)
223                         return 6;
224
225                 var t7 = Using (1.1);
226                 if (t7 () != 1.1)
227                         return 7;
228
229                 var t8 = Switch (55);
230                 if (t8 () != 55)
231                         return 8;
232
233                 var t9 = ForForeach (new[] { 4, 1 });
234                 if (t9 ()[0] != 4)
235                         return 9;
236
237                 var t10 = ArrayMultiMutate (new string[][] { new string[] { "a", "b", "c" }, new string[] { "1", "2", "3" }, new string[] { "A", "B", "C" } });
238                 if (t10 ()[2][2] != null)
239                         return 10;
240
241                 var array = new short[,] { { 10, 20 } };
242                 var t10a = ArrayMultiMutate (array);
243                 if (t10a () != array[0, 0].GetHashCode ())
244                         return 100;
245
246                 var t11 = TypeOf ("b");
247                 if (t11 () != typeof (string))
248                         return 11;
249
250                 var t12 = NestedTypeMutate<ulong> () ();
251                 if (t12[0] != 0 || t12[1] != 0)
252                         return 12;
253
254                 Console.WriteLine ("OK");
255                 return 0;
256         }
257 }