New test.
[mono.git] / mono / tests / marshal.cs
1 //
2 // marshal.cs: tests for the System.Runtime.InteropServices.Marshal class
3 //
4
5 using System;
6 using System.Reflection;
7 using System.Runtime.InteropServices;
8
9 public class Tests {
10
11         public static int Main (string[] args) {
12                 return TestDriver.RunTests (typeof (Tests), args);
13         }
14
15         public delegate int SimpleDelegate (int a);
16
17         public static int delegate_test (int a)
18         {
19                 return a + 1;
20         }
21
22         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
23         public static extern int mono_test_marshal_delegate (IntPtr ptr);
24
25         [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate")]
26         public static extern IntPtr mono_test_marshal_return_delegate (SimpleDelegate d);
27
28         [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate_2")]
29         public static extern IntPtr mono_test_marshal_return_delegate_2 ();
30
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;
36
37                 IntPtr fnPtr = (IntPtr)mi.Invoke (null, new object [] { new SimpleDelegate (delegate_test)});
38
39                 if (mono_test_marshal_delegate (fnPtr) != 3)
40                         return 1;
41
42                 return 0;
43         }
44
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;
50
51                 IntPtr ptr = mono_test_marshal_return_delegate (new SimpleDelegate (delegate_test));
52                 
53                 SimpleDelegate d = (SimpleDelegate)mi.Invoke (null, new object [] { ptr, typeof (SimpleDelegate) });
54
55                 return d (5) == 6 ? 0 : 1;
56         }
57
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;
64
65                 IntPtr ptr = mono_test_marshal_return_delegate_2 ();
66
67                 SimpleDelegate d = (SimpleDelegate)mi.Invoke (null, new object [] { ptr, typeof (SimpleDelegate) });
68
69                 return d (5) == 6 ? 0 : 1;
70         }
71 }