Merge pull request #1218 from AndreyAkinshin/master
[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 // (C) {Copyright holder}
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 int 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 int 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 int 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 int 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 int 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                 int retval1 = mono_return_int1(s1, 906);
77                 if (retval1 != 2*906) {
78                         Console.WriteLine("   int1 retval1: got {0} but expected {1}", retval1, 2*906);
79                         return 1;
80                 }
81
82                 int2 s2;
83                 s2.f1 = 1;
84                 s2.f2 = 2;
85                 int retval2 = mono_return_int2(s2, 906);
86                 if (retval2 != 2*906) {
87                         Console.WriteLine("   int2 retval2: got {0} but expected {1}", retval2, 2*906);
88                         return 1;
89                 }
90
91                 int3 s3;
92                 s3.f1 = 1;
93                 s3.f2 = 2;
94                 s3.f3 = 3;
95                 int retval3 = mono_return_int3(s3, 906);
96                 if (retval3 != 2*906) {
97                         Console.WriteLine("   int3 retval3: got {0} but expected {1}", retval3, 2*906);
98                         return 1;
99                 }
100
101                 int4 s4;
102                 s4.f1 = 1;
103                 s4.f2 = 2;
104                 s4.f3 = 3;
105                 s4.f4 = 4;
106                 int retval4 = mono_return_int4(s4, 906);
107                 if (retval4 != 2*906) {
108                         Console.WriteLine("   int4 retval4: got {0} but expected {1}", retval4, 2*906);
109                         return 1;
110                 }
111
112                 int5 s5;
113                 s5.f1 = 1;
114                 s5.f2 = 2;
115                 s5.f3 = 3;
116                 s5.f4 = 4;
117                 s5.f5 = 5;
118                 int retval5 = mono_return_int5(s5, 906);
119                 if (retval5 != 2*906) {
120                         Console.WriteLine("   int5 retval5: got {0} but expected {1}", retval5, 2*906);
121                         return 1;
122                 }
123
124
125                 return 0;
126         } // end Main
127 } // end class Test_int
128
129
130
131
132