merge -r 53370:58178
[mono.git] / mono / tests / marshal2.cs
1 using System;
2 using System.Runtime.InteropServices;
3
4 public class Test {
5
6
7         [StructLayout (LayoutKind.Sequential)]
8         public class SimpleObj {
9                 public int a;
10                 public int b;
11
12                 public void test () {}
13         }
14
15         [StructLayout (LayoutKind.Sequential)]
16         public struct SimpleStruct2 {
17                 public int a;
18                 public int b;
19         }
20         
21         [StructLayout (LayoutKind.Sequential, CharSet=CharSet.Ansi)]
22         public struct SimpleStruct {
23                 public int a;
24                 public bool bool1;
25                 public bool bool2;
26                 public int b;
27                 [MarshalAs (UnmanagedType.ByValArray, SizeConst=2)] public short[] a1;
28                 [MarshalAs (UnmanagedType.ByValTStr, SizeConst=4)] public string s1;
29                 public SimpleStruct2 emb1;
30                 public SimpleObj emb2;
31                 public string s2;
32                 public double x;
33         }
34         
35         public unsafe static int Main () {
36                 SimpleStruct ss = new SimpleStruct ();
37                 int size = Marshal.SizeOf (typeof (SimpleStruct));
38                 
39                 Console.WriteLine ("SimpleStruct:" + size);
40                 //if (size != 52)
41                 //return 1;
42                 
43                 IntPtr p = Marshal.AllocHGlobal (size);
44                 ss.a = 1;
45                 ss.bool1 = true;
46                 ss.bool2 = false;
47                 ss.b = 2;
48                 ss.a1 = new short [2];
49                 ss.a1 [0] = 6;
50                 ss.a1 [1] = 5;
51                 ss.s1 = "abcd";
52                 ss.emb1 = new SimpleStruct2 ();
53                 ss.emb1.a = 3;
54                 ss.emb1.b = 4;
55                 ss.emb2 = new SimpleObj ();
56                 ss.emb2.a = 10;
57                 ss.emb2.b = 11;
58                 ss.s2 = "just a test";
59                 ss.x = 1.5;
60                 
61                 Marshal.StructureToPtr (ss, p, false);
62                 Type t = ss.GetType ();
63                 
64                 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "a")) != 1)
65                         return 1;
66                 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "bool1")) != 1)
67                         return 2;
68                 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "bool2")) != 0)
69                         return 3;
70                 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "b")) != 2)
71                         return 4;
72                 if (Marshal.ReadInt16 (p, 16) != 6)
73                         return 5;
74                 if (Marshal.ReadInt16 (p, 18) != 5)
75                         return 6;
76                 if (Marshal.ReadByte (p, 20) != 97)
77                         return 7;
78                 if (Marshal.ReadByte (p, 21) != 98)
79                         return 8;
80                 if (Marshal.ReadByte (p, 22) != 99)
81                         return 9;
82                 if (Marshal.ReadByte (p, 23) != 0)
83                         return 10;
84                 if (Marshal.ReadInt32 (p, 24) != 3)
85                         return 11;
86                 if (Marshal.ReadInt32 (p, 28) != 4)
87                         return 12;
88                 if (Marshal.ReadInt32 (p, 32) != 10)
89                         return 13;
90                 if (Marshal.ReadInt32 (p, 36) != 11)
91                         return 14;
92                 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "s2")) == 0)
93                         return 15;
94
95                 SimpleStruct cp = (SimpleStruct)Marshal.PtrToStructure (p, ss.GetType ());
96
97                 if (cp.a != 1)
98                         return 16;
99
100                 if (cp.bool1 != true)
101                         return 17;
102
103                 if (cp.bool2 != false)
104                         return 18;
105
106                 if (cp.b != 2)
107                         return 19;
108
109                 if (cp.a1 [0] != 6)
110                         return 20;
111                 
112                 if (cp.a1 [1] != 5)
113                         return 21;
114
115                 if (cp.s1 != "abc")
116                         return 22;
117                 
118                 if (cp.emb1.a != 3)
119                         return 23;
120
121                 if (cp.emb1.b != 4)
122                         return 24;
123
124                 if (cp.emb2.a != 10)
125                         return 25;
126
127                 if (cp.emb2.b != 11)
128                         return 26;
129
130                 if (cp.s2 != "just a test")
131                         return 27;
132
133                 if (cp.x != 1.5)
134                         return 28;
135                 
136                 return 0;
137         }
138 }
139