2002-07-19 Dietmar Maurer <dietmar@ximian.com>
[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         }
33         
34         public unsafe static int Main () {
35                 SimpleStruct ss = new SimpleStruct ();
36                 int size = Marshal.SizeOf (typeof (SimpleStruct));
37                 
38                 Console.WriteLine ("SimpleStruct:" + size);
39                 if (size != 44)
40                         return 1;
41                 
42                 IntPtr p = Marshal.AllocHGlobal (size);
43                 ss.a = 1;
44                 ss.bool1 = true;
45                 ss.bool2 = false;
46                 ss.b = 2;
47                 ss.a1 = new short [2];
48                 ss.a1 [0] = 6;
49                 ss.a1 [1] = 5;
50                 ss.s1 = "abcd";
51                 ss.emb1 = new SimpleStruct2 ();
52                 ss.emb1.a = 3;
53                 ss.emb1.b = 4;
54                 ss.emb2 = new SimpleObj ();
55                 ss.emb2.a = 10;
56                 ss.emb2.b = 11;
57                 ss.s2 = "just a test";
58                 
59                 Marshal.StructureToPtr (ss, p, false);
60                 Type t = ss.GetType ();
61                 
62                 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "a")) != 1)
63                         return 1;
64                 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "bool1")) != 1)
65                         return 1;
66                 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "bool2")) != 0)
67                         return 1;
68                 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "b")) != 2)
69                         return 1;
70                 if (Marshal.ReadInt16 (p, 16) != 6)
71                         return 1;
72                 if (Marshal.ReadInt16 (p, 18) != 5)
73                         return 1;
74                 if (Marshal.ReadByte (p, 20) != 97)
75                         return 1;
76                 if (Marshal.ReadByte (p, 21) != 98)
77                         return 1;
78                 if (Marshal.ReadByte (p, 22) != 99)
79                         return 1;
80                 if (Marshal.ReadByte (p, 23) != 0)
81                         return 1;
82                 if (Marshal.ReadInt32 (p, 24) != 3)
83                         return 1;
84                 if (Marshal.ReadInt32 (p, 28) != 4)
85                         return 1;
86                 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "s2")) == 0)
87                         return 1;
88
89                 SimpleStruct cp = (SimpleStruct)Marshal.PtrToStructure (p, ss.GetType ());
90
91                 if (cp.a != 1)
92                         return 2;
93
94                 if (cp.bool1 != true)
95                         return 2;
96
97                 if (cp.bool2 != false)
98                         return 2;
99
100                 if (cp.b != 2)
101                         return 2;
102
103                 if (cp.a1 [0] != 6)
104                         return 2;
105                 
106                 if (cp.a1 [1] != 5)
107                         return 2;
108
109                 if (cp.s1 != "abc")
110                         return 3;
111                 
112                 if (cp.emb1.a != 3)
113                         return 2;
114
115                 if (cp.emb1.b != 4)
116                         return 2;
117
118                 if (cp.emb2.a != 10)
119                         return 2;
120
121                 if (cp.emb2.b != 11)
122                         return 2;
123
124                 if (cp.s2 != "just a test")
125                         return 2;
126                 
127                 return 0;
128         }
129 }
130