2006-09-05 Zoltan Varga <vargaz@gmail.com>
[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         public unsafe static int Main () 
27         {
28                 ///
29                 ///     Testing simple struct size
30                 ///
31                 if(Marshal.SizeOf(typeof(TestStruct1)) != 32)
32                 {
33                         return 1;
34                 }
35
36                 TestStruct1 myStruct = new TestStruct1();
37                 myStruct.a = 0x12345678;
38
39                 IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct1)));
40                 Marshal.StructureToPtr(myStruct, p, false);
41
42                 Type testType = typeof(TestStruct1);
43                 if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType, "a")) != myStruct.a)
44                         return 2;
45
46                 Marshal.FreeHGlobal(p);
47
48                 ///
49                 ///     Testing struct size with ByValTStr string
50                 ///
51                 if(Marshal.SizeOf(typeof(TestStruct2)) != 32)
52                         return 3;
53
54                 TestStruct2 myStruct2 = new TestStruct2();
55                 myStruct2.b = 0x12345678;
56
57                 p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct2)));
58                 Marshal.StructureToPtr(myStruct2, p, false);
59
60                 Type testType2 = typeof(TestStruct2);
61                 if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType2, "b")) != myStruct2.b)
62                         return 4;
63
64                 Marshal.FreeHGlobal(p);
65
66                 ///
67                 ///     Test structure size and struct with inheritance
68                 ///
69                 if(Marshal.SizeOf(typeof(TestStruct3)) != 64)
70                         return 5;
71
72                 TestStruct3 myStruct3 = new TestStruct3();
73                 myStruct3.b = 0x12345678;
74                 myStruct3.c = 0x76543210;
75                 p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct3)));
76                 Marshal.StructureToPtr(myStruct3, p, false);
77
78                 Type testType3 = typeof(TestStruct3);
79                 
80                 if(Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType3, "b")) != myStruct3.b)
81                         return 6;
82
83                 if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType3, "c")) != myStruct3.c) 
84                         return 7;
85
86                 Marshal.FreeHGlobal(p);
87                 
88                 ///
89                 ///     Also make sure OffsetOf returns the correct Exception.
90                 ///
91                 try {
92                         Marshal.OffsetOf(testType3, "blah");
93                         return 8;
94                 }
95                 catch(ArgumentException e)  {
96                         /// Way to go :)
97                 }
98                 catch(Exception e) {
99                         return 9;
100                 }
101
102                 return 0;
103         }
104 }
105