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