2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[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 static unsafe int test_0_ptr () {
97                 int[] arr = new int [10];
98                 fixed (void *p = &arr [5]) {
99                         object o = typeof (Tests).GetMethod ("Test").Invoke (null, new object [1] { new IntPtr (p) });
100                         void *p2 = Pointer.Unbox (o);
101                         if (new IntPtr (p) != new IntPtr (p2))
102                                 return 1;
103
104                         o = typeof (Tests).GetMethod ("Test").Invoke (null, new object [1] { null });
105                         p2 = Pointer.Unbox (o);
106                         if (new IntPtr (p2) != IntPtr.Zero)
107                                 return 1;
108                 }
109
110                 return 0;
111         }
112
113     public static unsafe int* Test (int *val) {
114                 Console.WriteLine (new IntPtr (val));
115         return val;
116     }
117 }