* icall.c: Add icall to access char conversion tables.
[mono.git] / mono / tests / pinvoke3.cs
1 //
2 // pinvoke3.cs:
3 //
4 //  Tests for native->managed marshalling
5 //
6
7 using System;
8 using System.Runtime.InteropServices;
9
10 public class Tests {
11
12         [StructLayout (LayoutKind.Sequential)]
13         public struct SimpleStruct {
14                 public bool a;
15                 public bool b;
16                 public bool c;
17                 public string d;
18         }
19
20         [StructLayout (LayoutKind.Sequential)]
21         public class SimpleClass {
22                 public bool a;
23                 public bool b;
24                 public bool c;
25                 public string d;
26         }
27
28         public static SimpleStruct delegate_test_struct (SimpleStruct ss)
29         {
30                 SimpleStruct res;
31
32                 res.a = !ss.a;
33                 res.b = !ss.b;
34                 res.c = !ss.c;
35                 res.d = ss.d + "-RES";
36
37                 return res;
38         }
39
40         public static int delegate_test_struct_byref (int a, ref SimpleStruct ss, int b)
41         {
42                 if (a == 1 && b == 2 && ss.a && !ss.b && ss.c && ss.d == "TEST2")
43                         return 0;
44
45                 return 1;
46         }
47
48         public static SimpleClass delegate_test_class (SimpleClass ss)
49         {
50                 if (ss == null)
51                         return null;
52
53                 if (! (!ss.a && ss.b && !ss.c && ss.d == "TEST"))
54                         return null;
55
56                 SimpleClass res = ss;
57
58                 return res;
59         }
60
61         public static int delegate_test_class_byref (ref SimpleClass ss)
62         {
63                 if (ss == null)
64                         return -1;
65
66                 if (!ss.a && ss.b && !ss.c && ss.d == "TEST") {
67                         ss.a = true;
68                         ss.b = false;
69                         ss.c = true;
70                         ss.d = "RES";
71
72                         return 0;
73                 }
74
75                 return 1;
76         }
77
78         public static int delegate_test_class_out (out SimpleClass ss)
79         {
80                 ss = new SimpleClass ();
81                 ss.a = true;
82                 ss.b = false;
83                 ss.c = true;
84                 ss.d = "RES";
85
86                 return 0;
87         }
88
89         [DllImport ("libtest", EntryPoint="mono_test_ref_vtype")]
90         public static extern int mono_test_ref_vtype (int a, ref SimpleStruct ss, int b, TestDelegate d);
91         
92         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate2")]
93         public static extern int mono_test_marshal_delegate2 (SimpleDelegate2 d);
94
95         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate4")]
96         public static extern int mono_test_marshal_delegate4 (SimpleDelegate4 d);
97
98         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate5")]
99         public static extern int mono_test_marshal_delegate5 (SimpleDelegate5 d);
100
101         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate6")]
102         public static extern int mono_test_marshal_delegate6 (SimpleDelegate5 d);
103
104         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate7")]
105         public static extern int mono_test_marshal_delegate7 (SimpleDelegate7 d);
106
107         public delegate int TestDelegate (int a, ref SimpleStruct ss, int b);
108
109         public delegate SimpleStruct SimpleDelegate2 (SimpleStruct ss);
110
111         public delegate SimpleClass SimpleDelegate4 (SimpleClass ss);
112
113         public delegate int SimpleDelegate5 (ref SimpleClass ss);
114
115         public delegate int SimpleDelegate7 (out SimpleClass ss);
116
117         public static int Main () {
118                 return TestDriver.RunTests (typeof (Tests));
119         }
120
121         /* Test structures as arguments and return values of delegates */
122         static int test_0_marshal_struct_delegate () {
123                 SimpleDelegate2 d = new SimpleDelegate2 (delegate_test_struct);
124
125                 return mono_test_marshal_delegate2 (d);
126         }
127
128         /* Test structures as byref arguments of delegates */
129         static int test_0_marshal_byref_struct_delegate () {
130                 SimpleStruct ss = new SimpleStruct ();
131                 TestDelegate d = new TestDelegate (delegate_test_struct_byref);
132                 
133                 ss.b = true;
134                 ss.d = "TEST1";
135
136                 if (mono_test_ref_vtype (1, ref ss, 2, d) != 0)
137                         return 1;
138                 
139                 if (! (ss.a && !ss.b && ss.c && ss.d == "TEST2"))
140                         return 2;
141                 
142                 return 0;
143         }
144
145         /* Test classes as arguments and return values of delegates */
146         static int test_0_marshal_class_delegate () {
147                 SimpleDelegate4 d = new SimpleDelegate4 (delegate_test_class);
148
149                 return mono_test_marshal_delegate4 (d);
150         }
151
152         /* Test classes as byref arguments of delegates */
153         static int test_0_marshal_byref_class_delegate () {
154                 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
155
156                 return mono_test_marshal_delegate5 (d);
157         }
158
159         /* Test classes as out arguments of delegates */
160         static int test_0_marshal_out_class_delegate () {
161                 SimpleDelegate7 d = new SimpleDelegate7 (delegate_test_class_out);
162
163                 return mono_test_marshal_delegate7 (d);
164         }
165
166         /* Test that the delegate wrapper correctly catches null byref arguments */
167         static int test_0_marshal_byref_class_delegate_null () {
168                 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
169                 
170                 try {
171                         mono_test_marshal_delegate6 (d);
172                         return 1;
173                 }
174                 catch (ArgumentNullException ex) {
175                         return 0;
176                 }
177         }
178 }