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