2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / tests / gtest-anon-24.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 Test
18 {
19         static Func<T[]> For<T> (List<T> list)
20         {
21                 T [] t = new T [2];
22                 return () => {
23                         for (int i = 0; i < t.Length; ++i) {
24                                 t [i] = list [i];
25                         }
26                         
27                         return t;
28                 };
29         }
30         
31         static Func<T> Throw<T> (T t)
32         {
33                 T l = t;
34                 return () => {
35                         throw new ApplicationException (l.ToString ());
36                 };
37         }
38         
39         static Func<Type> TypeOf<T> (T t)
40         {
41                 T l = t;
42                 return () => {
43                         l = t;
44                         var e = typeof (Disposable <T>);
45                         e = typeof (Disposable <>);
46                         e = typeof (IFoo <,>);
47                         return typeof (T);
48                 };
49         }
50         
51         static Func<T> Do<T> (T t)
52         {
53                 T l = t;
54                 return () => {
55                         T t2;
56                         do {
57                                 t2 = l;
58                         } while (default (T) != null);
59                         
60                         return t2;
61                 };
62         }
63         
64         static Func<T> Lock<T> (T t)
65         {
66                 T l = t;
67                 return () => {
68                         lock (l.GetType ())
69                         {
70                                 l = default (T);
71                                 return l;
72                         }
73                 };
74         }
75         
76         static Func<T> Catch<T> (T t)
77         {
78                 T l = t;
79                 return () => {
80                         try {
81                                 throw new ApplicationException (l.ToString ());
82                         } catch {
83                                 return l;
84                         }
85                 };
86         }
87         
88         static Func<T> Finally<T> (T t)
89         {
90                 T l = t;
91                 return () => {
92                         try {
93                                 l = Lock (l)();
94                         } finally {
95                                 l = default (T);
96                         }
97                         
98                         return l;
99                 };
100         }
101         
102         static Func<T> Using<T> (T t)
103         {
104                 T l = t;
105                 using (var d = new Disposable<T> ())
106                 {
107                         return () => {
108                                 return l;
109                         };
110                 }
111         }
112         
113         static Func<T> Switch<T> (T t)
114         {
115                 T l = t;
116                 int? i = 0;
117                 return () => {
118                         switch (i) {
119                                 default: return l;
120                         }
121                 };
122         }
123         
124         static Func<List<T>> ForForeach<T> (T[] t)
125         {
126                 return () => {
127                         foreach (T e in t)
128                                 return new List<T> () { e };
129                         
130                         throw new ApplicationException ();
131                 };
132         }
133         
134         public void ArrayMutate<T> (T[] array)
135         {
136                 int r = 4;
137                 Action<int> anonMeth = delegate (int slc) {
138                         long[] idx = new long[] { 0, 0 };
139                         for (int i = 0; i < r; i++) {
140                                 idx [0] = i;
141                         }
142                 };
143         }
144         
145         static Func<T[][]> ArrayMultiMutate<T> (T[][] array)
146         {
147                 return () => {
148                         for (int i = 0; i < 3; i++) {
149                                 array [i][i] = default (T);
150                         }
151                         
152                         return array;
153                 };
154         }
155         
156         public static int Main ()
157         {
158                 if (For (new List<int> { 5, 10 })() [1] != 10)
159                         return 1;
160                 
161                 var t = Throw (5);
162                 try {
163                         t ();
164                         return 2;
165                 } catch (ApplicationException)
166                 {
167                 }
168                 
169                 var t3 = Do ("rr");
170                 if (t3 () != "rr")
171                         return 3;
172
173                 var t4 = Lock ('f');
174                 if (t4 () != '\0')
175                         return 4;
176                 
177                 var t5 = Catch (3);
178                 if (t5 () != 3)
179                         return 5;
180
181                 var t6 = Finally (5);
182                 if (t6 () != 0)
183                         return 6;
184                 
185                 var t7 = Using (1.1);
186                 if (t7 () != 1.1)
187                         return 7;
188                 
189                 var t8 = Switch (55);
190                 if (t8 () != 55)
191                         return 8;
192                 
193                 var t9 = ForForeach (new [] { 4, 1 });
194                 if (t9 ()[0] != 4)
195                         return 9;
196                         
197                 var t10 = ArrayMultiMutate (new string [][] { new string [] { "a", "b", "c" }, new string [] { "1", "2", "3" }, new string [] { "A", "B", "C" }});
198                 if (t10 () [2] [2] != null)
199                         return 10;
200                 
201                 var t11 = TypeOf ("b");
202                 if (t11 () != typeof (string))
203                         return 11;
204
205                 Console.WriteLine ("OK");
206                 return 0;
207         }
208 }