Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / winx64structs.cs
1 using System;
2 using System.Runtime.InteropServices;
3
4 [AttributeUsage (AttributeTargets.Method)]
5 sealed class MonoPInvokeCallbackAttribute : Attribute {
6         public MonoPInvokeCallbackAttribute (Type t) {}
7 }
8
9 [StructLayout (LayoutKind.Sequential)]
10 struct winx64_struct1
11 {
12         public winx64_struct1 (byte ia)
13         {
14                 a = ia;
15         }
16         public byte a;
17 }
18
19 [StructLayout (LayoutKind.Sequential)]
20 struct winx64_struct2
21 {
22         public winx64_struct2 (byte ia, byte ib)
23         {
24                 a = ia;
25                 b = ib;
26         }
27         
28         public byte a;
29         public byte b;
30 }
31
32 [StructLayout (LayoutKind.Sequential)]
33 struct winx64_struct3
34 {
35         public winx64_struct3 (byte ia, byte ib, short ic)
36         {
37                 a = ia;
38                 b = ib;
39                 c = ic;
40         }
41         
42         public byte a;
43         public byte b;
44         public short c;
45 }
46
47 [StructLayout (LayoutKind.Sequential)]
48 struct winx64_struct4
49 {
50         public winx64_struct4 (byte ia, byte ib, short ic, uint id)
51         {
52                 a = ia;
53                 b = ib;
54                 c = ic;
55                 d = id;
56         }
57         
58         public byte a;
59         public byte b;
60         public short c;
61         public uint d;
62 }
63
64 [StructLayout (LayoutKind.Sequential)]
65 struct winx64_struct5
66 {
67         public winx64_struct5 (byte ia, byte ib, byte ic)
68         {
69                 a = ia;
70                 b = ib;
71                 c = ic;
72         }
73         
74         public byte a;
75         public byte b;
76         public byte c;
77 }
78
79 [StructLayout (LayoutKind.Sequential)]
80 struct winx64_struct6
81 {
82         public winx64_struct6 (winx64_struct1 ia, short ib, byte ic)
83         {
84                 a = ia;
85                 b = ib;
86                 c = ic;
87         }
88         
89         public winx64_struct1 a;
90         public short b;
91         public byte c;
92 }
93
94 [StructLayout (LayoutKind.Sequential)]
95 struct winx64_floatStruct
96 {
97         public winx64_floatStruct (float ia, float ib)
98         {
99                 a = ia;
100                 b = ib;
101         }
102         
103         public float a;
104         public float b;
105 }
106
107 [StructLayout (LayoutKind.Sequential)]
108 struct winx64_doubleStruct
109 {
110         public winx64_doubleStruct (double ia)
111         {
112                 a = ia;
113         }
114         
115         public double a;
116 }
117
118 class winx64structs
119 {
120         [DllImport ("libtest")]
121         static extern int mono_test_Winx64_struct1_in ([MarshalAs (UnmanagedType.Struct)] winx64_struct1 var);
122
123         [DllImport ("libtest")]
124         static extern int mono_test_Winx64_struct2_in ([MarshalAs (UnmanagedType.Struct)] winx64_struct2 var);
125
126         [DllImport ("libtest")]
127         static extern int mono_test_Winx64_struct3_in ([MarshalAs (UnmanagedType.Struct)] winx64_struct3 var);
128
129         [DllImport ("libtest")]
130         static extern int mono_test_Winx64_struct4_in ([MarshalAs (UnmanagedType.Struct)] winx64_struct4 var);
131
132         [DllImport ("libtest")]
133         static extern int mono_test_Winx64_struct5_in ([MarshalAs (UnmanagedType.Struct)] winx64_struct5 var);
134
135         [DllImport ("libtest")]
136         static extern int mono_test_Winx64_struct6_in ([MarshalAs (UnmanagedType.Struct)] winx64_struct6 var);
137
138         [DllImport ("libtest")]
139         static extern int mono_test_Winx64_structs_in1 ([MarshalAs (UnmanagedType.Struct)] winx64_struct1 var1,
140                                                    [MarshalAs (UnmanagedType.Struct)] winx64_struct2 var2,
141                                                    [MarshalAs (UnmanagedType.Struct)] winx64_struct3 var3,
142                                                    [MarshalAs (UnmanagedType.Struct)] winx64_struct4 var4);
143
144         [DllImport ("libtest")]
145         static extern int mono_test_Winx64_structs_in2 ([MarshalAs (UnmanagedType.Struct)] winx64_struct1 var1,
146                                                    [MarshalAs (UnmanagedType.Struct)] winx64_struct1 var2,
147                                                    [MarshalAs (UnmanagedType.Struct)] winx64_struct1 var3,
148                                                    [MarshalAs (UnmanagedType.Struct)] winx64_struct1 var4,
149                                                    [MarshalAs (UnmanagedType.Struct)] winx64_struct1 var5);
150
151         [DllImport ("libtest")]
152         static extern int mono_test_Winx64_structs_in3 ([MarshalAs (UnmanagedType.Struct)] winx64_struct1 var1,
153                                                    [MarshalAs (UnmanagedType.Struct)] winx64_struct5 var2,
154                                                    [MarshalAs (UnmanagedType.Struct)] winx64_struct1 var3,
155                                                    [MarshalAs (UnmanagedType.Struct)] winx64_struct5 var4,
156                                                    [MarshalAs (UnmanagedType.Struct)] winx64_struct1 var5,
157                                                    [MarshalAs (UnmanagedType.Struct)] winx64_struct5 var6);
158
159         [DllImport ("libtest")]
160         [return:MarshalAs (UnmanagedType.Struct)]
161         static extern winx64_struct1 mono_test_Winx64_struct1_ret ();
162
163         [DllImport ("libtest")]
164         [return: MarshalAs (UnmanagedType.Struct)]
165         static extern winx64_struct2 mono_test_Winx64_struct2_ret ();
166
167         [DllImport ("libtest")]
168         [return: MarshalAs (UnmanagedType.Struct)]
169         static extern winx64_struct3 mono_test_Winx64_struct3_ret ();
170
171         [DllImport ("libtest")]
172         [return: MarshalAs (UnmanagedType.Struct)]
173         static extern winx64_struct4 mono_test_Winx64_struct4_ret ();
174
175         [DllImport ("libtest")]
176         [return: MarshalAs (UnmanagedType.Struct)]
177         static extern winx64_struct5 mono_test_Winx64_struct5_ret ();
178
179         [DllImport ("libtest")]
180         [return: MarshalAs (UnmanagedType.Struct)]
181         static extern winx64_struct1 mono_test_Winx64_struct1_ret_5_args (byte a, byte b, byte c, byte d, byte e);
182
183         [DllImport ("libtest")]
184         [return: MarshalAs (UnmanagedType.Struct)]
185         static extern winx64_struct5 mono_test_Winx64_struct5_ret6_args (byte a, byte b, byte c, byte d, byte e ); 
186
187         [DllImport ("libtest")]
188         static extern int mono_test_Winx64_floatStruct ([MarshalAs (UnmanagedType.Struct)] winx64_floatStruct var);
189
190         [DllImport ("libtest")]
191         static extern int mono_test_Winx64_doubleStruct ([MarshalAs (UnmanagedType.Struct)] winx64_doubleStruct var);
192
193         public delegate int managed_struct1_delegate ([MarshalAs (UnmanagedType.Struct)] winx64_struct1 var);
194
195         [DllImport ("libtest")]
196         static extern int mono_test_managed_Winx64_struct1_in (managed_struct1_delegate func);
197
198         public delegate int managed_struct5_delegate ([MarshalAs (UnmanagedType.Struct)] winx64_struct5 var);
199
200         [DllImport ("libtest")]
201         static extern int mono_test_managed_Winx64_struct5_in (managed_struct5_delegate func);
202
203         public delegate int managed_struct1_struct5_delegate (winx64_struct1 var1, winx64_struct5 var2,
204                                                               winx64_struct1 var3, winx64_struct5 var4,
205                                                               winx64_struct1 var5, winx64_struct5 var6);
206
207         [DllImport ("libtest")]
208         static extern int mono_test_managed_Winx64_struct1_struct5_in (managed_struct1_struct5_delegate func);
209
210         [return:MarshalAs (UnmanagedType.Struct)]
211         public delegate winx64_struct1 mono_test_Winx64_struct1_ret_delegate ();
212
213         [DllImport ("libtest")]
214         static extern int mono_test_Winx64_struct1_ret_managed (mono_test_Winx64_struct1_ret_delegate func);
215
216         [return: MarshalAs (UnmanagedType.Struct)]
217         public delegate winx64_struct5 mono_test_Winx64_struct5_ret_delegate ();
218
219         [DllImport ("libtest")]
220         static extern int mono_test_Winx64_struct5_ret_managed (mono_test_Winx64_struct5_ret_delegate func);
221         
222
223         private static bool enableBroken = false;
224         
225         static int Main (string[] args)
226         {
227                 return TestDriver.RunTests (typeof (winx64structs), args);
228         }
229
230         public static int test_0_In_Args_Value_In_RCX ()
231         {
232                 int retCode;
233
234                 winx64_struct1 t_winx64_struct1 = new winx64_struct1 (123);
235
236                 if ((retCode = mono_test_Winx64_struct1_in (t_winx64_struct1)) != 0)
237                         return 100 + retCode;
238
239                 winx64_struct2 t_winx64_struct2 = new winx64_struct2 (4, 5);
240
241                 if ((retCode = mono_test_Winx64_struct2_in (t_winx64_struct2)) != 0)
242                         return 200 + retCode;
243
244                 winx64_struct3 t_winx64_struct3 = new winx64_struct3 (4, 5, 0x1234);
245
246                 if ((retCode = mono_test_Winx64_struct3_in (t_winx64_struct3)) != 0)
247                         return 300 + retCode;
248
249                 winx64_struct4 t_winx64_struct4 = new winx64_struct4 (4, 5, 0x1234, 0x87654321);
250
251                 if ((retCode = mono_test_Winx64_struct4_in (t_winx64_struct4)) != 0)
252                         return 400 + retCode;
253
254                 winx64_floatStruct t_winx64_floatStruct = new winx64_floatStruct (5.5F, 9.5F);
255
256                 if ((retCode = mono_test_Winx64_floatStruct (t_winx64_floatStruct)) != 0)
257                         return 500 + retCode;
258
259                 winx64_doubleStruct t_winx64_doubleStruct = new winx64_doubleStruct (5.5F);
260
261                 if ((retCode = mono_test_Winx64_doubleStruct (t_winx64_doubleStruct)) != 0)
262                         return 600 + retCode;
263
264                 return 0;
265         }
266
267         public static int test_0_In_Args_Values_In_Multiple_Registers ()
268         {
269                 int retCode; 
270                 
271                 winx64_struct1 t_winx64_struct1 = new winx64_struct1 (123);
272                 winx64_struct2 t_winx64_struct2 = new winx64_struct2 (4, 5);
273                 winx64_struct3 t_winx64_struct3 = new winx64_struct3 (4, 5, 0x1234);
274                 winx64_struct4 t_winx64_struct4 = new winx64_struct4 (4, 5, 0x1234, 0x87654321);
275                 
276                 if ((retCode = mono_test_Winx64_structs_in1 (t_winx64_struct1, t_winx64_struct2,
277                                                         t_winx64_struct3, t_winx64_struct4)) != 0)
278                         return 100 + retCode;
279
280                 
281                 return 0;
282         }
283
284         public static int test_0_Ret_In_RAX ()
285         {
286                 winx64_struct1 t_winx64_struct1 = mono_test_Winx64_struct1_ret ();
287                 if (t_winx64_struct1.a != 123)
288                         return 101;
289
290                 winx64_struct2 t_winx64_struct2 = mono_test_Winx64_struct2_ret ();
291                 if (t_winx64_struct2.a != 4)
292                         return 201;
293                 if (t_winx64_struct2.b != 5)
294                         return 202;
295
296                 winx64_struct3 t_winx64_struct3 = mono_test_Winx64_struct3_ret ();
297                 if (t_winx64_struct3.a != 4)
298                         return 301;
299                 if (t_winx64_struct3.b != 5)
300                         return 302;
301                 if (t_winx64_struct3.c != 0x1234)
302                         return 303;
303                 
304                 winx64_struct4 t_winx64_struct4 = mono_test_Winx64_struct4_ret ();
305                 if (t_winx64_struct4.a != 4)
306                         return 401;
307                 if (t_winx64_struct4.b != 5)
308                         return 402;
309                 if (t_winx64_struct4.c != 0x1234)
310                         return 403;
311                 if (t_winx64_struct4.d != 0x87654321)
312                         return 404;
313
314                 t_winx64_struct1 = mono_test_Winx64_struct1_ret_5_args (0x1, 0x0, 0x4, 0x10, 0x40);
315                 if (t_winx64_struct1.a != 0x55)
316                         return 501;
317
318                 return 0;
319         }
320
321         public static int test_0_Ret_In_Address ()
322         {
323                 winx64_struct5 t_winx64_struct5 = mono_test_Winx64_struct5_ret ();
324                 if (t_winx64_struct5.a != 4)
325                         return 101;
326                 if (t_winx64_struct5.b != 5)
327                         return 102;
328                 if (t_winx64_struct5.c != 6)
329                         return 103;
330
331                 t_winx64_struct5 = mono_test_Winx64_struct5_ret6_args (0x1, 0x4, 0x2, 0x8, 0x30);
332                 if (t_winx64_struct5.a != 0x5)
333                         return 201;
334                 if (t_winx64_struct5.b != 0xa)
335                         return 202;
336                 if (t_winx64_struct5.c != 0x30)
337                         return 203;
338
339                 return 0;
340         }
341
342         public static int test_0_In_Args_Values_In_Registers_and_Stack ()
343         {
344                 int retCode;
345
346                 winx64_struct1 var1 = new winx64_struct1 (1);
347                 winx64_struct1 var2 = new winx64_struct1 (2);
348                 winx64_struct1 var3 = new winx64_struct1 (3);
349                 winx64_struct1 var4 = new winx64_struct1 (4);
350                 winx64_struct1 var5 = new winx64_struct1 (5);
351
352                 if ((retCode = mono_test_Winx64_structs_in2 (var1, var2, var3, var4, var5)) != 0)
353                         return 100 + retCode;
354
355                 return 0;
356         }
357
358         public static int test_0_In_Args_Values_In_Registers_with_Stack_and_On_Stack ()
359         {
360                 int retCode;
361
362                 winx64_struct1 var1 = new winx64_struct1 (1);
363                 winx64_struct5 var2 = new winx64_struct5 (2, 3, 4);
364                 winx64_struct1 var3 = new winx64_struct1 (5);
365                 winx64_struct5 var4 = new winx64_struct5 (6, 7, 8);
366                 winx64_struct1 var5 = new winx64_struct1 (9);
367                 winx64_struct5 var6 = new winx64_struct5 (10, 11, 12);
368
369                 if ((retCode = mono_test_Winx64_structs_in3 (var1, var2, var3, var4, var5, var6)) != 0)
370                         return 100 + retCode;
371
372                 return 0;
373         }
374
375         public static int test_0_In_Args_Value_On_Stack_ADDR_In_RCX ()
376         {
377                 int retCode;
378
379                 winx64_struct5 t_winx64_struct5 = new winx64_struct5 (4, 5, 6);
380                 t_winx64_struct5.a = 4;
381                 t_winx64_struct5.b = 5;
382                 t_winx64_struct5.c = 6;
383
384                 if ((retCode = mono_test_Winx64_struct5_in (t_winx64_struct5)) != 0)
385                         return 100 + retCode;
386
387                 winx64_struct6 t_winx64_struct6 = new winx64_struct6 (new winx64_struct1 (4), 5, 6);            
388
389                 if ((retCode = mono_test_Winx64_struct6_in (t_winx64_struct6)) != 0)
390                         return 200 + retCode;
391
392                 return 0;
393         }
394
395         public static int test_0_In_Args_Value_In_RCX_Managed ()
396         {
397                 int retCode;
398
399                 managed_struct1_delegate s1Del = new managed_struct1_delegate (managed_struct1_test);
400
401                 if ((retCode = mono_test_managed_Winx64_struct1_in (s1Del)) != 0)
402                                 return 100 + retCode;
403
404                 return 0;
405         }
406
407         public static int test_0_In_Args_Value_On_Stack_ADDR_In_RCX_Managed ()
408         {
409                 int retCode;
410
411                 managed_struct5_delegate s1Del = new managed_struct5_delegate (managed_struct5_test);
412
413                 if ((retCode = mono_test_managed_Winx64_struct5_in (s1Del)) != 0)
414                         return 100 + retCode;
415
416                 return 0;
417         }
418
419         public static int test_0_In_Args_Values_In_Registers_with_Stack_and_On_Stack_Managed ()
420         {
421                 int retCode;
422
423                 managed_struct1_struct5_delegate s1Del = 
424                         new managed_struct1_struct5_delegate (managed_struct1_struct5_test);
425
426                 if ((retCode = mono_test_managed_Winx64_struct1_struct5_in (s1Del)) != 0)
427                         return 100 + retCode;
428
429                 return 0;
430         }
431
432         public static int test_0_Ret_In_RAX_managed ()
433         {
434                 int retCode;
435
436                 mono_test_Winx64_struct1_ret_delegate s1Del =
437                         new mono_test_Winx64_struct1_ret_delegate (mono_test_Winx64_struct1_ret_test);
438
439                 if ((retCode = mono_test_Winx64_struct1_ret_managed (s1Del)) != 0)
440                         return 100 + retCode;
441
442                 return 0;
443         }
444
445         public static int test_0_Ret_In_Address_managed ()
446         {
447                 int retCode;
448
449                 mono_test_Winx64_struct5_ret_delegate s1Del =
450                         new mono_test_Winx64_struct5_ret_delegate (mono_test_Winx64_struct5_ret_test);
451
452                 if ((retCode = mono_test_Winx64_struct5_ret_managed (s1Del)) != 0)
453                         return 100 + retCode;
454
455                 return 0;
456         }
457
458         [MonoPInvokeCallback (typeof (managed_struct1_delegate))]
459         public static int managed_struct1_test (winx64_struct1 var)
460         {
461                 if (var.a != 5)
462                         return 1;
463
464                 return 0;
465         }
466
467         [MonoPInvokeCallback (typeof (managed_struct5_delegate))]
468         public static int managed_struct5_test (winx64_struct5 var)
469         {
470                 if (var.a != 5)
471                         return 1;
472                 if (var.b != 0x10)
473                         return 2;
474                 if (var.c != 0x99)
475                         return 3;
476
477                 return 0;
478         }
479
480         [MonoPInvokeCallback (typeof (managed_struct1_struct5_delegate))]
481         public static int managed_struct1_struct5_test (winx64_struct1 var1, winx64_struct5 var2,
482                                                         winx64_struct1 var3, winx64_struct5 var4,
483                                                         winx64_struct1 var5, winx64_struct5 var6)
484         {
485                 if (var1.a != 1 || var3.a != 5)
486                         return 1;
487                 if (var2.a != 2 || var2.b != 3 || var2.c != 4 ||
488                     var4.a != 6 || var4.b != 7 || var4.c != 8)
489                         return 2;
490                 if (var5.a != 9)
491                         return 3;
492                 if (var6.a != 10 || var6.b != 11 || var6.c != 12)
493                         return 4;
494
495                 return 0;
496         }
497
498         [MonoPInvokeCallback (typeof (mono_test_Winx64_struct1_ret_delegate))]
499         public static winx64_struct1 mono_test_Winx64_struct1_ret_test ()
500         {
501                 return new winx64_struct1 (0x45);
502         }
503
504         [MonoPInvokeCallback (typeof (mono_test_Winx64_struct5_ret_delegate))]
505         public static winx64_struct5 mono_test_Winx64_struct5_ret_test ()
506         {
507                 return new winx64_struct5 (0x12, 0x34, 0x56);
508         }
509         
510 }