23fec490693ff134df0462fe9125fc60160bbd86
[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_0_string_ctor () {
136                 string res = (string)typeof (String).GetConstructor (new Type [] { typeof (char[]) }).Invoke (new object [] { new char [] { 'A', 'B', 'C' } });
137                 if (res == "ABC")
138                         return 0;
139                 else
140                         return 1;
141         }
142
143         public class Foo<T> {
144                 public T t;
145         }
146
147         public static int test_0_ginst_ref () {
148                 Foo<string> f = new Foo<string> { t = "A" };
149                 Foo<string> f2 = (Foo<string>)typeof (Tests).GetMethod ("data_types_ginst_ref").MakeGenericMethod (new Type [] { typeof (string) }).Invoke (null, new object [] { f });
150                 if (f2.t != "A")
151                         return 1;
152                 else
153                         return 0;
154         }
155
156         public static int test_0_ginst_vtype () {
157                 FooStruct<string> f = new FooStruct<string> { t = "A" };
158                 FooStruct<string> f2 = (FooStruct<string>)typeof (Tests).GetMethod ("data_types_ginst_vtype").MakeGenericMethod (new Type [] { typeof (string) }).Invoke (null, new object [] { f });
159                 if (f2.t != "A")
160                         return 1;
161                 else
162                         return 0;
163         }
164
165         public static Foo<T> data_types_ginst_ref<T> (Foo<T> f) {
166                 return f;
167         }
168
169         public struct FooStruct<T> {
170                 public T t;
171         }
172
173         public static FooStruct<T> data_types_ginst_vtype<T> (FooStruct<T> f) {
174                 return f;
175         }
176
177     public static unsafe int* data_types_ptr (int *val) {
178                 //Console.WriteLine (new IntPtr (val));
179         return val;
180     }
181
182         public static bool pack_u2 (ushort us) {
183                 return true;
184         }
185
186         public static bool pack_i2 (short value) {
187                 int c = 0;
188                 // Force 'value' to be register allocated
189                 for (int i = 0; i < value; ++i)
190                         c += value;
191           return value < 0x80;
192         }
193
194         // #11750
195         public static int test_0_i2_u2 () {
196                 typeof (Tests).GetMethod ("pack_u2").Invoke (null, new object [] { (ushort)0 });
197                 var res = typeof (Tests).GetMethod ("pack_i2").Invoke (null, new object [] { (short)-1 });
198                 return (bool)res ? 0 : 1;
199         }
200
201         public static bool pack_bool (bool b) {
202                 return true;
203         }
204
205         public static bool pack_i1 (sbyte value) {
206                 int c = 0;
207                 // Force 'value' to be register allocated
208                 for (int i = 0; i < value; ++i)
209                         c += value;
210                 return value < -1;
211         }
212
213         public static int test_0_i1_bool () {
214                 typeof (Tests).GetMethod ("pack_bool").Invoke (null, new object [] { true });
215                 var res = typeof (Tests).GetMethod ("pack_i1").Invoke (null, new object [] { (sbyte)-0x40 });
216                 return (bool)res ? 0 : 1;
217         }
218
219         struct Point {
220                 public int x, y;
221         }
222
223         struct Foo2 {
224                 public Point Location {
225                         get {
226                                 return new Point () { x = 10, y = 20 };
227                         }
228                 }
229         }
230
231         public static int test_0_vtype_method_vtype_ret () {
232                 var f = new Foo2 ();
233                 var p = (Point)typeof (Foo2).GetMethod ("get_Location").Invoke (f, null);
234                 if (p.x != 10 || p.y != 20)
235                         return 1;
236                 return 0;
237         }
238
239         public static int test_0_array_get_set () {
240                 int[,,] arr = new int [10, 10, 10];
241                 arr [0, 1, 2] = 42;
242                 var gm = arr.GetType ().GetMethod ("Get");
243                 int i = (int) gm.Invoke (arr, new object [] { 0, 1, 2 });
244                 if (i != 42)
245                         return 1;
246                 var sm = arr.GetType ().GetMethod ("Set");
247                 sm.Invoke (arr, new object [] { 0, 1, 2, 33 });
248                 if (arr [0, 1, 2] != 33)
249                         return 2;
250                 return 0;
251         }
252
253         public static int test_0_multi_dim_array_ctor () {
254         var type1 = Type.GetType ("System.Char[,]").GetTypeInfo ();
255
256                 ConstructorInfo ci = null;
257                 foreach (var c in type1.DeclaredConstructors) {
258                         if (c.GetParameters ().Length == 4)
259                                 ci = c;
260                 }
261         var res = ci.Invoke (new object[] { 1, 5, -10, 7 });
262                 var a = (Array)res;
263                 if (a.GetLength (0) != 5 || a.GetLowerBound (0) != 1 || a.GetLength (1) != 7 || a.GetLowerBound (1) != -10)
264                         return 1;
265                 return 0;
266         }
267
268         private static void method_invoke_no_modify_by_value_arg_helper (int dummy)
269     {
270     }
271
272     public static int test_0_method_invoke_no_modify_by_value_arg ()
273     {
274         var args = new object[] { null };
275         var method = typeof (Tests).GetMethod ("method_invoke_no_modify_by_value_arg_helper", BindingFlags.NonPublic | BindingFlags.Static);
276         method.Invoke (null, args);
277         if (args[0] == null)
278             return 0;
279         else
280             return 1;
281     }
282 }