X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mono%2Ftests%2Fmarshal2.cs;h=3e98a14a48780513be5c497917a81fd9add804d9;hb=249cee37a619bb6069399c129a24c798d756d627;hp=9ba085f63f09f8bdc0f71ecb5d13c8752b25cac9;hpb=6cfd2055426c190ca2f6a9f8ca3af2da6f6a79d0;p=mono.git diff --git a/mono/tests/marshal2.cs b/mono/tests/marshal2.cs index 9ba085f63f0..3e98a14a487 100644 --- a/mono/tests/marshal2.cs +++ b/mono/tests/marshal2.cs @@ -1,3 +1,7 @@ +// +// Tests for Marshal.StructureToPtr and PtrToStructure +// + using System; using System.Runtime.InteropServices; @@ -30,13 +34,19 @@ public class Test { public SimpleObj emb2; public string s2; public double x; + [MarshalAs (UnmanagedType.ByValArray, SizeConst=2)] public char[] a2; + } + + [StructLayout (LayoutKind.Sequential, CharSet=CharSet.Unicode)] + public struct ByValWStrStruct { + [MarshalAs (UnmanagedType.ByValTStr, SizeConst=4)] public string s1; + public int i; } public unsafe static int Main () { SimpleStruct ss = new SimpleStruct (); int size = Marshal.SizeOf (typeof (SimpleStruct)); - Console.WriteLine ("SimpleStruct:" + size); //if (size != 52) //return 1; @@ -57,6 +67,9 @@ public class Test { ss.emb2.b = 11; ss.s2 = "just a test"; ss.x = 1.5; + ss.a2 = new char [2]; + ss.a2 [0] = 'a'; + ss.a2 [1] = 'b'; Marshal.StructureToPtr (ss, p, false); Type t = ss.GetType (); @@ -89,8 +102,10 @@ public class Test { return 13; if (Marshal.ReadInt32 (p, 36) != 11) return 14; - if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "s2")) == 0) + if (Marshal.ReadByte (p, (int)Marshal.OffsetOf (t, "a2")) != 97) return 15; + if (Marshal.ReadByte (p, (int)Marshal.OffsetOf (t, "a2") + 1) != 98) + return 16; SimpleStruct cp = (SimpleStruct)Marshal.PtrToStructure (p, ss.GetType ()); @@ -132,7 +147,38 @@ public class Test { if (cp.x != 1.5) return 28; - + + if (cp.a2 [0] != 'a') + return 29; + + if (cp.a2 [1] != 'b') + return 30; + + /* ByValTStr with Unicode */ + ByValWStrStruct s = new ByValWStrStruct (); + + IntPtr p2 = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (ByValWStrStruct))); + Marshal.StructureToPtr(s, p2, false); + + /* Check that the ByValWStr is initialized correctly */ + for (int i = 0; i < 8; ++i) + if (Marshal.ReadByte (p2, i) != 0) + return 31; + + s.s1 = "ABCD"; + s.i = 55; + + Marshal.StructureToPtr(s, p2, false); + + ByValWStrStruct s2 = (ByValWStrStruct)Marshal.PtrToStructure (p2, typeof (ByValWStrStruct)); + + /* The fourth char is lost because of null-termination */ + if (s2.s1 != "ABC") + return 32; + + if (s2.i != 55) + return 33; + return 0; } }