update
[mcs.git] / tests / gtest-anon-24.cs
blob4bc12f7747cf00a7319c839b299017f055efc81c
1 using System;
2 using System.Collections.Generic;
4 class Disposable<T> : IDisposable
6 public void Dispose ()
11 class Test
13 static Func<T[]> For<T> (List<T> list)
15 T [] t = new T [2];
16 return () => {
17 for (int i = 0; i < t.Length; ++i) {
18 t [i] = list [i];
21 return t;
25 static Func<T> Throw<T> (T t)
27 T l = t;
28 return () => {
29 throw new ApplicationException (l.ToString ());
33 static Func<T> Do<T> (T t)
35 T l = t;
36 return () => {
37 T t2;
38 do {
39 t2 = l;
40 } while (default (T) != null);
42 return t2;
46 static Func<T> Lock<T> (T t)
48 T l = t;
49 return () => {
50 lock (l.GetType ())
52 l = default (T);
53 return l;
58 static Func<T> Catch<T> (T t)
60 T l = t;
61 return () => {
62 try {
63 throw new ApplicationException (l.ToString ());
64 } catch {
65 return l;
70 static Func<T> Finally<T> (T t)
72 T l = t;
73 return () => {
74 try {
75 l = Lock (l)();
76 } finally {
77 l = default (T);
80 return l;
84 static Func<T> Using<T> (T t)
86 T l = t;
87 using (var d = new Disposable<T> ())
89 return () => {
90 return l;
95 static Func<T> Switch<T> (T t)
97 T l = t;
98 int? i = 0;
99 return () => {
100 switch (i) {
101 default: return l;
106 static Func<List<T>> ForForeach<T> (T[] t)
108 return () => {
109 foreach (T e in t)
110 return new List<T> () { e };
112 throw new ApplicationException ();
116 public static int Main ()
118 if (For (new List<int> { 5, 10 })() [1] != 10)
119 return 1;
121 var t = Throw (5);
122 try {
123 t ();
124 return 2;
125 } catch (ApplicationException)
129 var t3 = Do ("rr");
130 if (t3 () != "rr")
131 return 3;
133 var t4 = Lock ('f');
134 if (t4 () != '\0')
135 return 4;
137 var t5 = Catch (3);
138 if (t5 () != 3)
139 return 5;
141 var t6 = Finally (5);
142 if (t6 () != 0)
143 return 6;
145 var t7 = Using (1.1);
146 if (t7 () != 1.1)
147 return 7;
149 var t8 = Switch (55);
150 if (t8 () != 55)
151 return 8;
153 var t9 = ForForeach (new [] { 4, 1 });
154 if (t9 ()[0] != 4)
155 return 9;
157 Console.WriteLine ("OK");
158 return 0;