2009-09-21 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / tests / runtime-invoke.cs
1 using System;
2 using System.Reflection;
3
4 public struct A
5 {
6         public override string ToString ()
7         {
8                 return "A";
9         }
10 }
11
12 public class D
13 {
14         public string Test ()
15         {
16                 return "Test";
17         }
18 }
19
20 enum Enum1
21 {
22         A,
23         B
24 }
25
26 enum Enum2
27 {
28         C,
29         D
30 }
31
32 class Tests
33 {
34         public static Enum1 return_enum1 () {
35                 return Enum1.A;
36         }
37
38         public static Enum2 return_enum2 () {
39                 return Enum2.C;
40         }
41
42         public static long return_long () {
43                 return 1234;
44         }
45
46         public static ulong return_ulong () {
47                 return UInt64.MaxValue - 5;
48         }
49
50         static int Main (string[] args)
51         {
52                 return TestDriver.RunTests (typeof (Tests), args);
53         }
54
55         public static int test_0_base () {
56                 Assembly ass = typeof (Tests).Assembly;
57                 Type a_type = ass.GetType ("A");
58                 MethodInfo a_method = a_type.GetMethod ("ToString");
59
60                 Type d_type = ass.GetType ("D");
61                 MethodInfo d_method = d_type.GetMethod ("Test");
62
63                 Console.WriteLine ("TEST: {0} {1}", a_method, d_method);
64
65                 A a = new A ();
66                 D d = new D ();
67
68                 object a_ret = a_method.Invoke (a, null);
69                 Console.WriteLine (a_ret);
70
71                 object d_ret = d_method.Invoke (d, null);
72                 Console.WriteLine (d_ret);
73
74                 return 0;
75         }
76
77         public static int test_0_enum_sharing () {
78                 /* Check sharing of wrappers returning enums */
79                 if (typeof (Tests).GetMethod ("return_enum1").Invoke (null, null).GetType () != typeof (Enum1))
80                         return 1;
81                 if (typeof (Tests).GetMethod ("return_enum2").Invoke (null, null).GetType () != typeof (Enum2))
82                         return 2;
83                 return 0;
84         }
85
86         public static int test_0_primitive_sharing () {
87                 /* Check sharing of wrappers returning primitive types */
88                 if (typeof (Tests).GetMethod ("return_long").Invoke (null, null).GetType () != typeof (long))
89                         return 3;
90                 if (typeof (Tests).GetMethod ("return_ulong").Invoke (null, null).GetType () != typeof (ulong))
91                         return 4;
92
93                 return 0;
94         }
95
96         public struct Foo
97         {
98                 public string ToString2 () {
99                         return "FOO";
100                 }
101         }
102
103         public static object GetNSObject (IntPtr i) {
104                 return i;
105         }
106
107         public static int test_0_vtype_method_sharing () {
108                 /* Check sharing of wrappers of vtype methods with static methods having an IntPtr argument */
109                 if ((string)(typeof (Foo).GetMethod ("ToString2").Invoke (new Foo (), null)) != "FOO")
110                         return 3;
111                 object o = typeof (Tests).GetMethod ("GetNSObject").Invoke (null, new object [] { new IntPtr (42) });
112                 if (!(o is IntPtr) || ((IntPtr)o != new IntPtr (42)))
113                         return 4;
114
115                 return 0;
116         }
117
118         public static unsafe int test_0_ptr () {
119                 int[] arr = new int [10];
120                 fixed (void *p = &arr [5]) {
121                         object o = typeof (Tests).GetMethod ("data_types_ptr").Invoke (null, new object [1] { new IntPtr (p) });
122                         void *p2 = Pointer.Unbox (o);
123                         if (new IntPtr (p) != new IntPtr (p2))
124                                 return 1;
125
126                         o = typeof (Tests).GetMethod ("data_types_ptr").Invoke (null, new object [1] { null });
127                         p2 = Pointer.Unbox (o);
128                         if (new IntPtr (p2) != IntPtr.Zero)
129                                 return 1;
130                 }
131
132                 return 0;
133         }
134
135         public static int test_42_int () {
136                 return (int)typeof (Tests).GetMethod ("data_types_int").Invoke (null, new object [] { Int32.MinValue, UInt32.MaxValue });
137         }
138
139         public static int test_42_short () {
140                 return (short)typeof (Tests).GetMethod ("data_types_short").Invoke (null, new object [] { short.MinValue, ushort.MaxValue });
141         }
142
143         public static int test_0_bool_char () {
144                 if ((int)typeof (Tests).GetMethod ("data_types_bool_char").Invoke (null, new object [] { true, false, 'A' }) != 0)
145                         return 1;
146                 return 0;
147         }
148
149         public static int test_0_byref_int () {
150                 if ((int)typeof (Tests).GetMethod ("data_types_byref_int").Invoke (null, new object [] { 42 }) != 0)
151                         return 1;
152                 return 0;
153         }
154
155         public static int test_0_long () {
156                 if ((long)typeof (Tests).GetMethod ("data_types_long").Invoke (null, new object [] { 0x123456789L, (ulong)0x123456789L }) == 0x12345678AL)
157                         return 0;
158                 else
159                         return 1;
160         }
161
162         public static int test_0_float_ret () {
163                 if ((float)typeof (Tests).GetMethod ("data_types_float_ret").Invoke (null, new object [] { }) == 0.123f)
164                         return 0;
165                 else
166                         return 1;
167         }
168
169         public static int test_0_double_ret () {
170                 if ((double)typeof (Tests).GetMethod ("data_types_double_ret").Invoke (null, new object [] { }) == 0.123f)
171                         return 0;
172                 else
173                         return 1;
174         }
175
176         public class Foo<T> {
177                 public T t;
178         }
179
180         public static int test_0_ginst_ref () {
181                 Foo<string> f = new Foo<string> { t = "A" };
182                 Foo<string> f2 = (Foo<string>)typeof (Tests).GetMethod ("data_types_ginst_ref").MakeGenericMethod (new Type [] { typeof (string) }).Invoke (null, new object [] { f });
183                 if (f2.t != "A")
184                         return 1;
185                 else
186                         return 0;
187         }
188
189         public static int test_0_ginst_vtype () {
190                 FooStruct<string> f = new FooStruct<string> { t = "A" };
191                 FooStruct<string> f2 = (FooStruct<string>)typeof (Tests).GetMethod ("data_types_ginst_vtype").MakeGenericMethod (new Type [] { typeof (string) }).Invoke (null, new object [] { f });
192                 if (f2.t != "A")
193                         return 1;
194                 else
195                         return 0;
196         }
197
198         public static Foo<T> data_types_ginst_ref<T> (Foo<T> f) {
199                 return f;
200         }
201
202         public struct FooStruct<T> {
203                 public T t;
204         }
205
206         public static FooStruct<T> data_types_ginst_vtype<T> (FooStruct<T> f) {
207                 return f;
208         }
209
210         public static int data_types_int (int i, uint ui) {
211                 if (i == Int32.MinValue && ui == UInt32.MaxValue)
212                         return 42;
213                 else
214                         return 1;
215         }
216
217         public static short data_types_short (short i, ushort ui) {
218                 if (i == short.MinValue && ui == ushort.MaxValue)
219                         return 42;
220                 else
221                         return 1;
222         }
223
224         public static int data_types_bool_char (bool b1, bool b2, char c) {
225                 if (b1 == true && b2 == false && c == 'A')
226                         return 0;
227                 else
228                         return 1;
229         }
230
231         public static int data_types_byref_int (ref int i) {
232                 if (i == 42)
233                         return 0;
234                 else
235                         return 1;
236         }
237
238         public static long data_types_long (long i, ulong ui) {
239                 if (i == 0x123456789L && ui == 0x123456789L)
240                         return 0x12345678AL;
241                 else
242                         return 1;
243         }
244
245         public static float data_types_float_ret () {
246                 return 0.123f;
247         }
248
249         public static double data_types_double_ret () {
250                 return 0.123f;
251         }
252
253     public static unsafe int* data_types_ptr (int *val) {
254                 //Console.WriteLine (new IntPtr (val));
255         return val;
256     }
257 }