[interp] disable assemblyresolve_event6.exe
[mono.git] / mono / tests / delegate.cs
1 using System;
2 using System.Runtime.InteropServices;
3
4 class A {
5         public static bool b_cctor_run = false;
6 }
7
8 class B {
9         static B () {
10                 A.b_cctor_run = true;
11         }
12         public static void method () {
13         }
14 }
15
16 delegate void DoIt ();
17
18 namespace Bah {
19 class Tests {
20         [DllImport("cygwin1.dll", EntryPoint="puts", CharSet=CharSet.Ansi)]
21         public static extern int puts (string name);
22
23         delegate void SimpleDelegate ();
24         delegate string NotSimpleDelegate (int a);
25         delegate int AnotherDelegate (string s);
26
27         delegate string StringDelegate (); 
28         
29         public int data;
30         
31         static void F () {
32                 Console.WriteLine ("Test.F from delegate");
33         }
34         public static string G (int a) {
35                 if (a != 2)
36                         throw new Exception ("Something went wrong in G");
37                 return "G got: " + a.ToString ();
38         }
39         public string H (int a) {
40                 if (a != 3)
41                         throw new Exception ("Something went wrong in H");
42                 return "H got: " + a.ToString () + " and " + data.ToString ();
43         }
44
45         public virtual void VF () {
46                 Console.WriteLine ("Test.VF from delegate");
47         }
48         
49         public Tests () {
50                 data = 5;
51         }
52
53         static int Main (String[] args) {
54                 return TestDriver.RunTests (typeof (Tests), args);
55         }
56
57         public static int test_0_tests () {
58                 // Check that creation of delegates do not runs the class cctor
59                 DoIt doit = new DoIt (B.method);
60                 if (A.b_cctor_run)
61                         return 1;
62
63                 Tests test = new Tests ();
64                 SimpleDelegate d = new SimpleDelegate (F);
65                 SimpleDelegate d1 = new SimpleDelegate (test.VF);
66                 NotSimpleDelegate d2 = new NotSimpleDelegate (G);
67                 NotSimpleDelegate d3 = new NotSimpleDelegate (test.H);
68                 d ();
69                 d1 ();
70                 // we run G() and H() before and after using them as delegates
71                 // to be sure we don't corrupt them.
72                 G (2);
73                 test.H (3);
74                 Console.WriteLine (d2 (2));
75                 Console.WriteLine (d3 (3));
76                 G (2);
77                 test.H (3);
78
79                 if (d.Method.Name != "F")
80                         return 1;
81
82                 if (d3.Method == null)
83                         return 1;
84                 
85                 object [] args = {3};
86                 Console.WriteLine (d3.DynamicInvoke (args));
87
88                 AnotherDelegate d4 = new AnotherDelegate (puts);
89                 if (d4.Method == null)
90                         return 1;
91
92                 Console.WriteLine (d4.Method);
93                 Console.WriteLine (d4.Method.Name);
94                 Console.WriteLine (d4.Method.DeclaringType);
95                 
96                 return 0;
97         }
98
99         public static int test_0_unbox_this () {
100                 int x = 10;
101                 StringDelegate d5 = new StringDelegate (x.ToString);
102                 return d5 () == "10" ? 0 : 1;
103         }
104
105         delegate long LongDelegate (long l);
106
107         static long long_delegate (long l) {
108                 return l + 1;
109         }
110
111         public static int test_56_long () {
112                 LongDelegate l = new LongDelegate (long_delegate);
113
114                 return (int)l (55);
115         }
116
117         delegate float FloatDelegate (float l);
118
119         static float float_delegate (float l) {
120                 return l + 1;
121         }
122
123         public static int test_56_float () {
124                 FloatDelegate l = new FloatDelegate (float_delegate);
125
126                 return (int)l (55);
127         }
128
129         delegate double DoubleDelegate (double l);
130
131         static double double_delegate (double l) {
132                 return l + 1;
133         }
134
135         public static int test_56_double () {
136                 DoubleDelegate l = new DoubleDelegate (double_delegate);
137
138                 return (int)l (55);
139         }
140
141         static int count = 0;
142
143         public static void inc_count () {
144                 count ++;
145         }
146
147         public static int test_0_multicast () {
148                 SimpleDelegate d = new SimpleDelegate (inc_count);
149
150                 d += inc_count;
151
152                 d ();
153                 return count == 2 ? 0 : 1;
154         }
155
156         public delegate int Delegate0 ();
157
158         public delegate int Delegate1 (int i);
159
160         public delegate int Delegate2 (int i, int j);
161
162         public int int_field;
163
164         public int adder0 () {
165                 return int_field;
166         }
167
168         public static int adder0_static () {
169                 return 1;
170         }
171
172         public int adder1 (int i) {
173                 return int_field + i;
174         }
175
176         public static int adder1_static (int i) {
177                 return i;
178         }
179
180         public int adder2 (int i, int j) {
181                 return int_field + i + j;
182         }
183
184         public static int adder2_static (int i, int j) {
185                 return i + j;
186         }
187
188         public static int test_0_delegate_opt () {
189                 Tests d = new Tests ();
190                 d.int_field = 1;
191
192                 if (new Delegate0 (d.adder0) () != 1)
193                         return 1;
194
195                 if (new Delegate1 (d.adder1) (2) != 3)
196                         return 2;
197
198                 if (new Delegate2 (d.adder2) (2, 3) != 6)
199                         return 3;
200
201                 if (new Delegate0 (adder0_static) () != 1)
202                         return 4;
203
204                 if (new Delegate1 (adder1_static) (2) != 2)
205                         return 5;
206
207                 if (new Delegate2 (adder2_static) (2, 3) != 5)
208                         return 6;
209
210                 return 0;
211         }
212 }
213 }