2010-06-17 Geoff Norton <gnorton@novell.com>
[mono.git] / mono / tests / marshal.cs
blob166a4d1de2c08d3d6f530905e62d409740b177f1
1 //
2 // marshal.cs: tests for the System.Runtime.InteropServices.Marshal class
3 //
5 using System;
6 using System.Reflection;
7 using System.Runtime.InteropServices;
9 public class Tests {
11 public static int Main (string[] args) {
12 return TestDriver.RunTests (typeof (Tests), args);
15 public delegate int SimpleDelegate (int a);
17 public static int delegate_test (int a)
19 return a + 1;
22 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
23 public static extern int mono_test_marshal_delegate (IntPtr ptr);
25 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate")]
26 public static extern IntPtr mono_test_marshal_return_delegate (SimpleDelegate d);
28 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate_2")]
29 public static extern IntPtr mono_test_marshal_return_delegate_2 ();
31 static int test_0_get_function_pointer_for_delegate () {
32 // This is a 2.0 feature
33 MethodInfo mi = typeof (Marshal).GetMethod ("GetFunctionPointerForDelegate");
34 if (mi == null)
35 return 0;
37 IntPtr fnPtr = (IntPtr)mi.Invoke (null, new object [] { new SimpleDelegate (delegate_test)});
39 if (mono_test_marshal_delegate (fnPtr) != 3)
40 return 1;
42 return 0;
45 static int test_0_get_delegate_for_function_pointer () {
46 // This is a 2.0 feature
47 MethodInfo mi = typeof (Marshal).GetMethod ("GetDelegateForFunctionPointer");
48 if (mi == null)
49 return 0;
51 IntPtr ptr = mono_test_marshal_return_delegate (new SimpleDelegate (delegate_test));
53 SimpleDelegate d = (SimpleDelegate)mi.Invoke (null, new object [] { ptr, typeof (SimpleDelegate) });
55 return d (5) == 6 ? 0 : 1;
58 /* Obtain a delegate from a native function pointer */
59 static int test_0_get_delegate_for_ftnptr_native () {
60 // This is a 2.0 feature
61 MethodInfo mi = typeof (Marshal).GetMethod ("GetDelegateForFunctionPointer");
62 if (mi == null)
63 return 0;
65 IntPtr ptr = mono_test_marshal_return_delegate_2 ();
67 SimpleDelegate d = (SimpleDelegate)mi.Invoke (null, new object [] { ptr, typeof (SimpleDelegate) });
69 return d (5) == 6 ? 0 : 1;