2004-02-14 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 ("libnot-found", EntryPoint="not_found")]
70         public static extern int mono_library_not_found ();
71
72         [DllImport ("libtest", EntryPoint="not_found")]
73         public static extern int mono_entry_point_not_found ();
74
75         [DllImport ("libtest.dll", EntryPoint="mono_test_marshal_char")]
76         public static extern int mono_test_marshal_char_2 (char a1);
77
78         [DllImport ("libtest", EntryPoint="mono_test_marshal_char")]
79         public static extern int mono_test_marshal_char (char a1);
80
81         [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_byref")]
82         public static extern int mono_test_marshal_bool_byref (int a, ref bool b, int c);
83
84         [DllImport ("libtest", EntryPoint="mono_test_marshal_array")]
85         public static extern int mono_test_marshal_array (int [] a1);
86
87         [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_array")]
88         public static extern int mono_test_marshal_inout_array ([In, Out] int [] a1);
89
90         [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_nonblittable_array", CharSet = CharSet.Unicode)]
91         public static extern int mono_test_marshal_inout_nonblittable_array ([In, Out] char [] a1);
92         
93         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
94         public static extern int mono_test_marshal_struct (SimpleStruct ss);
95
96         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2")]
97         public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss);
98
99         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2_2")]
100         public static extern int mono_test_marshal_struct2_2 (int i, int j, int k, SimpleStruct2 ss);
101
102         [DllImport ("libtest", EntryPoint="mono_test_marshal_point")]
103         public static extern int mono_test_marshal_point (Point p);
104
105         [DllImport ("libtest", EntryPoint="mono_test_marshal_mixed_point")]
106         public static extern int mono_test_marshal_mixed_point (MixedPoint p);
107
108         [DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
109         public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
110
111         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct_array")]
112         public static extern int mono_test_marshal_struct_array (SimpleStruct2[] ss);
113
114         [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
115         public static extern SimpleClass mono_test_marshal_class (int i, int j, int k, SimpleClass ss, int l);
116
117         [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_class")]
118         public static extern int mono_test_marshal_byref_class (ref SimpleClass ss);
119
120         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
121         public static extern int mono_test_marshal_delegate (SimpleDelegate d);
122
123         [DllImport ("libtest", EntryPoint="mono_test_return_vtype")]
124         public static extern SimpleStruct mono_test_return_vtype (IntPtr i);
125
126         [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
127         public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
128
129         [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_unicode", CharSet=CharSet.Unicode)]
130         public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb, int len);
131
132         [DllImport ("libtest", EntryPoint="mono_test_last_error", SetLastError=true)]
133         public static extern void mono_test_last_error (int err);
134
135         public delegate int SimpleDelegate (int a);
136
137         public static int Main () {
138                 return TestDriver.RunTests (typeof (Tests));
139         }
140
141         static int test_0_marshal_char () {
142                 return mono_test_marshal_char ('a');
143         }
144
145         static int test_1225_marshal_array () {
146                 int [] a1 = new int [50];
147                 for (int i = 0; i < 50; i++)
148                         a1 [i] = i;
149
150                 return mono_test_marshal_array (a1);
151         }
152
153         static int test_1225_marshal_inout_array () {
154                 int [] a1 = new int [50];
155                 for (int i = 0; i < 50; i++)
156                         a1 [i] = i;
157
158                 int res = mono_test_marshal_inout_array (a1);
159
160                 for (int i = 0; i < 50; i++)
161                         if (a1 [i] != 50 - i) {
162                                 Console.WriteLine ("X: " + i + " " + a1 [i]);
163                                 return 2;
164                         }
165
166                 return res;
167         }
168
169         static int test_0_marshal_inout_nonblittable_array () {
170                 char [] a1 = new char [10];
171                 for (int i = 0; i < 10; i++)
172                         a1 [i] = "Hello, World" [i];
173
174                 int res = mono_test_marshal_inout_nonblittable_array (a1);
175
176                 for (int i = 0; i < 10; i++)
177                         if (a1 [i] != 'F')
178                                 return 2;
179
180                 return res;
181         }
182
183         static int test_0_marshal_struct () {
184                 SimpleStruct ss = new  SimpleStruct ();
185                 ss.b = true;
186                 ss.d = "TEST";
187                 
188                 return mono_test_marshal_struct (ss);
189         }
190
191         static int test_0_marshal_struct2 () {
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 (ss2);
201         }
202
203         static int test_0_marshal_struct3 () {
204                 SimpleStruct2 ss2 = new  SimpleStruct2 ();
205                 ss2.b = true;
206                 ss2.d = "TEST";
207                 ss2.e = 99;
208                 ss2.f = 1.5;
209                 ss2.g = 42;
210                 ss2.h = 123L;
211
212                 return mono_test_marshal_struct2_2 (10, 11, 12, ss2);
213         }
214
215         static int test_0_marshal_empty_struct () {
216                 EmptyStruct es = new EmptyStruct ();
217
218                 if (mono_test_empty_struct (1, es, 2) != 0)
219                         return 1;
220                 
221                 return 0;
222         }
223
224         static int test_0_marshal_struct_array () {
225                 SimpleStruct2[] ss_arr = new SimpleStruct2 [2];
226
227                 SimpleStruct2 ss2 = new SimpleStruct2 ();
228                 ss2.b = true;
229                 ss2.d = "TEST";
230                 ss2.e = 99;
231                 ss2.f = 1.5;
232                 ss2.g = 42;
233                 ss2.h = 123L;
234
235                 ss_arr [0] = ss2;
236
237                 ss2.b = false;
238                 ss2.d = "TEST2";
239                 ss2.e = 100;
240                 ss2.f = 2.5;
241                 ss2.g = 43;
242                 ss2.h = 124L;
243
244                 ss_arr [1] = ss2;
245
246                 return mono_test_marshal_struct_array (ss_arr);
247         }
248
249         /* Test classes as arguments and return values */
250         static int test_0_marshal_class () {
251                 SimpleClass ss = new  SimpleClass ();
252                 ss.b = true;
253                 ss.d = "TEST";
254                 ss.e = 99;
255                 ss.f = 1.5;
256                 ss.g = 42;
257                 ss.h = 123L;
258
259                 SimpleClass res = mono_test_marshal_class (10, 11, 12, ss, 14);
260                 if (res == null)
261                         return 1;
262                 if  (! (res.a == ss.a && res.b == ss.b && res.c == ss.c && 
263                                 res.d == ss.d && res.e == ss.e && res.f == ss.f &&
264                                 res.g == ss.g && res.h == ss.h))
265                         return 2;
266
267                 /* Test null arguments and results */
268                 res = mono_test_marshal_class (10, 11, 12, null, 14);
269                 if (res != null)
270                         return 3;
271
272                 return 0;
273         }
274
275         static int test_0_marshal_byref_class () {
276                 SimpleClass ss = new  SimpleClass ();
277                 ss.b = true;
278                 ss.d = "TEST";
279                 ss.e = 99;
280                 ss.f = 1.5;
281                 ss.g = 42;
282                 ss.h = 123L;
283
284                 int res = mono_test_marshal_byref_class (ref ss);
285                 if (ss.d != "TEST-RES")
286                         return 1;
287
288                 return 0;
289         }
290
291         static int test_0_marshal_delegate () {
292                 SimpleDelegate d = new SimpleDelegate (delegate_test);
293
294                 return mono_test_marshal_delegate (d);
295         }
296
297         static int test_0_marshal_point () {
298                 Point pt = new Point();
299                 pt.x = 1.25;
300                 pt.y = 3.5;
301                 
302                 return mono_test_marshal_point(pt);
303         }
304
305         static int test_0_marshal_mixed_point () {
306                 MixedPoint mpt = new MixedPoint();
307                 mpt.x = 5;
308                 mpt.y = 6.75;
309                 
310                 return mono_test_marshal_mixed_point(mpt);
311         }
312
313         static int test_0_marshal_bool_byref () {
314                 bool b = true;
315                 if (mono_test_marshal_bool_byref (99, ref b, 100) != 1)
316                         return 1;
317                 b = false;
318                 if (mono_test_marshal_bool_byref (99, ref b, 100) != 0)
319                         return 12;
320                 if (b != true)
321                         return 13;
322
323                 return 0;
324         }
325
326         static int test_0_return_vtype () {
327                 SimpleStruct ss = mono_test_return_vtype (new IntPtr (5));
328
329                 if (!ss.a && ss.b && !ss.c && ss.d == "TEST")
330                         return 0;
331                 
332                 return 1;
333         }
334
335         static int test_0_marshal_stringbuilder () {
336                 StringBuilder sb = new StringBuilder(255);
337                 mono_test_marshal_stringbuilder (sb, sb.Capacity);
338                 String res = sb.ToString();
339
340                 if (res != "This is my message.  Isn't it nice?")
341                         return 1;  
342                 
343                 return 0;
344         }
345
346         static int test_0_marshal_stringbuilder_unicode () {
347                 StringBuilder sb = new StringBuilder(255);
348                 mono_test_marshal_stringbuilder_unicode (sb, sb.Capacity);
349                 String res = sb.ToString();
350
351                 if (res != "This is my message.  Isn't it nice?")
352                         return 1;  
353                 
354                 return 0;
355         }
356
357         static int test_0_last_error () {
358                 mono_test_last_error (5);
359                 if (Marshal.GetLastWin32Error () == 5)
360                         return 0;
361                 else
362                         return 1;
363         }
364
365         static int test_0_library_not_found () {
366
367                 try {
368                         mono_entry_point_not_found ();
369                         return 1;
370                 }
371                 catch (EntryPointNotFoundException) {
372                 }
373
374                 return 0;
375         }
376
377         static int test_0_entry_point_not_found () {
378
379                 try {
380                         mono_library_not_found ();
381                         return 1;
382                 }
383                 catch (DllNotFoundException) {
384                 }
385
386                 return 0;
387         }
388
389         /* Check that the runtime trims .dll from the library name */
390         static int test_0_trim_dll_from_name () {
391
392                 mono_test_marshal_char_2 ('A');
393
394                 return 0;
395         }
396 }