2009-03-04 Zoltan Varga <vargaz@gmail.com>
[mono-debugger.git] / mono / tests / pinvoke2.cs
blobd49c63ea4378eec3c5827e70212afc5170a2dd24
1 using System;
2 using System.Text;
3 using System.Runtime.InteropServices;
4 using System.Runtime.CompilerServices;
6 public class Tests {
8 public int int_field;
10 public static int delegate_test (int a)
12 if (a == 2)
13 return 0;
15 return 1;
18 public int delegate_test_instance (int a)
20 return int_field + a;
23 [StructLayout (LayoutKind.Sequential)]
24 public struct SimpleStruct {
25 public bool a;
26 public bool b;
27 public bool c;
28 public string d;
29 [MarshalAs(UnmanagedType.LPWStr)]
30 public string d2;
33 [StructLayout (LayoutKind.Sequential)]
34 public struct SimpleStructGen<T> {
35 public bool a;
36 public bool b;
37 public bool c;
38 public string d;
39 [MarshalAs(UnmanagedType.LPWStr)]
40 public string d2;
43 [StructLayout (LayoutKind.Sequential)]
44 public struct SimpleStruct2 {
45 public bool a;
46 public bool b;
47 public bool c;
48 public string d;
49 public byte e;
50 public double f;
51 public byte g;
52 public long h;
55 [StructLayout (LayoutKind.Sequential, Size=0)]
56 public struct EmptyStruct {
59 [StructLayout (LayoutKind.Sequential)]
60 public struct DelegateStruct {
61 public int a;
62 public SimpleDelegate del;
63 [MarshalAs(UnmanagedType.FunctionPtr)]
64 public SimpleDelegate del2;
65 [MarshalAs(UnmanagedType.FunctionPtr)]
66 public SimpleDelegate del3;
69 /* sparcv9 has complex conventions when passing structs with doubles in them
70 by value, some simple tests for them */
71 [StructLayout (LayoutKind.Sequential)]
72 public struct Point {
73 public double x;
74 public double y;
77 [StructLayout (LayoutKind.Sequential)]
78 public struct MixedPoint {
79 public int x;
80 public double y;
83 [StructLayout (LayoutKind.Sequential)]
84 public class SimpleClass {
85 public bool a;
86 public bool b;
87 public bool c;
88 public string d;
89 public byte e;
90 public double f;
91 public byte g;
92 public long h;
95 [StructLayout (LayoutKind.Sequential)]
96 public class EmptyClass {
99 [StructLayout (LayoutKind.Sequential)]
100 public struct LongAlignStruct {
101 public int a;
102 public long b;
103 public long c;
106 [StructLayout(LayoutKind.Sequential)]
107 public class BlittableClass
109 public int a = 1;
110 public int b = 2;
113 [StructLayout (LayoutKind.Sequential)]
114 class SimpleObj
116 public string str;
117 public int i;
120 [StructLayout(LayoutKind.Sequential)]
121 struct AsAnyStruct
123 public int i;
124 public int j;
125 public int k;
126 public String s;
128 public AsAnyStruct(int i, int j, int k, String s) {
129 this.i = i;
130 this.j = j;
131 this.k = k;
132 this.s = s;
136 [StructLayout(LayoutKind.Sequential)]
137 class AsAnyClass
139 public int i;
140 public int j;
141 public int k;
142 public String s;
144 public AsAnyClass(int i, int j, int k, String s) {
145 this.i = i;
146 this.j = j;
147 this.k = k;
151 [DllImport ("libnot-found", EntryPoint="not_found")]
152 public static extern int mono_library_not_found ();
154 [DllImport ("libtest", EntryPoint="not_found")]
155 public static extern int mono_entry_point_not_found ();
157 [DllImport ("libtest.dll", EntryPoint="mono_test_marshal_char")]
158 public static extern int mono_test_marshal_char_2 (char a1);
160 [DllImport ("test", EntryPoint="mono_test_marshal_char")]
161 public static extern int mono_test_marshal_char_3 (char a1);
163 [DllImport ("libtest", EntryPoint="mono_test_marshal_char")]
164 public static extern int mono_test_marshal_char (char a1);
166 [DllImport ("libtest", EntryPoint="mono_test_marshal_char_array", CharSet=CharSet.Unicode)]
167 public static extern int mono_test_marshal_char_array (char[] a1);
169 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_byref")]
170 public static extern int mono_test_marshal_bool_byref (int a, ref bool b, int c);
172 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_in_as_I1_U1")]
173 public static extern int mono_test_marshal_bool_in_as_I1 ([MarshalAs (UnmanagedType.I1)] bool bTrue, [MarshalAs (UnmanagedType.I1)] bool bFalse);
175 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_in_as_I1_U1")]
176 public static extern int mono_test_marshal_bool_in_as_U1 ([MarshalAs (UnmanagedType.U1)] bool bTrue, [MarshalAs (UnmanagedType.U1)] bool bFalse);
178 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_out_as_I1_U1")]
179 public static extern int mono_test_marshal_bool_out_as_I1 ([MarshalAs (UnmanagedType.I1)] out bool bTrue, [MarshalAs (UnmanagedType.I1)] out bool bFalse);
181 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_out_as_I1_U1")]
182 public static extern int mono_test_marshal_bool_out_as_U1 ([MarshalAs (UnmanagedType.U1)] out bool bTrue, [MarshalAs (UnmanagedType.U1)] out bool bFalse);
184 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_ref_as_I1_U1")]
185 public static extern int mono_test_marshal_bool_ref_as_I1 ([MarshalAs (UnmanagedType.I1)] ref bool bTrue, [MarshalAs (UnmanagedType.I1)] ref bool bFalse);
187 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_ref_as_I1_U1")]
188 public static extern int mono_test_marshal_bool_ref_as_U1 ([MarshalAs (UnmanagedType.U1)] ref bool bTrue, [MarshalAs (UnmanagedType.U1)] ref bool bFalse);
190 [DllImport ("libtest", EntryPoint="mono_test_marshal_array")]
191 public static extern int mono_test_marshal_array (int [] a1);
193 [DllImport ("libtest", EntryPoint="mono_test_marshal_empty_string_array")]
194 public static extern int mono_test_marshal_empty_string_array (string [] a1);
196 [DllImport ("libtest", EntryPoint="mono_test_marshal_string_array")]
197 public static extern int mono_test_marshal_string_array (string [] a1);
199 [DllImport ("libtest", EntryPoint="mono_test_marshal_unicode_string_array", CharSet=CharSet.Unicode)]
200 public static extern int mono_test_marshal_unicode_string_array (string [] a1, [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]string [] a2);
202 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_array")]
203 public static extern int mono_test_marshal_stringbuilder_array (StringBuilder [] a1);
205 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_array")]
206 public static extern int mono_test_marshal_inout_array ([In, Out] int [] a1);
208 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_array")]
209 public static extern int mono_test_marshal_out_array ([Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] int [] a1, int n);
211 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_nonblittable_array", CharSet = CharSet.Unicode)]
212 public static extern int mono_test_marshal_inout_nonblittable_array ([In, Out] char [] a1);
214 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
215 public static extern int mono_test_marshal_struct (SimpleStruct ss);
217 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
218 public static extern int mono_test_marshal_struct_gen (SimpleStructGen<string> ss);
220 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2")]
221 public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss);
223 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2_2")]
224 public static extern int mono_test_marshal_struct2_2 (int i, int j, int k, SimpleStruct2 ss);
226 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
227 public static extern int mono_test_marshal_byref_struct (ref SimpleStruct ss, bool a, bool b, bool c, String d);
229 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
230 public static extern int mono_test_marshal_byref_struct_in ([In] ref SimpleStruct ss, bool a, bool b, bool c, String d);
232 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
233 public static extern int mono_test_marshal_byref_struct_inout ([In, Out] ref SimpleStruct ss, bool a, bool b, bool c, String d);
235 [DllImport ("libtest", EntryPoint="mono_test_marshal_point")]
236 public static extern int mono_test_marshal_point (Point p);
238 [DllImport ("libtest", EntryPoint="mono_test_marshal_mixed_point")]
239 public static extern int mono_test_marshal_mixed_point (MixedPoint p);
241 [DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
242 public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
244 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct")]
245 public static extern int mono_test_marshal_lpstruct ([In, MarshalAs(UnmanagedType.LPStruct)] SimpleStruct ss);
247 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct_blittable")]
248 public static extern int mono_test_marshal_lpstruct_blittable ([In, MarshalAs(UnmanagedType.LPStruct)] Point p);
250 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct_array")]
251 public static extern int mono_test_marshal_struct_array (SimpleStruct2[] ss);
253 [DllImport ("libtest", EntryPoint="mono_test_marshal_long_align_struct_array")]
254 public static extern int mono_test_marshal_long_align_struct_array (LongAlignStruct[] ss);
256 [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
257 public static extern SimpleClass mono_test_marshal_class (int i, int j, int k, SimpleClass ss, int l);
259 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_class")]
260 public static extern int mono_test_marshal_byref_class (ref SimpleClass ss);
262 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
263 public static extern int mono_test_marshal_delegate (SimpleDelegate d);
265 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate_struct")]
266 public static extern DelegateStruct mono_test_marshal_delegate_struct (DelegateStruct d);
268 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate")]
269 public static extern SimpleDelegate mono_test_marshal_return_delegate (SimpleDelegate d);
271 [DllImport ("libtest", EntryPoint="mono_test_return_vtype")]
272 public static extern SimpleStruct mono_test_return_vtype (IntPtr i);
274 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
275 public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
277 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_default")]
278 public static extern void mono_test_marshal_stringbuilder_default (StringBuilder sb, int len);
280 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_unicode", CharSet=CharSet.Unicode)]
281 public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb, int len);
283 [DllImport ("libtest", EntryPoint="mono_test_last_error", SetLastError=true)]
284 public static extern void mono_test_last_error (int err);
286 [DllImport ("libtest", EntryPoint="mono_test_asany")]
287 public static extern int mono_test_asany ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
289 [DllImport ("libtest", EntryPoint="mono_test_asany", CharSet=CharSet.Unicode)]
290 public static extern int mono_test_asany_unicode ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
292 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_in")]
293 static extern void mono_test_asany_in ([MarshalAs(UnmanagedType.AsAny)][In] object obj);
295 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_out")]
296 static extern void mono_test_asany_out ([MarshalAs(UnmanagedType.AsAny)][Out] object obj);
297 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_inout")]
298 static extern void mono_test_asany_inout ([MarshalAs(UnmanagedType.AsAny)][In, Out] object obj);
300 [DllImport ("libtest")]
301 static extern int class_marshal_test0 (SimpleObj obj);
303 [DllImport ("libtest")]
304 static extern void class_marshal_test1 (out SimpleObj obj);
306 [DllImport ("libtest")]
307 static extern int class_marshal_test4 (SimpleObj obj);
309 [DllImport ("libtest")]
310 static extern int string_marshal_test0 (string str);
312 [DllImport ("libtest")]
313 static extern void string_marshal_test1 (out string str);
315 [DllImport ("libtest")]
316 static extern int string_marshal_test2 (ref string str);
318 [DllImport ("libtest")]
319 static extern int string_marshal_test3 (string str);
321 public delegate int SimpleDelegate (int a);
323 public static int Main (string[] args) {
324 return TestDriver.RunTests (typeof (Tests), args);
327 public static int test_0_marshal_char () {
328 return mono_test_marshal_char ('a');
331 public static int test_0_marshal_char_array () {
332 // a unicode char[] is implicitly marshalled as [Out]
333 char[] buf = new char [32];
334 mono_test_marshal_char_array (buf);
335 string s = new string (buf);
336 if (s.StartsWith ("abcdef"))
337 return 0;
338 else
339 return 1;
342 public static int test_1225_marshal_array () {
343 int [] a1 = new int [50];
344 for (int i = 0; i < 50; i++)
345 a1 [i] = i;
347 return mono_test_marshal_array (a1);
350 public static int test_1225_marshal_inout_array () {
351 int [] a1 = new int [50];
352 for (int i = 0; i < 50; i++)
353 a1 [i] = i;
355 int res = mono_test_marshal_inout_array (a1);
357 for (int i = 0; i < 50; i++)
358 if (a1 [i] != 50 - i) {
359 Console.WriteLine ("X: " + i + " " + a1 [i]);
360 return 2;
363 return res;
366 public static int test_0_marshal_out_array () {
367 int [] a1 = new int [50];
369 int res = mono_test_marshal_out_array (a1, 0);
371 for (int i = 0; i < 50; i++)
372 if (a1 [i] != i) {
373 Console.WriteLine ("X: " + i + " " + a1 [i]);
374 return 2;
377 return 0;
380 public static int test_0_marshal_inout_nonblittable_array () {
381 char [] a1 = new char [10];
382 for (int i = 0; i < 10; i++)
383 a1 [i] = "Hello, World" [i];
385 int res = mono_test_marshal_inout_nonblittable_array (a1);
387 for (int i = 0; i < 10; i++)
388 if (a1 [i] != 'F')
389 return 2;
391 return res;
394 public static int test_0_marshal_struct () {
395 SimpleStruct ss = new SimpleStruct ();
396 ss.b = true;
397 ss.d = "TEST";
399 return mono_test_marshal_struct (ss);
402 public static int test_0_marshal_struct_gen () {
403 SimpleStructGen<string> ss = new SimpleStructGen<string> ();
404 ss.b = true;
405 ss.d = "TEST";
407 return mono_test_marshal_struct_gen (ss);
410 public static int test_0_marshal_struct2 () {
411 SimpleStruct2 ss2 = new SimpleStruct2 ();
412 ss2.b = true;
413 ss2.d = "TEST";
414 ss2.e = 99;
415 ss2.f = 1.5;
416 ss2.g = 42;
417 ss2.h = 123L;
419 return mono_test_marshal_struct2 (ss2);
422 public static int test_0_marshal_struct3 () {
423 SimpleStruct2 ss2 = new SimpleStruct2 ();
424 ss2.b = true;
425 ss2.d = "TEST";
426 ss2.e = 99;
427 ss2.f = 1.5;
428 ss2.g = 42;
429 ss2.h = 123L;
431 return mono_test_marshal_struct2_2 (10, 11, 12, ss2);
434 public static int test_0_marshal_empty_struct () {
435 EmptyStruct es = new EmptyStruct ();
437 if (mono_test_empty_struct (1, es, 2) != 0)
438 return 1;
440 return 0;
443 public static int test_0_marshal_lpstruct () {
444 SimpleStruct ss = new SimpleStruct ();
445 ss.b = true;
446 ss.d = "TEST";
448 return mono_test_marshal_lpstruct (ss);
451 public static int test_0_marshal_lpstruct_blittable () {
452 Point p = new Point ();
453 p.x = 1.0;
454 p.y = 2.0;
456 return mono_test_marshal_lpstruct_blittable (p);
459 public static int test_0_marshal_struct_array () {
460 SimpleStruct2[] ss_arr = new SimpleStruct2 [2];
462 SimpleStruct2 ss2 = new SimpleStruct2 ();
463 ss2.b = true;
464 ss2.d = "TEST";
465 ss2.e = 99;
466 ss2.f = 1.5;
467 ss2.g = 42;
468 ss2.h = 123L;
470 ss_arr [0] = ss2;
472 ss2.b = false;
473 ss2.d = "TEST2";
474 ss2.e = 100;
475 ss2.f = 2.5;
476 ss2.g = 43;
477 ss2.h = 124L;
479 ss_arr [1] = ss2;
481 return mono_test_marshal_struct_array (ss_arr);
484 public static int test_105_marshal_long_align_struct_array () {
485 LongAlignStruct[] ss_arr = new LongAlignStruct [2];
487 LongAlignStruct ss = new LongAlignStruct ();
488 ss.a = 5;
489 ss.b = 10;
490 ss.c = 15;
492 ss_arr [0] = ss;
494 ss.a = 20;
495 ss.b = 25;
496 ss.c = 30;
498 ss_arr [1] = ss;
500 return mono_test_marshal_long_align_struct_array (ss_arr);
503 /* Test classes as arguments and return values */
504 public static int test_0_marshal_class () {
505 SimpleClass ss = new SimpleClass ();
506 ss.b = true;
507 ss.d = "TEST";
508 ss.e = 99;
509 ss.f = 1.5;
510 ss.g = 42;
511 ss.h = 123L;
513 SimpleClass res = mono_test_marshal_class (10, 11, 12, ss, 14);
514 if (res == null)
515 return 1;
516 if (! (res.a == ss.a && res.b == ss.b && res.c == ss.c &&
517 res.d == ss.d && res.e == ss.e && res.f == ss.f &&
518 res.g == ss.g && res.h == ss.h))
519 return 2;
521 /* Test null arguments and results */
522 res = mono_test_marshal_class (10, 11, 12, null, 14);
523 if (res != null)
524 return 3;
526 return 0;
529 public static int test_0_marshal_byref_class () {
530 SimpleClass ss = new SimpleClass ();
531 ss.b = true;
532 ss.d = "TEST";
533 ss.e = 99;
534 ss.f = 1.5;
535 ss.g = 42;
536 ss.h = 123L;
538 int res = mono_test_marshal_byref_class (ref ss);
539 if (ss.d != "TEST-RES")
540 return 1;
542 return 0;
545 public static int test_0_marshal_delegate () {
546 SimpleDelegate d = new SimpleDelegate (delegate_test);
548 return mono_test_marshal_delegate (d);
551 public static int test_34_marshal_instance_delegate () {
552 Tests t = new Tests ();
553 t.int_field = 32;
554 SimpleDelegate d = new SimpleDelegate (t.delegate_test_instance);
556 return mono_test_marshal_delegate (d);
559 public static int test_0_marshal_return_delegate () {
560 SimpleDelegate d = new SimpleDelegate (delegate_test);
562 SimpleDelegate d2 = mono_test_marshal_return_delegate (d);
564 return d2 (2);
567 public static int test_0_marshal_delegate_struct () {
568 DelegateStruct s = new DelegateStruct ();
570 s.a = 2;
571 s.del = new SimpleDelegate (delegate_test);
572 s.del2 = new SimpleDelegate (delegate_test);
573 s.del3 = null;
575 DelegateStruct res = mono_test_marshal_delegate_struct (s);
577 if (res.a != 0)
578 return 1;
579 if (res.del (2) != 0)
580 return 2;
581 if (res.del2 (2) != 0)
582 return 3;
583 if (res.del3 != null)
584 return 4;
586 return 0;
589 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_delegate")]
590 public static extern int mono_test_marshal_out_delegate (out SimpleDelegate d);
592 public static int test_3_marshal_out_delegate () {
593 SimpleDelegate d = null;
595 mono_test_marshal_out_delegate (out d);
597 return d (2);
600 public static int test_0_marshal_byref_struct () {
601 SimpleStruct s = new SimpleStruct ();
602 s.a = true;
603 s.b = false;
604 s.c = true;
605 s.d = "ABC";
606 s.d2 = "DEF";
608 int res = mono_test_marshal_byref_struct (ref s, true, false, true, "ABC");
609 if (res != 0)
610 return 1;
611 if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
612 return 2;
613 return 0;
616 public static int test_0_marshal_byref_struct_in () {
617 SimpleStruct s = new SimpleStruct ();
618 s.a = true;
619 s.b = false;
620 s.c = true;
621 s.d = "ABC";
622 s.d2 = "DEF";
624 int res = mono_test_marshal_byref_struct_in (ref s, true, false, true, "ABC");
625 if (res != 0)
626 return 1;
627 if (s.a != true || s.b != false || s.c != true || s.d != "ABC")
628 return 2;
629 return 0;
632 public static int test_0_marshal_byref_struct_inout () {
633 SimpleStruct s = new SimpleStruct ();
634 s.a = true;
635 s.b = false;
636 s.c = true;
637 s.d = "ABC";
638 s.d2 = "DEF";
640 int res = mono_test_marshal_byref_struct_inout (ref s, true, false, true, "ABC");
641 if (res != 0)
642 return 1;
643 if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
644 return 2;
645 return 0;
648 public static int test_0_marshal_point () {
649 Point pt = new Point();
650 pt.x = 1.25;
651 pt.y = 3.5;
653 return mono_test_marshal_point(pt);
656 public static int test_0_marshal_mixed_point () {
657 MixedPoint mpt = new MixedPoint();
658 mpt.x = 5;
659 mpt.y = 6.75;
661 return mono_test_marshal_mixed_point(mpt);
664 public static int test_0_marshal_bool_byref () {
665 bool b = true;
666 if (mono_test_marshal_bool_byref (99, ref b, 100) != 1)
667 return 1;
668 b = false;
669 if (mono_test_marshal_bool_byref (99, ref b, 100) != 0)
670 return 12;
671 if (b != true)
672 return 13;
674 return 0;
677 public static int test_0_marshal_bool_as_I1 () {
679 int ret;
680 bool bTrue, bFalse;
681 if ((ret = mono_test_marshal_bool_in_as_I1 (true, false)) != 0)
682 return ret;
684 if ((ret = mono_test_marshal_bool_out_as_I1 (out bTrue, out bFalse)) != 0)
685 return ret;
687 if(!bTrue)
688 return 10;
690 if(bFalse)
691 return 11;
693 if ((ret = mono_test_marshal_bool_ref_as_I1 (ref bTrue, ref bFalse)) != 0)
694 return ret;
696 if(bTrue)
697 return 12;
699 if(!bFalse)
700 return 13;
702 return 0;
705 public static int test_0_marshal_bool_as_U1 () {
707 int ret;
708 bool bTrue, bFalse;
709 if ((ret = mono_test_marshal_bool_in_as_U1 (true, false)) != 0)
710 return ret;
712 if ((ret = mono_test_marshal_bool_out_as_U1 (out bTrue, out bFalse)) != 0)
713 return ret;
715 if(!bTrue)
716 return 10;
718 if(bFalse)
719 return 11;
721 if ((ret = mono_test_marshal_bool_ref_as_U1 (ref bTrue, ref bFalse)) != 0)
722 return ret;
724 if(bTrue)
725 return 12;
727 if(!bFalse)
728 return 13;
730 return 0;
733 public static int test_0_return_vtype () {
734 SimpleStruct ss = mono_test_return_vtype (new IntPtr (5));
736 if (!ss.a && ss.b && !ss.c && ss.d == "TEST" && ss.d2 == "TEST2")
737 return 0;
739 return 1;
742 public static int test_0_marshal_stringbuilder () {
743 StringBuilder sb = new StringBuilder(255);
744 sb.Append ("ABCD");
745 mono_test_marshal_stringbuilder (sb, sb.Capacity);
746 String res = sb.ToString();
748 if (res != "This is my message. Isn't it nice?")
749 return 1;
751 // Test StringBuilder with default capacity (16)
752 StringBuilder sb2 = new StringBuilder();
753 mono_test_marshal_stringbuilder_default (sb2, sb2.Capacity);
754 if (sb2.ToString () != "This is my messa")
755 return 2;
757 return 0;
760 public static int test_0_marshal_stringbuilder_unicode () {
761 StringBuilder sb = new StringBuilder(255);
762 mono_test_marshal_stringbuilder_unicode (sb, sb.Capacity);
763 String res = sb.ToString();
765 if (res != "This is my message. Isn't it nice?")
766 return 1;
768 // Test StringBuilder with default capacity (16)
769 StringBuilder sb2 = new StringBuilder();
770 mono_test_marshal_stringbuilder_unicode (sb2, sb2.Capacity);
771 if (sb2.ToString () != "This is my messa")
772 return 2;
774 return 0;
777 public static int test_0_marshal_empty_string_array () {
778 return mono_test_marshal_empty_string_array (null);
781 public static int test_0_marshal_string_array () {
782 return mono_test_marshal_string_array (new String [] { "ABC", "DEF" });
785 public static int test_0_marshal_unicode_string_array () {
786 return mono_test_marshal_unicode_string_array (new String [] { "ABC", "DEF" }, new String [] { "ABC", "DEF" });
789 public static int test_0_marshal_stringbuilder_array () {
790 StringBuilder sb1 = new StringBuilder ("ABC");
791 StringBuilder sb2 = new StringBuilder ("DEF");
793 int res = mono_test_marshal_stringbuilder_array (new StringBuilder [] { sb1, sb2 });
794 if (res != 0)
795 return res;
796 if (sb1.ToString () != "DEF")
797 return 5;
798 if (sb2.ToString () != "ABC")
799 return 6;
800 return 0;
803 public static int test_0_last_error () {
804 mono_test_last_error (5);
805 if (Marshal.GetLastWin32Error () == 5)
806 return 0;
807 else
808 return 1;
811 public static int test_0_entry_point_not_found () {
813 try {
814 mono_entry_point_not_found ();
815 return 1;
817 catch (EntryPointNotFoundException) {
820 return 0;
823 public static int test_0_library_not_found () {
825 try {
826 mono_library_not_found ();
827 return 1;
829 catch (DllNotFoundException) {
832 return 0;
835 /* Check that the runtime trims .dll from the library name */
836 public static int test_0_trim_dll_from_name () {
838 mono_test_marshal_char_2 ('A');
840 return 0;
843 /* Check that the runtime adds lib to to the library name */
844 public static int test_0_add_lib_to_name () {
846 mono_test_marshal_char_3 ('A');
848 return 0;
851 class C {
852 public int i;
855 public static int test_0_asany () {
856 if (mono_test_asany (5, 1) != 0)
857 return 1;
859 if (mono_test_asany ("ABC", 2) != 0)
860 return 2;
862 SimpleStruct2 ss2 = new SimpleStruct2 ();
863 ss2.b = true;
864 ss2.d = "TEST";
865 ss2.e = 99;
866 ss2.f = 1.5;
867 ss2.g = 42;
868 ss2.h = 123L;
870 if (mono_test_asany (ss2, 3) != 0)
871 return 3;
873 if (mono_test_asany_unicode ("ABC", 4) != 0)
874 return 4;
876 try {
877 C c = new C ();
878 c.i = 5;
879 mono_test_asany (c, 0);
880 return 5;
882 catch (ArgumentException) {
885 try {
886 mono_test_asany (new Object (), 0);
887 return 6;
889 catch (ArgumentException) {
892 return 0;
895 /* AsAny marshalling + [In, Out] */
897 public static int test_0_asany_in () {
898 // Struct
899 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
900 mono_test_asany_in (str);
902 // Formatted Class
903 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
904 mono_test_asany_in (cls);
905 if ((cls.i != 1) || (cls.j != 2) || (cls.k != 3))
906 return 1;
908 // Boxed Struct
909 object obj = new AsAnyStruct(1,2,3, "ABC");
910 mono_test_asany_in (obj);
911 str = (AsAnyStruct)obj;
912 if ((str.i != 1) || (str.j != 2) || (str.k != 3))
913 return 2;
915 return 0;
918 public static int test_0_asany_out () {
919 // Struct
920 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
921 mono_test_asany_out (str);
923 // Formatted Class
924 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
925 mono_test_asany_out (cls);
926 if ((cls.i != 10) || (cls.j != 20) || (cls.k != 30))
927 return 1;
929 // Boxed Struct
930 object obj = new AsAnyStruct(1,2,3, "ABC");
931 mono_test_asany_out (obj);
932 str = (AsAnyStruct)obj;
933 if ((str.i != 10) || (str.j != 20) || (str.k != 30))
934 return 2;
936 return 0;
939 public static int test_0_asany_inout () {
940 // Struct
941 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
942 mono_test_asany_inout (str);
944 // Formatted Class
945 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
946 mono_test_asany_inout (cls);
947 if ((cls.i != 10) || (cls.j != 20) || (cls.k != 30))
948 return 1;
950 // Boxed Struct
951 object obj = new AsAnyStruct(1,2,3, "ABC");
952 mono_test_asany_inout (obj);
953 str = (AsAnyStruct)obj;
954 if ((str.i != 10) || (str.j != 20) || (str.k != 30))
955 return 2;
957 return 0;
960 /* Byref String Array */
962 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_string_array")]
963 public static extern int mono_test_marshal_byref_string_array (ref string[] data);
965 public static int test_0_byref_string_array () {
967 string[] arr = null;
969 if (mono_test_marshal_byref_string_array (ref arr) != 0)
970 return 1;
972 arr = new string[] { "Alpha", "Beta", "Gamma" };
974 if (mono_test_marshal_byref_string_array (ref arr) != 1)
975 return 2;
977 /* FIXME: Test returned array and out case */
979 return 0;
983 * AMD64 small structs-by-value tests.
986 /* TEST 1: 16 byte long INTEGER struct */
988 [StructLayout(LayoutKind.Sequential)]
989 public struct Amd64Struct1 {
990 public int i;
991 public int j;
992 public int k;
993 public int l;
996 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1")]
997 public static extern Amd64Struct1 mono_test_marshal_amd64_pass_return_struct1 (Amd64Struct1 s);
999 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1_many_args")]
1000 public static extern Amd64Struct1 mono_test_marshal_amd64_pass_return_struct1_many_args (Amd64Struct1 s, int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8);
1002 public static int test_0_amd64_struct1 () {
1003 Amd64Struct1 s = new Amd64Struct1 ();
1004 s.i = 5;
1005 s.j = -5;
1006 s.k = 0xffffff;
1007 s.l = 0xfffffff;
1009 Amd64Struct1 s2 = mono_test_marshal_amd64_pass_return_struct1 (s);
1011 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000)) ? 0 : 1;
1014 public static int test_0_amd64_struct1_many_args () {
1015 Amd64Struct1 s = new Amd64Struct1 ();
1016 s.i = 5;
1017 s.j = -5;
1018 s.k = 0xffffff;
1019 s.l = 0xfffffff;
1021 Amd64Struct1 s2 = mono_test_marshal_amd64_pass_return_struct1_many_args (s, 1, 2, 3, 4, 5, 6, 7, 8);
1023 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8)) ? 0 : 1;
1026 /* TEST 2: 8 byte long INTEGER struct */
1028 [StructLayout(LayoutKind.Sequential)]
1029 public struct Amd64Struct2 {
1030 public int i;
1031 public int j;
1034 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct2")]
1035 public static extern Amd64Struct2 mono_test_marshal_amd64_pass_return_struct2 (Amd64Struct2 s);
1037 public static int test_0_amd64_struct2 () {
1038 Amd64Struct2 s = new Amd64Struct2 ();
1039 s.i = 5;
1040 s.j = -5;
1042 Amd64Struct2 s2 = mono_test_marshal_amd64_pass_return_struct2 (s);
1044 return ((s2.i == 6) && (s2.j == -4)) ? 0 : 1;
1047 /* TEST 3: 4 byte long INTEGER struct */
1049 [StructLayout(LayoutKind.Sequential)]
1050 public struct Amd64Struct3 {
1051 public int i;
1054 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct3")]
1055 public static extern Amd64Struct3 mono_test_marshal_amd64_pass_return_struct3 (Amd64Struct3 s);
1057 public static int test_0_amd64_struct3 () {
1058 Amd64Struct3 s = new Amd64Struct3 ();
1059 s.i = -5;
1061 Amd64Struct3 s2 = mono_test_marshal_amd64_pass_return_struct3 (s);
1063 return (s2.i == -4) ? 0 : 1;
1066 /* Test 4: 16 byte long FLOAT struct */
1068 [StructLayout(LayoutKind.Sequential)]
1069 public struct Amd64Struct4 {
1070 public double d1, d2;
1073 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct4")]
1074 public static extern Amd64Struct4 mono_test_marshal_amd64_pass_return_struct4 (Amd64Struct4 s);
1076 public static int test_0_amd64_struct4 () {
1077 Amd64Struct4 s = new Amd64Struct4 ();
1078 s.d1 = 5.0;
1079 s.d2 = -5.0;
1081 Amd64Struct4 s2 = mono_test_marshal_amd64_pass_return_struct4 (s);
1083 return (s2.d1 == 6.0 && s2.d2 == -4.0) ? 0 : 1;
1087 * IA64 struct tests
1090 /* Test 5: Float HFA */
1092 [StructLayout(LayoutKind.Sequential)]
1093 public struct TestStruct5 {
1094 public float d1, d2;
1097 [DllImport ("libtest", EntryPoint="mono_test_marshal_ia64_pass_return_struct5")]
1098 public static extern TestStruct5 mono_test_marshal_ia64_pass_return_struct5 (double d1, double d2, TestStruct5 s, int i, double f3, double f4);
1100 public static int test_0_ia64_struct5 () {
1101 TestStruct5 s = new TestStruct5 ();
1102 s.d1 = 5.0f;
1103 s.d2 = -5.0f;
1105 TestStruct5 s2 = mono_test_marshal_ia64_pass_return_struct5 (1.0, 2.0, s, 5, 3.0, 4.0);
1107 return (s2.d1 == 13.0 && s2.d2 == 7.0) ? 0 : 1;
1110 /* Test 6: Double HFA */
1112 [StructLayout(LayoutKind.Sequential)]
1113 public struct TestStruct6 {
1114 public double d1, d2;
1117 [DllImport ("libtest", EntryPoint="mono_test_marshal_ia64_pass_return_struct6")]
1118 public static extern TestStruct6 mono_test_marshal_ia64_pass_return_struct6 (double d1, double d2, TestStruct6 s, int i, double f3, double f4);
1120 public static int test_0_ia64_struct6 () {
1121 TestStruct6 s = new TestStruct6 ();
1122 s.d1 = 6.0;
1123 s.d2 = -6.0;
1125 TestStruct6 s2 = mono_test_marshal_ia64_pass_return_struct6 (1.0, 2.0, s, 3, 4.0, 5.0);
1127 return (s2.d1 == 12.0 && s2.d2 == 3.0) ? 0 : 1;
1130 /* Blittable class */
1131 [DllImport("libtest")]
1132 private static extern BlittableClass TestBlittableClass (BlittableClass vl);
1134 public static int test_0_marshal_blittable_class () {
1135 BlittableClass v1 = new BlittableClass ();
1137 /* Since it is blittable, it looks like it is passed as in/out */
1138 BlittableClass v2 = TestBlittableClass (v1);
1140 if (v1.a != 2 || v1.b != 3)
1141 return 1;
1143 if (v2.a != 2 || v2.b != 3)
1144 return 2;
1146 // Test null
1147 BlittableClass v3 = TestBlittableClass (null);
1149 if (v3.a != 42 || v3.b != 43)
1150 return 3;
1152 return 0;
1156 * Generic structures
1159 [StructLayout(LayoutKind.Sequential)]
1160 public struct Amd64Struct1Gen<T> {
1161 public T i;
1162 public T j;
1163 public T k;
1164 public T l;
1167 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1")]
1168 public static extern Amd64Struct1Gen<int> mono_test_marshal_amd64_pass_return_struct1_gen (Amd64Struct1Gen<int> s);
1170 public static int test_0_amd64_struct1_gen () {
1171 Amd64Struct1Gen<int> s = new Amd64Struct1Gen<int> ();
1172 s.i = 5;
1173 s.j = -5;
1174 s.k = 0xffffff;
1175 s.l = 0xfffffff;
1177 Amd64Struct1Gen<int> s2 = mono_test_marshal_amd64_pass_return_struct1_gen (s);
1179 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000)) ? 0 : 1;
1183 * Other tests
1186 public static int test_0_marshal_byval_class () {
1187 SimpleObj obj0 = new SimpleObj ();
1188 obj0.str = "T1";
1189 obj0.i = 4;
1191 if (class_marshal_test0 (obj0) != 0)
1192 return 1;
1194 return 0;
1197 public static int test_0_marshal_byval_class_null () {
1198 if (class_marshal_test4 (null) != 0)
1199 return 1;
1201 return 0;
1204 public static int test_0_marshal_out_class () {
1205 SimpleObj obj1;
1207 class_marshal_test1 (out obj1);
1209 if (obj1.str != "ABC")
1210 return 1;
1212 if (obj1.i != 5)
1213 return 2;
1215 return 0;
1218 public static int test_0_marshal_string () {
1219 return string_marshal_test0 ("TEST0");
1222 public static int test_0_marshal_out_string () {
1223 string res;
1225 string_marshal_test1 (out res);
1227 if (res != "TEST1")
1228 return 1;
1230 return 0;
1233 public static int test_0_marshal_byref_string () {
1234 string res = "TEST1";
1236 return string_marshal_test2 (ref res);
1239 public static int test_0_marshal_null_string () {
1240 return string_marshal_test3 (null);
1243 [DllImport ("libtest", EntryPoint="mono_test_stdcall_name_mangling", CallingConvention=CallingConvention.StdCall)]
1244 public static extern int mono_test_stdcall_name_mangling (int a, int b, int c);
1246 public static int test_0_stdcall_name_mangling () {
1247 return mono_test_stdcall_name_mangling (0, 1, 2) == 3 ? 0 : 1;
1250 /* Float test */
1252 [DllImport ("libtest", EntryPoint="mono_test_marshal_pass_return_float")]
1253 public static extern float mono_test_marshal_pass_return_float (float f);
1255 public static int test_0_pass_return_float () {
1256 float f = mono_test_marshal_pass_return_float (1.5f);
1258 return (f == 2.5f) ? 0 : 1;
1262 * Pointers to structures can not be passed
1265 public struct CharInfo {
1266 public char Character;
1267 public short Attributes;
1270 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
1271 public static unsafe extern int mono_test_marshal_ptr_to_struct (CharInfo *ptr);
1273 public static unsafe int test_0_marshal_ptr_to_struct () {
1274 CharInfo [] buffer = new CharInfo [1];
1275 fixed (CharInfo *ptr = &buffer [0]) {
1276 try {
1277 mono_test_marshal_ptr_to_struct (ptr);
1278 return 1;
1280 catch (MarshalDirectiveException) {
1281 return 0;
1284 return 1;
1288 * LPWStr marshalling
1291 [DllImport("libtest", EntryPoint="test_lpwstr_marshal")]
1292 [return: MarshalAs(UnmanagedType.LPWStr)]
1293 private static extern string mono_test_marshal_lpwstr_marshal(
1294 [MarshalAs(UnmanagedType.LPWStr)] string s,
1295 int length );
1297 [DllImport("libtest", EntryPoint="test_lpwstr_marshal", CharSet=CharSet.Unicode)]
1298 private static extern string mono_test_marshal_lpwstr_marshal2(
1299 string s,
1300 int length );
1302 [DllImport("libtest", EntryPoint="test_lpwstr_marshal_out")]
1303 private static extern void mono_test_marshal_lpwstr_out_marshal(
1304 [MarshalAs(UnmanagedType.LPWStr)] out string s);
1306 [DllImport("libtest", EntryPoint="test_lpwstr_marshal_out", CharSet=CharSet.Unicode)]
1307 private static extern void mono_test_marshal_lpwstr_out_marshal2(
1308 out string s);
1310 public static int test_0_pass_return_lwpstr () {
1311 string s;
1313 mono_test_marshal_lpwstr_out_marshal (out s);
1315 if (s != "ABC")
1316 return 1;
1318 s = null;
1319 mono_test_marshal_lpwstr_out_marshal2 (out s);
1321 if (s != "ABC")
1322 return 2;
1324 return 0;
1327 public static int test_0_out_lwpstr () {
1328 string s = "ABC";
1330 string res = mono_test_marshal_lpwstr_marshal (s, s.Length);
1332 if (res != "ABC")
1333 return 1;
1335 string res2 = mono_test_marshal_lpwstr_marshal2 (s, s.Length);
1337 if (res2 != "ABC")
1338 return 2;
1340 return 0;
1344 * Byref bool marshalling
1347 [DllImport("libtest")]
1348 extern static int marshal_test_ref_bool
1350 int i,
1351 [MarshalAs(UnmanagedType.I1)] ref bool b1,
1352 [MarshalAs(UnmanagedType.VariantBool)] ref bool b2,
1353 ref bool b3
1356 public static int test_0_pass_byref_bool () {
1357 for (int i = 0; i < 8; i++)
1359 bool b1 = (i & 4) != 0;
1360 bool b2 = (i & 2) != 0;
1361 bool b3 = (i & 1) != 0;
1362 bool orig_b1 = b1, orig_b2 = b2, orig_b3 = b3;
1363 if (marshal_test_ref_bool(i, ref b1, ref b2, ref b3) != 0)
1364 return 4 * i + 1;
1365 if (b1 != !orig_b1)
1366 return 4 * i + 2;
1367 if (b2 != !orig_b2)
1368 return 4 * i + 3;
1369 if (b3 != !orig_b3)
1370 return 4 * i + 4;
1373 return 0;
1377 * Bool struct field marshalling
1380 struct BoolStruct
1382 public int i;
1383 [MarshalAs(UnmanagedType.I1)] public bool b1;
1384 [MarshalAs(UnmanagedType.VariantBool)] public bool b2;
1385 public bool b3;
1388 [DllImport("libtest")]
1389 extern static int marshal_test_bool_struct(ref BoolStruct s);
1391 public static int test_0_pass_bool_in_struct () {
1392 for (int i = 0; i < 8; i++)
1394 BoolStruct s = new BoolStruct();
1395 s.i = i;
1396 s.b1 = (i & 4) != 0;
1397 s.b2 = (i & 2) != 0;
1398 s.b3 = (i & 1) != 0;
1399 BoolStruct orig = s;
1400 if (marshal_test_bool_struct(ref s) != 0)
1401 return 4 * i + 33;
1402 if (s.b1 != !orig.b1)
1403 return 4 * i + 34;
1404 if (s.b2 != !orig.b2)
1405 return 4 * i + 35;
1406 if (s.b3 != !orig.b3)
1407 return 4 * i + 36;
1410 return 0;
1414 * Alignment of structs containing longs
1417 struct LongStruct2 {
1418 public long l;
1421 struct LongStruct {
1422 public int i;
1423 public LongStruct2 l;
1426 [DllImport("libtest")]
1427 extern static int mono_test_marshal_long_struct (ref LongStruct s);
1429 public static int test_47_pass_long_struct () {
1430 LongStruct s = new LongStruct ();
1431 s.i = 5;
1432 s.l = new LongStruct2 ();
1433 s.l.l = 42;
1435 return mono_test_marshal_long_struct (ref s);
1439 * Invoking pinvoke methods through delegates
1442 delegate int MyDelegate (string name);
1444 [DllImport ("libtest", EntryPoint="mono_test_puts_static")]
1445 public static extern int puts_static (string name);
1447 public static int test_0_invoke_pinvoke_through_delegate () {
1448 puts_static ("A simple Test for PInvoke 1");
1450 MyDelegate d = new MyDelegate (puts_static);
1451 d ("A simple Test for PInvoke 2");
1453 object [] args = {"A simple Test for PInvoke 3"};
1454 d.DynamicInvoke (args);
1456 return 0;
1460 * Missing virtual pinvoke methods
1463 public class T {
1465 public virtual object MyClone ()
1467 return null;
1471 public class T2 : T {
1472 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1473 public override extern object MyClone ();
1476 public static int test_0_missing_virtual_pinvoke_method () {
1477 T2 t = new T2 ();
1479 try {
1480 t.MyClone ();
1481 } catch (Exception ex) {
1482 return 0;
1485 return 1;