**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / Test / System.Runtime.InteropServices / MarshalTest.cs
blob8e6c1c479303fae7dc0f5050e7f00eccf006c2c6
1 //
2 // System.Runtime.InteropServices.Marshal Test Cases
3 //
4 // Authors:
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (c) 2004 Novell, Inc. (http://www.novell.com)
8 //
10 using NUnit.Framework;
11 using System;
12 using System.Runtime.InteropServices;
14 namespace MonoTests.System.Runtime.InteropServices
16 [TestFixture]
17 public class MarshalTest : Assertion
19 [StructLayout (LayoutKind.Sequential)]
20 class ClsSequential {
21 public int field;
24 class ClsNoLayout {
25 public int field;
28 [StructLayout (LayoutKind.Explicit)]
29 class ClsExplicit {
30 [FieldOffset (0)] public int field;
33 [StructLayout (LayoutKind.Sequential)]
34 struct StrSequential {
35 public int field;
38 struct StrNoLayout {
39 public int field;
42 [StructLayout (LayoutKind.Explicit)]
43 struct StrExplicit {
44 [FieldOffset (0)] public int field;
47 [Test]
48 public void ClassSequential ()
50 Marshal.SizeOf (typeof (ClsSequential));
53 [Test]
54 [ExpectedException (typeof (ArgumentException))]
55 public void ClassNoLayout ()
57 Marshal.SizeOf (typeof (ClsNoLayout));
60 [Test]
61 public void ClassExplicit ()
63 Marshal.SizeOf (typeof (ClsExplicit));
66 [Test]
67 public void StructSequential ()
69 Marshal.SizeOf (typeof (StrSequential));
72 [Test]
73 public void StructNoLayout ()
75 Marshal.SizeOf (typeof (StrNoLayout));
78 [Test]
79 public void StructExplicit ()
81 Marshal.SizeOf (typeof (StrExplicit));
84 [Test]
85 [ExpectedException (typeof (ArgumentException))]
86 public void ArrayType ()
88 Marshal.SizeOf (typeof (string[]));
91 [Test]
92 public void PtrToStringWithNull ()
94 AssertNull ("A", Marshal.PtrToStringAnsi (IntPtr.Zero));
95 AssertNull ("C", Marshal.PtrToStringUni (IntPtr.Zero));
98 [Test]
99 [ExpectedException (typeof (ArgumentNullException))]
100 public void PtrToStringWithNullThrow ()
102 AssertNull ("B", Marshal.PtrToStringAnsi (IntPtr.Zero, 0));
105 [Test]
106 [ExpectedException (typeof (ArgumentNullException))]
107 public void PtrToStringWithNullThrow2 ()
109 AssertNull ("D", Marshal.PtrToStringUni (IntPtr.Zero, 0));
112 [Test]
113 public unsafe void UnsafeAddrOfPinnedArrayElement () {
114 short[] sarr = new short [5];
115 sarr [2] = 3;
117 IntPtr ptr = Marshal.UnsafeAddrOfPinnedArrayElement (sarr, 2);
118 AssertEquals (3, *(short*)ptr.ToPointer ());
121 [Test]
122 public void AllocHGlobalZeroSize () {
123 IntPtr ptr = Marshal.AllocHGlobal (0);
124 Assert (ptr != IntPtr.Zero);
125 Marshal.FreeHGlobal (ptr);