[xbuild] Actually delete common files (CopyLocal) during Clean.
[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> Finally<T> (T t)
97         {
98                 T l = t;
99                 return () => {
100                         try {
101                                 l = Lock (l) ();
102                         } finally {
103                                 l = default (T);
104                         }
105
106                         return l;
107                 };
108         }
109
110         static Func<T> Using<T> (T t)
111         {
112                 T l = t;
113                 using (var d = new Disposable<T> ()) {
114                         return () => {
115                                 return l;
116                         };
117                 }
118         }
119
120         static Func<T> Switch<T> (T t)
121         {
122                 T l = t;
123                 int? i = 0;
124                 return () => {
125                         switch (i) {
126                         default: return l;
127                         }
128                 };
129         }
130
131         static Func<List<T>> ForForeach<T> (T[] t)
132         {
133                 return () => {
134                         foreach (T e in t)
135                                 return new List<T> () { e };
136
137                         throw new ApplicationException ();
138                 };
139         }
140
141         public void ArrayMutate<T> (T[] array)
142         {
143                 int r = 4;
144                 Action<int> anonMeth = delegate (int slc) {
145                         long[] idx = new long[] { 0, 0 };
146                         for (int i = 0; i < r; i++) {
147                                 idx[0] = i;
148                         }
149                 };
150         }
151
152         static Func<T[][]> ArrayMultiMutate<T> (T[][] array)
153         {
154                 return () => {
155                         for (int i = 0; i < 3; i++) {
156                                 array[i][i] = default (T);
157                         }
158
159                         return array;
160                 };
161         }
162
163         static Func<int> ArrayMultiMutate<T> (T[,] array)
164         {
165                 return () => {
166                         return array[0, 0].GetHashCode ();
167                 };
168         }
169
170         static Func<T[]> NestedTypeMutate<T> () where T : IEquatable<T>
171         {
172                 var local = new CA<T>.Nested ();
173                 return () => {
174                         return new[] { CA<T>.Nested.Value, local.Value2 };
175                 };
176         }
177
178         public static int Main ()
179         {
180                 if (For (new List<int> { 5, 10 }) ()[1] != 10)
181                         return 1;
182
183                 var t = Throw (5);
184                 try {
185                         t ();
186                         return 2;
187                 } catch (ApplicationException) {
188                 }
189
190                 var t3 = Do ("rr");
191                 if (t3 () != "rr")
192                         return 3;
193
194                 var t4 = Lock ('f');
195                 if (t4 () != '\0')
196                         return 4;
197
198                 var t5 = Catch (3);
199                 if (t5 () != 3)
200                         return 5;
201
202                 var t6 = Finally (5);
203                 if (t6 () != 0)
204                         return 6;
205
206                 var t7 = Using (1.1);
207                 if (t7 () != 1.1)
208                         return 7;
209
210                 var t8 = Switch (55);
211                 if (t8 () != 55)
212                         return 8;
213
214                 var t9 = ForForeach (new[] { 4, 1 });
215                 if (t9 ()[0] != 4)
216                         return 9;
217
218                 var t10 = ArrayMultiMutate (new string[][] { new string[] { "a", "b", "c" }, new string[] { "1", "2", "3" }, new string[] { "A", "B", "C" } });
219                 if (t10 ()[2][2] != null)
220                         return 10;
221
222                 var array = new short[,] { { 10, 20 } };
223                 var t10a = ArrayMultiMutate (array);
224                 if (t10a () != array[0, 0].GetHashCode ())
225                         return 100;
226
227                 var t11 = TypeOf ("b");
228                 if (t11 () != typeof (string))
229                         return 11;
230
231                 var t12 = NestedTypeMutate<ulong> () ();
232                 if (t12[0] != 0 || t12[1] != 0)
233                         return 12;
234
235                 Console.WriteLine ("OK");
236                 return 0;
237         }
238 }