2004-08-31 Zoltan Varga <vargaz@freemail.hu>
authorZoltan Varga <vargaz@gmail.com>
Tue, 31 Aug 2004 18:58:07 +0000 (18:58 -0000)
committerZoltan Varga <vargaz@gmail.com>
Tue, 31 Aug 2004 18:58:07 +0000 (18:58 -0000)
* marshal9.cs: Clean up and expand custom marshalling tests.

* marshal10.cs: Merge this into marshal9.cs.

svn path=/trunk/mono/; revision=33121

mono/tests/ChangeLog
mono/tests/Makefile.am
mono/tests/libtest.c
mono/tests/marshal10.cs [deleted file]
mono/tests/marshal9.cs

index a5b19e0260cd135c7999eb06406b01a4e3580a45..5a25e9e86f9596f57a7bf466f54f3b1f9532571e 100644 (file)
@@ -1,5 +1,9 @@
 2004-08-31  Zoltan Varga  <vargaz@freemail.hu>
 
+       * marshal9.cs: Clean up and expand custom marshalling tests.
+
+       * marshal10.cs: Merge this into marshal9.cs.
+
        * pinvoke12.cs: Remove, this was merged into pinvoke2.cs a long time
        ago.
 
index 2e747ac2a34ad2951ebc0dd89ae1c62f117fcf8c..cc44f2a4869a98873bd2983c7cf7517476ec1b5e 100644 (file)
@@ -150,7 +150,6 @@ TEST_CS_SRC=                        \
        marshal7.cs             \
        marshal8.cs             \
        marshal9.cs             \
-       marshal10.cs            \
        test-byval-in-struct.cs \
        thread.cs               \
        thread5.cs              \
index e47c0595e39e3a6884fa3c6667bb0ad0effd8c90..4fb874c9578238c71bab3183d58bf17fe1c632c6 100644 (file)
@@ -858,14 +858,6 @@ callFunction (intcharFunc f)
        f ("ABC");
 }
 
-int
-printInt (int* number)
-{
-       printf( "<%d>\n", *number );
-       return *number + 1;
-}
-
-
 typedef struct {
         const char* str;
         int i;
@@ -952,12 +944,6 @@ string_marshal_test3 (char *str)
        return 0;
 }
 
-const char *
-functionReturningString (void)
-{
-    return "ABC";
-}
-
 typedef struct {
        int a;
        int b;
@@ -1200,3 +1186,27 @@ mono_test_marshal_amd64_pass_return_struct4 (amd64_struct4 s)
 
        return s;
 }
