[2019-12] [threads] Add back mono_threads_attach_tools_thread as a public API (#18074)
[mono-project.git] / mono / tests / pinvoke_ppcd.cs
blob602706345e91e17d3e953e19f9ca8a3fa1ff84da
1 // pinvoke_ppcd.cs - Test cases for passing structures to and and returning
2 // structures from functions. This particular test is for
3 // structures consisting wholy of 8 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.
11 // Bill Seurer (seurer@linux.vnet.ibm.com)
13 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
16 using System;
17 using System.Runtime.InteropServices;
20 public class Test_double {
22 [DllImport ("libtest", EntryPoint="mono_return_double1")]
23 public static extern double1 mono_return_double1 (double1 s, int addend);
24 [StructLayout(LayoutKind.Sequential)]
25 public struct double1 {
26 public double f1;
28 [DllImport ("libtest", EntryPoint="mono_return_double2")]
29 public static extern double2 mono_return_double2 (double2 s, int addend);
30 [StructLayout(LayoutKind.Sequential)]
31 public struct double2 {
32 public double f1,f2;
34 // This structure is 1 element too large to use the special return
35 // rules.
36 [DllImport ("libtest", EntryPoint="mono_return_double3")]
37 public static extern double3 mono_return_double3 (double3 s, int addend);
38 [StructLayout(LayoutKind.Sequential)]
39 public struct double3 {
40 public double f1,f2,f3;
42 [DllImport ("libtest", EntryPoint="mono_return_double4")]
43 public static extern double4 mono_return_double4 (double4 s, int addend);
44 [StructLayout(LayoutKind.Sequential)]
45 public struct double4 {
46 public double f1,f2,f3,f4;
48 [DllImport ("libtest", EntryPoint="mono_return_double5")]
49 public static extern double5 mono_return_double5 (double5 s, int addend);
50 [StructLayout(LayoutKind.Sequential)]
51 public struct double5 {
52 public double f1,f2,f3,f4,f5;
54 [DllImport ("libtest", EntryPoint="mono_return_double6")]
55 public static extern double6 mono_return_double6 (double6 s, int addend);
56 [StructLayout(LayoutKind.Sequential)]
57 public struct double6 {
58 public double f1,f2,f3,f4,f5,f6;
60 [DllImport ("libtest", EntryPoint="mono_return_double7")]
61 public static extern double7 mono_return_double7 (double7 s, int addend);
62 [StructLayout(LayoutKind.Sequential)]
63 public struct double7 {
64 public double f1,f2,f3,f4,f5,f6,f7;
66 [DllImport ("libtest", EntryPoint="mono_return_double8")]
67 public static extern double8 mono_return_double8 (double8 s, int addend);
68 [StructLayout(LayoutKind.Sequential)]
69 public struct double8 {
70 public double f1,f2,f3,f4,f5,f6,f7,f8;
72 // This structure is 1 element too large to use the special parameter
73 // passing rules.
74 [DllImport ("libtest", EntryPoint="mono_return_double9")]
75 public static extern double9 mono_return_double9 (double9 s, int addend);
76 [StructLayout(LayoutKind.Sequential)]
77 public struct double9 {
78 public double f1,f2,f3,f4,f5,f6,f7,f8,f9;
81 // This structure has nested structures within it but they are
82 // homogenous and thus should still use the special rules.
83 public struct double2_nested1 {
84 public double f1;
86 public struct double2_nested2 {
87 public double f2;
89 [DllImport ("libtest", EntryPoint="mono_return_double2_nested")]
90 public static extern double2_nested mono_return_double2_nested (double2_nested s, int addend);
91 [StructLayout(LayoutKind.Sequential)]
92 public struct double2_nested {
93 public double2_nested1 nested1;
94 public double2_nested2 nested2;
97 [DllImport ("libtest", EntryPoint="mono_return_double_array4")]
98 public static extern double_array4 mono_return_double_array4 (double_array4 s, int addend);
99 [StructLayout(LayoutKind.Sequential)]
100 public unsafe struct double_array4 {
101 public fixed double f1[4];
105 public static int Main (string[] args) {
106 double1 s1;
107 s1.f1 = 1;
108 s1 = mono_return_double1(s1, 9);
109 if (s1.f1 != 1+9) {
110 Console.WriteLine(" double1 s1.f1: got {0} but expected {1}", s1.f1, 1+9);
111 return 1;
114 double2 s2;
115 s2.f1 = 1;
116 s2.f2 = 2;
117 s2 = mono_return_double2(s2, 9);
118 if (s2.f1 != 1+9) {
119 Console.WriteLine(" double2 s2.f1: got {0} but expected {1}", s2.f1, 1+9);
120 return 1;
122 if (s2.f2 != 2+9) {
123 Console.WriteLine(" double2 s2.f2: got {0} but expected {1}", s2.f2, 2+9);
124 return 2;
127 double3 s3;
128 s3.f1 = 1;
129 s3.f2 = 2;
130 s3.f3 = 3;
131 s3 = mono_return_double3(s3, 9);
132 if (s3.f1 != 1+9) {
133 Console.WriteLine(" double3 s3.f1: got {0} but expected {1}", s3.f1, 1+9);
134 return 1;
136 if (s3.f2 != 2+9) {
137 Console.WriteLine(" double3 s3.f2: got {0} but expected {1}", s3.f2, 2+9);
138 return 2;
140 if (s3.f3 != 3+9) {
141 Console.WriteLine(" double3 s3.f3: got {0} but expected {1}", s3.f3, 3+9);
142 return 3;
145 double4 s4;
146 s4.f1 = 1;
147 s4.f2 = 2;
148 s4.f3 = 3;
149 s4.f4 = 4;
150 s4 = mono_return_double4(s4, 9);
151 if (s4.f1 != 1+9) {
152 Console.WriteLine(" double4 s4.f1: got {0} but expected {1}", s4.f1, 1+9);
153 return 1;
155 if (s4.f2 != 2+9) {
156 Console.WriteLine(" double4 s4.f2: got {0} but expected {1}", s4.f2, 2+9);
157 return 2;
159 if (s4.f3 != 3+9) {
160 Console.WriteLine(" double4 s4.f3: got {0} but expected {1}", s4.f3, 3+9);
161 return 3;
163 if (s4.f4 != 4+9) {
164 Console.WriteLine(" double4 s4.f4: got {0} but expected {1}", s4.f4, 4+9);
165 return 4;
168 double5 s5;
169 s5.f1 = 1;
170 s5.f2 = 2;
171 s5.f3 = 3;
172 s5.f4 = 4;
173 s5.f5 = 5;
174 s5 = mono_return_double5(s5, 9);
175 if (s5.f1 != 1+9) {
176 Console.WriteLine(" double5 s5.f1: got {0} but expected {1}", s5.f1, 1+9);
177 return 1;
179 if (s5.f2 != 2+9) {
180 Console.WriteLine(" double5 s5.f2: got {0} but expected {1}", s5.f2, 2+9);
181 return 2;
183 if (s5.f3 != 3+9) {
184 Console.WriteLine(" double5 s5.f3: got {0} but expected {1}", s5.f3, 3+9);
185 return 3;
187 if (s5.f4 != 4+9) {
188 Console.WriteLine(" double5 s5.f4: got {0} but expected {1}", s5.f4, 4+9);
189 return 4;
191 if (s5.f5 != 5+9) {
192 Console.WriteLine(" double5 s5.f5: got {0} but expected {1}", s5.f5, 5+9);
193 return 5;
196 double6 s6;
197 s6.f1 = 1;
198 s6.f2 = 2;
199 s6.f3 = 3;
200 s6.f4 = 4;
201 s6.f5 = 5;
202 s6.f6 = 6;
203 s6 = mono_return_double6(s6, 9);
204 if (s6.f1 != 1+9) {
205 Console.WriteLine(" double6 s6.f1: got {0} but expected {1}", s6.f1, 1+9);
206 return 1;
208 if (s6.f2 != 2+9) {
209 Console.WriteLine(" double6 s6.f2: got {0} but expected {1}", s6.f2, 2+9);
210 return 2;
212 if (s6.f3 != 3+9) {
213 Console.WriteLine(" double6 s6.f3: got {0} but expected {1}", s6.f3, 3+9);
214 return 3;
216 if (s6.f4 != 4+9) {
217 Console.WriteLine(" double6 s6.f4: got {0} but expected {1}", s6.f4, 4+9);
218 return 4;
220 if (s6.f5 != 5+9) {
221 Console.WriteLine(" double6 s6.f5: got {0} but expected {1}", s6.f5, 5+9);
222 return 5;
224 if (s6.f6 != 6+9) {
225 Console.WriteLine(" double6 s6.f6: got {0} but expected {1}", s6.f6, 6+9);
226 return 6;
229 double7 s7;
230 s7.f1 = 1;
231 s7.f2 = 2;
232 s7.f3 = 3;
233 s7.f4 = 4;
234 s7.f5 = 5;
235 s7.f6 = 6;
236 s7.f7 = 7;
237 s7 = mono_return_double7(s7, 9);
238 if (s7.f1 != 1+9) {
239 Console.WriteLine(" double7 s7.f1: got {0} but expected {1}", s7.f1, 1+9);
240 return 1;
242 if (s7.f2 != 2+9) {
243 Console.WriteLine(" double7 s7.f2: got {0} but expected {1}", s7.f2, 2+9);
244 return 2;
246 if (s7.f3 != 3+9) {
247 Console.WriteLine(" double7 s7.f3: got {0} but expected {1}", s7.f3, 3+9);
248 return 3;
250 if (s7.f4 != 4+9) {
251 Console.WriteLine(" double7 s7.f4: got {0} but expected {1}", s7.f4, 4+9);
252 return 4;
254 if (s7.f5 != 5+9) {
255 Console.WriteLine(" double7 s7.f5: got {0} but expected {1}", s7.f5, 5+9);
256 return 5;
258 if (s7.f6 != 6+9) {
259 Console.WriteLine(" double7 s7.f6: got {0} but expected {1}", s7.f6, 6+9);
260 return 6;
262 if (s7.f7 != 7+9) {
263 Console.WriteLine(" double7 s7.f7: got {0} but expected {1}", s7.f7, 7+9);
264 return 7;
267 double8 s8;
268 s8.f1 = 1;
269 s8.f2 = 2;
270 s8.f3 = 3;
271 s8.f4 = 4;
272 s8.f5 = 5;
273 s8.f6 = 6;
274 s8.f7 = 7;
275 s8.f8 = 8;
276 s8 = mono_return_double8(s8, 9);
277 if (s8.f1 != 1+9) {
278 Console.WriteLine(" double8 s8.f1: got {0} but expected {1}", s8.f1, 1+9);
279 return 1;
281 if (s8.f2 != 2+9) {
282 Console.WriteLine(" double8 s8.f2: got {0} but expected {1}", s8.f2, 2+9);
283 return 2;
285 if (s8.f3 != 3+9) {
286 Console.WriteLine(" double8 s8.f3: got {0} but expected {1}", s8.f3, 3+9);
287 return 3;
289 if (s8.f4 != 4+9) {
290 Console.WriteLine(" double8 s8.f4: got {0} but expected {1}", s8.f4, 4+9);
291 return 4;
293 if (s8.f5 != 5+9) {
294 Console.WriteLine(" double8 s8.f5: got {0} but expected {1}", s8.f5, 5+9);
295 return 5;
297 if (s8.f6 != 6+9) {
298 Console.WriteLine(" double8 s8.f6: got {0} but expected {1}", s8.f6, 6+9);
299 return 6;
301 if (s8.f7 != 7+9) {
302 Console.WriteLine(" double8 s8.f7: got {0} but expected {1}", s8.f7, 7+9);
303 return 7;
305 if (s8.f8 != 8+9) {
306 Console.WriteLine(" double8 s8.f8: got {0} but expected {1}", s8.f8, 8+9);
307 return 8;
310 double9 s9;
311 s9.f1 = 1;
312 s9.f2 = 2;
313 s9.f3 = 3;
314 s9.f4 = 4;
315 s9.f5 = 5;
316 s9.f6 = 6;
317 s9.f7 = 7;
318 s9.f8 = 8;
319 s9.f9 = 9;
320 s9 = mono_return_double9(s9, 9);
321 if (s9.f1 != 1+9) {
322 Console.WriteLine(" double9 s9.f1: got {0} but expected {1}", s9.f1, 1+9);
323 return 1;
325 if (s9.f2 != 2+9) {
326 Console.WriteLine(" double9 s9.f2: got {0} but expected {1}", s9.f2, 2+9);
327 return 2;
329 if (s9.f3 != 3+9) {
330 Console.WriteLine(" double9 s9.f3: got {0} but expected {1}", s9.f3, 3+9);
331 return 3;
333 if (s9.f4 != 4+9) {
334 Console.WriteLine(" double9 s9.f4: got {0} but expected {1}", s9.f4, 4+9);
335 return 4;
337 if (s9.f5 != 5+9) {
338 Console.WriteLine(" double9 s9.f5: got {0} but expected {1}", s9.f5, 5+9);
339 return 5;
341 if (s9.f6 != 6+9) {
342 Console.WriteLine(" double9 s9.f6: got {0} but expected {1}", s9.f6, 6+9);
343 return 6;
345 if (s9.f7 != 7+9) {
346 Console.WriteLine(" double9 s9.f7: got {0} but expected {1}", s9.f7, 7+9);
347 return 7;
349 if (s9.f8 != 8+9) {
350 Console.WriteLine(" double9 s9.f8: got {0} but expected {1}", s9.f8, 8+9);
351 return 8;
353 if (s9.f9 != 9+9) {
354 Console.WriteLine(" double9 s9.f9: got {0} but expected {1}", s9.f9, 9+9);
355 return 9;
359 double2_nested sn2;
360 sn2.nested1.f1 = 1;
361 sn2.nested2.f2 = 2;
362 sn2 = mono_return_double2_nested(sn2, 9);
363 if (sn2.nested1.f1 != 1+9) {
364 Console.WriteLine(" double2_nested sn2.nested1.f1: got {0} but expected {1}", sn2.nested1.f1, 1+9);
365 return 1;
367 if (sn2.nested2.f2 != 2+9) {
368 Console.WriteLine(" double2_nested sn2.nested2.f2: got {0} but expected {1}", sn2.nested2.f2, 2+9);
369 return 2;
373 // NOTE: this test does not work properly because mini_type_is_hfa in mini-codegen.c does not handle arrays.
374 // Uncomment this when mini_type_is_hfa is fixed.
375 unsafe {
376 double_array4 sa4;
377 sa4.f1[0] = 1;
378 sa4.f1[1] = 2;
379 sa4 = mono_return_double_array4(sa4, 9);
380 if (sa4.f1[0] != 1+9) {
381 Console.WriteLine(" double_array4 sa4.f1[0]: got {0} but expected {1}", sa4.f1[0], 1+9);
382 return 1;
384 if (sa4.f1[1] != 2+9) {
385 Console.WriteLine(" double_array4 sa4.f1[1]: got {0} but expected {1}", sa4.f1[1], 2+9);
386 return 2;
388 if (sa4.f1[2] != 3+9) {
389 Console.WriteLine(" double_array4 sa4.f1[2]: got {0} but expected {1}", sa4.f1[2], 3+9);
390 return 3;
392 if (sa4.f1[3] != 4+9) {
393 Console.WriteLine(" double_array4 sa4.f1[3]: got {0} but expected {1}", sa4.f1[3], 4+9);
394 return 4;
399 return 0;
400 } // end Main
401 } // end class Test_double