Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / thunks.cs
1 using System;
2 using System.Reflection;
3 using System.Runtime.InteropServices;
4
5 public class Test
6 {
7         [DllImport ("libtest")]
8                 public static extern int test_method_thunk (int test_id, IntPtr testMethodHandle,
9                 IntPtr createObjectHandle);
10
11         static void RunTests(int series, Type type)
12         {
13                 const string Prefix = "Test";
14                 MethodInfo createObjectMethod = type.GetMethod ("CreateObject");
15                 
16                 foreach (MethodInfo mi in type.GetMethods ()) {
17                         string name = mi.Name;
18                         if (!name.StartsWith (Prefix))
19                                 continue;
20
21                         int id = Convert.ToInt32 (name.Substring (Prefix.Length));
22
23                         int res = test_method_thunk (series + id, mi.MethodHandle.Value, createObjectMethod.MethodHandle.Value);
24
25                         if (res != 0) {
26                                 Console.WriteLine ("{0} returned {1}", mi, res);
27                                 Environment.Exit ((id << 3) + res);
28                         }
29                 }
30         }
31
32         public static int Main ()
33         {
34                 RunTests (0, typeof (Test));
35                 RunTests (100, typeof (TestStruct));
36                 return 0;
37         }
38
39         public static object CreateObject ()
40         {
41                 return new Test ();
42         }
43
44         public static void Test0 ()
45         {
46         }
47
48         public static int Test1 ()
49         {
50                 return 42;
51         }
52
53         public static string Test2 (string s)
54         {
55                 return s;
56         }
57
58         public string Test3 (string a)
59         {
60                 return a;
61         }
62
63         public int Test4 (string a, int i)
64         {
65                 return i;
66         }
67
68         public int Test5 (string a, int i)
69         {
70                 throw new NotImplementedException ();
71         }
72
73         public bool Test6 (byte a1, short a2, int a3, long a4, float a5, double a6, string a7)
74         {
75                 return  a1 == 254 &&
76                         a2 == 32700 &&
77                         a3 == -245378 &&
78                         a4 == 6789600 &&
79                         (Math.Abs (a5 - 3.1415) < 0.001) &&
80                         (Math.Abs (a6 - 3.1415) < 0.001) &&
81                         a7 == "Test6";
82         }
83
84         public static long Test7 ()
85         {
86                 return Int64.MaxValue;
87         }
88
89         public static void Test8 (ref byte a1, ref short a2, ref int a3, ref long a4, ref float a5, ref double a6, ref string a7)
90         {
91                 a1 = 254;
92                 a2 = 32700;
93                 a3 = -245378;
94                 a4 = 6789600;
95                 a5 = 3.1415f;
96                 a6 = 3.1415;
97                 a7 = "Test8";
98         }
99
100         public static void Test9 (ref byte a1, ref short a2, ref int a3, ref long a4, ref float a5, ref double a6, ref string a7)
101         {
102                 throw new NotImplementedException ();
103         }
104
105         public static void Test10 (ref Test obj)
106         {
107                 obj = new Test ();
108         }
109 }
110
111
112 public struct TestStruct
113 {
114         public int A;
115         public double B;
116
117         public static object CreateObject ()
118         {
119                 return new TestStruct ();
120         }
121
122         public static bool Test0 (TestStruct s)
123         {
124                 bool res =  s.A == 42 && Math.Abs (s.B - 3.1415) < 0.001;
125
126                 /* these changes must not be visible in unmanaged code */
127                 s.A = 12;
128                 s.B = 13;
129
130                 return res;
131         }
132
133         public static void Test1 (ref TestStruct s)
134         {
135                 s.A = 42;
136                 s.B = 3.1415;
137         }
138
139         public static TestStruct Test2 ()
140         {
141                 TestStruct s = new TestStruct ();
142                 s.A = 42;
143                 s.B = 3.1415;
144                 return s;
145         }
146
147         public void Test3 ()
148         {
149                 A = 1;
150                 B = 17;
151         }
152 }