codeowners update
[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 struct AStruct {
33         public int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
34         public int a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
35 }
36
37 class Tests
38 {
39         public static Enum1 return_enum1 () {
40                 return Enum1.A;
41         }
42
43         public static Enum2 return_enum2 () {
44                 return Enum2.C;
45         }
46
47         public static long return_long () {
48                 return 1234;
49         }
50
51         public static ulong return_ulong () {
52                 return UInt64.MaxValue - 5;
53         }
54
55         public static object return_t<T> (T t) {
56                 return (object)t;
57         }
58
59         static int Main (string[] args)
60         {
61                 return TestDriver.RunTests (typeof (Tests), args);
62         }
63
64         public static int test_0_base () {
65                 Assembly ass = typeof (Tests).Assembly;
66                 Type a_type = ass.GetType ("A");
67                 MethodInfo a_method = a_type.GetMethod ("ToString");
68
69                 Type d_type = ass.GetType ("D");
70                 MethodInfo d_method = d_type.GetMethod ("Test");
71
72                 Console.WriteLine ("TEST: {0} {1}", a_method, d_method);
73
74                 A a = new A ();
75                 D d = new D ();
76
77                 object a_ret = a_method.Invoke (a, null);
78                 Console.WriteLine (a_ret);
79
80                 object d_ret = d_method.Invoke (d, null);
81                 Console.WriteLine (d_ret);
82
83                 return 0;
84         }
85
86         public static int test_0_enum_sharing () {
87                 /* Check sharing of wrappers returning enums */
88                 if (typeof (Tests).GetMethod ("return_enum1").Invoke (null, null).GetType () != typeof (Enum1))
89                         return 1;
90                 if (typeof (Tests).GetMethod ("return_enum2").Invoke (null, null).GetType () != typeof (Enum2))
91                         return 2;
92                 return 0;
93         }
94
95         public static int test_0_primitive_sharing () {
96                 /* Check sharing of wrappers returning primitive types */
97                 if (typeof (Tests).GetMethod ("return_long").Invoke (null, null).GetType () != typeof (long))
98                         return 3;
99                 if (typeof (Tests).GetMethod ("return_ulong").Invoke (null, null).GetType () != typeof (ulong))
100                         return 4;
101
102                 return 0;
103         }
104
105         public struct Foo
106         {
107                 public string ToString2 () {
108                         return "FOO";
109                 }
110         }
111
112         public static object GetNSObject (IntPtr i) {
113                 return i;
114         }
115
116         public static int test_0_vtype_method_sharing () {
117                 /* Check sharing of wrappers of vtype methods with static methods having an IntPtr argument */
118                 if ((string)(typeof (Foo).GetMethod ("ToString2").Invoke (new Foo (), null)) != "FOO")
119                         return 3;
120                 object o = typeof (Tests).GetMethod ("GetNSObject").Invoke (null, new object [] { new IntPtr (42) });
121                 if (!(o is IntPtr) || ((IntPtr)o != new IntPtr (42)))
122                         return 4;
123
124                 return 0;
125         }
126
127         public static unsafe int test_0_ptr () {
128                 int[] arr = new int [10];
129                 fixed (void *p = &arr [5]) {
130                         object o = typeof (Tests).GetMethod ("data_types_ptr").Invoke (null, new object [1] { new IntPtr (p) });
131                         void *p2 = Pointer.Unbox (o);
132                         if (new IntPtr (p) != new IntPtr (p2))
133                                 return 1;
134
135                         o = typeof (Tests).GetMethod ("data_types_ptr").Invoke (null, new object [1] { null });
136                         p2 = Pointer.Unbox (o);
137                         if (new IntPtr (p2) != IntPtr.Zero)
138                                 return 1;
139                 }
140
141                 return 0;
142         }
143
144         public static int test_0_string_ctor () {
145                 string res = (string)typeof (String).GetConstructor (new Type [] { typeof (char[]) }).Invoke (new object [] { new char [] { 'A', 'B', 'C' } });
146                 if (res == "ABC")
147                         return 0;
148                 else
149                         return 1;
150         }
151
152         public class Foo<T> {
153                 public T t;
154         }
155
156         public static int test_0_ginst_ref () {
157                 Foo<string> f = new Foo<string> { t = "A" };
158                 Foo<string> f2 = (Foo<string>)typeof (Tests).GetMethod ("data_types_ginst_ref").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 int test_0_ginst_vtype () {
166                 FooStruct<string> f = new FooStruct<string> { t = "A" };
167                 FooStruct<string> f2 = (FooStruct<string>)typeof (Tests).GetMethod ("data_types_ginst_vtype").MakeGenericMethod (new Type [] { typeof (string) }).Invoke (null, new object [] { f });
168                 if (f2.t != "A")
169                         return 1;
170                 else
171                         return 0;
172         }
173
174         public static Foo<T> data_types_ginst_ref<T> (Foo<T> f) {
175                 return f;
176         }
177
178         public struct FooStruct<T> {
179                 public T t;
180         }
181
182         public static FooStruct<T> data_types_ginst_vtype<T> (FooStruct<T> f) {
183                 return f;
184         }
185
186     public static unsafe int* data_types_ptr (int *val) {
187                 //Console.WriteLine (new IntPtr (val));
188         return val;
189     }
190
191         public static bool pack_u2 (ushort us) {
192                 return true;
193         }
194
195         public static bool pack_i2 (short value) {
196                 int c = 0;
197                 // Force 'value' to be register allocated
198                 for (int i = 0; i < value; ++i)
199                         c += value;
200           return value < 0x80;
201         }
202
203         // #11750
204         public static int test_0_i2_u2 () {
205                 typeof (Tests).GetMethod ("pack_u2").Invoke (null, new object [] { (ushort)0 });
206                 var res = typeof (Tests).GetMethod ("pack_i2").Invoke (null, new object [] { (short)-1 });
207                 return (bool)res ? 0 : 1;
208         }
209
210         public static bool pack_bool (bool b) {
211                 return true;
212         }
213
214         public static bool pack_i1 (sbyte value) {
215                 int c = 0;
216                 // Force 'value' to be register allocated
217                 for (int i = 0; i < value; ++i)
218                         c += value;
219                 return value < -1;
220         }
221
222         public static int test_0_i1_bool () {
223                 typeof (Tests).GetMethod ("pack_bool").Invoke (null, new object [] { true });
224                 var res = typeof (Tests).GetMethod ("pack_i1").Invoke (null, new object [] { (sbyte)-0x40 });
225                 return (bool)res ? 0 : 1;
226         }
227
228         struct Point {
229                 public int x, y;
230         }
231
232         struct Foo2 {
233                 public Point Location {
234                         get {
235                                 return new Point () { x = 10, y = 20 };
236                         }
237                 }
238         }
239
240         public static int test_0_vtype_method_vtype_ret () {
241                 var f = new Foo2 ();
242                 var p = (Point)typeof (Foo2).GetMethod ("get_Location").Invoke (f, null);
243                 if (p.x != 10 || p.y != 20)
244                         return 1;
245                 return 0;
246         }
247
248         public static int test_0_array_get_set () {
249                 int[,,] arr = new int [10, 10, 10];
250                 arr [0, 1, 2] = 42;
251                 var gm = arr.GetType ().GetMethod ("Get");
252                 int i = (int) gm.Invoke (arr, new object [] { 0, 1, 2 });
253                 if (i != 42)
254                         return 1;
255                 var sm = arr.GetType ().GetMethod ("Set");
256                 sm.Invoke (arr, new object [] { 0, 1, 2, 33 });
257                 if (arr [0, 1, 2] != 33)
258                         return 2;
259                 return 0;
260         }
261
262         public static int test_0_multi_dim_array_ctor () {
263         var type1 = Type.GetType ("System.Char[,]").GetTypeInfo ();
264
265                 ConstructorInfo ci = null;
266                 foreach (var c in type1.DeclaredConstructors) {
267                         if (c.GetParameters ().Length == 4)
268                                 ci = c;
269                 }
270         var res = ci.Invoke (new object[] { 1, 5, -10, 7 });
271                 var a = (Array)res;
272                 if (a.GetLength (0) != 5 || a.GetLowerBound (0) != 1 || a.GetLength (1) != 7 || a.GetLowerBound (1) != -10)
273                         return 1;
274                 return 0;
275         }
276
277         private static void method_invoke_no_modify_by_value_arg_helper (int dummy)
278     {
279     }
280
281     public static int test_0_method_invoke_no_modify_by_value_arg ()
282     {
283         var args = new object[] { null };
284         var method = typeof (Tests).GetMethod ("method_invoke_no_modify_by_value_arg_helper", BindingFlags.NonPublic | BindingFlags.Static);
285         method.Invoke (null, args);
286         if (args[0] == null)
287             return 0;
288         else
289             return 1;
290     }
291
292         public static int test_0_large_arg ()
293         {
294                 var arg = new AStruct ();
295                 arg.a1 = 1;
296                 arg.a2 = 2;
297                 arg.a3 = 3;
298                 arg.a20 = 20;
299                 var res = typeof (Tests).GetMethod ("return_t").MakeGenericMethod (new Type [] { typeof (AStruct) }).Invoke (null, new object [] { arg });
300                 var arg2 = (AStruct)res;
301                 if (arg2.a20 == 20)
302                         return 0;
303                 else
304                         return 1;
305         }
306 }