Add [Category ("NotWorking")] to failing test.
[mono.git] / mono / tests / marshal7.cs
1 using System;
2 using System.Runtime.InteropServices;
3
4 public class Test 
5 {
6         [StructLayout(LayoutKind.Sequential, Size=32)]
7         public class TestStruct1 
8         {
9                 public int a;
10         }
11
12         [StructLayout(LayoutKind.Sequential, Size=32)]
13         public class TestStruct2 
14         {
15                 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=16)]
16                 public string   a;
17                 public int              b;
18         }
19
20         [StructLayout(LayoutKind.Sequential, Size=32)]
21         public class TestStruct3 : TestStruct2
22         {
23                 public int              c;
24         }
25
26         [StructLayout (LayoutKind.Sequential)]
27         public class TestStruct4
28         {
29                 [MarshalAs (UnmanagedType.Interface)]
30                 object itf;
31         }
32
33         [StructLayout (LayoutKind.Sequential)]
34         public class TestStruct5
35         {
36                 [MarshalAs (UnmanagedType.IUnknown)]
37                 object itf;
38         }
39
40         [StructLayout (LayoutKind.Sequential)]
41         public class TestStruct6
42         {
43                 [MarshalAs (UnmanagedType.IDispatch)]
44                 object itf;
45         }
46
47         [StructLayout (LayoutKind.Sequential)]
48         public class TestStruct7
49         {
50                 [MarshalAs (UnmanagedType.Struct)]
51                 object itf;
52         }
53
54         // Size should be 12 in both 32 and 64 bits
55         [StructLayout (LayoutKind.Explicit)]
56         struct TestStruct8 {
57                 [FieldOffset (0)]
58                 public int a;
59                 [FieldOffset (4)]
60                 public ulong b;
61         }
62
63         // Size should be 12 in both 32 and 64 bits
64         [StructLayout (LayoutKind.Explicit, Size=12)]
65         struct TestStruct9 {
66                 [FieldOffset (0)]
67                 public int a;
68                 [FieldOffset (4)]
69                 public ulong b;
70         }
71
72         // Size should be 11 in both 32 and 64 bits
73         [StructLayout (LayoutKind.Explicit)]
74         struct TestStruct10 {
75                 [FieldOffset (0)]
76                 public int a;
77                 [FieldOffset (3)]
78                 public ulong b;
79         }
80
81         // Size should be 11 in both 32 and 64 bits
82         [StructLayout (LayoutKind.Explicit, Size=11)]
83         struct TestStruct11 {
84                 [FieldOffset (0)]
85                 public int a;
86                 [FieldOffset (3)]
87                 public ulong b;
88         }
89
90         [StructLayout (LayoutKind.Explicit, Pack=1)]
91         struct TestStruct12 {
92                 [FieldOffset (0)]
93                 public short a;
94                 [FieldOffset (2)]
95                 public int b;
96         }
97
98         public unsafe static int Main () 
99         {
100                 ///
101                 ///     Testing simple struct size
102                 ///
103                 if(Marshal.SizeOf(typeof(TestStruct1)) != 32)
104                 {
105                         return 1;
106                 }
107
108                 TestStruct1 myStruct = new TestStruct1();
109                 myStruct.a = 0x12345678;
110
111                 IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct1)));
112                 Marshal.StructureToPtr(myStruct, p, false);
113
114                 Type testType = typeof(TestStruct1);
115                 if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType, "a")) != myStruct.a)
116                         return 2;
117
118                 Marshal.FreeHGlobal(p);
119
120                 ///
121                 ///     Testing struct size with ByValTStr string
122                 ///
123                 if(Marshal.SizeOf(typeof(TestStruct2)) != 32)
124                         return 3;
125
126                 TestStruct2 myStruct2 = new TestStruct2();
127                 myStruct2.b = 0x12345678;
128
129                 p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct2)));
130                 Marshal.StructureToPtr(myStruct2, p, false);
131
132                 Type testType2 = typeof(TestStruct2);
133                 if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType2, "b")) != myStruct2.b)
134                         return 4;
135
136                 Marshal.FreeHGlobal(p);
137
138                 ///
139                 ///     Test structure size and struct with inheritance
140                 ///
141                 if(Marshal.SizeOf(typeof(TestStruct3)) != 64)
142                         return 5;
143
144                 TestStruct3 myStruct3 = new TestStruct3();
145                 myStruct3.b = 0x12345678;
146                 myStruct3.c = 0x76543210;
147                 p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct3)));
148                 Marshal.StructureToPtr(myStruct3, p, false);
149
150                 Type testType3 = typeof(TestStruct3);
151                 
152                 if(Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType3, "b")) != myStruct3.b)
153                         return 6;
154
155                 if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType3, "c")) != myStruct3.c) 
156                         return 7;
157
158                 Marshal.FreeHGlobal(p);
159                 
160                 ///
161                 ///     Also make sure OffsetOf returns the correct Exception.
162                 ///
163                 try {
164                         Marshal.OffsetOf(testType3, "blah");
165                         return 8;
166                 }
167                 catch(ArgumentException e)  {
168                         /// Way to go :)
169                 }
170                 catch(Exception e) {
171                         return 9;
172                 }
173
174                 // test size of structs with objects
175                 if (Marshal.SizeOf (typeof (TestStruct4)) != IntPtr.Size)
176                         return 10;
177                 if (Marshal.SizeOf (typeof (TestStruct5)) != IntPtr.Size)
178                         return 11;
179                 if (Marshal.SizeOf (typeof (TestStruct6)) != IntPtr.Size)
180                         return 12;
181                 // a VARIANT is 
182                 if (Marshal.SizeOf (typeof (TestStruct7)) != 16)
183                         return 13;
184                 if (Marshal.SizeOf (typeof (TestStruct8)) != 16)
185                         return 14;
186                 if (Marshal.SizeOf (typeof (TestStruct9)) != 12)
187                         return 15;
188                 if (Marshal.SizeOf (typeof (TestStruct10)) != 16)
189                         return 16;
190                 if (Marshal.SizeOf (typeof (TestStruct11)) != 11)
191                         return 17;
192                 if (Marshal.SizeOf (typeof (TestStruct12)) != 6)
193                         return 18;
194                 return 0;
195         }
196 }
197