3 using System.Runtime.InteropServices;
7 public static int delegate_test (int a)
15 [StructLayout (LayoutKind.Sequential)]
16 public struct SimpleStruct {
23 [StructLayout (LayoutKind.Sequential)]
24 public struct SimpleStruct2 {
35 [StructLayout (LayoutKind.Sequential, Size=0)]
36 public struct EmptyStruct {
39 [StructLayout (LayoutKind.Sequential)]
40 public struct DelegateStruct {
42 public SimpleDelegate del;
43 [MarshalAs(UnmanagedType.FunctionPtr)]
44 public SimpleDelegate del2;
47 /* sparcv9 has complex conventions when passing structs with doubles in them
48 by value, some simple tests for them */
49 [StructLayout (LayoutKind.Sequential)]
55 [StructLayout (LayoutKind.Sequential)]
56 public struct MixedPoint {
61 [StructLayout (LayoutKind.Sequential)]
62 public class SimpleClass {
73 [StructLayout (LayoutKind.Sequential)]
74 public class EmptyClass {
77 [StructLayout (LayoutKind.Sequential)]
78 public struct LongAlignStruct {
84 [StructLayout(LayoutKind.Sequential)]
85 public class VectorList
91 [StructLayout (LayoutKind.Sequential)]
98 [DllImport ("libnot-found", EntryPoint="not_found")]
99 public static extern int mono_library_not_found ();
101 [DllImport ("libtest", EntryPoint="not_found")]
102 public static extern int mono_entry_point_not_found ();
104 [DllImport ("libtest.dll", EntryPoint="mono_test_marshal_char")]
105 public static extern int mono_test_marshal_char_2 (char a1);
107 [DllImport ("test", EntryPoint="mono_test_marshal_char")]
108 public static extern int mono_test_marshal_char_3 (char a1);
110 [DllImport ("libtest", EntryPoint="mono_test_marshal_char")]
111 public static extern int mono_test_marshal_char (char a1);
113 [DllImport ("libtest", EntryPoint="mono_test_marshal_char_array")]
114 public static extern int mono_test_marshal_char_array (char[] a1);
116 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_byref")]
117 public static extern int mono_test_marshal_bool_byref (int a, ref bool b, int c);
119 [DllImport ("libtest", EntryPoint="mono_test_marshal_array")]
120 public static extern int mono_test_marshal_array (int [] a1);
122 [DllImport ("libtest", EntryPoint="mono_test_marshal_empty_string_array")]
123 public static extern int mono_test_marshal_empty_string_array (string [] a1);
125 [DllImport ("libtest", EntryPoint="mono_test_marshal_string_array")]
126 public static extern int mono_test_marshal_string_array (string [] a1);
128 [DllImport ("libtest", EntryPoint="mono_test_marshal_unicode_string_array", CharSet=CharSet.Unicode)]
129 public static extern int mono_test_marshal_unicode_string_array (string [] a1, [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]string [] a2);
131 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_array")]
132 public static extern int mono_test_marshal_stringbuilder_array (StringBuilder [] a1);
134 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_array")]
135 public static extern int mono_test_marshal_inout_array ([In, Out] int [] a1);
137 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_nonblittable_array", CharSet = CharSet.Unicode)]
138 public static extern int mono_test_marshal_inout_nonblittable_array ([In, Out] char [] a1);
140 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
141 public static extern int mono_test_marshal_struct (SimpleStruct ss);
143 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2")]
144 public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss);
146 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2_2")]
147 public static extern int mono_test_marshal_struct2_2 (int i, int j, int k, SimpleStruct2 ss);
149 [DllImport ("libtest", EntryPoint="mono_test_marshal_point")]
150 public static extern int mono_test_marshal_point (Point p);
152 [DllImport ("libtest", EntryPoint="mono_test_marshal_mixed_point")]
153 public static extern int mono_test_marshal_mixed_point (MixedPoint p);
155 [DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
156 public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
158 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct_array")]
159 public static extern int mono_test_marshal_struct_array (SimpleStruct2[] ss);
161 [DllImport ("libtest", EntryPoint="mono_test_marshal_long_align_struct_array")]
162 public static extern int mono_test_marshal_long_align_struct_array (LongAlignStruct[] ss);
164 [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
165 public static extern SimpleClass mono_test_marshal_class (int i, int j, int k, SimpleClass ss, int l);
167 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_class")]
168 public static extern int mono_test_marshal_byref_class (ref SimpleClass ss);
170 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
171 public static extern int mono_test_marshal_delegate (SimpleDelegate d);
173 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate_struct")]
174 public static extern int mono_test_marshal_delegate_struct (DelegateStruct d);
176 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate")]
177 public static extern SimpleDelegate mono_test_marshal_return_delegate (SimpleDelegate d);
179 [DllImport ("libtest", EntryPoint="mono_test_return_vtype")]
180 public static extern SimpleStruct mono_test_return_vtype (IntPtr i);
182 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
183 public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
185 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_unicode", CharSet=CharSet.Unicode)]
186 public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb, int len);
188 [DllImport ("libtest", EntryPoint="mono_test_last_error", SetLastError=true)]
189 public static extern void mono_test_last_error (int err);
191 [DllImport ("libtest", EntryPoint="mono_test_asany")]
192 public static extern int mono_test_asany ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
194 [DllImport ("libtest", EntryPoint="mono_test_asany", CharSet=CharSet.Unicode)]
195 public static extern int mono_test_asany_unicode ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
197 [DllImport ("libtest")]
198 static extern int class_marshal_test0 (SimpleObj obj);
200 [DllImport ("libtest")]
201 static extern void class_marshal_test1 (out SimpleObj obj);
203 [DllImport ("libtest")]
204 static extern int class_marshal_test2 (ref SimpleObj obj);
206 [DllImport ("libtest")]
207 static extern int class_marshal_test4 (SimpleObj obj);
209 public delegate int SimpleDelegate (int a);
211 public static int Main (string[] args) {
212 return TestDriver.RunTests (typeof (Tests), args);
215 static int test_0_marshal_char () {
216 return mono_test_marshal_char ('a');
219 static int test_0_marshal_char_array () {
220 // char[] is implicitly marshalled as [Out]
221 char[] buf = new char [32];
222 mono_test_marshal_char_array (buf);
223 string s = new string (buf);
224 if (s.StartsWith ("abcdef"))
230 static int test_1225_marshal_array () {
231 int [] a1 = new int [50];
232 for (int i = 0; i < 50; i++)
235 return mono_test_marshal_array (a1);
238 static int test_1225_marshal_inout_array () {
239 int [] a1 = new int [50];
240 for (int i = 0; i < 50; i++)
243 int res = mono_test_marshal_inout_array (a1);
245 for (int i = 0; i < 50; i++)
246 if (a1 [i] != 50 - i) {
247 Console.WriteLine ("X: " + i + " " + a1 [i]);
254 static int test_0_marshal_inout_nonblittable_array () {
255 char [] a1 = new char [10];
256 for (int i = 0; i < 10; i++)
257 a1 [i] = "Hello, World" [i];
259 int res = mono_test_marshal_inout_nonblittable_array (a1);
261 for (int i = 0; i < 10; i++)
268 static int test_0_marshal_struct () {
269 SimpleStruct ss = new SimpleStruct ();
273 return mono_test_marshal_struct (ss);
276 static int test_0_marshal_struct2 () {
277 SimpleStruct2 ss2 = new SimpleStruct2 ();
285 return mono_test_marshal_struct2 (ss2);
288 static int test_0_marshal_struct3 () {
289 SimpleStruct2 ss2 = new SimpleStruct2 ();
297 return mono_test_marshal_struct2_2 (10, 11, 12, ss2);
300 static int test_0_marshal_empty_struct () {
301 EmptyStruct es = new EmptyStruct ();
303 if (mono_test_empty_struct (1, es, 2) != 0)
309 static int test_0_marshal_struct_array () {
310 SimpleStruct2[] ss_arr = new SimpleStruct2 [2];
312 SimpleStruct2 ss2 = new SimpleStruct2 ();
331 return mono_test_marshal_struct_array (ss_arr);
334 static int test_105_marshal_long_align_struct_array () {
335 LongAlignStruct[] ss_arr = new LongAlignStruct [2];
337 LongAlignStruct ss = new LongAlignStruct ();
350 return mono_test_marshal_long_align_struct_array (ss_arr);
353 /* Test classes as arguments and return values */
354 static int test_0_marshal_class () {
355 SimpleClass ss = new SimpleClass ();
363 SimpleClass res = mono_test_marshal_class (10, 11, 12, ss, 14);
366 if (! (res.a == ss.a && res.b == ss.b && res.c == ss.c &&
367 res.d == ss.d && res.e == ss.e && res.f == ss.f &&
368 res.g == ss.g && res.h == ss.h))
371 /* Test null arguments and results */
372 res = mono_test_marshal_class (10, 11, 12, null, 14);
379 static int test_0_marshal_byref_class () {
380 SimpleClass ss = new SimpleClass ();
388 int res = mono_test_marshal_byref_class (ref ss);
389 if (ss.d != "TEST-RES")
395 static int test_0_marshal_delegate () {
396 SimpleDelegate d = new SimpleDelegate (delegate_test);
398 return mono_test_marshal_delegate (d);
401 static int test_0_marshal_return_delegate () {
402 SimpleDelegate d = new SimpleDelegate (delegate_test);
404 SimpleDelegate d2 = mono_test_marshal_return_delegate (d);
409 static int test_0_marshal_delegate_struct () {
410 DelegateStruct s = new DelegateStruct ();
413 s.del = new SimpleDelegate (delegate_test);
414 s.del2 = new SimpleDelegate (delegate_test);
416 return mono_test_marshal_delegate_struct (s);
419 static int test_0_marshal_point () {
420 Point pt = new Point();
424 return mono_test_marshal_point(pt);
427 static int test_0_marshal_mixed_point () {
428 MixedPoint mpt = new MixedPoint();
432 return mono_test_marshal_mixed_point(mpt);
435 static int test_0_marshal_bool_byref () {
437 if (mono_test_marshal_bool_byref (99, ref b, 100) != 1)
440 if (mono_test_marshal_bool_byref (99, ref b, 100) != 0)
448 static int test_0_return_vtype () {
449 SimpleStruct ss = mono_test_return_vtype (new IntPtr (5));
451 if (!ss.a && ss.b && !ss.c && ss.d == "TEST")
457 static int test_0_marshal_stringbuilder () {
458 StringBuilder sb = new StringBuilder(255);
460 mono_test_marshal_stringbuilder (sb, sb.Capacity);
461 String res = sb.ToString();
463 if (res != "This is my message. Isn't it nice?")
469 static int test_0_marshal_stringbuilder_unicode () {
470 StringBuilder sb = new StringBuilder(255);
471 mono_test_marshal_stringbuilder_unicode (sb, sb.Capacity);
472 String res = sb.ToString();
474 if (res != "This is my message. Isn't it nice?")
480 static int test_0_marshal_empty_string_array () {
481 return mono_test_marshal_empty_string_array (null);
484 static int test_0_marshal_string_array () {
485 return mono_test_marshal_string_array (new String [] { "ABC", "DEF" });
488 static int test_0_marshal_unicode_string_array () {
489 return mono_test_marshal_unicode_string_array (new String [] { "ABC", "DEF" }, new String [] { "ABC", "DEF" });
492 static int test_0_marshal_stringbuilder_array () {
493 StringBuilder sb1 = new StringBuilder ("ABC");
494 StringBuilder sb2 = new StringBuilder ("DEF");
496 int res = mono_test_marshal_stringbuilder_array (new StringBuilder [] { sb1, sb2 });
499 if (sb1.ToString () != "DEF")
501 if (sb2.ToString () != "ABC")
506 static int test_0_last_error () {
507 mono_test_last_error (5);
508 if (Marshal.GetLastWin32Error () == 5)
514 static int test_0_library_not_found () {
517 mono_entry_point_not_found ();
520 catch (EntryPointNotFoundException) {
526 static int test_0_entry_point_not_found () {
529 mono_library_not_found ();
532 catch (DllNotFoundException) {
538 /* Check that the runtime trims .dll from the library name */
539 static int test_0_trim_dll_from_name () {
541 mono_test_marshal_char_2 ('A');
546 /* Check that the runtime adds lib to to the library name */
547 static int test_0_add_lib_to_name () {
549 mono_test_marshal_char_3 ('A');
558 static int test_0_asany () {
559 if (mono_test_asany (5, 1) != 0)
562 if (mono_test_asany ("ABC", 2) != 0)
565 SimpleStruct2 ss2 = new SimpleStruct2 ();
573 if (mono_test_asany (ss2, 3) != 0)
576 if (mono_test_asany_unicode ("ABC", 4) != 0)
582 mono_test_asany (c, 0);
585 catch (ArgumentException) {
589 mono_test_asany (new Object (), 0);
592 catch (ArgumentException) {
598 /* Byref String Array */
600 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_string_array")]
601 public static extern int mono_test_marshal_byref_string_array (ref string[] data);
603 public static int test_0_byref_string_array () {
607 if (mono_test_marshal_byref_string_array (ref arr) != 0)
610 arr = new string[] { "Alpha", "Beta", "Gamma" };
612 if (mono_test_marshal_byref_string_array (ref arr) != 1)
615 /* FIXME: Test returned array and out case */
621 * AMD64 small structs-by-value tests.
624 /* TEST 1: 16 byte long INTEGER struct */
626 [StructLayout(LayoutKind.Sequential)]
627 public struct Amd64Struct1 {
634 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1")]
635 public static extern Amd64Struct1 mono_test_marshal_amd64_pass_return_struct1 (Amd64Struct1 s);
637 public static int test_0_amd64_struct1 () {
638 Amd64Struct1 s = new Amd64Struct1 ();
644 Amd64Struct1 s2 = mono_test_marshal_amd64_pass_return_struct1 (s);
646 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000)) ? 0 : 1;
649 /* TEST 2: 8 byte long INTEGER struct */
651 [StructLayout(LayoutKind.Sequential)]
652 public struct Amd64Struct2 {
657 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct2")]
658 public static extern Amd64Struct2 mono_test_marshal_amd64_pass_return_struct2 (Amd64Struct2 s);
660 public static int test_0_amd64_struct2 () {
661 Amd64Struct2 s = new Amd64Struct2 ();
665 Amd64Struct2 s2 = mono_test_marshal_amd64_pass_return_struct2 (s);
667 return ((s2.i == 6) && (s2.j == -4)) ? 0 : 1;
670 /* TEST 3: 4 byte long INTEGER struct */
672 [StructLayout(LayoutKind.Sequential)]
673 public struct Amd64Struct3 {
677 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct3")]
678 public static extern Amd64Struct3 mono_test_marshal_amd64_pass_return_struct3 (Amd64Struct3 s);
680 public static int test_0_amd64_struct3 () {
681 Amd64Struct3 s = new Amd64Struct3 ();
684 Amd64Struct3 s2 = mono_test_marshal_amd64_pass_return_struct3 (s);
686 return (s2.i == -4) ? 0 : 1;
689 /* Test 4: 16 byte long FLOAT struct */
691 [StructLayout(LayoutKind.Sequential)]
692 public struct Amd64Struct4 {
693 public double d1, d2;
696 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct4")]
697 public static extern Amd64Struct4 mono_test_marshal_amd64_pass_return_struct4 (Amd64Struct4 s);
699 public static int test_0_amd64_struct4 () {
700 Amd64Struct4 s = new Amd64Struct4 ();
704 Amd64Struct4 s2 = mono_test_marshal_amd64_pass_return_struct4 (s);
706 return (s2.d1 == 6.0 && s2.d2 == -4.0) ? 0 : 1;
709 /* Blittable class */
710 [DllImport("libtest")]
711 private static extern VectorList TestVectorList (VectorList vl);
713 public static int test_0_marshal_blittable_class () {
714 VectorList v1 = new VectorList ();
716 /* Since it is blittable, it looks like it is passed as in/out */
717 VectorList v2 = TestVectorList (v1);
719 if (v1.a != 2 || v1.b != 3)
722 if (v2.a != 2 || v2.b != 3)
728 static int test_0_marshal_byval_class () {
729 SimpleObj obj0 = new SimpleObj ();
733 if (class_marshal_test0 (obj0) != 0)
739 static int test_0_marshal_byval_class_null () {
740 if (class_marshal_test4 (null) != 0)
746 static int test_0_marshal_out_class () {
749 class_marshal_test1 (out obj1);
751 if (obj1.str != "ABC")
760 [DllImport ("libtest", EntryPoint="mono_test_stdcall_name_mangling", CallingConvention=CallingConvention.StdCall)]
761 public static extern int mono_test_stdcall_name_mangling (int a, int b, int c);
763 static int test_0_stdcall_name_mangling () {
764 return mono_test_stdcall_name_mangling (0, 1, 2) == 3 ? 0 : 1;