remove executable bit from {errors,tests}/*.cs
[mono.git] / mcs / tests / gtest-anon-24.cs
1 using System;
2 using System.Collections.Generic;
3
4 class Disposable<T> : IDisposable
5 {
6         public void Dispose ()
7         {
8         }
9 }
10
11 class Test
12 {
13         static Func<T[]> For<T> (List<T> list)
14         {
15                 T [] t = new T [2];
16                 return () => {
17                         for (int i = 0; i < t.Length; ++i) {
18                                 t [i] = list [i];
19                         }
20                         
21                         return t;
22                 };
23         }
24         
25         static Func<T> Throw<T> (T t)
26         {
27                 T l = t;
28                 return () => {
29                         throw new ApplicationException (l.ToString ());
30                 };
31         }
32         
33         static Func<T> Do<T> (T t)
34         {
35                 T l = t;
36                 return () => {
37                         T t2;
38                         do {
39                                 t2 = l;
40                         } while (default (T) != null);
41                         
42                         return t2;
43                 };
44         }
45         
46         static Func<T> Lock<T> (T t)
47         {
48                 T l = t;
49                 return () => {
50                         lock (l.GetType ())
51                         {
52                                 l = default (T);
53                                 return l;
54                         }
55                 };
56         }
57         
58         static Func<T> Catch<T> (T t)
59         {
60                 T l = t;
61                 return () => {
62                         try {
63                                 throw new ApplicationException (l.ToString ());
64                         } catch {
65                                 return l;
66                         }
67                 };
68         }
69         
70         static Func<T> Finally<T> (T t)
71         {
72                 T l = t;
73                 return () => {
74                         try {
75                                 l = Lock (l)();
76                         } finally {
77                                 l = default (T);
78                         }
79                         
80                         return l;
81                 };
82         }
83         
84         static Func<T> Using<T> (T t)
85         {
86                 T l = t;
87                 using (var d = new Disposable<T> ())
88                 {
89                         return () => {
90                                 return l;
91                         };
92                 }
93         }
94         
95         static Func<T> Switch<T> (T t)
96         {
97                 T l = t;
98                 int? i = 0;
99                 return () => {
100                         switch (i) {
101                                 default: return l;
102                         }
103                 };
104         }
105         
106         static Func<List<T>> ForForeach<T> (T[] t)
107         {
108                 return () => {
109                         foreach (T e in t)
110                                 return new List<T> () { e };
111                         
112                         throw new ApplicationException ();
113                 };
114         }
115         
116         public static int Main ()
117         {
118                 if (For (new List<int> { 5, 10 })() [1] != 10)
119                         return 1;
120                 
121                 var t = Throw (5);
122                 try {
123                         t ();
124                         return 2;
125                 } catch (ApplicationException)
126                 {
127                 }
128                 
129                 var t3 = Do ("rr");
130                 if (t3 () != "rr")
131                         return 3;
132
133                 var t4 = Lock ('f');
134                 if (t4 () != '\0')
135                         return 4;
136                 
137                 var t5 = Catch (3);
138                 if (t5 () != 3)
139                         return 5;
140
141                 var t6 = Finally (5);
142                 if (t6 () != 0)
143                         return 6;
144                 
145                 var t7 = Using (1.1);
146                 if (t7 () != 1.1)
147                         return 7;
148                 
149                 var t8 = Switch (55);
150                 if (t8 () != 55)
151                         return 8;
152                 
153                 var t9 = ForForeach (new [] { 4, 1 });
154                 if (t9 ()[0] != 4)
155                         return 9;
156                 
157                 Console.WriteLine ("OK");
158                 return 0;
159         }
160 }