4 // Tests for native->managed marshalling
8 using System.Runtime.InteropServices;
12 [StructLayout (LayoutKind.Sequential)]
13 public struct SimpleStruct {
20 [StructLayout (LayoutKind.Sequential)]
21 public class SimpleClass {
28 public static SimpleStruct delegate_test_struct (SimpleStruct ss)
35 res.d = ss.d + "-RES";
40 public static int delegate_test_struct_byref (int a, ref SimpleStruct ss, int b)
42 if (a == 1 && b == 2 && ss.a && !ss.b && ss.c && ss.d == "TEST2")
48 public static SimpleClass delegate_test_class (SimpleClass ss)
53 if (! (!ss.a && ss.b && !ss.c && ss.d == "TEST"))
61 public static int delegate_test_class_byref (ref SimpleClass ss)
66 if (!ss.a && ss.b && !ss.c && ss.d == "TEST") {
78 public static int delegate_test_class_out (out SimpleClass ss)
80 ss = new SimpleClass ();
89 [DllImport ("libtest", EntryPoint="mono_test_ref_vtype")]
90 public static extern int mono_test_ref_vtype (int a, ref SimpleStruct ss, int b, TestDelegate d);
92 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate2")]
93 public static extern int mono_test_marshal_delegate2 (SimpleDelegate2 d);
95 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate4")]
96 public static extern int mono_test_marshal_delegate4 (SimpleDelegate4 d);
98 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate5")]
99 public static extern int mono_test_marshal_delegate5 (SimpleDelegate5 d);
101 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate6")]
102 public static extern int mono_test_marshal_delegate6 (SimpleDelegate5 d);
104 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate7")]
105 public static extern int mono_test_marshal_delegate7 (SimpleDelegate7 d);
107 public delegate int TestDelegate (int a, ref SimpleStruct ss, int b);
109 public delegate SimpleStruct SimpleDelegate2 (SimpleStruct ss);
111 public delegate SimpleClass SimpleDelegate4 (SimpleClass ss);
113 public delegate int SimpleDelegate5 (ref SimpleClass ss);
115 public delegate int SimpleDelegate7 (out SimpleClass ss);
117 public static int Main () {
118 return TestDriver.RunTests (typeof (Tests));
121 /* Test structures as arguments and return values of delegates */
122 static int test_0_marshal_struct_delegate () {
123 SimpleDelegate2 d = new SimpleDelegate2 (delegate_test_struct);
125 return mono_test_marshal_delegate2 (d);
128 /* Test structures as byref arguments of delegates */
129 static int test_0_marshal_byref_struct_delegate () {
130 SimpleStruct ss = new SimpleStruct ();
131 TestDelegate d = new TestDelegate (delegate_test_struct_byref);
136 if (mono_test_ref_vtype (1, ref ss, 2, d) != 0)
139 if (! (ss.a && !ss.b && ss.c && ss.d == "TEST2"))
145 /* Test classes as arguments and return values of delegates */
146 static int test_0_marshal_class_delegate () {
147 SimpleDelegate4 d = new SimpleDelegate4 (delegate_test_class);
149 return mono_test_marshal_delegate4 (d);
152 /* Test classes as byref arguments of delegates */
153 static int test_0_marshal_byref_class_delegate () {
154 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
156 return mono_test_marshal_delegate5 (d);
159 /* Test classes as out arguments of delegates */
160 static int test_0_marshal_out_class_delegate () {
161 SimpleDelegate7 d = new SimpleDelegate7 (delegate_test_class_out);
163 return mono_test_marshal_delegate7 (d);
166 /* Test that the delegate wrapper correctly catches null byref arguments */
167 static int test_0_marshal_byref_class_delegate_null () {
168 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
171 mono_test_marshal_delegate6 (d);
174 catch (ArgumentNullException ex) {