2008-07-07 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         private static bool enableBroken = false;
194         
195         static int Main (string[] args)
196         {
197                 return TestDriver.RunTests (typeof (winx64structs), args);
198         }
199
200         public static int test_0_In_Args_Value_In_RCX ()
201         {
202                 int retCode;
203
204                 winx64_struct1 t_winx64_struct1 = new winx64_struct1 (123);
205
206                 if ((retCode = mono_test_Winx64_struct1_in (t_winx64_struct1)) != 0)
207                         return 100 + retCode;
208
209                 winx64_struct2 t_winx64_struct2 = new winx64_struct2 (4, 5);
210
211                 if ((retCode = mono_test_Winx64_struct2_in (t_winx64_struct2)) != 0)
212                         return 200 + retCode;
213
214                 winx64_struct3 t_winx64_struct3 = new winx64_struct3 (4, 5, 0x1234);
215
216                 if ((retCode = mono_test_Winx64_struct3_in (t_winx64_struct3)) != 0)
217                         return 300 + retCode;
218
219                 winx64_struct4 t_winx64_struct4 = new winx64_struct4 (4, 5, 0x1234, 0x87654321);
220
221                 if ((retCode = mono_test_Winx64_struct4_in (t_winx64_struct4)) != 0)
222                         return 400 + retCode;
223
224                 winx64_floatStruct t_winx64_floatStruct = new winx64_floatStruct (5.5F, 9.5F);
225
226                 if ((retCode = mono_test_Winx64_floatStruct (t_winx64_floatStruct)) != 0)
227                         return 500 + retCode;
228
229                 winx64_doubleStruct t_winx64_doubleStruct = new winx64_doubleStruct (5.5F);
230
231                 if ((retCode = mono_test_Winx64_doubleStruct (t_winx64_doubleStruct)) != 0)
232                         return 600 + retCode;
233
234                 return 0;
235         }
236
237         public static int test_0_In_Args_Values_In_Multiple_Registers ()
238         {
239                 int retCode; 
240                 
241                 winx64_struct1 t_winx64_struct1 = new winx64_struct1 (123);
242                 winx64_struct2 t_winx64_struct2 = new winx64_struct2 (4, 5);
243                 winx64_struct3 t_winx64_struct3 = new winx64_struct3 (4, 5, 0x1234);
244                 winx64_struct4 t_winx64_struct4 = new winx64_struct4 (4, 5, 0x1234, 0x87654321);
245                 
246                 if ((retCode = mono_test_Winx64_structs_in1 (t_winx64_struct1, t_winx64_struct2,
247                                                         t_winx64_struct3, t_winx64_struct4)) != 0)
248                         return 100 + retCode;
249
250                 
251                 return 0;
252         }
253
254         public static int test_0_Ret_In_RAX ()
255         {
256                 winx64_struct1 t_winx64_struct1 = mono_test_Winx64_struct1_ret ();
257                 if (t_winx64_struct1.a != 123)
258                         return 101;
259
260                 winx64_struct2 t_winx64_struct2 = mono_test_Winx64_struct2_ret ();
261                 if (t_winx64_struct2.a != 4)
262                         return 201;
263                 if (t_winx64_struct2.b != 5)
264                         return 202;
265
266                 winx64_struct3 t_winx64_struct3 = mono_test_Winx64_struct3_ret ();
267                 if (t_winx64_struct3.a != 4)
268                         return 301;
269                 if (t_winx64_struct3.b != 5)
270                         return 302;
271                 if (t_winx64_struct3.c != 0x1234)
272                         return 303;
273                 
274                 winx64_struct4 t_winx64_struct4 = mono_test_Winx64_struct4_ret ();
275                 if (t_winx64_struct4.a != 4)
276                         return 401;
277                 if (t_winx64_struct4.b != 5)
278                         return 402;
279                 if (t_winx64_struct4.c != 0x1234)
280                         return 403;
281                 if (t_winx64_struct4.d != 0x87654321)
282                         return 404;
283
284                 t_winx64_struct1 = mono_test_Winx64_struct1_ret_5_args (0x1, 0x0, 0x4, 0x10, 0x40);
285                 if (t_winx64_struct1.a != 0x55)
286                         return 501;
287
288                 return 0;
289         }
290
291         public static int test_0_Ret_In_Address ()
292         {
293                 winx64_struct5 t_winx64_struct5 = mono_test_Winx64_struct5_ret ();
294                 if (t_winx64_struct5.a != 4)
295                         return 101;
296                 if (t_winx64_struct5.b != 5)
297                         return 102;
298                 if (t_winx64_struct5.c != 6)
299                         return 103;
300
301                 t_winx64_struct5 = mono_test_Winx64_struct5_ret6_args (0x1, 0x4, 0x2, 0x8, 0x30);
302                 if (t_winx64_struct5.a != 0x5)
303                         return 201;
304                 if (t_winx64_struct5.b != 0xa)
305                         return 202;
306                 if (t_winx64_struct5.c != 0x30)
307                         return 203;
308
309                 return 0;
310         }
311
312         public static int test_0_In_Args_Values_In_Registers_and_Stack ()
313         {
314                 int retCode;
315
316                 winx64_struct1 var1 = new winx64_struct1 (1);
317                 winx64_struct1 var2 = new winx64_struct1 (2);
318                 winx64_struct1 var3 = new winx64_struct1 (3);
319                 winx64_struct1 var4 = new winx64_struct1 (4);
320                 winx64_struct1 var5 = new winx64_struct1 (5);
321
322                 if ((retCode = mono_test_Winx64_structs_in2 (var1, var2, var3, var4, var5)) != 0)
323                         return 100 + retCode;
324
325                 return 0;
326         }
327
328         public static int test_0_In_Args_Values_In_Registers_with_Stack_and_On_Stack ()
329         {
330                 int retCode;
331
332                 winx64_struct1 var1 = new winx64_struct1 (1);
333                 winx64_struct5 var2 = new winx64_struct5 (2, 3, 4);
334                 winx64_struct1 var3 = new winx64_struct1 (5);
335                 winx64_struct5 var4 = new winx64_struct5 (6, 7, 8);
336                 winx64_struct1 var5 = new winx64_struct1 (9);
337                 winx64_struct5 var6 = new winx64_struct5 (10, 11, 12);
338
339                 if ((retCode = mono_test_Winx64_structs_in3 (var1, var2, var3, var4, var5, var6)) != 0)
340                         return 100 + retCode;
341
342                 return 0;
343         }
344
345         public static int test_0_In_Args_Value_On_Stack_ADDR_In_RCX ()
346         {
347                 int retCode;
348
349                 winx64_struct5 t_winx64_struct5 = new winx64_struct5 (4, 5, 6);
350                 t_winx64_struct5.a = 4;
351                 t_winx64_struct5.b = 5;
352                 t_winx64_struct5.c = 6;
353
354                 if ((retCode = mono_test_Winx64_struct5_in (t_winx64_struct5)) != 0)
355                         return 100 + retCode;
356
357                 winx64_struct6 t_winx64_struct6 = new winx64_struct6 (new winx64_struct1 (4), 5, 6);            
358
359                 if ((retCode = mono_test_Winx64_struct6_in (t_winx64_struct6)) != 0)
360                         return 200 + retCode;
361
362                 return 0;
363         }
364
365         public static int test_0_In_Args_Value_In_RCX_Managed ()
366         {
367                 int retCode;
368
369                 managed_struct1_delegate s1Del = new managed_struct1_delegate (managed_struct1_test);
370
371                 if ((retCode = mono_test_managed_Winx64_struct1_in (s1Del)) != 0)
372                                 return 100 + retCode;
373
374                 return 0;
375         }
376
377         public static int managed_struct1_test (winx64_struct1 var)
378         {
379                 if (var.a != 5)
380                         return 1;
381
382                 return 0;
383         }
384 }