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 ("libtest", EntryPoint="mono_test_marshal_char")]
108 public static extern int mono_test_marshal_char (char a1);
110 [DllImport ("libtest", EntryPoint="mono_test_marshal_char_array")]
111 public static extern int mono_test_marshal_char_array (char[] a1);
113 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_byref")]
114 public static extern int mono_test_marshal_bool_byref (int a, ref bool b, int c);
116 [DllImport ("libtest", EntryPoint="mono_test_marshal_array")]
117 public static extern int mono_test_marshal_array (int [] a1);
119 [DllImport ("libtest", EntryPoint="mono_test_marshal_empty_string_array")]
120 public static extern int mono_test_marshal_empty_string_array (string [] a1);
122 [DllImport ("libtest", EntryPoint="mono_test_marshal_string_array")]
123 public static extern int mono_test_marshal_string_array (string [] a1);
125 [DllImport ("libtest", EntryPoint="mono_test_marshal_unicode_string_array", CharSet=CharSet.Unicode)]
126 public static extern int mono_test_marshal_unicode_string_array (string [] a1, [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]string [] a2);
128 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_array")]
129 public static extern int mono_test_marshal_stringbuilder_array (StringBuilder [] a1);
131 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_array")]
132 public static extern int mono_test_marshal_inout_array ([In, Out] int [] a1);
134 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_nonblittable_array", CharSet = CharSet.Unicode)]
135 public static extern int mono_test_marshal_inout_nonblittable_array ([In, Out] char [] a1);
137 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
138 public static extern int mono_test_marshal_struct (SimpleStruct ss);
140 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2")]
141 public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss);
143 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2_2")]
144 public static extern int mono_test_marshal_struct2_2 (int i, int j, int k, SimpleStruct2 ss);
146 [DllImport ("libtest", EntryPoint="mono_test_marshal_point")]
147 public static extern int mono_test_marshal_point (Point p);
149 [DllImport ("libtest", EntryPoint="mono_test_marshal_mixed_point")]
150 public static extern int mono_test_marshal_mixed_point (MixedPoint p);
152 [DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
153 public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
155 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct_array")]
156 public static extern int mono_test_marshal_struct_array (SimpleStruct2[] ss);
158 [DllImport ("libtest", EntryPoint="mono_test_marshal_long_align_struct_array")]
159 public static extern int mono_test_marshal_long_align_struct_array (LongAlignStruct[] ss);
161 [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
162 public static extern SimpleClass mono_test_marshal_class (int i, int j, int k, SimpleClass ss, int l);
164 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_class")]
165 public static extern int mono_test_marshal_byref_class (ref SimpleClass ss);
167 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
168 public static extern int mono_test_marshal_delegate (SimpleDelegate d);
170 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate_struct")]
171 public static extern int mono_test_marshal_delegate_struct (DelegateStruct d);
173 [DllImport ("libtest", EntryPoint="mono_test_return_vtype")]
174 public static extern SimpleStruct mono_test_return_vtype (IntPtr i);
176 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
177 public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
179 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_unicode", CharSet=CharSet.Unicode)]
180 public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb, int len);
182 [DllImport ("libtest", EntryPoint="mono_test_last_error", SetLastError=true)]
183 public static extern void mono_test_last_error (int err);
185 [DllImport ("libtest", EntryPoint="mono_test_asany")]
186 public static extern int mono_test_asany ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
188 [DllImport ("libtest", EntryPoint="mono_test_asany", CharSet=CharSet.Unicode)]
189 public static extern int mono_test_asany_unicode ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
191 [DllImport ("libtest")]
192 static extern int class_marshal_test0 (SimpleObj obj);
194 [DllImport ("libtest")]
195 static extern void class_marshal_test1 (out SimpleObj obj);
197 [DllImport ("libtest")]
198 static extern int class_marshal_test2 (ref SimpleObj obj);
200 [DllImport ("libtest")]
201 static extern int class_marshal_test4 (SimpleObj obj);
203 public delegate int SimpleDelegate (int a);
205 public static int Main (string[] args) {
206 return TestDriver.RunTests (typeof (Tests), args);
209 static int test_0_marshal_char () {
210 return mono_test_marshal_char ('a');
213 static int test_0_marshal_char_array () {
214 // char[] is implicitly marshalled as [Out]
215 char[] buf = new char [32];
216 mono_test_marshal_char_array (buf);
217 string s = new string (buf);
218 if (s.StartsWith ("abcdef"))
224 static int test_1225_marshal_array () {
225 int [] a1 = new int [50];
226 for (int i = 0; i < 50; i++)
229 return mono_test_marshal_array (a1);
232 static int test_1225_marshal_inout_array () {
233 int [] a1 = new int [50];
234 for (int i = 0; i < 50; i++)
237 int res = mono_test_marshal_inout_array (a1);
239 for (int i = 0; i < 50; i++)
240 if (a1 [i] != 50 - i) {
241 Console.WriteLine ("X: " + i + " " + a1 [i]);
248 static int test_0_marshal_inout_nonblittable_array () {
249 char [] a1 = new char [10];
250 for (int i = 0; i < 10; i++)
251 a1 [i] = "Hello, World" [i];
253 int res = mono_test_marshal_inout_nonblittable_array (a1);
255 for (int i = 0; i < 10; i++)
262 static int test_0_marshal_struct () {
263 SimpleStruct ss = new SimpleStruct ();
267 return mono_test_marshal_struct (ss);
270 static int test_0_marshal_struct2 () {
271 SimpleStruct2 ss2 = new SimpleStruct2 ();
279 return mono_test_marshal_struct2 (ss2);
282 static int test_0_marshal_struct3 () {
283 SimpleStruct2 ss2 = new SimpleStruct2 ();
291 return mono_test_marshal_struct2_2 (10, 11, 12, ss2);
294 static int test_0_marshal_empty_struct () {
295 EmptyStruct es = new EmptyStruct ();
297 if (mono_test_empty_struct (1, es, 2) != 0)
303 static int test_0_marshal_struct_array () {
304 SimpleStruct2[] ss_arr = new SimpleStruct2 [2];
306 SimpleStruct2 ss2 = new SimpleStruct2 ();
325 return mono_test_marshal_struct_array (ss_arr);
328 static int test_105_marshal_long_align_struct_array () {
329 LongAlignStruct[] ss_arr = new LongAlignStruct [2];
331 LongAlignStruct ss = new LongAlignStruct ();
344 return mono_test_marshal_long_align_struct_array (ss_arr);
347 /* Test classes as arguments and return values */
348 static int test_0_marshal_class () {
349 SimpleClass ss = new SimpleClass ();
357 SimpleClass res = mono_test_marshal_class (10, 11, 12, ss, 14);
360 if (! (res.a == ss.a && res.b == ss.b && res.c == ss.c &&
361 res.d == ss.d && res.e == ss.e && res.f == ss.f &&
362 res.g == ss.g && res.h == ss.h))
365 /* Test null arguments and results */
366 res = mono_test_marshal_class (10, 11, 12, null, 14);
373 static int test_0_marshal_byref_class () {
374 SimpleClass ss = new SimpleClass ();
382 int res = mono_test_marshal_byref_class (ref ss);
383 if (ss.d != "TEST-RES")
389 static int test_0_marshal_delegate () {
390 SimpleDelegate d = new SimpleDelegate (delegate_test);
392 return mono_test_marshal_delegate (d);
395 static int test_0_marshal_delegate_struct () {
396 DelegateStruct s = new DelegateStruct ();
399 s.del = new SimpleDelegate (delegate_test);
400 s.del2 = new SimpleDelegate (delegate_test);
402 return mono_test_marshal_delegate_struct (s);
405 static int test_0_marshal_point () {
406 Point pt = new Point();
410 return mono_test_marshal_point(pt);
413 static int test_0_marshal_mixed_point () {
414 MixedPoint mpt = new MixedPoint();
418 return mono_test_marshal_mixed_point(mpt);
421 static int test_0_marshal_bool_byref () {
423 if (mono_test_marshal_bool_byref (99, ref b, 100) != 1)
426 if (mono_test_marshal_bool_byref (99, ref b, 100) != 0)
434 static int test_0_return_vtype () {
435 SimpleStruct ss = mono_test_return_vtype (new IntPtr (5));
437 if (!ss.a && ss.b && !ss.c && ss.d == "TEST")
443 static int test_0_marshal_stringbuilder () {
444 StringBuilder sb = new StringBuilder(255);
446 mono_test_marshal_stringbuilder (sb, sb.Capacity);
447 String res = sb.ToString();
449 if (res != "This is my message. Isn't it nice?")
455 static int test_0_marshal_stringbuilder_unicode () {
456 StringBuilder sb = new StringBuilder(255);
457 mono_test_marshal_stringbuilder_unicode (sb, sb.Capacity);
458 String res = sb.ToString();
460 if (res != "This is my message. Isn't it nice?")
466 static int test_0_marshal_empty_string_array () {
467 return mono_test_marshal_empty_string_array (null);
470 static int test_0_marshal_string_array () {
471 return mono_test_marshal_string_array (new String [] { "ABC", "DEF" });
474 static int test_0_marshal_unicode_string_array () {
475 return mono_test_marshal_unicode_string_array (new String [] { "ABC", "DEF" }, new String [] { "ABC", "DEF" });
478 static int test_0_marshal_stringbuilder_array () {
479 StringBuilder sb1 = new StringBuilder ("ABC");
480 StringBuilder sb2 = new StringBuilder ("DEF");
482 int res = mono_test_marshal_stringbuilder_array (new StringBuilder [] { sb1, sb2 });
485 if (sb1.ToString () != "DEF")
487 if (sb2.ToString () != "ABC")
492 static int test_0_last_error () {
493 mono_test_last_error (5);
494 if (Marshal.GetLastWin32Error () == 5)
500 static int test_0_library_not_found () {
503 mono_entry_point_not_found ();
506 catch (EntryPointNotFoundException) {
512 static int test_0_entry_point_not_found () {
515 mono_library_not_found ();
518 catch (DllNotFoundException) {
524 /* Check that the runtime trims .dll from the library name */
525 static int test_0_trim_dll_from_name () {
527 mono_test_marshal_char_2 ('A');
536 static int test_0_asany () {
537 if (mono_test_asany (5, 1) != 0)
540 if (mono_test_asany ("ABC", 2) != 0)
543 SimpleStruct2 ss2 = new SimpleStruct2 ();
551 if (mono_test_asany (ss2, 3) != 0)
554 if (mono_test_asany_unicode ("ABC", 4) != 0)
560 mono_test_asany (c, 0);
563 catch (ArgumentException) {
567 mono_test_asany (new Object (), 0);
570 catch (ArgumentException) {
576 /* Byref String Array */
578 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_string_array")]
579 public static extern int mono_test_marshal_byref_string_array (ref string[] data);
581 public static int test_0_byref_string_array () {
585 if (mono_test_marshal_byref_string_array (ref arr) != 0)
588 arr = new string[] { "Alpha", "Beta", "Gamma" };
590 if (mono_test_marshal_byref_string_array (ref arr) != 1)
593 /* FIXME: Test returned array and out case */
599 * AMD64 small structs-by-value tests.
602 /* TEST 1: 16 byte long INTEGER struct */
604 [StructLayout(LayoutKind.Sequential)]
605 public struct Amd64Struct1 {
612 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1")]
613 public static extern Amd64Struct1 mono_test_marshal_amd64_pass_return_struct1 (Amd64Struct1 s);
615 public static int test_0_amd64_struct1 () {
616 Amd64Struct1 s = new Amd64Struct1 ();
622 Amd64Struct1 s2 = mono_test_marshal_amd64_pass_return_struct1 (s);
624 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000)) ? 0 : 1;
627 /* TEST 2: 8 byte long INTEGER struct */
629 [StructLayout(LayoutKind.Sequential)]
630 public struct Amd64Struct2 {
635 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct2")]
636 public static extern Amd64Struct2 mono_test_marshal_amd64_pass_return_struct2 (Amd64Struct2 s);
638 public static int test_0_amd64_struct2 () {
639 Amd64Struct2 s = new Amd64Struct2 ();
643 Amd64Struct2 s2 = mono_test_marshal_amd64_pass_return_struct2 (s);
645 return ((s2.i == 6) && (s2.j == -4)) ? 0 : 1;
648 /* TEST 3: 4 byte long INTEGER struct */
650 [StructLayout(LayoutKind.Sequential)]
651 public struct Amd64Struct3 {
655 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct3")]
656 public static extern Amd64Struct3 mono_test_marshal_amd64_pass_return_struct3 (Amd64Struct3 s);
658 public static int test_0_amd64_struct3 () {
659 Amd64Struct3 s = new Amd64Struct3 ();
662 Amd64Struct3 s2 = mono_test_marshal_amd64_pass_return_struct3 (s);
664 return (s2.i == -4) ? 0 : 1;
667 /* Test 4: 16 byte long FLOAT struct */
669 [StructLayout(LayoutKind.Sequential)]
670 public struct Amd64Struct4 {
671 public double d1, d2;
674 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct4")]
675 public static extern Amd64Struct4 mono_test_marshal_amd64_pass_return_struct4 (Amd64Struct4 s);
677 public static int test_0_amd64_struct4 () {
678 Amd64Struct4 s = new Amd64Struct4 ();
682 Amd64Struct4 s2 = mono_test_marshal_amd64_pass_return_struct4 (s);
684 return (s2.d1 == 6.0 && s2.d2 == -4.0) ? 0 : 1;
687 /* Blittable class */
688 [DllImport("libtest")]
689 private static extern VectorList TestVectorList (VectorList vl);
691 public static int test_0_marshal_blittable_class () {
692 VectorList v1 = new VectorList ();
694 /* Since it is blittable, it looks like it is passed as in/out */
695 VectorList v2 = TestVectorList (v1);
697 if (v1.a != 2 || v1.b != 3)
700 if (v2.a != 2 || v2.b != 3)
706 static int test_0_marshal_byval_class () {
707 SimpleObj obj0 = new SimpleObj ();
711 if (class_marshal_test0 (obj0) != 0)
717 static int test_0_marshal_byval_class_null () {
718 if (class_marshal_test4 (null) != 0)
724 static int test_0_marshal_out_class () {
727 class_marshal_test1 (out obj1);
729 if (obj1.str != "ABC")