Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / pinvoke_ppci.cs
1 // pinvoke_ppci.cs - Test cases for passing structures to and and returning
2 //                   structures from functions.  This particular test is for
3 //                   structures consisting wholy of 4 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 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
14 //
15
16 using System;
17 using System.Runtime.InteropServices;
18
19
20 public class Test_int {
21
22         [DllImport ("libtest", EntryPoint="mono_return_int1")]
23         public static extern int1 mono_return_int1 (int1 s, int addend);
24         [StructLayout(LayoutKind.Sequential)]
25         public struct int1 {
26                 public int f1;
27         }
28         [DllImport ("libtest", EntryPoint="mono_return_int2")]
29         public static extern int2 mono_return_int2 (int2 s, int addend);
30         [StructLayout(LayoutKind.Sequential)]
31         public struct int2 {
32                 public int f1,f2;
33         }
34         [DllImport ("libtest", EntryPoint="mono_return_int3")]
35         public static extern int3 mono_return_int3 (int3 s, int addend);
36         [StructLayout(LayoutKind.Sequential)]
37         public struct int3 {
38                 public int f1,f2,f3;
39         }
40         [DllImport ("libtest", EntryPoint="mono_return_int4")]
41         public static extern int4 mono_return_int4 (int4 s, int addend);
42         [StructLayout(LayoutKind.Sequential)]
43         public struct int4 {
44                 public int f1,f2,f3,f4;
45         }
46         // This structure is 1 element too large to use the special return
47         //  rules.
48         [DllImport ("libtest", EntryPoint="mono_return_int5")]
49         public static extern int5 mono_return_int5 (int5 s, int addend);
50         [StructLayout(LayoutKind.Sequential)]
51         public struct int5 {
52                 public int f1,f2,f3,f4,f5;
53         }
54
55         // This structure has nested structures within it but they are
56         //  homogenous and thus should still use the special rules.
57         public struct int4_nested1 {
58                 public int f1;
59         };
60         public struct int4_nested2 {
61                 public int f4;
62         };
63         [DllImport ("libtest", EntryPoint="mono_return_int4_nested")]
64         public static extern int4_nested mono_return_int4_nested (int4_nested s, int addend);
65         [StructLayout(LayoutKind.Sequential)]
66         public struct int4_nested {
67                 public int4_nested1 nested1;
68                 public int f2,f3;
69                 public int4_nested2 nested2;
70         }
71
72         public static int Main (string[] args) {
73
74                 int1 s1;
75                 s1.f1 = 1;
76                 s1 = mono_return_int1(s1, 906);
77                 if (s1.f1 != 1+906) {
78                         Console.WriteLine("   int1 s1.f1: got {0} but expected {1}", s1.f1, 1+906);
79                         return 1;
80                 }
81
82                 int2 s2;
83                 s2.f1 = 1;
84                 s2.f2 = 2;
85                 s2 = mono_return_int2(s2, 906);
86                 if (s2.f1 != 1+906) {
87                         Console.WriteLine("   int2 s2.f1: got {0} but expected {1}", s2.f1, 1+906);
88                         return 1;
89                 }
90                 if (s2.f2 != 2+906) {
91                         Console.WriteLine("   int2 s2.f2: got {0} but expected {1}", s2.f2, 2+906);
92                         return 2;
93                 }
94
95                 int3 s3;
96                 s3.f1 = 1;
97                 s3.f2 = 2;
98                 s3.f3 = 3;
99                 s3 = mono_return_int3(s3, 906);
100                 if (s3.f1 != 1+906) {
101                         Console.WriteLine("   int3 s3.f1: got {0} but expected {1}", s3.f1, 1+906);
102                         return 1;
103                 }
104                 if (s3.f2 != 2+906) {
105                         Console.WriteLine("   int3 s3.f2: got {0} but expected {1}", s3.f2, 2+906);
106                         return 2;
107                 }
108                 if (s3.f3 != 3+906) {
109                         Console.WriteLine("   int3 s3.f3: got {0} but expected {1}", s3.f3, 3+906);
110                         return 3;
111                 }
112
113                 int4 s4;
114                 s4.f1 = 1;
115                 s4.f2 = 2;
116                 s4.f3 = 3;
117                 s4.f4 = 4;
118                 s4 = mono_return_int4(s4, 906);
119                 if (s4.f1 != 1+906) {
120                         Console.WriteLine("   int4 s4.f1: got {0} but expected {1}", s4.f1, 1+906);
121                         return 1;
122                 }
123                 if (s4.f2 != 2+906) {
124                         Console.WriteLine("   int4 s4.f2: got {0} but expected {1}", s4.f2, 2+906);
125                         return 2;
126                 }
127                 if (s4.f3 != 3+906) {
128                         Console.WriteLine("   int4 s4.f3: got {0} but expected {1}", s4.f3, 3+906);
129                         return 3;
130                 }
131                 if (s4.f4 != 4+906) {
132                         Console.WriteLine("   int4 s4.f4: got {0} but expected {1}", s4.f4, 4+906);
133                         return 4;
134                 }
135
136                 int5 s5;
137                 s5.f1 = 1;
138                 s5.f2 = 2;
139                 s5.f3 = 3;
140                 s5.f4 = 4;
141                 s5.f5 = 5;
142                 s5 = mono_return_int5(s5, 906);
143                 if (s5.f1 != 1+906) {
144                         Console.WriteLine("   int5 s5.f1: got {0} but expected {1}", s5.f1, 1+906);
145                         return 1;
146                 }
147                 if (s5.f2 != 2+906) {
148                         Console.WriteLine("   int5 s5.f2: got {0} but expected {1}", s5.f2, 2+906);
149                         return 2;
150                 }
151                 if (s5.f3 != 3+906) {
152                         Console.WriteLine("   int5 s5.f3: got {0} but expected {1}", s5.f3, 3+906);
153                         return 3;
154                 }
155                 if (s5.f4 != 4+906) {
156                         Console.WriteLine("   int5 s5.f4: got {0} but expected {1}", s5.f4, 4+906);
157                         return 4;
158                 }
159                 if (s5.f5 != 5+906) {
160                         Console.WriteLine("   int5 s5.f5: got {0} but expected {1}", s5.f5, 5+906);
161                         return 5;
162                 }
163
164
165                 int4_nested sn4;
166                 sn4.nested1.f1 = 1;
167                 sn4.f2 = 2;
168                 sn4.f3 = 3;
169                 sn4.nested2.f4 = 4;
170                 sn4 = mono_return_int4_nested(sn4, 906);
171                 if (sn4.nested1.f1 != 1+906) {
172                         Console.WriteLine("   int4_nested sn4.nested1.f1: got {0} but expected {1}", sn4.nested1.f1, 1+906);
173                         return 1;
174                 }
175                 if (sn4.f2 != 2+906) {
176                         Console.WriteLine("   int4_nested sn4.f2: got {0} but expected {1}", sn4.f2, 2+906);
177                         return 2;
178                 }
179                 if (sn4.f3 != 3+906) {
180                         Console.WriteLine("   int4_nested sn4.f3: got {0} but expected {1}", sn4.f3, 3+906);
181                         return 3;
182                 }
183                 if (sn4.nested2.f4 != 4+906) {
184                         Console.WriteLine("   int4_nested sn4.nested2.f4: got {0} but expected {1}", sn4.nested2.f4, 4+906);
185                         return 4;
186                 }
187
188                 return 0;
189         } // end Main
190 } // end class Test_int
191