2010-01-04 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / tests / handleref.cs
blob271e4f4850c410027635e1a4bb405f164567140b
1 using System;
2 using System.Runtime.InteropServices;
4 public class Tests {
6 //
7 // This is not permitted, should throw an exception
8 //
9 [DllImport ("libtest")]
10 public static extern void mono_safe_handle_ref (ref HandleRef handle);
12 [DllImport ("libtest", EntryPoint="mono_xr_as_handle")]
13 public static extern HandleRef mono_xr_as_handle (HandleRef r);
15 [DllImport ("libtest")]
16 public static extern int mono_xr (HandleRef sh);
19 // Mono should throw exceptions on ref HandleRefs
21 public static int test_0_ref_handleref ()
23 object o = new object ();
24 HandleRef s = new HandleRef (o, (IntPtr) 0xeadcafe);
25 try {
26 mono_safe_handle_ref (ref s);
27 } catch (MarshalDirectiveException){
28 return 0;
30 // failed
31 return 1;
35 // Mono should throw excentions on return HandleRefs
37 public static int test_0_handleref_return ()
39 object o = new object ();
40 HandleRef s = new HandleRef (o, (IntPtr) 0xeadcafe);
41 try {
42 HandleRef ret = mono_xr_as_handle (s);
43 } catch (MarshalDirectiveException){
44 return 0;
46 // failed
47 return 1;
50 public static int test_0_marshal_handleref_argument ()
52 object o = new object ();
53 Console.WriteLine ("BEFORE");
54 HandleRef s = new HandleRef (o, (IntPtr) 0xeadcafe);
55 if (mono_xr (s) != (0xeadcafe + 1234))
56 return 1;
57 Console.WriteLine ("AFTER");
58 return 0;
61 [StructLayout (LayoutKind.Sequential)]
62 public struct StructTest {
63 public int a;
64 public HandleRef handle1;
65 public HandleRef handle2;
66 public int b;
69 [DllImport ("libtest")]
70 public static extern int mono_safe_handle_struct_ref (ref StructTest test);
72 [DllImport ("libtest")]
73 public static extern int mono_safe_handle_struct (StructTest test);
75 static StructTest x = new StructTest ();
77 public static int test_0_marshal_safehandle_field ()
79 x.a = 1234;
80 x.b = 8743;
81 object o = new object ();
82 x.handle1 = new HandleRef (o, (IntPtr) 0x7080feed);
83 x.handle2 = new HandleRef (o, (IntPtr) 0x1234abcd);
85 if (mono_safe_handle_struct (x) != 0xf00f)
86 return 1;
88 return 0;
91 public static int test_0_marshal_safehandle_field_ref ()
93 x.a = 1234;
94 x.b = 8743;
95 object o = new object ();
96 x.handle1 = new HandleRef (o, (IntPtr) 0x7080feed);
97 x.handle2 = new HandleRef (o, (IntPtr) 0x1234abcd);
99 if (mono_safe_handle_struct_ref (ref x) != 0xf00d)
100 return 1;
102 return 0;
105 static int Main ()
107 return TestDriver.RunTests (typeof (Tests));