Bump external/ikdasm to master.
[mono-project.git] / mono / tests / runtime-invoke.cs
blob624feae80cd7822e0dfaddd6ae60013655f43385
1 using System;
2 using System.Reflection;
4 public struct A
6 public override string ToString ()
8 return "A";
12 public class D
14 public string Test ()
16 return "Test";
20 enum Enum1
26 enum Enum2
32 class Tests
34 public static Enum1 return_enum1 () {
35 return Enum1.A;
38 public static Enum2 return_enum2 () {
39 return Enum2.C;
42 public static long return_long () {
43 return 1234;
46 public static ulong return_ulong () {
47 return UInt64.MaxValue - 5;
50 static int Main (string[] args)
52 return TestDriver.RunTests (typeof (Tests), args);
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");
60 Type d_type = ass.GetType ("D");
61 MethodInfo d_method = d_type.GetMethod ("Test");
63 Console.WriteLine ("TEST: {0} {1}", a_method, d_method);
65 A a = new A ();
66 D d = new D ();
68 object a_ret = a_method.Invoke (a, null);
69 Console.WriteLine (a_ret);
71 object d_ret = d_method.Invoke (d, null);
72 Console.WriteLine (d_ret);
74 return 0;
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;
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;
93 return 0;
96 public struct Foo
98 public string ToString2 () {
99 return "FOO";
103 public static object GetNSObject (IntPtr i) {
104 return i;
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;
115 return 0;
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;
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;
132 return 0;
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;
143 public class Foo<T> {
144 public T t;
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;
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;
165 public static Foo<T> data_types_ginst_ref<T> (Foo<T> f) {
166 return f;
169 public struct FooStruct<T> {
170 public T t;
173 public static FooStruct<T> data_types_ginst_vtype<T> (FooStruct<T> f) {
174 return f;
177 public static unsafe int* data_types_ptr (int *val) {
178 //Console.WriteLine (new IntPtr (val));
179 return val;
182 public static bool pack_u2 (ushort us) {
183 return true;
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;
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;
201 public static bool pack_bool (bool b) {
202 return true;
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;
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;
219 struct Point {
220 public int x, y;
223 struct Foo2 {
224 public Point Location {
225 get {
226 return new Point () { x = 10, y = 20 };
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;
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;