Merge pull request #2816 from xmcclure/profile-clean-0
[mono.git] / mono / tests / pinvoke2.cs
index 71c51a7d814253544288637233c608619309ac3f..5e0f474ec40ac451ce6034bf5af400896980c0ca 100644 (file)
@@ -1,5 +1,6 @@
 //
 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
 //
 
 using System;
@@ -61,6 +62,10 @@ public class Tests {
        public struct EmptyStruct {
        }
 
+       [StructLayout (LayoutKind.Sequential, Size=1)]
+       public struct EmptyStructCpp {
+       }
+
        [StructLayout (LayoutKind.Sequential)]
        public struct DelegateStruct {
                public int a;
@@ -71,6 +76,11 @@ public class Tests {
                public SimpleDelegate del3;
        }
 
+       [StructLayout (LayoutKind.Sequential)]
+       public struct SingleDoubleStruct {
+               public double d;
+       }
+
        /* sparcv9 has complex conventions when passing structs with doubles in them 
           by value, some simple tests for them */
        [StructLayout (LayoutKind.Sequential)]
@@ -222,6 +232,12 @@ public class Tests {
        [DllImport ("libtest", EntryPoint="mono_test_marshal_out_array")]
        public static extern int mono_test_marshal_out_array ([Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] int [] a1, int n);
 
+       [DllImport ("libtest", EntryPoint="mono_test_marshal_out_byref_array_out_size_param")]
+       public static extern int mono_test_marshal_out_byref_array_out_size_param ([Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] out int [] a1, out int n);
+
+       [DllImport ("libtest", EntryPoint="mono_test_marshal_out_lparray_out_size_param")]
+       public static extern int mono_test_marshal_out_lparray_out_size_param ([Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] int [] a1, out int n);
+
        [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_nonblittable_array", CharSet = CharSet.Unicode)]
        public static extern int mono_test_marshal_inout_nonblittable_array ([In, Out] char [] a1);
        
@@ -255,6 +271,12 @@ public class Tests {
        [DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
        public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
 
+       [DllImport ("libtest", EntryPoint="mono_test_return_empty_struct")]
+       public static extern EmptyStruct mono_test_return_empty_struct (int a);
+
+       [DllImport ("libtest", EntryPoint="mono_test_return_empty_struct")]
+       public static extern EmptyStructCpp mono_test_return_empty_struct_cpp (int a);
+
        [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct")]
        public static extern int mono_test_marshal_lpstruct ([In, MarshalAs(UnmanagedType.LPStruct)] SimpleStruct ss);
 
@@ -288,12 +310,12 @@ public class Tests {
        [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
        public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
 
-       [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder2")]
-       public static extern void mono_test_marshal_stringbuilder2 (StringBuilder sb, int len);
-
        [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_default")]
        public static extern void mono_test_marshal_stringbuilder_default (StringBuilder sb, int len);
 
+       [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_append")]
+       public static extern void mono_test_marshal_stringbuilder_append (StringBuilder sb, int len);
+
        [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_unicode", CharSet=CharSet.Unicode)]
        public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb, int len);
 
@@ -403,6 +425,35 @@ public class Tests {
                return 0;
        }
 
+       public static int test_0_marshal_out_byref_array_out_size_param () {
+               int [] a1 = null;
+               int len;
+
+               int res = mono_test_marshal_out_byref_array_out_size_param (out a1, out len);
+               if (len != 4)
+                       return 1;
+               for (int i = 0; i < len; i++)
+                       if (a1 [i] != i)
+                               return 2;
+               return 0;
+       }
+
+       public static int test_0_marshal_out_lparray_out_size_param () {
+               int [] a1 = null;
+               int len;
+
+               a1 = new int [10];
+               int res = mono_test_marshal_out_lparray_out_size_param (a1, out len);
+               // Check that a1 was not overwritten
+               a1.GetHashCode ();
+               if (len != 4)
+                       return 1;
+               for (int i = 0; i < len; i++)
+                       if (a1 [i] != i)
+                               return 2;
+               return 0;
+       }
+
        public static int test_0_marshal_inout_nonblittable_array () {
                char [] a1 = new char [10];
                for (int i = 0; i < 10; i++)
@@ -462,9 +513,22 @@ public class Tests {
 
                if (mono_test_empty_struct (1, es, 2) != 0)
                        return 1;
+
+               mono_test_return_empty_struct (42);
+
+               return 0;
+       }
+
+       /* FIXME: This doesn't work on all platforms */
+       /*
+       public static int test_0_marshal_empty_struct_cpp () {
+               EmptyStructCpp es = new EmptyStructCpp ();
+
+               mono_test_return_empty_struct_cpp (42);
                
                return 0;
        }
+       */
 
        public static int test_0_marshal_lpstruct () {
                SimpleStruct ss = new  SimpleStruct ();
@@ -787,11 +851,33 @@ public class Tests {
                if (res != "This is my message.  Isn't it nice?")
                        return 1;  
 
-               // Test that cached_str is cleared
-               mono_test_marshal_stringbuilder2 (sb, sb.Capacity);
-               res = sb.ToString();
-               if (res != "EFGH")
-                       return 2;
+               // Test StringBuilder with default capacity (16)
+               StringBuilder sb2 = new StringBuilder();
+               mono_test_marshal_stringbuilder_default (sb2, sb2.Capacity);
+               if (sb2.ToString () != "This is my messa")
+                       return 3;
+
+               return 0;
+       }
+
+       public static int test_0_marshal_stringbuilder_append () {
+               const String in_sentinel = "MONO_";
+               const String out_sentinel = "CSHARP_";
+               const int iterations = 100;
+               StringBuilder sb = new StringBuilder(255);
+               StringBuilder check = new StringBuilder(255);
+
+               for (int i = 0; i < iterations; i++) {
+                       sb.Append (in_sentinel[i % in_sentinel.Length]);
+                       check.Append (out_sentinel[i % out_sentinel.Length]);
+
+                       mono_test_marshal_stringbuilder_append (sb, sb.Length);
+
+                       String res = sb.ToString();
+                       String checkRev = check.ToString();
+                       if (res != checkRev)
+                               return 1;
+               }
 
                // Test StringBuilder with default capacity (16)
                StringBuilder sb2 = new StringBuilder();
@@ -1800,5 +1886,16 @@ public class Tests {
 
                return 0;
        }
+
+       [DllImport ("libtest", EntryPoint = "mono_test_marshal_return_single_double_struct")]
+       public static extern SingleDoubleStruct mono_test_marshal_return_single_double_struct ();
+
+       public static int test_0_x86_single_double_struct_ret () {
+               double d = mono_test_marshal_return_single_double_struct ().d;
+               if (d != 3.0)
+                       return 1;
+               else
+                       return 0;
+       }
 }