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                 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 != 56)
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 1;
68                 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "bool2")) != 0)
69                         return 1;
70                 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "b")) != 2)
71                         return 1;
72                 if (Marshal.ReadInt16 (p, 16) != 6)
73                         return 1;
74                 if (Marshal.ReadInt16 (p, 18) != 5)
75                         return 1;
76                 if (Marshal.ReadByte (p, 20) != 97)
77                         return 1;
78                 if (Marshal.ReadByte (p, 21) != 98)
79                         return 1;
80                 if (Marshal.ReadByte (p, 22) != 99)
81                         return 1;
82                 if (Marshal.ReadByte (p, 23) != 0)
83                         return 1;
84                 if (Marshal.ReadInt32 (p, 24) != 3)
85                         return 1;
86                 if (Marshal.ReadInt32 (p, 28) != 4)
87                         return 1;
88                 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "s2")) == 0)
89                         return 1;
90
91                 SimpleStruct cp = (SimpleStruct)Marshal.PtrToStructure (p, ss.GetType ());
92
93                 if (cp.a != 1)
94                         return 2;
95
96                 if (cp.bool1 != true)
97                         return 2;
98
99                 if (cp.bool2 != false)
100                         return 2;
101
102                 if (cp.b != 2)
103                         return 2;
104
105                 if (cp.a1 [0] != 6)
106                         return 2;
107                 
108                 if (cp.a1 [1] != 5)
109                         return 2;
110
111                 if (cp.s1 != "abc")
112                         return 3;
113                 
114                 if (cp.emb1.a != 3)
115                         return 2;
116
117                 if (cp.emb1.b != 4)
118                         return 2;
119
120                 if (cp.emb2.a != 10)
121                         return 2;
122
123                 if (cp.emb2.b != 11)
124                         return 2;
125
126                 if (cp.s2 != "just a test")
127                         return 2;
128
129                 if (cp.x != 1.5)
130                         return 2;
131                 
132                 return 0;
133         }
134 }
135