Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / handleref.cs
1 using System;
2 using System.Runtime.InteropServices;
3
4 public class Tests {
5
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);
11
12         [DllImport ("libtest", EntryPoint="mono_xr_as_handle")]
13         public static extern HandleRef mono_xr_as_handle (HandleRef r);
14
15         [DllImport ("libtest")]
16         public static extern int mono_xr (HandleRef sh);
17
18         //
19         // Mono should throw exceptions on ref HandleRefs
20         //
21         public static int test_0_ref_handleref ()
22         {
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;
29                 }
30                 // failed
31                 return 1;
32         }
33
34         //
35         // Mono should throw excentions on return HandleRefs
36         //
37         public static int test_0_handleref_return ()
38         {
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;
45                 }
46                 // failed
47                 return 1;
48         }
49         
50         public static int test_0_marshal_handleref_argument ()
51         {
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;
59         }
60
61         [StructLayout (LayoutKind.Sequential)]
62         public struct StructTest {
63                 public int a;
64                 public HandleRef handle1;
65                 public HandleRef handle2;
66                 public int b;
67         }
68
69         [DllImport ("libtest")]
70         public static extern int mono_safe_handle_struct_ref (ref StructTest test);
71
72         [DllImport ("libtest")]
73         public static extern int mono_safe_handle_struct (StructTest test);
74
75         static StructTest x = new StructTest ();
76
77         public static int test_0_marshal_safehandle_field ()
78         {
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);
84
85                 if (mono_safe_handle_struct (x) != 0xf00f)
86                         return 1;
87
88                 return 0;
89         }
90
91         public static int test_0_marshal_safehandle_field_ref ()
92         {
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);
98                 
99                 if (mono_safe_handle_struct_ref (ref x) != 0xf00d)
100                         return 1;
101
102                 return 0;
103         }
104         
105         static int Main ()
106         {
107                 return TestDriver.RunTests (typeof (Tests));
108         }
109 }