+
+void*
+mono_test_marshal_pass_return_custom (int i, guint32 *ptr, int j)
+{
+       /* ptr will be freed by CleanupNative, so make a copy */
+       guint32 *res = g_new0 (guint32, 1);
+
+       *res = *ptr;
+       return res;
+}
+
+typedef void *(*PassReturnPtrDelegate) (void *ptr);
+
+int
+mono_test_marshal_pass_return_custom_in_delegate (PassReturnPtrDelegate del)
+{
+       guint32 num = 10;
+
+       guint32 *ptr = del (&num);
+
+       return *ptr;
+}
+
+
diff --git a/mono/tests/marshal10.cs b/mono/tests/marshal10.cs
deleted file mode 100644 (file)
index 5c244d8..0000000
+++ /dev/null
@@ -1,77 +0,0 @@
-// A demonstration of a custom marshaler that marshals
-// unmanaged to managed data.
-
-using System;
-using System.Runtime.InteropServices;
-
-public class MyMarshal: ICustomMarshaler
-{
-
-       // GetInstance() is not part of ICustomMarshaler, but
-       // custom marshalers are required to implement this
-       // method.
-       public static ICustomMarshaler GetInstance (string s)
-       {
-               Console.WriteLine ("GetInstance called");
-               return new MyMarshal ();
-       }
-       
-       public void CleanUpManagedData (object managedObj)
-       {
-               Console.WriteLine ("CleanUpManagedData called");
-       }
-
-       public void CleanUpNativeData (IntPtr pNativeData)
-       {
-               Console.WriteLine("CleanUpNativeData called");
-               if (pNativeData != IntPtr.Zero)
-                       Marshal.FreeHGlobal (pNativeData);
-       }
-
-
-       // I really do not understand the purpose of this method
-       // or went it would be called. In fact, Rotor never seems
-       // to call it.
-       public int GetNativeDataSize ()
-       {
-               Console.WriteLine("GetNativeDataSize() called");
-               return 4;
-       }
-
-       public IntPtr MarshalManagedToNative (object managedObj)
-       {
-               Console.WriteLine("MarshalManagedToNative()");
-               return IntPtr.Zero;
-       }
-
-
-       // Convert a pointer to unmanaged data into a System.Object.
-       // This method simply converts the unmanaged Ansi C-string
-       // into a System.String and surrounds it with asterisks
-       // to differentiate it from the default marshaler.
-       public object MarshalNativeToManaged (IntPtr pNativeData)
-       {
-               Console.WriteLine("MarshalNativeToManaged()");
-               return "*" + Marshal.PtrToStringAnsi( pNativeData ) + "*";
-       }
-}
-
-public class Testing
-{
-       [DllImport("libtest")]
-       [return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MyMarshal))]
-       private static extern string functionReturningString();
-
-       public static int Main()
-       {
-               string res = functionReturningString();
-
-               if (res != "*ABC*")
-                       return 1;
-
-               return 0;
-       }
-
-
-
-}
index 88e9e89ae7d28cb212cc0813a976c8245ae748d3..84f74b411ea58ab128e4351e9a0191155a74bff1 100644 (file)
@@ -1,43 +1,47 @@
-// A demonstration of a custom marshaler that marshals
-// unmanaged to managed data.
+//
+// marshal9.cs: tests for custom marshalling
+//
 
 using System;
 using System.Runtime.InteropServices;
 
-public class MyMarshal: ICustomMarshaler
+public class Marshal1 : ICustomMarshaler
 {
+       int param;
 
-       // GetInstance() is not part of ICustomMarshaler, but
-       // custom marshalers are required to implement this
-       // method.
-       public static ICustomMarshaler GetInstance (string s)
-       {
-               Console.WriteLine ("GetInstance called");
-               return new MyMarshal ();
+       public static int cleanup_managed_count = 0;
+
+       public static int cleanup_native_count = 0;
+
+       public Marshal1 (int param) {
+               this.param = param;
        }
-       
+
+       public static ICustomMarshaler GetInstance (string s) {
+               int param = Int32.Parse (s);
+               return new Marshal1 (param);
+       }
+
        public void CleanUpManagedData (object managedObj)
        {
-               Console.WriteLine ("CleanUpManagedData called");
+               //Console.WriteLine ("CleanUpManagedData called");
+               cleanup_managed_count ++;
        }
 
        public void CleanUpNativeData (IntPtr pNativeData)
        {
-               Console.WriteLine("CleanUpNativeData called");
-               if (pNativeData != IntPtr.Zero) {
-                       IntPtr realPtr = new IntPtr (pNativeData.ToInt64 () - Marshal.SizeOf (typeof (int)));
-
-                       Marshal.FreeHGlobal (realPtr);
-               }
+               //Console.WriteLine("CleanUpNativeData:" + pNativeData);
+               /* Might be allocated in libtest.c using g_new0 so dont free it */
+               //Marshal.FreeHGlobal (pNativeData);
+               cleanup_native_count ++;
        }
 
-
        // I really do not understand the purpose of this method
        // or went it would be called. In fact, Rotor never seems
        // to call it.
        public int GetNativeDataSize ()
        {
-               Console.WriteLine("GetNativeDataSize() called");
+               //Console.WriteLine("GetNativeDataSize() called");
                return 4;
        }
 
@@ -46,57 +50,70 @@ public class MyMarshal: ICustomMarshaler
                int number;
                IntPtr ptr;
 
-               try {
-                       number = Convert.ToInt32 (managedObj);
-                       ptr = Marshal.AllocHGlobal (8);
-                       Marshal.WriteInt32 (ptr, 0);
-                       Marshal.WriteInt32 (new IntPtr (ptr.ToInt64 () + Marshal.SizeOf (typeof(int))), number);
-                       return new IntPtr (ptr.ToInt64 () + Marshal.SizeOf (typeof (int)));
-               } catch {
-                       return IntPtr.Zero;
-               }
-       }
+               number = Convert.ToInt32 (managedObj);
+               ptr = Marshal.AllocHGlobal (4);
+               Marshal.WriteInt32 (ptr, number);
 
+               //Console.WriteLine ("ToNative: " + ptr);
+               return ptr;
+       }
 
