2009-11-20 Gonzalo Paniagua Javier <gonzalo@novell.com>
[mono.git] / mono / tests / pinvoke3.cs
index ccdaa38fedde6a18bb593ff893e27261ff61f9e9..ec2250839d3c9bae8a6175b020bd2b24010d5bd3 100644 (file)
@@ -5,6 +5,7 @@
 //
 
 using System;
+using System.Text;
 using System.Runtime.InteropServices;
 
 public class Tests {
@@ -119,6 +120,14 @@ public class Tests {
                return s == "ABC" ? 0 : 1;
        }
 
+       public static int delegate_test_string_builder_marshalling (StringBuilder s)
+       {
+               if (s == null)
+                       return 2;
+               else
+                       return s.ToString () == "ABC" ? 0 : 1;
+       }
+
        [DllImport ("libtest", EntryPoint="mono_test_ref_vtype")]
        public static extern int mono_test_ref_vtype (int a, ref SimpleStruct ss, int b, TestDelegate d);
 
@@ -151,6 +160,9 @@ public class Tests {
        [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate10")]
        public static extern int mono_test_marshal_delegate10 (SimpleDelegate9 d);
 
+       [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate8")]
+       public static extern int mono_test_marshal_delegate11 (SimpleDelegate11 d, string s);
+
        [DllImport ("libtest", EntryPoint="mono_test_marshal_primitive_byref_delegate")]
        public static extern int mono_test_marshal_primitive_byref_delegate (PrimitiveByrefDelegate d);
 
@@ -173,6 +185,8 @@ public class Tests {
 
        public delegate int SimpleDelegate9 (return_int_delegate del);
 
+       public delegate int SimpleDelegate11 (StringBuilder s1);
+
        public delegate int PrimitiveByrefDelegate (ref int i);
 
        public delegate return_int_delegate ReturnDelegateDelegate ();
@@ -241,6 +255,16 @@ public class Tests {
                return mono_test_marshal_delegate8 (d, "ABC");
        }
 
+       /* Test string builder marshalling with delegates */
+       public static int test_0_marshal_string_builder_delegate () {
+               SimpleDelegate11 d = new SimpleDelegate11 (delegate_test_string_builder_marshalling);
+
+               if (mono_test_marshal_delegate11 (d, null) != 2)
+                       return 2;
+
+               return mono_test_marshal_delegate11 (d, "ABC");
+       }
+
        /* Test that the delegate wrapper correctly catches null byref arguments */
        public static int test_0_marshal_byref_class_delegate_null () {
                SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
@@ -837,6 +861,30 @@ public class Tests {
                return mono_test_marshal_array_delegate8 (arr, 2, new ArrayDelegate8 (array_delegate8));
        }
 
+       /* Array with size param of type long */
+
+       public delegate int ArrayDelegate8_2 (long i, 
+                                                                               string j, 
+                                                                               [In, MarshalAs(UnmanagedType.LPArray, 
+                                                                                                          ArraySubType=UnmanagedType.LPStr, SizeParamIndex=0)] string[] arr);
+
+       [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate_long")]
+       public static extern int mono_test_marshal_array_delegate8_2 (string[] arr, long len, ArrayDelegate8_2 d);
+
+       public static int array_delegate8_2 (long i, string j, string[] arr) {
+               if (arr.Length != 2)
+                       return 1;
+               if ((arr [0] != "ABC") || (arr [1] != "DEF"))
+                       return 2;
+               return 0;
+       }
+
+       public static int test_0_marshal_array_delegate_long_param () { 
+               string[] arr = new string [] { "ABC", "DEF" };
+               return mono_test_marshal_array_delegate8_2 (arr, arr.Length, new ArrayDelegate8_2 (array_delegate8_2));
+       }
+
+
        /*
         * [Out] blittable arrays
         */
@@ -962,4 +1010,24 @@ public class Tests {
                return mono_test_marshal_return_string_array_delegate (new ReturnArrayDelegate (return_array_delegate_null));
        }
 
+       /*
+        * Byref string marshalling
+        */
+       public delegate int ByrefStringDelegate (ref string s);
+
+       [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_string_delegate")]
+       public static extern int mono_test_marshal_byref_string_delegate (ByrefStringDelegate d);
+
+       public static int byref_string_delegate (ref string s) {
+               if (s != "ABC")
+                       return 1;
+
+               s = "DEF";
+
+               return 0;
+       }
+
+       public static int test_0_marshal_byref_string_delegate () {     
+               return mono_test_marshal_byref_string_delegate (new ByrefStringDelegate (byref_string_delegate));
+       }
 }