2003-11-14 Nick Drochak <ndrochak@gol.com>
[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_inout_array")]
79         public static extern int mono_test_marshal_inout_array ([In, Out] int [] a1);
80
81         [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_nonblittable_array", CharSet = CharSet.Unicode)]
82         public static extern int mono_test_marshal_inout_nonblittable_array ([In, Out] char [] a1);
83         
84         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
85         public static extern int mono_test_marshal_struct (SimpleStruct ss);
86
87         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2")]
88         public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss);
89
90         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2_2")]
91         public static extern int mono_test_marshal_struct2_2 (int i, int j, int k, SimpleStruct2 ss);
92
93         [DllImport ("libtest", EntryPoint="mono_test_marshal_point")]
94         public static extern int mono_test_marshal_point (Point p);
95
96         [DllImport ("libtest", EntryPoint="mono_test_marshal_mixed_point")]
97         public static extern int mono_test_marshal_mixed_point (MixedPoint p);
98
99         [DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
100         public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
101
102         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct_array")]
103         public static extern int mono_test_marshal_struct_array (SimpleStruct2[] ss);
104
105         [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
106         public static extern SimpleClass mono_test_marshal_class (int i, int j, int k, SimpleClass ss, int l);
107
108         [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_class")]
109         public static extern int mono_test_marshal_byref_class (ref SimpleClass ss);
110
111         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
112         public static extern int mono_test_marshal_delegate (SimpleDelegate d);
113
114         [DllImport ("libtest", EntryPoint="mono_test_return_vtype")]
115         public static extern SimpleStruct mono_test_return_vtype (IntPtr i);
116
117         [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
118         public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
119
120         [DllImport ("libtest", EntryPoint="mono_test_last_error", SetLastError=true)]
121         public static extern void mono_test_last_error (int err);
122
123         public delegate int SimpleDelegate (int a);
124
125         public static int Main () {
126                 return TestDriver.RunTests (typeof (Tests));
127         }
128
129         static int test_0_marshal_char () {
130                 return mono_test_marshal_char ('a');
131         }
132
133         static int test_1225_marshal_array () {
134                 int [] a1 = new int [50];
135                 for (int i = 0; i < 50; i++)
136                         a1 [i] = i;
137
138                 return mono_test_marshal_array (a1);
139         }
140
141         static int test_1225_marshal_inout_array () {
142                 int [] a1 = new int [50];
143                 for (int i = 0; i < 50; i++)
144                         a1 [i] = i;
145
146                 int res = mono_test_marshal_inout_array (a1);
147
148                 for (int i = 0; i < 50; i++)
149                         if (a1 [i] != 50 - i) {
150                                 Console.WriteLine ("X: " + i + " " + a1 [i]);
151                                 return 2;
152                         }
153
154                 return res;
155         }
156
157         static int test_0_marshal_inout_nonblittable_array () {
158                 char [] a1 = new char [10];
159                 for (int i = 0; i < 10; i++)
160                         a1 [i] = "Hello, World" [i];
161
162                 int res = mono_test_marshal_inout_nonblittable_array (a1);
163
164                 for (int i = 0; i < 10; i++)
165                         if (a1 [i] != 'F')
166                                 return 2;
167
168                 return res;
169         }
170
171         static int test_0_marshal_struct () {
172                 SimpleStruct ss = new  SimpleStruct ();
173                 ss.b = true;
174                 ss.d = "TEST";
175                 
176                 return mono_test_marshal_struct (ss);
177         }
178
179         static int test_0_marshal_struct2 () {
180                 SimpleStruct2 ss2 = new  SimpleStruct2 ();
181                 ss2.b = true;
182                 ss2.d = "TEST";
183                 ss2.e = 99;
184                 ss2.f = 1.5;
185                 ss2.g = 42;
186                 ss2.h = 123L;
187
188                 return mono_test_marshal_struct2 (ss2);
189         }
190
191         static int test_0_marshal_struct3 () {
192                 SimpleStruct2 ss2 = new  SimpleStruct2 ();
193                 ss2.b = true;
194                 ss2.d = "TEST";
195                 ss2.e = 99;
196                 ss2.f = 1.5;
197                 ss2.g = 42;
198                 ss2.h = 123L;
199
200                 return mono_test_marshal_struct2_2 (10, 11, 12, ss2);
201         }
202
203         static int test_0_marshal_empty_struct () {
204                 EmptyStruct es = new EmptyStruct ();
205
206                 if (mono_test_empty_struct (1, es, 2) != 0)
207                         return 1;
208                 
209                 return 0;
210         }
211
212         static int test_0_marshal_struct_array () {
213                 SimpleStruct2[] ss_arr = new SimpleStruct2 [2];
214
215                 SimpleStruct2 ss2 = new SimpleStruct2 ();
216                 ss2.b = true;
217                 ss2.d = "TEST";
218                 ss2.e = 99;
219                 ss2.f = 1.5;
220                 ss2.g = 42;
221                 ss2.h = 123L;
222
223                 ss_arr [0] = ss2;
224
225                 ss2.b = false;
226                 ss2.d = "TEST2";
227                 ss2.e = 100;
228                 ss2.f = 2.5;
229                 ss2.g = 43;
230                 ss2.h = 124L;
231
232                 ss_arr [1] = ss2;
233
234                 return mono_test_marshal_struct_array (ss_arr);
235         }
236
237         /* Test classes as arguments and return values */
238         static int test_0_marshal_class () {
239                 SimpleClass ss = new  SimpleClass ();
240                 ss.b = true;
241                 ss.d = "TEST";
242                 ss.e = 99;
243                 ss.f = 1.5;
244                 ss.g = 42;
245                 ss.h = 123L;
246
247                 SimpleClass res = mono_test_marshal_class (10, 11, 12, ss, 14);
248                 if (res == null)
249                         return 1;
250                 if  (! (res.a == ss.a && res.b == ss.b && res.c == ss.c && 
251                                 res.d == ss.d && res.e == ss.e && res.f == ss.f &&
252                                 res.g == ss.g && res.h == ss.h))
253                         return 2;
254
255                 /* Test null arguments and results */
256                 res = mono_test_marshal_class (10, 11, 12, null, 14);
257                 if (res != null)
258                         return 3;
259
260                 return 0;
261         }
262
263         static int test_0_marshal_byref_class () {
264                 SimpleClass ss = new  SimpleClass ();
265                 ss.b = true;
266                 ss.d = "TEST";
267                 ss.e = 99;
268                 ss.f = 1.5;
269                 ss.g = 42;
270                 ss.h = 123L;
271
272                 int res = mono_test_marshal_byref_class (ref ss);
273                 if (ss.d != "TEST-RES")
274                         return 1;
275
276                 return 0;
277         }
278
279         static int test_0_marshal_delegate () {
280                 SimpleDelegate d = new SimpleDelegate (delegate_test);
281
282                 return mono_test_marshal_delegate (d);
283         }
284
285         static int test_0_marshal_point () {
286                 Point pt = new Point();
287                 pt.x = 1.25;
288                 pt.y = 3.5;
289                 
290                 return mono_test_marshal_point(pt);
291         }
292
293         static int test_0_marshal_mixed_point () {
294                 MixedPoint mpt = new MixedPoint();
295                 mpt.x = 5;
296                 mpt.y = 6.75;
297                 
298                 return mono_test_marshal_mixed_point(mpt);
299         }
300
301         static int test_0_marshal_bool_byref () {
302                 bool b = true;
303                 if (mono_test_marshal_bool_byref (99, ref b, 100) != 1)
304                         return 1;
305                 b = false;
306                 if (mono_test_marshal_bool_byref (99, ref b, 100) != 0)
307                         return 12;
308                 if (b != true)
309                         return 13;
310
311                 return 0;
312         }
313
314         static int test_0_return_vtype () {
315                 SimpleStruct ss = mono_test_return_vtype (new IntPtr (5));
316
317                 if (!ss.a && ss.b && !ss.c && ss.d == "TEST")
318                         return 0;
319                 
320                 return 1;
321         }
322
323         static int test_0_marshal_stringbuilder () {
324                 StringBuilder sb = new StringBuilder(255);
325                 mono_test_marshal_stringbuilder (sb, sb.Capacity);
326                 String res = sb.ToString();
327
328                 if (res != "This is my message.  Isn't it nice?")
329                         return 1;  
330                 
331                 return 0;
332         }
333
334         static int test_0_last_error () {
335                 mono_test_last_error (5);
336                 if (Marshal.GetLastWin32Error () == 5)
337                         return 0;
338                 else
339                         return 1;
340         }
341 }