2 // WeakReferenceTest.cs - NUnit Test Cases for WeakReference
5 // Sebastien Pouliot (sebastien@ximian.com)
7 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using NUnit.Framework;
34 namespace MonoTests.System {
37 public class WeakReferenceTest {
40 public void WeakReference_Object_Null ()
42 WeakReference wr = new WeakReference (null);
43 Assert.IsFalse (wr.IsAlive, "IsAlive");
44 Assert.IsNull (wr.Target, "Target");
45 Assert.IsFalse (wr.TrackResurrection, "TrackResurrection");
49 public void WeakReference_Object_Null_TrackResurrection_True ()
51 WeakReference wr = new WeakReference (null, true);
52 Assert.IsFalse (wr.IsAlive, "IsAlive");
53 Assert.IsNull (wr.Target, "Target");
54 Assert.IsTrue (wr.TrackResurrection, "TrackResurrection");
58 public void WeakReference_Object_Null_TrackResurrection_False ()
60 WeakReference wr = new WeakReference (null, false);
61 Assert.IsFalse (wr.IsAlive, "IsAlive");
62 Assert.IsNull (wr.Target, "Target");
63 Assert.IsFalse (wr.TrackResurrection, "TrackResurrection");
67 public void WeakReference_Object ()
69 using (Stream s = Stream.Null) {
70 WeakReference wr = new WeakReference (s);
71 Assert.IsTrue (wr.IsAlive, "IsAlive");
72 Assert.AreSame (s, wr.Target, "Target");
73 Assert.IsFalse (wr.TrackResurrection, "TrackResurrection");
78 public void WeakReference_Object_TrackResurrection_True ()
80 using (Stream s = Stream.Null) {
81 WeakReference wr = new WeakReference (s, true);
82 Assert.IsTrue (wr.IsAlive, "IsAlive");
83 Assert.AreSame (s, wr.Target, "Target");
84 Assert.IsTrue (wr.TrackResurrection, "TrackResurrection");
89 public void WeakReference_Object_TrackResurrection_False ()
91 using (Stream s = Stream.Null) {
92 WeakReference wr = new WeakReference (s, false);
93 Assert.IsTrue (wr.IsAlive, "IsAlive");
94 Assert.AreSame (s, wr.Target, "Target");
95 Assert.IsFalse (wr.TrackResurrection, "TrackResurrection");
102 public static bool failed;
105 wr = new WeakReference (new object ());
111 } catch (Exception) {
118 public void WeakReference_IsAlive_Finalized ()
123 GC.WaitForPendingFinalizers ();
124 Assert.IsFalse (Foo.failed);