* cpu-pentium.md basic-float.cs Fixed bug on fpu spills (see bug 54467),
[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         [StructLayout (LayoutKind.Sequential)]
40         public struct DelegateStruct {
41                 public int a;
42                 public SimpleDelegate del;
43                 [MarshalAs(UnmanagedType.FunctionPtr)] 
44                 public SimpleDelegate del2;
45         }
46
47         /* sparcv9 has complex conventions when passing structs with doubles in them 
48            by value, some simple tests for them */
49         [StructLayout (LayoutKind.Sequential)]
50         public struct Point {
51                 public double x;
52                 public double y;
53         }
54
55         [StructLayout (LayoutKind.Sequential)]
56         public struct MixedPoint {
57                 public int x;
58                 public double y;
59         }
60
61         [StructLayout (LayoutKind.Sequential)]
62         public class SimpleClass {
63                 public bool a;
64                 public bool b;
65                 public bool c;
66                 public string d;
67                 public byte e;
68                 public double f;
69                 public byte g;
70                 public long h;
71         }
72
73         [StructLayout (LayoutKind.Sequential)]
74         public class EmptyClass {
75         }
76
77         [StructLayout (LayoutKind.Sequential)]
78         public struct LongAlignStruct {
79                 public int a;
80                 public long b;
81                 public long c;
82         }
83
84         [DllImport ("libnot-found", EntryPoint="not_found")]
85         public static extern int mono_library_not_found ();
86
87         [DllImport ("libtest", EntryPoint="not_found")]
88         public static extern int mono_entry_point_not_found ();
89
90         [DllImport ("libtest.dll", EntryPoint="mono_test_marshal_char")]
91         public static extern int mono_test_marshal_char_2 (char a1);
92
93         [DllImport ("libtest", EntryPoint="mono_test_marshal_char")]
94         public static extern int mono_test_marshal_char (char a1);
95
96         [DllImport ("libtest", EntryPoint="mono_test_marshal_char_array")]
97         public static extern int mono_test_marshal_char_array (char[] a1);
98
99         [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_byref")]
100         public static extern int mono_test_marshal_bool_byref (int a, ref bool b, int c);
101
102         [DllImport ("libtest", EntryPoint="mono_test_marshal_array")]
103         public static extern int mono_test_marshal_array (int [] a1);
104
105         [DllImport ("libtest", EntryPoint="mono_test_marshal_empty_string_array")]
106         public static extern int mono_test_marshal_empty_string_array (string [] a1);
107
108         [DllImport ("libtest", EntryPoint="mono_test_marshal_string_array")]
109         public static extern int mono_test_marshal_string_array (string [] a1);
110
111         [DllImport ("libtest", EntryPoint="mono_test_marshal_unicode_string_array", CharSet=CharSet.Unicode)]
112         public static extern int mono_test_marshal_unicode_string_array (string [] a1, [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]string [] a2);
113
114         [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_array")]
115         public static extern int mono_test_marshal_inout_array ([In, Out] int [] a1);
116
117         [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_nonblittable_array", CharSet = CharSet.Unicode)]
118         public static extern int mono_test_marshal_inout_nonblittable_array ([In, Out] char [] a1);
119         
120         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
121         public static extern int mono_test_marshal_struct (SimpleStruct ss);
122
123         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2")]
124         public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss);
125
126         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2_2")]
127         public static extern int mono_test_marshal_struct2_2 (int i, int j, int k, SimpleStruct2 ss);
128
129         [DllImport ("libtest", EntryPoint="mono_test_marshal_point")]
130         public static extern int mono_test_marshal_point (Point p);
131
132         [DllImport ("libtest", EntryPoint="mono_test_marshal_mixed_point")]
133         public static extern int mono_test_marshal_mixed_point (MixedPoint p);
134
135         [DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
136         public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
137
138         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct_array")]
139         public static extern int mono_test_marshal_struct_array (SimpleStruct2[] ss);
140
141         [DllImport ("libtest", EntryPoint="mono_test_marshal_long_align_struct_array")]
142         public static extern int mono_test_marshal_long_align_struct_array (LongAlignStruct[] ss);
143
144         [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
145         public static extern SimpleClass mono_test_marshal_class (int i, int j, int k, SimpleClass ss, int l);
146
147         [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_class")]
148         public static extern int mono_test_marshal_byref_class (ref SimpleClass ss);
149
150         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
151         public static extern int mono_test_marshal_delegate (SimpleDelegate d);
152
153         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate_struct")]
154         public static extern int mono_test_marshal_delegate_struct (DelegateStruct d);
155
156         [DllImport ("libtest", EntryPoint="mono_test_return_vtype")]
157         public static extern SimpleStruct mono_test_return_vtype (IntPtr i);
158
159         [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
160         public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
161
162         [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_unicode", CharSet=CharSet.Unicode)]
163         public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb, int len);
164
165         [DllImport ("libtest", EntryPoint="mono_test_last_error", SetLastError=true)]
166         public static extern void mono_test_last_error (int err);
167
168         [DllImport ("libtest", EntryPoint="mono_test_asany")]
169         public static extern int mono_test_asany ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
170
171         [DllImport ("libtest", EntryPoint="mono_test_asany", CharSet=CharSet.Unicode)]
172         public static extern int mono_test_asany_unicode ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
173
174         public delegate int SimpleDelegate (int a);
175
176         public static int Main () {
177                 return TestDriver.RunTests (typeof (Tests));
178         }
179
180         static int test_0_marshal_char () {
181                 return mono_test_marshal_char ('a');
182         }
183
184         static int test_0_marshal_char_array () {
185                 // char[] is implicitly marshalled as [Out]
186                 char[] buf = new char [32];
187                 mono_test_marshal_char_array (buf);
188                 string s = new string (buf);
189                 if (s.StartsWith ("abcdef"))
190                         return 0;
191                 else
192                         return 1;
193         }
194
195         static int test_1225_marshal_array () {
196                 int [] a1 = new int [50];
197                 for (int i = 0; i < 50; i++)
198                         a1 [i] = i;
199
200                 return mono_test_marshal_array (a1);
201         }
202
203         static int test_1225_marshal_inout_array () {
204                 int [] a1 = new int [50];
205                 for (int i = 0; i < 50; i++)
206                         a1 [i] = i;
207
208                 int res = mono_test_marshal_inout_array (a1);
209
210                 for (int i = 0; i < 50; i++)
211                         if (a1 [i] != 50 - i) {
212                                 Console.WriteLine ("X: " + i + " " + a1 [i]);
213                                 return 2;
214                         }
215
216                 return res;
217         }
218
219         static int test_0_marshal_inout_nonblittable_array () {
220                 char [] a1 = new char [10];
221                 for (int i = 0; i < 10; i++)
222                         a1 [i] = "Hello, World" [i];
223
224                 int res = mono_test_marshal_inout_nonblittable_array (a1);
225
226                 for (int i = 0; i < 10; i++)
227                         if (a1 [i] != 'F')
228                                 return 2;
229
230                 return res;
231         }
232
233         static int test_0_marshal_struct () {
234                 SimpleStruct ss = new  SimpleStruct ();
235                 ss.b = true;
236                 ss.d = "TEST";
237                 
238                 return mono_test_marshal_struct (ss);
239         }
240
241         static int test_0_marshal_struct2 () {
242                 SimpleStruct2 ss2 = new  SimpleStruct2 ();
243                 ss2.b = true;
244                 ss2.d = "TEST";
245                 ss2.e = 99;
246                 ss2.f = 1.5;
247                 ss2.g = 42;
248                 ss2.h = 123L;
249
250                 return mono_test_marshal_struct2 (ss2);
251         }
252
253         static int test_0_marshal_struct3 () {
254                 SimpleStruct2 ss2 = new  SimpleStruct2 ();
255                 ss2.b = true;
256                 ss2.d = "TEST";
257                 ss2.e = 99;
258                 ss2.f = 1.5;
259                 ss2.g = 42;
260                 ss2.h = 123L;
261
262                 return mono_test_marshal_struct2_2 (10, 11, 12, ss2);
263         }
264
265         static int test_0_marshal_empty_struct () {
266                 EmptyStruct es = new EmptyStruct ();
267
268                 if (mono_test_empty_struct (1, es, 2) != 0)
269                         return 1;
270                 
271                 return 0;
272         }
273
274         static int test_0_marshal_struct_array () {
275                 SimpleStruct2[] ss_arr = new SimpleStruct2 [2];
276
277                 SimpleStruct2 ss2 = new SimpleStruct2 ();
278                 ss2.b = true;
279                 ss2.d = "TEST";
280                 ss2.e = 99;
281                 ss2.f = 1.5;
282                 ss2.g = 42;
283                 ss2.h = 123L;
284
285                 ss_arr [0] = ss2;
286
287                 ss2.b = false;
288                 ss2.d = "TEST2";
289                 ss2.e = 100;
290                 ss2.f = 2.5;
291                 ss2.g = 43;
292                 ss2.h = 124L;
293
294                 ss_arr [1] = ss2;
295
296                 return mono_test_marshal_struct_array (ss_arr);
297         }
298
299         static int test_105_marshal_long_align_struct_array () {
300                 LongAlignStruct[] ss_arr = new LongAlignStruct [2];
301
302                 LongAlignStruct ss = new LongAlignStruct ();
303                 ss.a = 5;
304                 ss.b = 10;
305                 ss.c = 15;
306
307                 ss_arr [0] = ss;
308
309                 ss.a = 20;
310                 ss.b = 25;
311                 ss.c = 30;
312
313                 ss_arr [1] = ss;
314
315                 return mono_test_marshal_long_align_struct_array (ss_arr);
316         }
317
318         /* Test classes as arguments and return values */
319         static int test_0_marshal_class () {
320                 SimpleClass ss = new  SimpleClass ();
321                 ss.b = true;
322                 ss.d = "TEST";
323                 ss.e = 99;
324                 ss.f = 1.5;
325                 ss.g = 42;
326                 ss.h = 123L;
327
328                 SimpleClass res = mono_test_marshal_class (10, 11, 12, ss, 14);
329                 if (res == null)
330                         return 1;
331                 if  (! (res.a == ss.a && res.b == ss.b && res.c == ss.c && 
332                                 res.d == ss.d && res.e == ss.e && res.f == ss.f &&
333                                 res.g == ss.g && res.h == ss.h))
334                         return 2;
335
336                 /* Test null arguments and results */
337                 res = mono_test_marshal_class (10, 11, 12, null, 14);
338                 if (res != null)
339                         return 3;
340
341                 return 0;
342         }
343
344         static int test_0_marshal_byref_class () {
345                 SimpleClass ss = new  SimpleClass ();
346                 ss.b = true;
347                 ss.d = "TEST";
348                 ss.e = 99;
349                 ss.f = 1.5;
350                 ss.g = 42;
351                 ss.h = 123L;
352
353                 int res = mono_test_marshal_byref_class (ref ss);
354                 if (ss.d != "TEST-RES")
355                         return 1;
356
357                 return 0;
358         }
359
360         static int test_0_marshal_delegate () {
361                 SimpleDelegate d = new SimpleDelegate (delegate_test);
362
363                 return mono_test_marshal_delegate (d);
364         }
365
366         static int test_0_marshal_delegate_struct () {
367                 DelegateStruct s = new DelegateStruct ();
368
369                 s.a = 2;
370                 s.del = new SimpleDelegate (delegate_test);
371                 s.del2 = new SimpleDelegate (delegate_test);
372
373                 return mono_test_marshal_delegate_struct (s);
374         }
375
376         static int test_0_marshal_point () {
377                 Point pt = new Point();
378                 pt.x = 1.25;
379                 pt.y = 3.5;
380                 
381                 return mono_test_marshal_point(pt);
382         }
383
384         static int test_0_marshal_mixed_point () {
385                 MixedPoint mpt = new MixedPoint();
386                 mpt.x = 5;
387                 mpt.y = 6.75;
388                 
389                 return mono_test_marshal_mixed_point(mpt);
390         }
391
392         static int test_0_marshal_bool_byref () {
393                 bool b = true;
394                 if (mono_test_marshal_bool_byref (99, ref b, 100) != 1)
395                         return 1;
396                 b = false;
397                 if (mono_test_marshal_bool_byref (99, ref b, 100) != 0)
398                         return 12;
399                 if (b != true)
400                         return 13;
401
402                 return 0;
403         }
404
405         static int test_0_return_vtype () {
406                 SimpleStruct ss = mono_test_return_vtype (new IntPtr (5));
407
408                 if (!ss.a && ss.b && !ss.c && ss.d == "TEST")
409                         return 0;
410                 
411                 return 1;
412         }
413
414         static int test_0_marshal_stringbuilder () {
415                 StringBuilder sb = new StringBuilder(255);
416                 mono_test_marshal_stringbuilder (sb, sb.Capacity);
417                 String res = sb.ToString();
418
419                 if (res != "This is my message.  Isn't it nice?")
420                         return 1;  
421                 
422                 return 0;
423         }
424
425         static int test_0_marshal_stringbuilder_unicode () {
426                 StringBuilder sb = new StringBuilder(255);
427                 mono_test_marshal_stringbuilder_unicode (sb, sb.Capacity);
428                 String res = sb.ToString();
429
430                 if (res != "This is my message.  Isn't it nice?")
431                         return 1;  
432                 
433                 return 0;
434         }
435
436         static int test_0_marshal_empty_string_array () {
437                 return mono_test_marshal_empty_string_array (null);
438         }
439
440         static int test_0_marshal_string_array () {
441                 return mono_test_marshal_string_array (new String [] { "ABC", "DEF" });
442         }
443
444         static int test_0_marshal_unicode_string_array () {
445                 return mono_test_marshal_unicode_string_array (new String [] { "ABC", "DEF" }, new String [] { "ABC", "DEF" });
446         }
447
448         static int test_0_last_error () {
449                 mono_test_last_error (5);
450                 if (Marshal.GetLastWin32Error () == 5)
451                         return 0;
452                 else
453                         return 1;
454         }
455
456         static int test_0_library_not_found () {
457
458                 try {
459                         mono_entry_point_not_found ();
460                         return 1;
461                 }
462                 catch (EntryPointNotFoundException) {
463                 }
464
465                 return 0;
466         }
467
468         static int test_0_entry_point_not_found () {
469
470                 try {
471                         mono_library_not_found ();
472                         return 1;
473                 }
474                 catch (DllNotFoundException) {
475                 }
476
477                 return 0;
478         }
479
480         /* Check that the runtime trims .dll from the library name */
481         static int test_0_trim_dll_from_name () {
482
483                 mono_test_marshal_char_2 ('A');
484
485                 return 0;
486         }
487
488         class C {
489                 public int i;
490         }
491
492         static int test_0_asany () {
493                 if (mono_test_asany (5, 1) != 0)
494                         return 1;
495
496                 if (mono_test_asany ("ABC", 2) != 0)
497                         return 2;
498
499                 SimpleStruct2 ss2 = new  SimpleStruct2 ();
500                 ss2.b = true;
501                 ss2.d = "TEST";
502                 ss2.e = 99;
503                 ss2.f = 1.5;
504                 ss2.g = 42;
505                 ss2.h = 123L;
506
507                 if (mono_test_asany (ss2, 3) != 0)
508                         return 3;
509
510                 if (mono_test_asany_unicode ("ABC", 4) != 0)
511                         return 4;
512
513                 try {
514                         C c = new C ();
515                         c.i = 5;
516                         mono_test_asany (c, 0);
517                         return 5;
518                 }
519                 catch (ArgumentException) {
520                 }
521
522                 try {
523                         mono_test_asany (new Object (), 0);
524                         return 6;
525                 }
526                 catch (ArgumentException) {
527                 }
528
529                 return 0;
530         }
531 }