Fix bug #566087.
[mcs.git] / tests / gtest-anon-24.cs
blobfe5d056788cc62673d0500d92632286e5a76b08d
1 using System;
2 using System.Collections.Generic;
4 // Generics mutate tests
6 class Disposable<T> : IDisposable
8 public void Dispose ()
13 class Test
15 static Func<T[]> For<T> (List<T> list)
17 T [] t = new T [2];
18 return () => {
19 for (int i = 0; i < t.Length; ++i) {
20 t [i] = list [i];
23 return t;
27 static Func<T> Throw<T> (T t)
29 T l = t;
30 return () => {
31 throw new ApplicationException (l.ToString ());
35 static Func<T> Do<T> (T t)
37 T l = t;
38 return () => {
39 T t2;
40 do {
41 t2 = l;
42 } while (default (T) != null);
44 return t2;
48 static Func<T> Lock<T> (T t)
50 T l = t;
51 return () => {
52 lock (l.GetType ())
54 l = default (T);
55 return l;
60 static Func<T> Catch<T> (T t)
62 T l = t;
63 return () => {
64 try {
65 throw new ApplicationException (l.ToString ());
66 } catch {
67 return l;
72 static Func<T> Finally<T> (T t)
74 T l = t;
75 return () => {
76 try {
77 l = Lock (l)();
78 } finally {
79 l = default (T);
82 return l;
86 static Func<T> Using<T> (T t)
88 T l = t;
89 using (var d = new Disposable<T> ())
91 return () => {
92 return l;
97 static Func<T> Switch<T> (T t)
99 T l = t;
100 int? i = 0;
101 return () => {
102 switch (i) {
103 default: return l;
108 static Func<List<T>> ForForeach<T> (T[] t)
110 return () => {
111 foreach (T e in t)
112 return new List<T> () { e };
114 throw new ApplicationException ();
118 public void ArrayMutate<T> (T[] array)
120 int r = 4;
121 Action<int> anonMeth = delegate (int slc) {
122 long[] idx = new long[] { 0, 0 };
123 for (int i = 0; i < r; i++) {
124 idx [0] = i;
129 static Func<T[][]> ArrayMultiMutate<T> (T[][] array)
131 return () => {
132 for (int i = 0; i < 3; i++) {
133 array [i][i] = default (T);
136 return array;
140 public static int Main ()
142 if (For (new List<int> { 5, 10 })() [1] != 10)
143 return 1;
145 var t = Throw (5);
146 try {
147 t ();
148 return 2;
149 } catch (ApplicationException)
153 var t3 = Do ("rr");
154 if (t3 () != "rr")
155 return 3;
157 var t4 = Lock ('f');
158 if (t4 () != '\0')
159 return 4;
161 var t5 = Catch (3);
162 if (t5 () != 3)
163 return 5;
165 var t6 = Finally (5);
166 if (t6 () != 0)
167 return 6;
169 var t7 = Using (1.1);
170 if (t7 () != 1.1)
171 return 7;
173 var t8 = Switch (55);
174 if (t8 () != 55)
175 return 8;
177 var t9 = ForForeach (new [] { 4, 1 });
178 if (t9 ()[0] != 4)
179 return 9;
181 var t10 = ArrayMultiMutate (new string [][] { new string [] { "a", "b", "c" }, new string [] { "1", "2", "3" }, new string [] { "A", "B", "C" }});
182 if (t10 () [2] [2] != null)
183 return 10;
185 Console.WriteLine ("OK");
186 return 0;