2 // System.Runtime.InteropServices.Marshal Test Cases
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (c) 2004 Novell, Inc. (http://www.novell.com)
10 using NUnit
.Framework
;
12 using System
.Runtime
.InteropServices
;
14 namespace MonoTests
.System
.Runtime
.InteropServices
17 public class MarshalTest
: Assertion
19 [StructLayout (LayoutKind
.Sequential
)]
28 [StructLayout (LayoutKind
.Explicit
)]
30 [FieldOffset (0)] public int field
;
33 [StructLayout (LayoutKind
.Sequential
)]
34 struct StrSequential
{
42 [StructLayout (LayoutKind
.Explicit
)]
44 [FieldOffset (0)] public int field
;
48 public void ClassSequential ()
50 Marshal
.SizeOf (typeof (ClsSequential
));
54 [ExpectedException (typeof (ArgumentException
))]
55 public void ClassNoLayout ()
57 Marshal
.SizeOf (typeof (ClsNoLayout
));
61 public void ClassExplicit ()
63 Marshal
.SizeOf (typeof (ClsExplicit
));
67 public void StructSequential ()
69 Marshal
.SizeOf (typeof (StrSequential
));
73 public void StructNoLayout ()
75 Marshal
.SizeOf (typeof (StrNoLayout
));
79 public void StructExplicit ()
81 Marshal
.SizeOf (typeof (StrExplicit
));
85 [ExpectedException (typeof (ArgumentException
))]
86 public void ArrayType ()
88 Marshal
.SizeOf (typeof (string[]));
92 public void PtrToStringWithNull ()
94 AssertNull ("A", Marshal
.PtrToStringAnsi (IntPtr
.Zero
));
95 AssertNull ("C", Marshal
.PtrToStringUni (IntPtr
.Zero
));
99 [ExpectedException (typeof (ArgumentNullException
))]
100 public void PtrToStringWithNullThrow ()
102 AssertNull ("B", Marshal
.PtrToStringAnsi (IntPtr
.Zero
, 0));
106 [ExpectedException (typeof (ArgumentNullException
))]
107 public void PtrToStringWithNullThrow2 ()
109 AssertNull ("D", Marshal
.PtrToStringUni (IntPtr
.Zero
, 0));
113 public unsafe void UnsafeAddrOfPinnedArrayElement () {
114 short[] sarr
= new short [5];
117 IntPtr ptr
= Marshal
.UnsafeAddrOfPinnedArrayElement (sarr
, 2);
118 AssertEquals (3, *(short*)ptr
.ToPointer ());
122 public void AllocHGlobalZeroSize () {
123 IntPtr ptr
= Marshal
.AllocHGlobal (0);
124 Assert (ptr
!= IntPtr
.Zero
);
125 Marshal
.FreeHGlobal (ptr
);