2003-10-13 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / tests / pinvoke2.cs
1 using System;
2 using System.Text;
3 using System.Runtime.InteropServices;
4
5 public class Tests {
6
7         public static int delegate_test (int a)
8         {
9                 if (a == 2)
10                         return 0;
11
12                 return 1;
13         }
14         
15         [StructLayout (LayoutKind.Sequential)]
16         public struct SimpleStruct {
17                 public bool a;
18                 public bool b;
19                 public bool c;
20                 public string d;
21         }
22
23         [StructLayout (LayoutKind.Sequential)]
24         public struct SimpleStruct2 {
25                 public bool a;
26                 public bool b;
27                 public bool c;
28                 public string d;
29                 public byte e;
30                 public double f;
31                 public byte g;
32                 public long h;
33         }
34
35         [StructLayout (LayoutKind.Sequential, Size=0)]
36         public struct EmptyStruct {
37         }
38
39         /* sparcv9 has complex conventions when passing structs with doubles in them 
40            by value, some simple tests for them */
41         [StructLayout (LayoutKind.Sequential)]
42         public struct Point {
43                 public double x;
44                 public double y;
45         }
46
47         [StructLayout (LayoutKind.Sequential)]
48         public struct MixedPoint {
49                 public int x;
50                 public double y;
51         }
52
53         [StructLayout (LayoutKind.Sequential)]
54         public class SimpleClass {
55                 public bool a;
56                 public bool b;
57                 public bool c;
58                 public string d;
59                 public byte e;
60                 public double f;
61                 public byte g;
62                 public long h;
63         }
64
65         [StructLayout (LayoutKind.Sequential)]
66         public class EmptyClass {
67         }
68
69         [DllImport ("libtest", EntryPoint="mono_test_marshal_char")]
70         public static extern int mono_test_marshal_char (char a1);
71
72         [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_byref")]
73         public static extern int mono_test_marshal_bool_byref (int a, ref bool b, int c);
74
75         [DllImport ("libtest", EntryPoint="mono_test_marshal_array")]
76         public static extern int mono_test_marshal_array (int [] a1);
77         
78         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
79         public static extern int mono_test_marshal_struct (SimpleStruct ss);
80
81         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2")]
82         public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss);
83
84         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2_2")]
85         public static extern int mono_test_marshal_struct2_2 (int i, int j, int k, SimpleStruct2 ss);
86
87         [DllImport ("libtest", EntryPoint="mono_test_marshal_point")]
88         public static extern int mono_test_marshal_point (Point p);
89
90         [DllImport ("libtest", EntryPoint="mono_test_marshal_mixed_point")]
91         public static extern int mono_test_marshal_mixed_point (MixedPoint p);
92
93         [DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
94         public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
95
96         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct_array")]
97         public static extern int mono_test_marshal_struct_array (SimpleStruct2[] ss);
98
99         [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
100         public static extern SimpleClass mono_test_marshal_class (int i, int j, int k, SimpleClass ss, int l);
101
102         [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_class")]
103         public static extern int mono_test_marshal_byref_class (ref SimpleClass ss);
104
105         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
106         public static extern int mono_test_marshal_delegate (SimpleDelegate d);
107
108         [DllImport ("libtest", EntryPoint="mono_test_return_vtype")]
109         public static extern SimpleStruct mono_test_return_vtype (IntPtr i);
110
111         [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
112         public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
113
114         public delegate int SimpleDelegate (int a);
115
116         public static int Main () {
117                 return TestDriver.RunTests (typeof (Tests));
118         }
119
120         static int test_0_marshal_char () {
121                 return mono_test_marshal_char ('a');
122         }
123
124         static int test_1225_marshal_array () {
125                 int [] a1 = new int [50];
126                 for (int i = 0; i < 50; i++)
127                         a1 [i] = i;
128
129                 return mono_test_marshal_array (a1);
130         }
131
132         static int test_0_marshal_struct () {
133                 SimpleStruct ss = new  SimpleStruct ();
134                 ss.b = true;
135                 ss.d = "TEST";
136                 
137                 return mono_test_marshal_struct (ss);
138         }
139
140         static int test_0_marshal_struct2 () {
141                 SimpleStruct2 ss2 = new  SimpleStruct2 ();
142                 ss2.b = true;
143                 ss2.d = "TEST";
144                 ss2.e = 99;
145                 ss2.f = 1.5;
146                 ss2.g = 42;
147                 ss2.h = 123L;
148
149                 return mono_test_marshal_struct2 (ss2);
150         }
151
152         static int test_0_marshal_struct3 () {
153                 SimpleStruct2 ss2 = new  SimpleStruct2 ();
154                 ss2.b = true;
155                 ss2.d = "TEST";
156                 ss2.e = 99;
157                 ss2.f = 1.5;
158                 ss2.g = 42;
159                 ss2.h = 123L;
160
161                 return mono_test_marshal_struct2_2 (10, 11, 12, ss2);
162         }
163
164         static int test_0_marshal_empty_struct () {
165                 EmptyStruct es = new EmptyStruct ();
166
167                 if (mono_test_empty_struct (1, es, 2) != 0)
168                         return 1;
169                 
170                 return 0;
171         }
172
173         static int test_0_marshal_struct_array () {
174                 SimpleStruct2[] ss_arr = new SimpleStruct2 [2];
175
176                 SimpleStruct2 ss2 = new SimpleStruct2 ();
177                 ss2.b = true;
178                 ss2.d = "TEST";
179                 ss2.e = 99;
180                 ss2.f = 1.5;
181                 ss2.g = 42;
182                 ss2.h = 123L;
183
184                 ss_arr [0] = ss2;
185
186                 ss2.b = false;
187                 ss2.d = "TEST2";
188                 ss2.e = 100;
189                 ss2.f = 2.5;
190                 ss2.g = 43;
191                 ss2.h = 124L;
192
193                 ss_arr [1] = ss2;
194
195                 return mono_test_marshal_struct_array (ss_arr);
196         }
197
198         /* Test classes as arguments and return values */
199         static int test_0_marshal_class () {
200                 SimpleClass ss = new  SimpleClass ();
201                 ss.b = true;
202                 ss.d = "TEST";
203                 ss.e = 99;
204                 ss.f = 1.5;
205                 ss.g = 42;
206                 ss.h = 123L;
207
208                 SimpleClass res = mono_test_marshal_class (10, 11, 12, ss, 14);
209                 if (res == null)
210                         return 1;
211                 if  (! (res.a == ss.a && res.b == ss.b && res.c == ss.c && 
212                                 res.d == ss.d && res.e == ss.e && res.f == ss.f &&
213                                 res.g == ss.g && res.h == ss.h))
214                         return 2;
215
216                 /* Test null arguments and results */
217                 res = mono_test_marshal_class (10, 11, 12, null, 14);
218                 if (res != null)
219                         return 3;
220
221                 return 0;
222         }
223
224         static int test_0_marshal_byref_class () {
225                 SimpleClass ss = new  SimpleClass ();
226                 ss.b = true;
227                 ss.d = "TEST";
228                 ss.e = 99;
229                 ss.f = 1.5;
230                 ss.g = 42;
231                 ss.h = 123L;
232
233                 int res = mono_test_marshal_byref_class (ref ss);
234                 if (ss.d != "TEST-RES")
235                         return 1;
236
237                 return 0;
238         }
239
240         static int test_0_marshal_delegate () {
241                 SimpleDelegate d = new SimpleDelegate (delegate_test);
242
243                 return mono_test_marshal_delegate (d);
244         }
245
246         static int test_0_marshal_point () {
247                 Point pt = new Point();
248                 pt.x = 1.25;
249                 pt.y = 3.5;
250                 
251                 return mono_test_marshal_point(pt);
252         }
253
254         static int test_0_marshal_mixed_point () {
255                 MixedPoint mpt = new MixedPoint();
256                 mpt.x = 5;
257                 mpt.y = 6.75;
258                 
259                 return mono_test_marshal_mixed_point(mpt);
260         }
261
262         static int test_0_marshal_bool_byref () {
263                 bool b = true;
264                 if (mono_test_marshal_bool_byref (99, ref b, 100) != 1)
265                         return 1;
266                 b = false;
267                 if (mono_test_marshal_bool_byref (99, ref b, 100) != 0)
268                         return 12;
269                 if (b != true)
270                         return 13;
271
272                 return 0;
273         }
274
275         static int test_0_return_vtype () {
276                 SimpleStruct ss = mono_test_return_vtype (new IntPtr (5));
277
278                 if (!ss.a && ss.b && !ss.c && ss.d == "TEST")
279                         return 0;
280                 
281                 return 1;
282         }
283
284         static int test_0_marshal_stringbuilder () {
285                 StringBuilder sb = new StringBuilder(255);
286                 mono_test_marshal_stringbuilder (sb, sb.Capacity);
287                 String res = sb.ToString();
288
289                 if (res != "This is my message.  Isn't it nice?")
290                         return 1;  
291                 
292                 return 0;
293         }
294 }