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 [DllImport ("libnot-found", EntryPoint="not_found")]
85 public static extern int mono_library_not_found ();
87 [DllImport ("libtest", EntryPoint="not_found")]
88 public static extern int mono_entry_point_not_found ();
90 [DllImport ("libtest.dll", EntryPoint="mono_test_marshal_char")]
91 public static extern int mono_test_marshal_char_2 (char a1);
93 [DllImport ("libtest", EntryPoint="mono_test_marshal_char")]
94 public static extern int mono_test_marshal_char (char a1);
96 [DllImport ("libtest", EntryPoint="mono_test_marshal_char_array")]
97 public static extern int mono_test_marshal_char_array (char[] a1);
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);
102 [DllImport ("libtest", EntryPoint="mono_test_marshal_array")]
103 public static extern int mono_test_marshal_array (int [] a1);
105 [DllImport ("libtest", EntryPoint="mono_test_marshal_empty_string_array")]
106 public static extern int mono_test_marshal_empty_string_array (string [] a1);
108 [DllImport ("libtest", EntryPoint="mono_test_marshal_string_array")]
109 public static extern int mono_test_marshal_string_array (string [] a1);
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);
114 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_array")]
115 public static extern int mono_test_marshal_stringbuilder_array (StringBuilder [] a1);
117 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_array")]
118 public static extern int mono_test_marshal_inout_array ([In, Out] int [] a1);
120 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_nonblittable_array", CharSet = CharSet.Unicode)]
121 public static extern int mono_test_marshal_inout_nonblittable_array ([In, Out] char [] a1);
123 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
124 public static extern int mono_test_marshal_struct (SimpleStruct ss);
126 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2")]
127 public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss);
129 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2_2")]
130 public static extern int mono_test_marshal_struct2_2 (int i, int j, int k, SimpleStruct2 ss);
132 [DllImport ("libtest", EntryPoint="mono_test_marshal_point")]
133 public static extern int mono_test_marshal_point (Point p);
135 [DllImport ("libtest", EntryPoint="mono_test_marshal_mixed_point")]
136 public static extern int mono_test_marshal_mixed_point (MixedPoint p);
138 [DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
139 public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
141 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct_array")]
142 public static extern int mono_test_marshal_struct_array (SimpleStruct2[] ss);
144 [DllImport ("libtest", EntryPoint="mono_test_marshal_long_align_struct_array")]
145 public static extern int mono_test_marshal_long_align_struct_array (LongAlignStruct[] ss);
147 [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
148 public static extern SimpleClass mono_test_marshal_class (int i, int j, int k, SimpleClass ss, int l);
150 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_class")]
151 public static extern int mono_test_marshal_byref_class (ref SimpleClass ss);
153 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
154 public static extern int mono_test_marshal_delegate (SimpleDelegate d);
156 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate_struct")]
157 public static extern int mono_test_marshal_delegate_struct (DelegateStruct d);
159 [DllImport ("libtest", EntryPoint="mono_test_return_vtype")]
160 public static extern SimpleStruct mono_test_return_vtype (IntPtr i);
162 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
163 public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
165 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_unicode", CharSet=CharSet.Unicode)]
166 public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb, int len);
168 [DllImport ("libtest", EntryPoint="mono_test_last_error", SetLastError=true)]
169 public static extern void mono_test_last_error (int err);
171 [DllImport ("libtest", EntryPoint="mono_test_asany")]
172 public static extern int mono_test_asany ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
174 [DllImport ("libtest", EntryPoint="mono_test_asany", CharSet=CharSet.Unicode)]
175 public static extern int mono_test_asany_unicode ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
177 public delegate int SimpleDelegate (int a);
179 public static int Main () {
180 return TestDriver.RunTests (typeof (Tests));
183 static int test_0_marshal_char () {
184 return mono_test_marshal_char ('a');
187 static int test_0_marshal_char_array () {
188 // char[] is implicitly marshalled as [Out]
189 char[] buf = new char [32];
190 mono_test_marshal_char_array (buf);
191 string s = new string (buf);
192 if (s.StartsWith ("abcdef"))
198 static int test_1225_marshal_array () {
199 int [] a1 = new int [50];
200 for (int i = 0; i < 50; i++)
203 return mono_test_marshal_array (a1);
206 static int test_1225_marshal_inout_array () {
207 int [] a1 = new int [50];
208 for (int i = 0; i < 50; i++)
211 int res = mono_test_marshal_inout_array (a1);
213 for (int i = 0; i < 50; i++)
214 if (a1 [i] != 50 - i) {
215 Console.WriteLine ("X: " + i + " " + a1 [i]);
222 static int test_0_marshal_inout_nonblittable_array () {
223 char [] a1 = new char [10];
224 for (int i = 0; i < 10; i++)
225 a1 [i] = "Hello, World" [i];
227 int res = mono_test_marshal_inout_nonblittable_array (a1);
229 for (int i = 0; i < 10; i++)
236 static int test_0_marshal_struct () {
237 SimpleStruct ss = new SimpleStruct ();
241 return mono_test_marshal_struct (ss);
244 static int test_0_marshal_struct2 () {
245 SimpleStruct2 ss2 = new SimpleStruct2 ();
253 return mono_test_marshal_struct2 (ss2);
256 static int test_0_marshal_struct3 () {
257 SimpleStruct2 ss2 = new SimpleStruct2 ();
265 return mono_test_marshal_struct2_2 (10, 11, 12, ss2);
268 static int test_0_marshal_empty_struct () {
269 EmptyStruct es = new EmptyStruct ();
271 if (mono_test_empty_struct (1, es, 2) != 0)
277 static int test_0_marshal_struct_array () {
278 SimpleStruct2[] ss_arr = new SimpleStruct2 [2];
280 SimpleStruct2 ss2 = new SimpleStruct2 ();
299 return mono_test_marshal_struct_array (ss_arr);
302 static int test_105_marshal_long_align_struct_array () {
303 LongAlignStruct[] ss_arr = new LongAlignStruct [2];
305 LongAlignStruct ss = new LongAlignStruct ();
318 return mono_test_marshal_long_align_struct_array (ss_arr);
321 /* Test classes as arguments and return values */
322 static int test_0_marshal_class () {
323 SimpleClass ss = new SimpleClass ();
331 SimpleClass res = mono_test_marshal_class (10, 11, 12, ss, 14);
334 if (! (res.a == ss.a && res.b == ss.b && res.c == ss.c &&
335 res.d == ss.d && res.e == ss.e && res.f == ss.f &&
336 res.g == ss.g && res.h == ss.h))
339 /* Test null arguments and results */
340 res = mono_test_marshal_class (10, 11, 12, null, 14);
347 static int test_0_marshal_byref_class () {
348 SimpleClass ss = new SimpleClass ();
356 int res = mono_test_marshal_byref_class (ref ss);
357 if (ss.d != "TEST-RES")
363 static int test_0_marshal_delegate () {
364 SimpleDelegate d = new SimpleDelegate (delegate_test);
366 return mono_test_marshal_delegate (d);
369 static int test_0_marshal_delegate_struct () {
370 DelegateStruct s = new DelegateStruct ();
373 s.del = new SimpleDelegate (delegate_test);
374 s.del2 = new SimpleDelegate (delegate_test);
376 return mono_test_marshal_delegate_struct (s);
379 static int test_0_marshal_point () {
380 Point pt = new Point();
384 return mono_test_marshal_point(pt);
387 static int test_0_marshal_mixed_point () {
388 MixedPoint mpt = new MixedPoint();
392 return mono_test_marshal_mixed_point(mpt);
395 static int test_0_marshal_bool_byref () {
397 if (mono_test_marshal_bool_byref (99, ref b, 100) != 1)
400 if (mono_test_marshal_bool_byref (99, ref b, 100) != 0)
408 static int test_0_return_vtype () {
409 SimpleStruct ss = mono_test_return_vtype (new IntPtr (5));
411 if (!ss.a && ss.b && !ss.c && ss.d == "TEST")
417 static int test_0_marshal_stringbuilder () {
418 StringBuilder sb = new StringBuilder(255);
420 mono_test_marshal_stringbuilder (sb, sb.Capacity);
421 String res = sb.ToString();
423 if (res != "This is my message. Isn't it nice?")
429 static int test_0_marshal_stringbuilder_unicode () {
430 StringBuilder sb = new StringBuilder(255);
431 mono_test_marshal_stringbuilder_unicode (sb, sb.Capacity);
432 String res = sb.ToString();
434 if (res != "This is my message. Isn't it nice?")
440 static int test_0_marshal_empty_string_array () {
441 return mono_test_marshal_empty_string_array (null);
444 static int test_0_marshal_string_array () {
445 return mono_test_marshal_string_array (new String [] { "ABC", "DEF" });
448 static int test_0_marshal_unicode_string_array () {
449 return mono_test_marshal_unicode_string_array (new String [] { "ABC", "DEF" }, new String [] { "ABC", "DEF" });
452 static int test_0_marshal_stringbuilder_array () {
453 StringBuilder sb1 = new StringBuilder ("ABC");
454 StringBuilder sb2 = new StringBuilder ("DEF");
456 int res = mono_test_marshal_stringbuilder_array (new StringBuilder [] { sb1, sb2 });
459 if (sb1.ToString () != "DEF")
461 if (sb2.ToString () != "ABC")
466 static int test_0_last_error () {
467 mono_test_last_error (5);
468 if (Marshal.GetLastWin32Error () == 5)
474 static int test_0_library_not_found () {
477 mono_entry_point_not_found ();
480 catch (EntryPointNotFoundException) {
486 static int test_0_entry_point_not_found () {
489 mono_library_not_found ();
492 catch (DllNotFoundException) {
498 /* Check that the runtime trims .dll from the library name */
499 static int test_0_trim_dll_from_name () {
501 mono_test_marshal_char_2 ('A');
510 static int test_0_asany () {
511 if (mono_test_asany (5, 1) != 0)
514 if (mono_test_asany ("ABC", 2) != 0)
517 SimpleStruct2 ss2 = new SimpleStruct2 ();
525 if (mono_test_asany (ss2, 3) != 0)
528 if (mono_test_asany_unicode ("ABC", 4) != 0)
534 mono_test_asany (c, 0);
537 catch (ArgumentException) {
541 mono_test_asany (new Object (), 0);
544 catch (ArgumentException) {