-       // Convert a pointer to unmanaged data into a System.Object.
-       // This method simply converts the unmanaged Ansi C-string
-       // into a System.String and surrounds it with asterisks
-       // to differentiate it from the default marshaler.
        public object MarshalNativeToManaged (IntPtr pNativeData)
        {
-               return "*" + Marshal.PtrToStringAnsi( pNativeData ) + "*";
+               //Console.WriteLine ("ToManaged: " + pNativeData);
+               return param + Marshal.ReadInt32 (pNativeData);
        }
 }
 
-public class Testing
+public class Tests
 {
-       [DllImport("libtest")]
-       private static extern int printInt([MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MyMarshal ))] object number );
+       public static int Main (string[] args) {
+               return TestDriver.RunTests (typeof (Tests));
+       }
 
-       [DllImport("libtest")]
-       private static extern void callFunction (Delegate d);
+       [DllImport ("libtest")]
+       [return : MarshalAs(UnmanagedType.CustomMarshaler,MarshalTypeRef = typeof
+                                               (Marshal1), MarshalCookie = "5")]
+       private static extern object mono_test_marshal_pass_return_custom (int i,  
+                                                                                                                                       [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")] object number, int j);
 
-       delegate void Del ([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MyMarshal))] string x);
+       public static int test_0_pass_return () {
 
-       public static void TestMethod (string s)
-       {
-               Console.WriteLine("s = {0}", s);
-               if (s != "*ABC*")
-                       throw new Exception ("received wrong value");
-       }
+               Marshal1.cleanup_managed_count = 0;
+               Marshal1.cleanup_native_count = 0;
 
-       public static int Main()
-       {
-               object x = 5;
-               if (printInt (x) != 6)
+               int res = (int)mono_test_marshal_pass_return_custom (5, 10, 5);
+
+               if (Marshal1.cleanup_managed_count != 0)
                        return 1;
+               if (Marshal1.cleanup_native_count != 2)
+                       return 2;
+
+               return res == 15 ? 0 : 3;
+       }
 
-               Del del = new Del (TestMethod);
-               callFunction (del);
+       [return : MarshalAs(UnmanagedType.CustomMarshaler,MarshalTypeRef = typeof
+(Marshal1), MarshalCookie = "5")] public delegate object pass_return_int_delegate ([MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Marshal1), MarshalCookie = "5")] object o);
 
-               return 0;
+       [DllImport ("libtest")]
+       private static extern int mono_test_marshal_pass_return_custom_in_delegate (pass_return_int_delegate del);
+
+       private static object pass_return_int (object i) {
+               return (int)i;
        }
 
+       public static int test_0_pass_return_delegate () {
+
+               Marshal1.cleanup_managed_count = 0;
+               Marshal1.cleanup_native_count = 0;
 
+               int res = mono_test_marshal_pass_return_custom_in_delegate (new pass_return_int_delegate (pass_return_int));
 
+               if (Marshal1.cleanup_managed_count != 2)
+                       return 1;
+               if (Marshal1.cleanup_native_count != 0)
+                       return 2;
+
+               return res == 15 ? 0 : 3;
+       }
 }