2 // ConditionalWeakTableTest.cs
5 // Rodrigo Kumpera <rkumpera@novell.com>
7 // Copyright (C) 2010 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.
31 using NUnit.Framework;
33 using System.Reflection;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
36 using System.Runtime.Serialization;
37 using System.Security.Permissions;
38 using System.Collections.Generic;
39 using System.Threading;
42 namespace MonoTests.System.Runtime.CompilerServices {
45 public class ConditionalWeakTableTest {
50 public Link(object obj) {
58 public void GetValue () {
59 var cwt = new ConditionalWeakTable <object,object> ();
62 cwt.GetValue (null, k => null);
64 } catch (ArgumentNullException) {}
67 cwt.GetValue (20, null);
69 } catch (ArgumentNullException) {}
73 object val = cwt.GetValue (key, k => new Link (k));
74 Assert.IsTrue (val != null, "#2");
75 Assert.AreEqual (typeof (Link), val.GetType () , "#3");
77 Assert.AreEqual (val, cwt.GetValue (key, k => new object ()), "#4");
81 public void GetOrCreateValue () {
82 var cwt = new ConditionalWeakTable <object,object> ();
85 cwt.GetOrCreateValue (null);
87 } catch (ArgumentNullException) {}
91 object val = cwt.GetOrCreateValue (key);
92 Assert.IsTrue (val != null, "#2");
93 Assert.AreEqual (typeof (object), val.GetType () , "#3");
95 Assert.AreEqual (val, cwt.GetOrCreateValue (key), "#4");
97 var cwt2 = new ConditionalWeakTable <object, string> ();
99 cwt2.GetOrCreateValue (key);
101 } catch (MissingMethodException) {}
105 public void Remove () {
106 var cwt = new ConditionalWeakTable <object,object> ();
107 object c = new Key ();
114 } catch (ArgumentNullException) {}
117 Assert.IsFalse (cwt.Remove ("x"), "#1");
118 Assert.IsTrue (cwt.Remove (c), "#2");
119 Assert.IsFalse (cwt.Remove (c), "#3");
124 var cwt = new ConditionalWeakTable <object,object> ();
125 object c = new Key ();
127 cwt.Add (c, new Link (c));
130 cwt.Add (c, new Link (c));
132 } catch (ArgumentException) {}
134 cwt.Add ("zzz", null);//ok
137 cwt.Add (null, new Link (c));
139 } catch (ArgumentNullException) {}
143 public void TryGetValue () {
144 var cwt = new ConditionalWeakTable <object,object> ();
146 object c = new Key ();
151 cwt.TryGetValue (null, out res);
153 } catch (ArgumentNullException) {}
155 Assert.IsFalse (cwt.TryGetValue ("foo", out res), "#1");
156 Assert.IsTrue (cwt.TryGetValue (c, out res), "#2");
157 Assert.AreEqual ("foo", res, "#3");
161 static void FillStuff (ConditionalWeakTable <object,object> cwt,
162 out List<object> keepAlive, out List<WeakReference> keys) {
164 keepAlive = new List<object> ();
165 keys = new List<WeakReference> ();
167 object a = new Key ();
168 object b = new Key ();
169 object c = new Key ();
171 cwt.Add (a, new string ("str0".ToCharArray ()));
173 cwt.Add (c, new Link (a));
176 keys.Add (new WeakReference(a));
177 keys.Add (new WeakReference(b));
178 keys.Add (new WeakReference(c));
182 public void Reachability () {
183 var cwt = new ConditionalWeakTable <object,object> ();
185 List<object> keepAlive;
186 List<WeakReference> keys;
187 FillStuff (cwt, out keepAlive, out keys);
191 Assert.IsTrue (keys [0].IsAlive, "r0");
192 Assert.IsFalse (keys [1].IsAlive, "r1");
193 Assert.IsTrue (keys [2].IsAlive, "r2");
196 Assert.IsTrue (cwt.TryGetValue (keepAlive [0], out res), "ka0");
197 Assert.IsTrue (res is Link, "ka1");
199 Link link = res as Link;
200 Assert.IsTrue (cwt.TryGetValue (link.obj, out res), "ka2");
201 Assert.AreEqual ("str0", res, "ka3");
205 static List<WeakReference> FillWithNetwork (ConditionalWeakTable <object,object> cwt)
208 object[] keys = new object [K];
209 for (int i = 0; i < K; ++i)
210 keys[i] = new object ();
212 Random rand = new Random ();
214 /*produce a complex enough network of links*/
215 for (int i = 0; i < K; ++i)
216 cwt.Add (keys [i], new Link (keys [rand.Next (K)]));
218 var res = new List<WeakReference> ();
220 for (int i = 0; i < 10; ++i)
221 res.Add (new WeakReference (keys [rand.Next (K)]));
222 Array.Clear (keys, 0, keys.Length);
228 public void InsertStress () {
229 var cwt = new ConditionalWeakTable <object,object> ();
231 var a = new object ();
232 var b = new object ();
234 cwt.Add (a, new object ());
235 cwt.Add (b, new object ());
237 List<WeakReference> res = null;
238 ThreadStart dele = () => { res = FillWithNetwork (cwt); };
239 var th = new Thread(dele);
246 for (int i = 0; i < res.Count; ++i)
247 Assert.IsFalse (res [i].IsAlive, "#r" + i);
250 static List<WeakReference> FillWithNetwork2 (ConditionalWeakTable <object,object>[] cwt) {
252 cwt[0] = new ConditionalWeakTable <object,object> ();
253 var res = FillWithNetwork (cwt[0]);
258 static void ForcePromotion () {
259 var o = new object[64000];
261 for (int i = 0; i < 64000; ++i)
265 static List<object> FillReachable (ConditionalWeakTable <object,object>[] cwt)
267 var res = new List<object> ();
268 for (int i = 0; i < 10; ++i) {
269 res.Add (new object ());
270 cwt[0].Add (res [i], i);
277 public void OldGenStress () {
278 var cwt = new ConditionalWeakTable <object,object>[1];
279 List<object> k = null;
280 List<WeakReference> res, res2;
283 ThreadStart dele = () => {
284 res = FillWithNetwork2 (cwt);
286 k = FillReachable (cwt);
287 res2 = FillWithNetwork2 (cwt);
290 var th = new Thread(dele);
296 for (int i = 0; i < res.Count; ++i)
297 Assert.IsFalse (res [i].IsAlive, "#r0-" + i);
298 for (int i = 0; i < res2.Count; ++i)
299 Assert.IsFalse (res2 [i].IsAlive, "#r1-" + i);
301 for (int i = 0; i < k.Count; ++i) {
303 Assert.IsTrue (cwt[0].TryGetValue (k [i], out val), "k0-" + i);
304 Assert.AreEqual (i, val, "k1-" + i);
309 static List<GCHandle> FillTable3 (ConditionalWeakTable <object,object> cwt) {
310 var handles = new List<GCHandle> ();
317 handles.Add (GCHandle.Alloc (a, GCHandleType.Pinned));
318 handles.Add (GCHandle.Alloc (b, GCHandleType.Pinned));
319 handles.Add (GCHandle.Alloc (k1, GCHandleType.Pinned));
320 handles.Add (GCHandle.Alloc (k2, GCHandleType.Pinned));
329 static void MakeObjMovable (List<GCHandle> handles)
331 for (int i = 0; i < handles.Count; ++i) {
332 object o = handles[i].Target;
334 handles[i] = GCHandle.Alloc (o, GCHandleType.Normal);
338 static void ForceMinor () {
339 for (int i = 0; i < 64000; ++i)
343 public void PromotedCwtPointingToYoungStuff () {
344 var cwt = new ConditionalWeakTable <object,object> ();
346 var handles = FillTable3 (cwt);
350 /*Be 100% sure it will be on the young gen*/
352 /*cwt array now will be on old gen*/
357 //Make them non pinned
358 MakeObjMovable (handles);
362 //Force a minor GC - this should cause
371 Assert.IsTrue (cwt.TryGetValue (handles[0].Target, out r1), "#1");
372 Assert.IsTrue (cwt.TryGetValue (handles[1].Target, out r2), "#2");
378 static object _lock1 = new object ();
379 static object _lock2 = new object ();
380 static int reachable = 0;
382 public class FinalizableLink {
384 ConditionalWeakTable <object,object> cwt;
387 public FinalizableLink (int id, object obj, ConditionalWeakTable <object,object> cwt) {
393 ~FinalizableLink () {
396 bool res = cwt.TryGetValue (this, out obj);
400 lock (_lock2) { Monitor.Pulse (_lock2); }
404 static void FillWithFinalizable (ConditionalWeakTable <object,object> cwt)
406 object a = new object ();
407 object b = new FinalizableLink (0, a, cwt);
411 for (int i = 1; i < 20; ++i) {
412 b = new FinalizableLink (i, b, cwt);
418 public void FinalizableObjectsThatRetainDeadKeys ()
421 var cwt = new ConditionalWeakTable <object,object> ();
422 ThreadStart dele = () => { FillWithFinalizable (cwt); };
423 var th = new Thread(dele);
429 Assert.AreEqual (0, reachable, "#1");
434 lock (_lock2) { Monitor.Wait (_lock2, 1000); }
436 Assert.AreEqual (20, reachable, "#1");