cleol
[mcs.git] / tests / gtest-anon-24.cs
blob8c97345554f85b719cde1149a20e0b2572da7504
1 using System;
2 using System.Collections.Generic;
4 // Generics mutate tests
6 class Disposable<T> : IDisposable
8 public void Dispose ()
13 interface IFoo<TOne,TTwo>
17 class Test
19 static Func<T[]> For<T> (List<T> list)
21 T [] t = new T [2];
22 return () => {
23 for (int i = 0; i < t.Length; ++i) {
24 t [i] = list [i];
27 return t;
31 static Func<T> Throw<T> (T t)
33 T l = t;
34 return () => {
35 throw new ApplicationException (l.ToString ());
39 static Func<Type> TypeOf<T> (T t)
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);
51 static Func<T> Do<T> (T t)
53 T l = t;
54 return () => {
55 T t2;
56 do {
57 t2 = l;
58 } while (default (T) != null);
60 return t2;
64 static Func<T> Lock<T> (T t)
66 T l = t;
67 return () => {
68 lock (l.GetType ())
70 l = default (T);
71 return l;
76 static Func<T> Catch<T> (T t)
78 T l = t;
79 return () => {
80 try {
81 throw new ApplicationException (l.ToString ());
82 } catch {
83 return l;
88 static Func<T> Finally<T> (T t)
90 T l = t;
91 return () => {
92 try {
93 l = Lock (l)();
94 } finally {
95 l = default (T);
98 return l;
102 static Func<T> Using<T> (T t)
104 T l = t;
105 using (var d = new Disposable<T> ())
107 return () => {
108 return l;
113 static Func<T> Switch<T> (T t)
115 T l = t;
116 int? i = 0;
117 return () => {
118 switch (i) {
119 default: return l;
124 static Func<List<T>> ForForeach<T> (T[] t)
126 return () => {
127 foreach (T e in t)
128 return new List<T> () { e };
130 throw new ApplicationException ();
134 public void ArrayMutate<T> (T[] array)
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;
145 static Func<T[][]> ArrayMultiMutate<T> (T[][] array)
147 return () => {
148 for (int i = 0; i < 3; i++) {
149 array [i][i] = default (T);
152 return array;
156 public static int Main ()
158 if (For (new List<int> { 5, 10 })() [1] != 10)
159 return 1;
161 var t = Throw (5);
162 try {
163 t ();
164 return 2;
165 } catch (ApplicationException)
169 var t3 = Do ("rr");
170 if (t3 () != "rr")
171 return 3;
173 var t4 = Lock ('f');
174 if (t4 () != '\0')
175 return 4;
177 var t5 = Catch (3);
178 if (t5 () != 3)
179 return 5;
181 var t6 = Finally (5);
182 if (t6 () != 0)
183 return 6;
185 var t7 = Using (1.1);
186 if (t7 () != 1.1)
187 return 7;
189 var t8 = Switch (55);
190 if (t8 () != 55)
191 return 8;
193 var t9 = ForForeach (new [] { 4, 1 });
194 if (t9 ()[0] != 4)
195 return 9;
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;
201 var t11 = TypeOf ("b");
202 if (t11 () != typeof (string))
203 return 11;
205 Console.WriteLine ("OK");
206 return 0;