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 /* sparcv9 has complex conventions when passing structs with doubles in them
40 by value, some simple tests for them */
41 [StructLayout (LayoutKind.Sequential)]
47 [StructLayout (LayoutKind.Sequential)]
48 public struct MixedPoint {
53 [StructLayout (LayoutKind.Sequential)]
54 public class SimpleClass {
65 [StructLayout (LayoutKind.Sequential)]
66 public class EmptyClass {
69 [DllImport ("libnot-found", EntryPoint="not_found")]
70 public static extern int mono_library_not_found ();
72 [DllImport ("libtest", EntryPoint="not_found")]
73 public static extern int mono_entry_point_not_found ();
75 [DllImport ("libtest.dll", EntryPoint="mono_test_marshal_char")]
76 public static extern int mono_test_marshal_char_2 (char a1);
78 [DllImport ("libtest", EntryPoint="mono_test_marshal_char")]
79 public static extern int mono_test_marshal_char (char a1);
81 [DllImport ("libtest", EntryPoint="mono_test_marshal_char_array")]
82 public static extern int mono_test_marshal_char_array (char[] a1);
84 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_byref")]
85 public static extern int mono_test_marshal_bool_byref (int a, ref bool b, int c);
87 [DllImport ("libtest", EntryPoint="mono_test_marshal_array")]
88 public static extern int mono_test_marshal_array (int [] a1);
90 [DllImport ("libtest", EntryPoint="mono_test_marshal_empty_string_array")]
91 public static extern int mono_test_marshal_empty_string_array (string [] a1);
93 [DllImport ("libtest", EntryPoint="mono_test_marshal_string_array")]
94 public static extern int mono_test_marshal_string_array (string [] a1);
96 [DllImport ("libtest", EntryPoint="mono_test_marshal_unicode_string_array", CharSet=CharSet.Unicode)]
97 public static extern int mono_test_marshal_unicode_string_array (string [] a1, [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]string [] a2);
99 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_array")]
100 public static extern int mono_test_marshal_inout_array ([In, Out] int [] a1);
102 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_nonblittable_array", CharSet = CharSet.Unicode)]
103 public static extern int mono_test_marshal_inout_nonblittable_array ([In, Out] char [] a1);
105 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
106 public static extern int mono_test_marshal_struct (SimpleStruct ss);
108 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2")]
109 public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss);
111 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2_2")]
112 public static extern int mono_test_marshal_struct2_2 (int i, int j, int k, SimpleStruct2 ss);
114 [DllImport ("libtest", EntryPoint="mono_test_marshal_point")]
115 public static extern int mono_test_marshal_point (Point p);
117 [DllImport ("libtest", EntryPoint="mono_test_marshal_mixed_point")]
118 public static extern int mono_test_marshal_mixed_point (MixedPoint p);
120 [DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
121 public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
123 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct_array")]
124 public static extern int mono_test_marshal_struct_array (SimpleStruct2[] ss);
126 [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
127 public static extern SimpleClass mono_test_marshal_class (int i, int j, int k, SimpleClass ss, int l);
129 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_class")]
130 public static extern int mono_test_marshal_byref_class (ref SimpleClass ss);
132 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
133 public static extern int mono_test_marshal_delegate (SimpleDelegate d);
135 [DllImport ("libtest", EntryPoint="mono_test_return_vtype")]
136 public static extern SimpleStruct mono_test_return_vtype (IntPtr i);
138 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
139 public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
141 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_unicode", CharSet=CharSet.Unicode)]
142 public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb, int len);
144 [DllImport ("libtest", EntryPoint="mono_test_last_error", SetLastError=true)]
145 public static extern void mono_test_last_error (int err);
147 public delegate int SimpleDelegate (int a);
149 public static int Main () {
150 return TestDriver.RunTests (typeof (Tests));
153 static int test_0_marshal_char () {
154 return mono_test_marshal_char ('a');
157 static int test_0_marshal_char_array () {
158 // char[] is implicitly marshalled as [Out]
159 char[] buf = new char [32];
160 mono_test_marshal_char_array (buf);
161 string s = new string (buf);
162 if (s.StartsWith ("abcdef"))
168 static int test_1225_marshal_array () {
169 int [] a1 = new int [50];
170 for (int i = 0; i < 50; i++)
173 return mono_test_marshal_array (a1);
176 static int test_1225_marshal_inout_array () {
177 int [] a1 = new int [50];
178 for (int i = 0; i < 50; i++)
181 int res = mono_test_marshal_inout_array (a1);
183 for (int i = 0; i < 50; i++)
184 if (a1 [i] != 50 - i) {
185 Console.WriteLine ("X: " + i + " " + a1 [i]);
192 static int test_0_marshal_inout_nonblittable_array () {
193 char [] a1 = new char [10];
194 for (int i = 0; i < 10; i++)
195 a1 [i] = "Hello, World" [i];
197 int res = mono_test_marshal_inout_nonblittable_array (a1);
199 for (int i = 0; i < 10; i++)
206 static int test_0_marshal_struct () {
207 SimpleStruct ss = new SimpleStruct ();
211 return mono_test_marshal_struct (ss);
214 static int test_0_marshal_struct2 () {
215 SimpleStruct2 ss2 = new SimpleStruct2 ();
223 return mono_test_marshal_struct2 (ss2);
226 static int test_0_marshal_struct3 () {
227 SimpleStruct2 ss2 = new SimpleStruct2 ();
235 return mono_test_marshal_struct2_2 (10, 11, 12, ss2);
238 static int test_0_marshal_empty_struct () {
239 EmptyStruct es = new EmptyStruct ();
241 if (mono_test_empty_struct (1, es, 2) != 0)
247 static int test_0_marshal_struct_array () {
248 SimpleStruct2[] ss_arr = new SimpleStruct2 [2];
250 SimpleStruct2 ss2 = new SimpleStruct2 ();
269 return mono_test_marshal_struct_array (ss_arr);
272 /* Test classes as arguments and return values */
273 static int test_0_marshal_class () {
274 SimpleClass ss = new SimpleClass ();
282 SimpleClass res = mono_test_marshal_class (10, 11, 12, ss, 14);
285 if (! (res.a == ss.a && res.b == ss.b && res.c == ss.c &&
286 res.d == ss.d && res.e == ss.e && res.f == ss.f &&
287 res.g == ss.g && res.h == ss.h))
290 /* Test null arguments and results */
291 res = mono_test_marshal_class (10, 11, 12, null, 14);
298 static int test_0_marshal_byref_class () {
299 SimpleClass ss = new SimpleClass ();
307 int res = mono_test_marshal_byref_class (ref ss);
308 if (ss.d != "TEST-RES")
314 static int test_0_marshal_delegate () {
315 SimpleDelegate d = new SimpleDelegate (delegate_test);
317 return mono_test_marshal_delegate (d);
320 static int test_0_marshal_point () {
321 Point pt = new Point();
325 return mono_test_marshal_point(pt);
328 static int test_0_marshal_mixed_point () {
329 MixedPoint mpt = new MixedPoint();
333 return mono_test_marshal_mixed_point(mpt);
336 static int test_0_marshal_bool_byref () {
338 if (mono_test_marshal_bool_byref (99, ref b, 100) != 1)
341 if (mono_test_marshal_bool_byref (99, ref b, 100) != 0)
349 static int test_0_return_vtype () {
350 SimpleStruct ss = mono_test_return_vtype (new IntPtr (5));
352 if (!ss.a && ss.b && !ss.c && ss.d == "TEST")
358 static int test_0_marshal_stringbuilder () {
359 StringBuilder sb = new StringBuilder(255);
360 mono_test_marshal_stringbuilder (sb, sb.Capacity);
361 String res = sb.ToString();
363 if (res != "This is my message. Isn't it nice?")
369 static int test_0_marshal_stringbuilder_unicode () {
370 StringBuilder sb = new StringBuilder(255);
371 mono_test_marshal_stringbuilder_unicode (sb, sb.Capacity);
372 String res = sb.ToString();
374 if (res != "This is my message. Isn't it nice?")
380 static int test_0_marshal_empty_string_array () {
381 return mono_test_marshal_empty_string_array (null);
384 static int test_0_marshal_string_array () {
385 return mono_test_marshal_string_array (new String [] { "ABC", "DEF" });
388 static int test_0_marshal_unicode_string_array () {
389 return mono_test_marshal_unicode_string_array (new String [] { "ABC", "DEF" }, new String [] { "ABC", "DEF" });
392 static int test_0_last_error () {
393 mono_test_last_error (5);
394 if (Marshal.GetLastWin32Error () == 5)
400 static int test_0_library_not_found () {
403 mono_entry_point_not_found ();
406 catch (EntryPointNotFoundException) {
412 static int test_0_entry_point_not_found () {
415 mono_library_not_found ();
418 catch (DllNotFoundException) {
424 /* Check that the runtime trims .dll from the library name */
425 static int test_0_trim_dll_from_name () {
427 mono_test_marshal_char_2 ('A');