New test.
[mono.git] / mono / tests / pinvoke11.cs
1 using System;
2 using System.Runtime.InteropServices;
3
4 [StructLayout(LayoutKind.Sequential)]
5 public struct ss
6 {
7         public int i1;
8 }
9
10 [StructLayout(LayoutKind.Explicit)]
11 public struct su
12 {
13         [FieldOffset(0)] public int i1;
14         [FieldOffset(0)] public int i2;
15 }
16
17 [StructLayout(LayoutKind.Sequential)]
18 public struct cs
19 {
20         public bool b1;
21         public int i1;
22         public su u1;
23 }
24
25 /* various small structs for testing struct-by-value where they are handled specially 
26    on some platforms.
27 */
28 [StructLayout(LayoutKind.Sequential)]
29 public struct sc1
30 {
31         public byte c0;
32 }
33
34 [StructLayout(LayoutKind.Sequential)]
35 public struct sc3
36 {
37         public byte c0;
38         public byte c1;
39         public byte c2;
40 }
41
42 [StructLayout(LayoutKind.Sequential)]
43 public struct sc5
44 {
45         public byte c0;
46         public byte c1;
47         public byte c2;
48         public byte c3;
49         public byte c4;
50 }
51
52 public class Test
53 {
54         [DllImport ("libtest")]
55         public static extern int mono_union_test_1 (cs a);
56
57         [DllImport ("libtest")]
58         public static extern int mono_return_int (int a);
59
60         [DllImport ("libtest", EntryPoint="mono_return_int_ss")]
61         public static extern int mono_return_int_ss (ss a);
62
63         [DllImport ("libtest", EntryPoint="mono_return_ss")]
64         public static extern ss mono_return_ss (ss a);
65
66         [DllImport ("libtest", EntryPoint="mono_return_sc1")]
67         public static extern sc1 mono_return_sc1 (sc1 a);
68
69         [DllImport ("libtest", EntryPoint="mono_return_sc3")]
70         public static extern sc3 mono_return_sc3 (sc3 a);
71
72         [DllImport ("libtest", EntryPoint="mono_return_sc5")]
73         public static extern sc5 mono_return_sc5 (sc5 a);
74
75         [DllImport ("libtest", EntryPoint="mono_return_int_su")]
76         public static extern int mono_return_int_su (su a);
77
78         static int Main()
79         {
80                 if (mono_return_int (5) != 5)
81                         return 1;
82
83                 ss s1;
84                 s1.i1 = 4;
85                 if (mono_return_int_ss (s1) != 4)
86                         return 2;
87
88                 s1 = mono_return_ss (s1);
89                 if (s1.i1 != 5)
90                         return 3;
91                 
92                 su s2;
93                 s2.i1 = 2;
94                 s2.i2 = 3;
95                 if (mono_return_int_su (s2) != 3)
96                         return 4;
97                 
98                 s2.i1 = 2;
99                 if (mono_return_int_su (s2) != 2)
100                         return 5;
101
102
103                 cs s3;
104                 s3.b1 = false;
105                 s3.i1 = 12;
106                 s3.u1.i1 = 2;
107                 s3.u1.i2 = 1;
108                 
109                 if (mono_union_test_1 (s3) != 13)
110                         return 6;
111
112                 s3.u1.i1 = 2;
113                 if (mono_union_test_1 (s3) != 14)
114                         return 7;
115
116                 s3.b1 = true;
117                 if (mono_union_test_1 (s3) != 15)
118                         return 8;
119
120                 sc1 s4;
121                 s4.c0 = 3;
122                 s4 = mono_return_sc1(s4);
123                 if (s4.c0 != 4)
124                         return 9;
125
126                 sc3 s5;
127                 s5.c0 = 4;
128                 s5.c1 = 5;
129                 s5.c2 = 6;
130                 s5 = mono_return_sc3(s5);
131                 if (s5.c0 != 5 || s5.c1 != 7 || s5.c2 != 9)
132                         return 10;
133
134                 sc5 s6;
135                 s6.c0 = 4;
136                 s6.c1 = 5;
137                 s6.c2 = 6;
138                 s6.c3 = 7;
139                 s6.c4 = 8;
140                 s6 = mono_return_sc5(s6);
141                 if (s6.c0 != 5 || s6.c1 != 7 || s6.c2 != 9 || s6.c3 != 11 || s6.c4 != 13)
142                         return 11;
143
144                 return 0;
145         }
146 }
147