a57a909850083ded55c984f0db75b7650915a952
[mono.git] / mono / tests / pinvoke_ppcs.cs
1 // pinvoke_ppcs.cs - Test cases for passing structures to and and returning
2 //                   structures from functions.  This particular test is for
3 //                   structures consisting wholy of 2 byte fields.
4 //
5 //                   The Power ABI version 2 allows for special parameter
6 //                   passing and returning optimizations for certain
7 //                   structures of homogenous composition (like all ints).
8 //                   This set of tests checks all the possible combinations
9 //                   that use the special parm/return rules and one beyond.
10 //
11 // Bill Seurer (seurer@linux.vnet.ibm.com)
12 //
13 // (C) {Copyright holder}
14 //
15
16 using System;
17 using System.Runtime.InteropServices;
18
19
20 public class Test_short {
21
22         [DllImport ("libtest", EntryPoint="mono_return_short1")]
23         public static extern short1 mono_return_short1 (short1 s, int addend);
24         [StructLayout(LayoutKind.Sequential)]
25         public struct short1 {
26                 public short f1;
27         }
28         [DllImport ("libtest", EntryPoint="mono_return_short2")]
29         public static extern short2 mono_return_short2 (short2 s, int addend);
30         [StructLayout(LayoutKind.Sequential)]
31         public struct short2 {
32                 public short f1,f2;
33         }
34         [DllImport ("libtest", EntryPoint="mono_return_short3")]
35         public static extern short3 mono_return_short3 (short3 s, int addend);
36         [StructLayout(LayoutKind.Sequential)]
37         public struct short3 {
38                 public short f1,f2,f3;
39         }
40         [DllImport ("libtest", EntryPoint="mono_return_short4")]
41         public static extern short4 mono_return_short4 (short4 s, int addend);
42         [StructLayout(LayoutKind.Sequential)]
43         public struct short4 {
44                 public short f1,f2,f3,f4;
45         }
46         [DllImport ("libtest", EntryPoint="mono_return_short5")]
47         public static extern short5 mono_return_short5 (short5 s, int addend);
48         [StructLayout(LayoutKind.Sequential)]
49         public struct short5 {
50                 public short f1,f2,f3,f4,f5;
51         }
52         [DllImport ("libtest", EntryPoint="mono_return_short6")]
53         public static extern short6 mono_return_short6 (short6 s, int addend);
54         [StructLayout(LayoutKind.Sequential)]
55         public struct short6 {
56                 public short f1,f2,f3,f4,f5,f6;
57         }
58         [DllImport ("libtest", EntryPoint="mono_return_short7")]
59         public static extern short7 mono_return_short7 (short7 s, int addend);
60         [StructLayout(LayoutKind.Sequential)]
61         public struct short7 {
62                 public short f1,f2,f3,f4,f5,f6,f7;
63         }
64         [DllImport ("libtest", EntryPoint="mono_return_short8")]
65         public static extern short8 mono_return_short8 (short8 s, int addend);
66         [StructLayout(LayoutKind.Sequential)]
67         public struct short8 {
68                 public short f1,f2,f3,f4,f5,f6,f7,f8;
69         }
70         // This structure is 1 element too large to use the special return
71         //  rules.
72         [DllImport ("libtest", EntryPoint="mono_return_short9")]
73         public static extern short9 mono_return_short9 (short9 s, int addend);
74         [StructLayout(LayoutKind.Sequential)]
75         public struct short9 {
76                 public short f1,f2,f3,f4,f5,f6,f7,f8,f9;
77         }
78
79         // This structure has nested structures within it but they are
80         //  homogenous and thus should still use the special rules.
81         public struct short8_nested1 {
82                 public short f1;
83         };
84         public struct short8_nested2 {
85                 public short f8;
86         };
87         [DllImport ("libtest", EntryPoint="mono_return_short8_nested")]
88         public static extern short8_nested mono_return_short8_nested (short8_nested s, int addend);
89         [StructLayout(LayoutKind.Sequential)]
90         public struct short8_nested {
91                 public short8_nested1 nested1;
92                 public short f2,f3,f4,f5,f6,f7;
93                 public short8_nested2 nested2;
94         }
95
96         public static int Main (string[] args) {
97
98                 short1 s1;
99                 s1.f1 = 1;
100                 s1 = mono_return_short1(s1, 90);
101                 if (s1.f1 != 1+90) {
102                         Console.WriteLine("   short1 s1.f1: got {0} but expected {1}", s1.f1, 1+90);
103                         return 1;
104                 }
105
106                 short2 s2;
107                 s2.f1 = 1;
108                 s2.f2 = 2;
109                 s2 = mono_return_short2(s2, 90);
110                 if (s2.f1 != 1+90) {
111                         Console.WriteLine("   short2 s2.f1: got {0} but expected {1}", s2.f1, 1+90);
112                         return 1;
113                 }
114                 if (s2.f2 != 2+90) {
115                         Console.WriteLine("   short2 s2.f2: got {0} but expected {1}", s2.f2, 2+90);
116                         return 2;
117                 }
118
119                 short3 s3;
120                 s3.f1 = 1;
121                 s3.f2 = 2;
122                 s3.f3 = 3;
123                 s3 = mono_return_short3(s3, 90);
124                 if (s3.f1 != 1+90) {
125                         Console.WriteLine("   short3 s3.f1: got {0} but expected {1}", s3.f1, 1+90);
126                         return 1;
127                 }
128                 if (s3.f2 != 2+90) {
129                         Console.WriteLine("   short3 s3.f2: got {0} but expected {1}", s3.f2, 2+90);
130                         return 2;
131                 }
132                 if (s3.f3 != 3+90) {
133                         Console.WriteLine("   short3 s3.f3: got {0} but expected {1}", s3.f3, 3+90);
134                         return 3;
135                 }
136
137                 short4 s4;
138                 s4.f1 = 1;
139                 s4.f2 = 2;
140                 s4.f3 = 3;
141                 s4.f4 = 4;
142                 s4 = mono_return_short4(s4, 90);
143                 if (s4.f1 != 1+90) {
144                         Console.WriteLine("   short4 s4.f1: got {0} but expected {1}", s4.f1, 1+90);
145                         return 1;
146                 }
147                 if (s4.f2 != 2+90) {
148                         Console.WriteLine("   short4 s4.f2: got {0} but expected {1}", s4.f2, 2+90);
149                         return 2;
150                 }
151                 if (s4.f3 != 3+90) {
152                         Console.WriteLine("   short4 s4.f3: got {0} but expected {1}", s4.f3, 3+90);
153                         return 3;
154                 }
155                 if (s4.f4 != 4+90) {
156                         Console.WriteLine("   short4 s4.f4: got {0} but expected {1}", s4.f4, 4+90);
157                         return 4;
158                 }
159
160                 short5 s5;
161                 s5.f1 = 1;
162                 s5.f2 = 2;
163                 s5.f3 = 3;
164                 s5.f4 = 4;
165                 s5.f5 = 5;
166                 s5 = mono_return_short5(s5, 90);
167                 if (s5.f1 != 1+90) {
168                         Console.WriteLine("   short5 s5.f1: got {0} but expected {1}", s5.f1, 1+90);
169                         return 1;
170                 }
171                 if (s5.f2 != 2+90) {
172                         Console.WriteLine("   short5 s5.f2: got {0} but expected {1}", s5.f2, 2+90);
173                         return 2;
174                 }
175                 if (s5.f3 != 3+90) {
176                         Console.WriteLine("   short5 s5.f3: got {0} but expected {1}", s5.f3, 3+90);
177                         return 3;
178                 }
179                 if (s5.f4 != 4+90) {
180                         Console.WriteLine("   short5 s5.f4: got {0} but expected {1}", s5.f4, 4+90);
181                         return 4;
182                 }
183                 if (s5.f5 != 5+90) {
184                         Console.WriteLine("   short5 s5.f5: got {0} but expected {1}", s5.f5, 5+90);
185                         return 5;
186                 }
187
188                 short6 s6;
189                 s6.f1 = 1;
190                 s6.f2 = 2;
191                 s6.f3 = 3;
192                 s6.f4 = 4;
193                 s6.f5 = 5;
194                 s6.f6 = 6;
195                 s6 = mono_return_short6(s6, 90);
196                 if (s6.f1 != 1+90) {
197                         Console.WriteLine("   short6 s6.f1: got {0} but expected {1}", s6.f1, 1+90);
198                         return 1;
199                 }
200                 if (s6.f2 != 2+90) {
201                         Console.WriteLine("   short6 s6.f2: got {0} but expected {1}", s6.f2, 2+90);
202                         return 2;
203                 }
204                 if (s6.f3 != 3+90) {
205                         Console.WriteLine("   short6 s6.f3: got {0} but expected {1}", s6.f3, 3+90);
206                         return 3;
207                 }
208                 if (s6.f4 != 4+90) {
209                         Console.WriteLine("   short6 s6.f4: got {0} but expected {1}", s6.f4, 4+90);
210                         return 4;
211                 }
212                 if (s6.f5 != 5+90) {
213                         Console.WriteLine("   short6 s6.f5: got {0} but expected {1}", s6.f5, 5+90);
214                         return 5;
215                 }
216                 if (s6.f6 != 6+90) {
217                         Console.WriteLine("   short6 s6.f6: got {0} but expected {1}", s6.f6, 6+90);
218                         return 6;
219                 }
220
221                 short7 s7;
222                 s7.f1 = 1;
223                 s7.f2 = 2;
224                 s7.f3 = 3;
225                 s7.f4 = 4;
226                 s7.f5 = 5;
227                 s7.f6 = 6;
228                 s7.f7 = 7;
229                 s7 = mono_return_short7(s7, 90);
230                 if (s7.f1 != 1+90) {
231                         Console.WriteLine("   short7 s7.f1: got {0} but expected {1}", s7.f1, 1+90);
232                         return 1;
233                 }
234                 if (s7.f2 != 2+90) {
235                         Console.WriteLine("   short7 s7.f2: got {0} but expected {1}", s7.f2, 2+90);
236                         return 2;
237                 }
238                 if (s7.f3 != 3+90) {
239                         Console.WriteLine("   short7 s7.f3: got {0} but expected {1}", s7.f3, 3+90);
240                         return 3;
241                 }
242                 if (s7.f4 != 4+90) {
243                         Console.WriteLine("   short7 s7.f4: got {0} but expected {1}", s7.f4, 4+90);
244                         return 4;
245                 }
246                 if (s7.f5 != 5+90) {
247                         Console.WriteLine("   short7 s7.f5: got {0} but expected {1}", s7.f5, 5+90);
248                         return 5;
249                 }
250                 if (s7.f6 != 6+90) {
251                         Console.WriteLine("   short7 s7.f6: got {0} but expected {1}", s7.f6, 6+90);
252                         return 6;
253                 }
254                 if (s7.f7 != 7+90) {
255                         Console.WriteLine("   short7 s7.f7: got {0} but expected {1}", s7.f7, 7+90);
256                         return 7;
257                 }
258
259                 short8 s8;
260                 s8.f1 = 1;
261                 s8.f2 = 2;
262                 s8.f3 = 3;
263                 s8.f4 = 4;
264                 s8.f5 = 5;
265                 s8.f6 = 6;
266                 s8.f7 = 7;
267                 s8.f8 = 8;
268                 s8 = mono_return_short8(s8, 90);
269                 if (s8.f1 != 1+90) {
270                         Console.WriteLine("   short8 s8.f1: got {0} but expected {1}", s8.f1, 1+90);
271                         return 1;
272                 }
273                 if (s8.f2 != 2+90) {
274                         Console.WriteLine("   short8 s8.f2: got {0} but expected {1}", s8.f2, 2+90);
275                         return 2;
276                 }
277                 if (s8.f3 != 3+90) {
278                         Console.WriteLine("   short8 s8.f3: got {0} but expected {1}", s8.f3, 3+90);
279                         return 3;
280                 }
281                 if (s8.f4 != 4+90) {
282                         Console.WriteLine("   short8 s8.f4: got {0} but expected {1}", s8.f4, 4+90);
283                         return 4;
284                 }
285                 if (s8.f5 != 5+90) {
286                         Console.WriteLine("   short8 s8.f5: got {0} but expected {1}", s8.f5, 5+90);
287                         return 5;
288                 }
289                 if (s8.f6 != 6+90) {
290                         Console.WriteLine("   short8 s8.f6: got {0} but expected {1}", s8.f6, 6+90);
291                         return 6;
292                 }
293                 if (s8.f7 != 7+90) {
294                         Console.WriteLine("   short8 s8.f7: got {0} but expected {1}", s8.f7, 7+90);
295                         return 7;
296                 }
297                 if (s8.f8 != 8+90) {
298                         Console.WriteLine("   short8 s8.f8: got {0} but expected {1}", s8.f8, 8+90);
299                         return 8;
300                 }
301
302                 short9 s9;
303                 s9.f1 = 1;
304                 s9.f2 = 2;
305                 s9.f3 = 3;
306                 s9.f4 = 4;
307                 s9.f5 = 5;
308                 s9.f6 = 6;
309                 s9.f7 = 7;
310                 s9.f8 = 8;
311                 s9.f9 = 9;
312                 s9 = mono_return_short9(s9, 90);
313                 if (s9.f1 != 1+90) {
314                         Console.WriteLine("   short9 s9.f1: got {0} but expected {1}", s9.f1, 1+90);
315                         return 1;
316                 }
317                 if (s9.f2 != 2+90) {
318                         Console.WriteLine("   short9 s9.f2: got {0} but expected {1}", s9.f2, 2+90);
319                         return 2;
320                 }
321                 if (s9.f3 != 3+90) {
322                         Console.WriteLine("   short9 s9.f3: got {0} but expected {1}", s9.f3, 3+90);
323                         return 3;
324                 }
325                 if (s9.f4 != 4+90) {
326                         Console.WriteLine("   short9 s9.f4: got {0} but expected {1}", s9.f4, 4+90);
327                         return 4;
328                 }
329                 if (s9.f5 != 5+90) {
330                         Console.WriteLine("   short9 s9.f5: got {0} but expected {1}", s9.f5, 5+90);
331                         return 5;
332                 }
333                 if (s9.f6 != 6+90) {
334                         Console.WriteLine("   short9 s9.f6: got {0} but expected {1}", s9.f6, 6+90);
335                         return 6;
336                 }
337                 if (s9.f7 != 7+90) {
338                         Console.WriteLine("   short9 s9.f7: got {0} but expected {1}", s9.f7, 7+90);
339                         return 7;
340                 }
341                 if (s9.f8 != 8+90) {
342                         Console.WriteLine("   short9 s9.f8: got {0} but expected {1}", s9.f8, 8+90);
343                         return 8;
344                 }
345                 if (s9.f9 != 9+90) {
346                         Console.WriteLine("   short9 s9.f9: got {0} but expected {1}", s9.f9, 9+90);
347                         return 9;
348                 }
349
350
351                 short8_nested sn8;
352                 sn8.nested1.f1 = 1;
353                 sn8.f2 = 2;
354                 sn8.f3 = 3;
355                 sn8.f4 = 4;
356                 sn8.f5 = 5;
357                 sn8.f6 = 6;
358                 sn8.f7 = 7;
359                 sn8.nested2.f8 = 8;
360                 sn8 = mono_return_short8_nested(sn8, 90);
361                 if (sn8.nested1.f1 != 1+90) {
362                         Console.WriteLine("   short8_nested sn8.nested1.f1: got {0} but expected {1}", sn8.nested1.f1, 1+90);
363                         return 1;
364                 }
365                 if (sn8.f2 != 2+90) {
366                         Console.WriteLine("   short8_nested sn8.f2: got {0} but expected {1}", sn8.f2, 2+90);
367                         return 2;
368                 }
369                 if (sn8.f3 != 3+90) {
370                         Console.WriteLine("   short8_nested sn8.f3: got {0} but expected {1}", sn8.f3, 3+90);
371                         return 3;
372                 }
373                 if (sn8.f4 != 4+90) {
374                         Console.WriteLine("   short8_nested sn8.f4: got {0} but expected {1}", sn8.f4, 4+90);
375                         return 4;
376                 }
377                 if (sn8.f5 != 5+90) {
378                         Console.WriteLine("   short8_nested sn8.f5: got {0} but expected {1}", sn8.f5, 5+90);
379                         return 5;
380                 }
381                 if (sn8.f6 != 6+90) {
382                         Console.WriteLine("   short8_nested sn8.f6: got {0} but expected {1}", sn8.f6, 6+90);
383                         return 6;
384                 }
385                 if (sn8.f7 != 7+90) {
386                         Console.WriteLine("   short8_nested sn8.f7: got {0} but expected {1}", sn8.f7, 7+90);
387                         return 7;
388                 }
389                 if (sn8.nested2.f8 != 8+90) {
390                         Console.WriteLine("   short8_nested sn8.nested2.f8: got {0} but expected {1}", sn8.nested2.f8, 8+90);
391                         return 8;
392                 }
393
394                 return 0;
395         } // end Main
396 } // end class Test_short
397