[sgen] Add missing memory barrier
[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                 IntPtr fnPtr = Marshal.GetFunctionPointerForDelegate (new SimpleDelegate (delegate_test));
33
34                 if (mono_test_marshal_delegate (fnPtr) != 3)
35                         return 1;
36
37                 return 0;
38         }
39
40         static int test_0_get_delegate_for_function_pointer () {
41                 IntPtr ptr = mono_test_marshal_return_delegate (new SimpleDelegate (delegate_test));
42
43                 SimpleDelegate d = (SimpleDelegate)Marshal.GetDelegateForFunctionPointer (ptr, typeof (SimpleDelegate));
44
45                 return d (5) == 6 ? 0 : 1;
46         }
47
48         /* Obtain a delegate from a native function pointer */
49         static int test_0_get_delegate_for_ftnptr_native () {
50                 IntPtr ptr = mono_test_marshal_return_delegate_2 ();
51
52                 SimpleDelegate d = (SimpleDelegate)Marshal.GetDelegateForFunctionPointer (ptr, typeof (SimpleDelegate));
53
54                 return d (5) == 6 ? 0 : 1;
55         }
56 }