Add [Category ("NotWorking")] to failing test.
[mono.git] / mcs / class / corlib / Test / System / WeakReferenceTest.cs
1 //
2 // WeakReferenceTest.cs - NUnit Test Cases for WeakReference
3 //
4 // Author:
5 //      Sebastien Pouliot (sebastien@ximian.com)
6 //
7 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 using System;
30 using System.IO;
31
32 using NUnit.Framework;
33
34 namespace MonoTests.System {
35
36         [TestFixture]
37         public class WeakReferenceTest {
38
39                 [Test]
40                 public void WeakReference_Object_Null ()
41                 {
42                         WeakReference wr = new WeakReference (null);
43                         Assert.IsFalse (wr.IsAlive, "IsAlive");
44                         Assert.IsNull (wr.Target, "Target");
45                         Assert.IsFalse (wr.TrackResurrection, "TrackResurrection");
46                 }
47
48                 [Test]
49                 public void WeakReference_Object_Null_TrackResurrection_True ()
50                 {
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");
55                 }
56
57                 [Test]
58                 public void WeakReference_Object_Null_TrackResurrection_False ()
59                 {
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");
64                 }
65
66                 [Test]
67                 public void WeakReference_Object ()
68                 {
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");
74                         }
75                 }
76
77                 [Test]
78                 public void WeakReference_Object_TrackResurrection_True ()
79                 {
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");
85                         }
86                 }
87
88                 [Test]
89                 public void WeakReference_Object_TrackResurrection_False ()
90                 {
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");
96                         }
97                 }
98
99                 class Foo {
100                         WeakReference wr;
101
102                         public static bool failed;
103
104                         public Foo () {
105                                 wr = new WeakReference (new object ());
106                         }
107
108                         ~Foo () {
109                                 try {
110                                         var b = wr.IsAlive;
111                                 } catch (Exception) {
112                                         failed = true;
113                                 }
114                         }
115                 }
116
117                 [Test]
118                 public void WeakReference_IsAlive_Finalized ()
119                 {
120                         var f = new Foo ();
121                         f = null;
122                         GC.Collect ();
123                         GC.WaitForPendingFinalizers ();
124                         Assert.IsFalse (Foo.failed);
125                 }
126         }
127 }
128