[tests] Test loading references from LoadFrom and LoadFile contexts
[mono-project.git] / mono / tests / marshal.cs
blob1213f8c5cbf6ec4746c6d26c5157f403ef680bb1
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 [AttributeUsage (AttributeTargets.Method)]
12 sealed class MonoPInvokeCallbackAttribute : Attribute {
13 public MonoPInvokeCallbackAttribute (Type t) {}
16 public static int Main (string[] args) {
17 return TestDriver.RunTests (typeof (Tests), args);
20 public delegate int SimpleDelegate (int a);
22 [MonoPInvokeCallback (typeof (SimpleDelegate))]
23 public static int delegate_test (int a)
25 return a + 1;
28 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
29 public static extern int mono_test_marshal_delegate (IntPtr ptr);
31 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate")]
32 public static extern IntPtr mono_test_marshal_return_delegate (SimpleDelegate d);
34 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate_2")]
35 public static extern IntPtr mono_test_marshal_return_delegate_2 ();
37 static int test_0_get_function_pointer_for_delegate () {
38 IntPtr fnPtr = Marshal.GetFunctionPointerForDelegate (new SimpleDelegate (delegate_test));
40 if (mono_test_marshal_delegate (fnPtr) != 3)
41 return 1;
43 return 0;
46 static int test_0_get_delegate_for_function_pointer () {
47 IntPtr ptr = mono_test_marshal_return_delegate (new SimpleDelegate (delegate_test));
49 SimpleDelegate d = (SimpleDelegate)Marshal.GetDelegateForFunctionPointer (ptr, typeof (SimpleDelegate));
51 return d (5) == 6 ? 0 : 1;
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 ();
58 SimpleDelegate d = (SimpleDelegate)Marshal.GetDelegateForFunctionPointer (ptr, typeof (SimpleDelegate));
60 return d (5) == 6 ? 0 : 1;