2004-03-14 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / tests / pinvoke2.cs
1 using System;
2 using System.Text;
3 using System.Runtime.InteropServices;
4
5 public class Tests {
6
7         public static int delegate_test (int a)
8         {
9                 if (a == 2)
10                         return 0;
11
12                 return 1;
13         }
14         
15         [StructLayout (LayoutKind.Sequential)]
16         public struct SimpleStruct {
17                 public bool a;
18                 public bool b;
19                 public bool c;
20                 public string d;
21         }
22
23         [StructLayout (LayoutKind.Sequential)]
24         public struct SimpleStruct2 {
25                 public bool a;
26                 public bool b;
27                 public bool c;
28                 public string d;
29                 public byte e;
30                 public double f;
31                 public byte g;
32                 public long h;
33         }
34
35         [StructLayout (LayoutKind.Sequential, Size=0)]
36         public struct EmptyStruct {
37         }
38
39         /* sparcv9 has complex conventions when passing structs with doubles in them 
40            by value, some simple tests for them */
41         [StructLayout (LayoutKind.Sequential)]
42         public struct Point {
43                 public double x;
44                 public double y;
45         }
46
47         [StructLayout (LayoutKind.Sequential)]
48         public struct MixedPoint {
49                 public int x;
50                 public double y;
51         }
52
53         [StructLayout (LayoutKind.Sequential)]
54         public class SimpleClass {
55                 public bool a;
56                 public bool b;
57                 public bool c;
58                 public string d;
59                 public byte e;
60                 public double f;
61                 public byte g;
62                 public long h;
63         }
64
65         [StructLayout (LayoutKind.Sequential)]
66         public class EmptyClass {
67         }
68
69         [DllImport ("libnot-found", EntryPoint="not_found")]
70         public static extern int mono_library_not_found ();
71
72         [DllImport ("libtest", EntryPoint="not_found")]
73         public static extern int mono_entry_point_not_found ();
74
75         [DllImport ("libtest.dll", EntryPoint="mono_test_marshal_char")]
76         public static extern int mono_test_marshal_char_2 (char a1);
77
78         [DllImport ("libtest", EntryPoint="mono_test_marshal_char")]
79         public static extern int mono_test_marshal_char (char a1);
80
81         [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_byref")]
82         public static extern int mono_test_marshal_bool_byref (int a, ref bool b, int c);
83
84         [DllImport ("libtest", EntryPoint="mono_test_marshal_array")]
85         public static extern int mono_test_marshal_array (int [] a1);
86
87         [DllImport ("libtest", EntryPoint="mono_test_marshal_string_array")]
88         public static extern int mono_test_marshal_string_array (string [] a1);
89
90         [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_array")]
91         public static extern int mono_test_marshal_inout_array ([In, Out] int [] a1);
92
93         [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_nonblittable_array", CharSet = CharSet.Unicode)]
94         public static extern int mono_test_marshal_inout_nonblittable_array ([In, Out] char [] a1);
95         
96         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
97         public static extern int mono_test_marshal_struct (SimpleStruct ss);
98
99         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2")]
100         public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss);
101
102         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2_2")]
103         public static extern int mono_test_marshal_struct2_2 (int i, int j, int k, SimpleStruct2 ss);
104
105         [DllImport ("libtest", EntryPoint="mono_test_marshal_point")]
106         public static extern int mono_test_marshal_point (Point p);
107
108         [DllImport ("libtest", EntryPoint="mono_test_marshal_mixed_point")]
109         public static extern int mono_test_marshal_mixed_point (MixedPoint p);
110
111         [DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
112         public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
113
114         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct_array")]
115         public static extern int mono_test_marshal_struct_array (SimpleStruct2[] ss);
116
117         [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
118         public static extern SimpleClass mono_test_marshal_class (int i, int j, int k, SimpleClass ss, int l);
119
120         [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_class")]
121         public static extern int mono_test_marshal_byref_class (ref SimpleClass ss);
122
123         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
124         public static extern int mono_test_marshal_delegate (SimpleDelegate d);
125
126         [DllImport ("libtest", EntryPoint="mono_test_return_vtype")]
127         public static extern SimpleStruct mono_test_return_vtype (IntPtr i);
128
129         [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
130         public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
131
132         [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_unicode", CharSet=CharSet.Unicode)]
133         public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb, int len);
134
135         [DllImport ("libtest", EntryPoint="mono_test_last_error", SetLastError=true)]
136         public static extern void mono_test_last_error (int err);
137
138         public delegate int SimpleDelegate (int a);
139
140         public static int Main () {
141                 return TestDriver.RunTests (typeof (Tests));
142         }
143
144         static int test_0_marshal_char () {
145                 return mono_test_marshal_char ('a');
146         }
147
148         static int test_1225_marshal_array () {
149                 int [] a1 = new int [50];
150                 for (int i = 0; i < 50; i++)
151                         a1 [i] = i;
152
153                 return mono_test_marshal_array (a1);
154         }
155
156         static int test_1225_marshal_inout_array () {
157                 int [] a1 = new int [50];
158                 for (int i = 0; i < 50; i++)
159                         a1 [i] = i;
160
161                 int res = mono_test_marshal_inout_array (a1);
162
163                 for (int i = 0; i < 50; i++)
164                         if (a1 [i] != 50 - i) {
165                                 Console.WriteLine ("X: " + i + " " + a1 [i]);
166                                 return 2;
167                         }
168
169                 return res;
170         }
171
172         static int test_0_marshal_inout_nonblittable_array () {
173                 char [] a1 = new char [10];
174                 for (int i = 0; i < 10; i++)
175                         a1 [i] = "Hello, World" [i];
176
177                 int res = mono_test_marshal_inout_nonblittable_array (a1);
178
179                 for (int i = 0; i < 10; i++)
180                         if (a1 [i] != 'F')
181                                 return 2;
182
183                 return res;
184         }
185
186         static int test_0_marshal_struct () {
187                 SimpleStruct ss = new  SimpleStruct ();
188                 ss.b = true;
189                 ss.d = "TEST";
190                 
191                 return mono_test_marshal_struct (ss);
192         }
193
194         static int test_0_marshal_struct2 () {
195                 SimpleStruct2 ss2 = new  SimpleStruct2 ();
196                 ss2.b = true;
197                 ss2.d = "TEST";
198                 ss2.e = 99;
199                 ss2.f = 1.5;
200                 ss2.g = 42;
201                 ss2.h = 123L;
202
203                 return mono_test_marshal_struct2 (ss2);
204         }
205
206         static int test_0_marshal_struct3 () {
207                 SimpleStruct2 ss2 = new  SimpleStruct2 ();
208                 ss2.b = true;
209                 ss2.d = "TEST";
210                 ss2.e = 99;
211                 ss2.f = 1.5;
212                 ss2.g = 42;
213                 ss2.h = 123L;
214
215                 return mono_test_marshal_struct2_2 (10, 11, 12, ss2);
216         }
217
218         static int test_0_marshal_empty_struct () {
219                 EmptyStruct es = new EmptyStruct ();
220
221                 if (mono_test_empty_struct (1, es, 2) != 0)
222                         return 1;
223                 
224                 return 0;
225         }
226
227         static int test_0_marshal_struct_array () {
228                 SimpleStruct2[] ss_arr = new SimpleStruct2 [2];
229
230                 SimpleStruct2 ss2 = new SimpleStruct2 ();
231                 ss2.b = true;
232                 ss2.d = "TEST";
233                 ss2.e = 99;
234                 ss2.f = 1.5;
235                 ss2.g = 42;
236                 ss2.h = 123L;
237
238                 ss_arr [0] = ss2;
239
240                 ss2.b = false;
241                 ss2.d = "TEST2";
242                 ss2.e = 100;
243                 ss2.f = 2.5;
244                 ss2.g = 43;
245                 ss2.h = 124L;
246
247                 ss_arr [1] = ss2;
248
249                 return mono_test_marshal_struct_array (ss_arr);
250         }
251
252         /* Test classes as arguments and return values */
253         static int test_0_marshal_class () {
254                 SimpleClass ss = new  SimpleClass ();
255                 ss.b = true;
256                 ss.d = "TEST";
257                 ss.e = 99;
258                 ss.f = 1.5;
259                 ss.g = 42;
260                 ss.h = 123L;
261
262                 SimpleClass res = mono_test_marshal_class (10, 11, 12, ss, 14);
263                 if (res == null)
264                         return 1;
265                 if  (! (res.a == ss.a && res.b == ss.b && res.c == ss.c && 
266                                 res.d == ss.d && res.e == ss.e && res.f == ss.f &&
267                                 res.g == ss.g && res.h == ss.h))
268                         return 2;
269
270                 /* Test null arguments and results */
271                 res = mono_test_marshal_class (10, 11, 12, null, 14);
272                 if (res != null)
273                         return 3;
274
275                 return 0;
276         }
277
278         static int test_0_marshal_byref_class () {
279                 SimpleClass ss = new  SimpleClass ();
280                 ss.b = true;
281                 ss.d = "TEST";
282                 ss.e = 99;
283                 ss.f = 1.5;
284                 ss.g = 42;
285                 ss.h = 123L;
286
287                 int res = mono_test_marshal_byref_class (ref ss);
288                 if (ss.d != "TEST-RES")
289                         return 1;
290
291                 return 0;
292         }
293
294         static int test_0_marshal_delegate () {
295                 SimpleDelegate d = new SimpleDelegate (delegate_test);
296
297                 return mono_test_marshal_delegate (d);
298         }
299
300         static int test_0_marshal_point () {
301                 Point pt = new Point();
302                 pt.x = 1.25;
303                 pt.y = 3.5;
304                 
305                 return mono_test_marshal_point(pt);
306         }
307
308         static int test_0_marshal_mixed_point () {
309                 MixedPoint mpt = new MixedPoint();
310                 mpt.x = 5;
311                 mpt.y = 6.75;
312                 
313                 return mono_test_marshal_mixed_point(mpt);
314         }
315
316         static int test_0_marshal_bool_byref () {
317                 bool b = true;
318                 if (mono_test_marshal_bool_byref (99, ref b, 100) != 1)
319                         return 1;
320                 b = false;
321                 if (mono_test_marshal_bool_byref (99, ref b, 100) != 0)
322                         return 12;
323                 if (b != true)
324                         return 13;
325
326                 return 0;
327         }
328
329         static int test_0_return_vtype () {
330                 SimpleStruct ss = mono_test_return_vtype (new IntPtr (5));
331
332                 if (!ss.a && ss.b && !ss.c && ss.d == "TEST")
333                         return 0;
334                 
335                 return 1;
336         }
337
338         static int test_0_marshal_stringbuilder () {
339                 StringBuilder sb = new StringBuilder(255);
340                 mono_test_marshal_stringbuilder (sb, sb.Capacity);
341                 String res = sb.ToString();
342
343                 if (res != "This is my message.  Isn't it nice?")
344                         return 1;  
345                 
346                 return 0;
347         }
348
349         static int test_0_marshal_stringbuilder_unicode () {
350                 StringBuilder sb = new StringBuilder(255);
351                 mono_test_marshal_stringbuilder_unicode (sb, sb.Capacity);
352                 String res = sb.ToString();
353
354                 if (res != "This is my message.  Isn't it nice?")
355                         return 1;  
356                 
357                 return 0;
358         }
359
360         static int test_0_marshal_empty_string_array () {
361                 mono_test_marshal_string_array (null);
362                 return 0;
363         }
364
365         static int test_0_last_error () {
366                 mono_test_last_error (5);
367                 if (Marshal.GetLastWin32Error () == 5)
368                         return 0;
369                 else
370                         return 1;
371         }
372
373         static int test_0_library_not_found () {
374
375                 try {
376                         mono_entry_point_not_found ();
377                         return 1;
378                 }
379                 catch (EntryPointNotFoundException) {
380                 }
381
382                 return 0;
383         }
384
385         static int test_0_entry_point_not_found () {
386
387                 try {
388                         mono_library_not_found ();
389                         return 1;
390                 }
391                 catch (DllNotFoundException) {
392                 }
393
394                 return 0;
395         }
396
397         /* Check that the runtime trims .dll from the library name */
398         static int test_0_trim_dll_from_name () {
399
400                 mono_test_marshal_char_2 ('A');
401
402                 return 0;
403         }
404 }