New 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         public unsafe static int Main () 
55         {
56                 ///
57                 ///     Testing simple struct size
58                 ///
59                 if(Marshal.SizeOf(typeof(TestStruct1)) != 32)
60                 {
61                         return 1;
62                 }
63
64                 TestStruct1 myStruct = new TestStruct1();
65                 myStruct.a = 0x12345678;
66
67                 IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct1)));
68                 Marshal.StructureToPtr(myStruct, p, false);
69
70                 Type testType = typeof(TestStruct1);
71                 if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType, "a")) != myStruct.a)
72                         return 2;
73
74                 Marshal.FreeHGlobal(p);
75
76                 ///
77                 ///     Testing struct size with ByValTStr string
78                 ///
79                 if(Marshal.SizeOf(typeof(TestStruct2)) != 32)
80                         return 3;
81
82                 TestStruct2 myStruct2 = new TestStruct2();
83                 myStruct2.b = 0x12345678;
84
85                 p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct2)));
86                 Marshal.StructureToPtr(myStruct2, p, false);
87
88                 Type testType2 = typeof(TestStruct2);
89                 if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType2, "b")) != myStruct2.b)
90                         return 4;
91
92                 Marshal.FreeHGlobal(p);
93
94                 ///
95                 ///     Test structure size and struct with inheritance
96                 ///
97                 if(Marshal.SizeOf(typeof(TestStruct3)) != 64)
98                         return 5;
99
100                 TestStruct3 myStruct3 = new TestStruct3();
101                 myStruct3.b = 0x12345678;
102                 myStruct3.c = 0x76543210;
103                 p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct3)));
104                 Marshal.StructureToPtr(myStruct3, p, false);
105
106                 Type testType3 = typeof(TestStruct3);
107                 
108                 if(Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType3, "b")) != myStruct3.b)
109                         return 6;
110
111                 if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType3, "c")) != myStruct3.c) 
112                         return 7;
113
114                 Marshal.FreeHGlobal(p);
115                 
116                 ///
117                 ///     Also make sure OffsetOf returns the correct Exception.
118                 ///
119                 try {
120                         Marshal.OffsetOf(testType3, "blah");
121                         return 8;
122                 }
123                 catch(ArgumentException e)  {
124                         /// Way to go :)
125                 }
126                 catch(Exception e) {
127                         return 9;
128                 }
129
130                 // test size of structs with objects
131                 if (Marshal.SizeOf (typeof (TestStruct4)) != IntPtr.Size)
132                         return 10;
133                 if (Marshal.SizeOf (typeof (TestStruct5)) != IntPtr.Size)
134                         return 11;
135                 if (Marshal.SizeOf (typeof (TestStruct6)) != IntPtr.Size)
136                         return 12;
137                 // a VARIANT is 
138                 if (Marshal.SizeOf (typeof (TestStruct7)) != 16)
139                         return 13;
140
141                 return 0;
142         }
143 }
144