Merge pull request #1624 from esdrubal/getprocesstimes
[mono.git] / mono / tests / pinvoke2.cs
index 6dd8512eacd47305b34a5a3b5c79f8e236316018..abc81f8d70d77fe44b7a2588b0c7e98d96825339 100644 (file)
@@ -71,6 +71,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)]
@@ -84,6 +89,15 @@ public class Tests {
                public int x;
                public double y;
        }
+       
+       [StructLayout (LayoutKind.Sequential)]
+       public struct TinyStruct {
+               public TinyStruct (int i)
+               {
+                       this.i = i;
+               }
+               public int i;
+       }
 
        [StructLayout (LayoutKind.Sequential)]
        public class SimpleClass {
@@ -213,6 +227,9 @@ 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_inout_nonblittable_array", CharSet = CharSet.Unicode)]
        public static extern int mono_test_marshal_inout_nonblittable_array ([In, Out] char [] a1);
        
@@ -282,6 +299,9 @@ public class Tests {
        [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);
 
@@ -289,7 +309,7 @@ public class Tests {
        public static extern void mono_test_marshal_stringbuilder_out (out StringBuilder sb);
 
        [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_ref")]
-       public static extern void mono_test_marshal_stringbuilder_ref (ref StringBuilder sb);
+       public static extern int mono_test_marshal_stringbuilder_ref (ref StringBuilder sb);
 
        [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_out_unicode", CharSet=CharSet.Unicode)]
        public static extern void mono_test_marshal_stringbuilder_out_unicode (out StringBuilder sb);
@@ -391,6 +411,19 @@ 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_inout_nonblittable_array () {
                char [] a1 = new char [10];
                for (int i = 0; i < 10; i++)
@@ -779,7 +812,35 @@ public class Tests {
                StringBuilder sb2 = new StringBuilder();
                mono_test_marshal_stringbuilder_default (sb2, sb2.Capacity);
                if (sb2.ToString () != "This is my messa")
-                       return 2;
+                       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();
+               mono_test_marshal_stringbuilder_default (sb2, sb2.Capacity);
+               if (sb2.ToString () != "This is my messa")
+                       return 3;
 
                return 0;
        }
@@ -819,17 +880,17 @@ public class Tests {
                return 0;
        }
 
-       /* 
-        * Not yet supported
        public static int test_0_marshal_stringbuilder_ref () {
                StringBuilder sb = new StringBuilder ();
-               mono_test_marshal_stringbuilder_ref (ref sb);
+               sb.Append ("ABC");
+               int res = mono_test_marshal_stringbuilder_ref (ref sb);
+               if (res != 0)
+                       return 1;
                
                if (sb.ToString () != "This is my message.  Isn't it nice?")
-                       return 1;  
+                       return 2;  
                return 0;
        }
-       */
 
        public static int test_0_marshal_empty_string_array () {
                return mono_test_marshal_empty_string_array (null);
@@ -1302,6 +1363,26 @@ public class Tests {
                return string_marshal_test3 (null);
        }
 
+#if FALSE
+       [DllImport ("libtest", EntryPoint="mono_test_stdcall_mismatch_1", CallingConvention=CallingConvention.StdCall)]
+       public static extern int mono_test_stdcall_mismatch_1 (int a, int b, int c);
+
+       /* Test mismatched called conventions, the native function is cdecl */
+       public static int test_0_stdcall_mismatch_1 () {
+               mono_test_stdcall_mismatch_1 (0, 1, 2);
+               return 0;
+       }
+
+       [DllImport ("libtest", EntryPoint="mono_test_stdcall_mismatch_2", CallingConvention=CallingConvention.Cdecl)]
+       public static extern int mono_test_stdcall_mismatch_2 (int a, int b, int c);
+
+       /* Test mismatched called conventions, the native function is stdcall */
+       public static int test_0_stdcall_mismatch_2 () {
+               mono_test_stdcall_mismatch_2 (0, 1, 2);
+               return 0;
+       }
+#endif
+
        [DllImport ("libtest", EntryPoint="mono_test_stdcall_name_mangling", CallingConvention=CallingConvention.StdCall)]
        public static extern int mono_test_stdcall_name_mangling (int a, int b, int c);
 
@@ -1716,5 +1797,62 @@ public class Tests {
                return 1;
        }
 
+       [DllImport ("libtest", EntryPoint="mono_test_has_thiscall")]
+       public static extern int mono_test_has_thiscall ();
+
+       [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall1", CallingConvention=CallingConvention.ThisCall)]
+       public static extern int mono_test_native_thiscall (int a);
+
+       [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall2", CallingConvention=CallingConvention.ThisCall)]
+       public static extern int mono_test_native_thiscall (int a, int b);
+
+       [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall3", CallingConvention=CallingConvention.ThisCall)]
+       public static extern int mono_test_native_thiscall (int a, int b, int c);
+
+       [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall1", CallingConvention=CallingConvention.ThisCall)]
+       public static extern int mono_test_native_thiscall (TinyStruct a);
+
+       [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall2", CallingConvention=CallingConvention.ThisCall)]
+       public static extern int mono_test_native_thiscall (TinyStruct a, int b);
+
+       [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall3", CallingConvention=CallingConvention.ThisCall)]
+       public static extern int mono_test_native_thiscall (TinyStruct a, int b, int c);
+
+       public static int test_0_native_thiscall ()
+       {
+               if (mono_test_has_thiscall () == 0)
+                       return 0;
+
+               if (mono_test_native_thiscall (1968329802) != 1968329802)
+                       return 1;
+
+               if (mono_test_native_thiscall (268894549, 1212675791) != 1481570339)
+                       return 2;
+
+               if (mono_test_native_thiscall (1288082683, -421187449, -1733670329) != -866775098)
+                       return 3;
+
+               if (mono_test_native_thiscall (new TinyStruct(1968329802)) != 1968329802)
+                       return 4;
+
+               if (mono_test_native_thiscall (new TinyStruct(268894549), 1212675791) != 1481570339)
+                       return 5;
+
+               if (mono_test_native_thiscall (new TinyStruct(1288082683), -421187449, -1733670329) != -866775098)
+                       return 6;
+
+               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;
+       }
 }