2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / Test / System.Runtime.InteropServices / GCHandleTest.cs
blob98e99c7c981f4e6fafae321f4926531574fac2c4
1 //
2 // System.Runtime.InteropServices.GCHandle Test Cases
3 //
4 // Authors:
5 // Paolo Molaro (lupus@ximian.com)
6 //
7 // Copyright (C) 2005, 2009 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 GCHandleTest
19 static GCHandle handle;
21 [Test]
22 public void DefaultZeroValue_Allocated ()
24 Assert.IsFalse (handle.IsAllocated, "IsAllocated");
27 [Test]
28 [ExpectedException (typeof (InvalidOperationException))]
29 public void DefaultZeroValue_Target ()
31 Assert.IsNull (handle.Target, "Target");
34 [Test]
35 public void AllocNull ()
37 IntPtr ptr = (IntPtr) GCHandle.Alloc (null);
38 Assert.IsFalse (ptr == IntPtr.Zero, "ptr");
39 GCHandle gch = (GCHandle) ptr;
40 Assert.IsTrue (gch.IsAllocated, "IsAllocated");
41 Assert.IsNull (gch.Target, "Target");
44 [Test]
45 public void AllocNullWeakTrack ()
47 GCHandle gch = GCHandle.Alloc (null, GCHandleType.WeakTrackResurrection);
48 Assert.IsTrue (gch.IsAllocated, "IsAllocated");
49 Assert.IsNull (gch.Target, "Target");
52 [Test]
53 [ExpectedException (typeof (InvalidOperationException))]
54 public void AddrOfPinnedObjectNormal ()
56 GCHandle handle = GCHandle.Alloc (new Object (), GCHandleType.Normal);
57 try {
58 IntPtr ptr = handle.AddrOfPinnedObject();
60 finally {
61 handle.Free();
65 [Test]
66 [ExpectedException (typeof (InvalidOperationException))]
67 public void AddrOfPinnedObjectWeak ()
69 GCHandle handle = GCHandle.Alloc (new Object (), GCHandleType.Weak);
70 try {
71 IntPtr ptr = handle.AddrOfPinnedObject();
73 finally {
74 handle.Free();
78 [Test]
79 [ExpectedException (typeof (InvalidOperationException))]
80 public void AddrOfPinnedObjectWeakTrackResurrection ()
82 GCHandle handle = GCHandle.Alloc (new Object (), GCHandleType.WeakTrackResurrection);
83 try {
84 IntPtr ptr = handle.AddrOfPinnedObject();
86 finally {
87 handle.Free();
91 [Test]
92 public void AddrOfPinnedObjectNull ()
94 GCHandle handle = GCHandle.Alloc (null, GCHandleType.Pinned);
95 try {
96 IntPtr ptr = handle.AddrOfPinnedObject();
97 Assert.AreEqual (new IntPtr (0), ptr);
99 finally {
100 handle.Free();
104 [Test]
105 [Ignore ("throw non-catchable ExecutionEngineException")]
106 [ExpectedException (typeof (ExecutionEngineException))]
107 public void AllocMinusOne ()
109 // -1 is a special value used by the mono runtime
110 // looks like it's special too in MS CLR (since it will crash)
111 GCHandle.Alloc (null, (GCHandleType) (-1));
114 [Test]
115 public void AllocInvalidType ()
117 GCHandle gch = GCHandle.Alloc (null, (GCHandleType) Int32.MinValue);
118 try {
119 Assert.IsTrue (gch.IsAllocated, "IsAllocated");
120 Assert.IsNull (gch.Target, "Target");
122 finally {
123 gch.Free ();