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_inout_array")]
115 public static extern int mono_test_marshal_inout_array ([In, Out] int [] a1);
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);
120 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
121 public static extern int mono_test_marshal_struct (SimpleStruct ss);
123 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2")]
124 public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss);
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);
129 [DllImport ("libtest", EntryPoint="mono_test_marshal_point")]
130 public static extern int mono_test_marshal_point (Point p);
132 [DllImport ("libtest", EntryPoint="mono_test_marshal_mixed_point")]
133 public static extern int mono_test_marshal_mixed_point (MixedPoint p);
135 [DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
136 public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
138 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct_array")]
139 public static extern int mono_test_marshal_struct_array (SimpleStruct2[] ss);
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);
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);
147 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_class")]
148 public static extern int mono_test_marshal_byref_class (ref SimpleClass ss);
150 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
151 public static extern int mono_test_marshal_delegate (SimpleDelegate d);
153 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate_struct")]
154 public static extern int mono_test_marshal_delegate_struct (DelegateStruct d);
156 [DllImport ("libtest", EntryPoint="mono_test_return_vtype")]
157 public static extern SimpleStruct mono_test_return_vtype (IntPtr i);
159 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
160 public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
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);
165 [DllImport ("libtest", EntryPoint="mono_test_last_error", SetLastError=true)]
166 public static extern void mono_test_last_error (int err);
168 [DllImport ("libtest", EntryPoint="mono_test_asany")]
169 public static extern int mono_test_asany ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
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);
174 public delegate int SimpleDelegate (int a);
176 public static int Main () {
177 return TestDriver.RunTests (typeof (Tests));
180 static int test_0_marshal_char () {
181 return mono_test_marshal_char ('a');
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"))
195 static int test_1225_marshal_array () {
196 int [] a1 = new int [50];
197 for (int i = 0; i < 50; i++)
200 return mono_test_marshal_array (a1);
203 static int test_1225_marshal_inout_array () {
204 int [] a1 = new int [50];
205 for (int i = 0; i < 50; i++)
208 int res = mono_test_marshal_inout_array (a1);
210 for (int i = 0; i < 50; i++)
211 if (a1 [i] != 50 - i) {
212 Console.WriteLine ("X: " + i + " " + a1 [i]);
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];
224 int res = mono_test_marshal_inout_nonblittable_array (a1);
226 for (int i = 0; i < 10; i++)
233 static int test_0_marshal_struct () {
234 SimpleStruct ss = new SimpleStruct ();
238 return mono_test_marshal_struct (ss);
241 static int test_0_marshal_struct2 () {
242 SimpleStruct2 ss2 = new SimpleStruct2 ();
250 return mono_test_marshal_struct2 (ss2);
253 static int test_0_marshal_struct3 () {
254 SimpleStruct2 ss2 = new SimpleStruct2 ();
262 return mono_test_marshal_struct2_2 (10, 11, 12, ss2);
265 static int test_0_marshal_empty_struct () {
266 EmptyStruct es = new EmptyStruct ();
268 if (mono_test_empty_struct (1, es, 2) != 0)
274 static int test_0_marshal_struct_array () {
275 SimpleStruct2[] ss_arr = new SimpleStruct2 [2];
277 SimpleStruct2 ss2 = new SimpleStruct2 ();
296 return mono_test_marshal_struct_array (ss_arr);
299 static int test_105_marshal_long_align_struct_array () {
300 LongAlignStruct[] ss_arr = new LongAlignStruct [2];
302 LongAlignStruct ss = new LongAlignStruct ();
315 return mono_test_marshal_long_align_struct_array (ss_arr);
318 /* Test classes as arguments and return values */
319 static int test_0_marshal_class () {
320 SimpleClass ss = new SimpleClass ();
328 SimpleClass res = mono_test_marshal_class (10, 11, 12, ss, 14);
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))
336 /* Test null arguments and results */
337 res = mono_test_marshal_class (10, 11, 12, null, 14);
344 static int test_0_marshal_byref_class () {
345 SimpleClass ss = new SimpleClass ();
353 int res = mono_test_marshal_byref_class (ref ss);
354 if (ss.d != "TEST-RES")
360 static int test_0_marshal_delegate () {
361 SimpleDelegate d = new SimpleDelegate (delegate_test);
363 return mono_test_marshal_delegate (d);
366 static int test_0_marshal_delegate_struct () {
367 DelegateStruct s = new DelegateStruct ();
370 s.del = new SimpleDelegate (delegate_test);
371 s.del2 = new SimpleDelegate (delegate_test);
373 return mono_test_marshal_delegate_struct (s);
376 static int test_0_marshal_point () {
377 Point pt = new Point();
381 return mono_test_marshal_point(pt);
384 static int test_0_marshal_mixed_point () {
385 MixedPoint mpt = new MixedPoint();
389 return mono_test_marshal_mixed_point(mpt);
392 static int test_0_marshal_bool_byref () {
394 if (mono_test_marshal_bool_byref (99, ref b, 100) != 1)
397 if (mono_test_marshal_bool_byref (99, ref b, 100) != 0)
405 static int test_0_return_vtype () {
406 SimpleStruct ss = mono_test_return_vtype (new IntPtr (5));
408 if (!ss.a && ss.b && !ss.c && ss.d == "TEST")
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();
419 if (res != "This is my message. Isn't it nice?")
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();
430 if (res != "This is my message. Isn't it nice?")
436 static int test_0_marshal_empty_string_array () {
437 return mono_test_marshal_empty_string_array (null);
440 static int test_0_marshal_string_array () {
441 return mono_test_marshal_string_array (new String [] { "ABC", "DEF" });
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" });
448 static int test_0_last_error () {
449 mono_test_last_error (5);
450 if (Marshal.GetLastWin32Error () == 5)
456 static int test_0_library_not_found () {
459 mono_entry_point_not_found ();
462 catch (EntryPointNotFoundException) {
468 static int test_0_entry_point_not_found () {
471 mono_library_not_found ();
474 catch (DllNotFoundException) {
480 /* Check that the runtime trims .dll from the library name */
481 static int test_0_trim_dll_from_name () {
483 mono_test_marshal_char_2 ('A');
492 static int test_0_asany () {
493 if (mono_test_asany (5, 1) != 0)
496 if (mono_test_asany ("ABC", 2) != 0)
499 SimpleStruct2 ss2 = new SimpleStruct2 ();
507 if (mono_test_asany (ss2, 3) != 0)
510 if (mono_test_asany_unicode ("ABC", 4) != 0)
516 mono_test_asany (c, 0);
519 catch (ArgumentException) {
523 mono_test_asany (new Object (), 0);
526 catch (ArgumentException) {