Merge pull request #5210 from alexrp/profiler-runtime-settings
[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         [AttributeUsage (AttributeTargets.Method)]
12         sealed class MonoPInvokeCallbackAttribute : Attribute {
13                 public MonoPInvokeCallbackAttribute (Type t) {}
14         }
15
16         public static int Main (string[] args) {
17                 return TestDriver.RunTests (typeof (Tests), args);
18         }
19
20         public delegate int SimpleDelegate (int a);
21
22         [MonoPInvokeCallback (typeof (SimpleDelegate))]
23         public static int delegate_test (int a)
24         {
25                 return a + 1;
26         }
27
28         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
29         public static extern int mono_test_marshal_delegate (IntPtr ptr);
30
31         [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate")]
32         public static extern IntPtr mono_test_marshal_return_delegate (SimpleDelegate d);
33
34         [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate_2")]
35         public static extern IntPtr mono_test_marshal_return_delegate_2 ();
36
37         static int test_0_get_function_pointer_for_delegate () {
38                 IntPtr fnPtr = Marshal.GetFunctionPointerForDelegate (new SimpleDelegate (delegate_test));
39
40                 if (mono_test_marshal_delegate (fnPtr) != 3)
41                         return 1;
42
43                 return 0;
44         }
45
46         static int test_0_get_delegate_for_function_pointer () {
47                 IntPtr ptr = mono_test_marshal_return_delegate (new SimpleDelegate (delegate_test));
48
49                 SimpleDelegate d = (SimpleDelegate)Marshal.GetDelegateForFunctionPointer (ptr, typeof (SimpleDelegate));
50
51                 return d (5) == 6 ? 0 : 1;
52         }
53
54         /* Obtain a delegate from a native function pointer */
55         static int test_0_get_delegate_for_ftnptr_native () {
56                 IntPtr ptr = mono_test_marshal_return_delegate_2 ();
57
58                 SimpleDelegate d = (SimpleDelegate)Marshal.GetDelegateForFunctionPointer (ptr, typeof (SimpleDelegate));
59
60                 return d (5) == 6 ? 0 : 1;
61         }
62 }