2 // System.Runtime.InteropServices.GCHandle Test Cases
5 // Paolo Molaro (lupus@ximian.com)
7 // Copyright (C) 2005, 2009 Novell, Inc (http://www.novell.com)
10 using NUnit
.Framework
;
12 using System
.Runtime
.InteropServices
;
14 namespace MonoTests
.System
.Runtime
.InteropServices
17 public class GCHandleTest
19 static GCHandle handle
;
22 public void DefaultZeroValue_Allocated ()
24 Assert
.IsFalse (handle
.IsAllocated
, "IsAllocated");
28 [ExpectedException (typeof (InvalidOperationException
))]
29 public void DefaultZeroValue_Target ()
31 Assert
.IsNull (handle
.Target
, "Target");
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");
45 public void AllocNullWeakTrack ()
47 GCHandle gch
= GCHandle
.Alloc (null, GCHandleType
.WeakTrackResurrection
);
48 Assert
.IsTrue (gch
.IsAllocated
, "IsAllocated");
49 Assert
.IsNull (gch
.Target
, "Target");
53 [ExpectedException (typeof (InvalidOperationException
))]
54 public void AddrOfPinnedObjectNormal ()
56 GCHandle handle
= GCHandle
.Alloc (new Object (), GCHandleType
.Normal
);
58 IntPtr ptr
= handle
.AddrOfPinnedObject();
66 [ExpectedException (typeof (InvalidOperationException
))]
67 public void AddrOfPinnedObjectWeak ()
69 GCHandle handle
= GCHandle
.Alloc (new Object (), GCHandleType
.Weak
);
71 IntPtr ptr
= handle
.AddrOfPinnedObject();
79 [ExpectedException (typeof (InvalidOperationException
))]
80 public void AddrOfPinnedObjectWeakTrackResurrection ()
82 GCHandle handle
= GCHandle
.Alloc (new Object (), GCHandleType
.WeakTrackResurrection
);
84 IntPtr ptr
= handle
.AddrOfPinnedObject();
92 public void AddrOfPinnedObjectNull ()
94 GCHandle handle
= GCHandle
.Alloc (null, GCHandleType
.Pinned
);
96 IntPtr ptr
= handle
.AddrOfPinnedObject();
97 Assert
.AreEqual (new IntPtr (0), ptr
);
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));
115 public void AllocInvalidType ()
117 GCHandle gch
= GCHandle
.Alloc (null, (GCHandleType
) Int32
.MinValue
);
119 Assert
.IsTrue (gch
.IsAllocated
, "IsAllocated");
120 Assert
.IsNull (gch
.Target
, "Target");