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) {
57 public override string ToString () {
64 public override string ToString () {
65 return "value-" + Foo;
71 public void GetValue () {
72 var cwt = new ConditionalWeakTable <object,object> ();
75 cwt.GetValue (null, k => null);
77 } catch (ArgumentNullException) {}
80 cwt.GetValue (20, null);
82 } catch (ArgumentNullException) {}
86 object val = cwt.GetValue (key, k => new Link (k));
87 Assert.IsTrue (val != null, "#2");
88 Assert.AreEqual (typeof (Link), val.GetType () , "#3");
90 Assert.AreEqual (val, cwt.GetValue (key, k => new object ()), "#4");
94 public void GetOrCreateValue () {
95 var cwt = new ConditionalWeakTable <object,object> ();
98 cwt.GetOrCreateValue (null);
100 } catch (ArgumentNullException) {}
104 object val = cwt.GetOrCreateValue (key);
105 Assert.IsTrue (val != null, "#2");
106 Assert.AreEqual (typeof (object), val.GetType () , "#3");
108 Assert.AreEqual (val, cwt.GetOrCreateValue (key), "#4");
110 var cwt2 = new ConditionalWeakTable <object, string> ();
112 cwt2.GetOrCreateValue (key);
114 } catch (MissingMethodException) {}
118 public void Remove () {
119 var cwt = new ConditionalWeakTable <object,object> ();
120 object c = new Key ();
127 } catch (ArgumentNullException) {}
130 Assert.IsFalse (cwt.Remove ("x"), "#1");
131 Assert.IsTrue (cwt.Remove (c), "#2");
132 Assert.IsFalse (cwt.Remove (c), "#3");
137 var cwt = new ConditionalWeakTable <object,object> ();
138 object c = new Key ();
140 cwt.Add (c, new Link (c));
143 cwt.Add (c, new Link (c));
145 } catch (ArgumentException) {}
147 cwt.Add ("zzz", null);//ok
150 cwt.Add (null, new Link (c));
152 } catch (ArgumentNullException) {}
156 public void TryGetValue () {
157 var cwt = new ConditionalWeakTable <object,object> ();
159 object c = new Key ();
164 cwt.TryGetValue (null, out res);
166 } catch (ArgumentNullException) {}
168 Assert.IsFalse (cwt.TryGetValue ("foo", out res), "#1");
169 Assert.IsTrue (cwt.TryGetValue (c, out res), "#2");
170 Assert.AreEqual ("foo", res, "#3");
174 static void FillStuff (ConditionalWeakTable <object,object> cwt,
175 out List<object> keepAlive, out List<WeakReference> keys) {
177 keepAlive = new List<object> ();
178 keys = new List<WeakReference> ();
180 object a = new Key ();
181 object b = new Key ();
182 object c = new Key ();
184 cwt.Add (a, new string ("str0".ToCharArray ()));
186 cwt.Add (c, new Link (a));
189 keys.Add (new WeakReference(a));
190 keys.Add (new WeakReference(b));
191 keys.Add (new WeakReference(c));
195 public void Reachability () {
196 if (GC.MaxGeneration == 0) /*Boehm doesn't handle ephemerons */
197 Assert.Ignore ("Not working on Boehm.");
198 var cwt = new ConditionalWeakTable <object,object> ();
199 List<object> keepAlive = null;
200 List<WeakReference> keys = null;
201 Thread t = new Thread (delegate () {
202 FillStuff (cwt, out keepAlive, out keys);
209 Assert.IsTrue (keys [0].IsAlive, "r0");
210 Assert.IsFalse (keys [1].IsAlive, "r1");
211 Assert.IsTrue (keys [2].IsAlive, "r2");
214 Assert.IsTrue (cwt.TryGetValue (keepAlive [0], out res), "ka0");
215 Assert.IsTrue (res is Link, "ka1");
217 Link link = res as Link;
218 Assert.IsTrue (cwt.TryGetValue (link.obj, out res), "ka2");
219 Assert.AreEqual ("str0", res, "ka3");
223 static List<WeakReference> FillWithNetwork (ConditionalWeakTable <object,object> cwt)
226 object[] keys = new object [K];
227 for (int i = 0; i < K; ++i)
228 keys[i] = new object ();
230 Random rand = new Random ();
232 /*produce a complex enough network of links*/
233 for (int i = 0; i < K; ++i)
234 cwt.Add (keys [i], new Link (keys [rand.Next (K)]));
236 var res = new List<WeakReference> ();
238 for (int i = 0; i < 10; ++i)
239 res.Add (new WeakReference (keys [rand.Next (K)]));
240 Array.Clear (keys, 0, keys.Length);
246 public void InsertStress () {
247 if (GC.MaxGeneration == 0) /*Boehm doesn't handle ephemerons */
248 Assert.Ignore ("Not working on Boehm.");
249 var cwt = new ConditionalWeakTable <object,object> ();
251 var a = new object ();
252 var b = new object ();
254 cwt.Add (a, new object ());
255 cwt.Add (b, new object ());
257 List<WeakReference> res = null;
258 ThreadStart dele = () => { res = FillWithNetwork (cwt); };
259 var th = new Thread(dele);
266 for (int i = 0; i < res.Count; ++i)
267 Assert.IsFalse (res [i].IsAlive, "#r" + i);
270 static List<WeakReference> FillWithNetwork2 (ConditionalWeakTable <object,object>[] cwt) {
272 cwt[0] = new ConditionalWeakTable <object,object> ();
273 var res = FillWithNetwork (cwt[0]);
278 static void ForcePromotion () {
279 var o = new object[64000];
281 for (int i = 0; i < 64000; ++i)
285 static List<object> FillReachable (ConditionalWeakTable <object,object>[] cwt)
287 var res = new List<object> ();
288 for (int i = 0; i < 10; ++i) {
289 res.Add (new object ());
290 cwt[0].Add (res [i], i);
297 public void OldGenStress () {
298 if (GC.MaxGeneration == 0) /*Boehm doesn't handle ephemerons */
299 Assert.Ignore ("Not working on Boehm.");
300 var cwt = new ConditionalWeakTable <object,object>[1];
301 List<object> k = null;
302 List<WeakReference> res, res2;
305 ThreadStart dele = () => {
306 res = FillWithNetwork2 (cwt);
308 k = FillReachable (cwt);
309 res2 = FillWithNetwork2 (cwt);
312 var th = new Thread(dele);
318 for (int i = 0; i < res.Count; ++i)
319 Assert.IsFalse (res [i].IsAlive, "#r0-" + i);
320 for (int i = 0; i < res2.Count; ++i)
321 Assert.IsFalse (res2 [i].IsAlive, "#r1-" + i);
323 for (int i = 0; i < k.Count; ++i) {
325 Assert.IsTrue (cwt[0].TryGetValue (k [i], out val), "k0-" + i);
326 Assert.AreEqual (i, val, "k1-" + i);
331 static List<GCHandle> FillTable3 (ConditionalWeakTable <object,object> cwt) {
332 var handles = new List<GCHandle> ();
339 handles.Add (GCHandle.Alloc (a, GCHandleType.Pinned));
340 handles.Add (GCHandle.Alloc (b, GCHandleType.Pinned));
341 handles.Add (GCHandle.Alloc (k1, GCHandleType.Pinned));
342 handles.Add (GCHandle.Alloc (k2, GCHandleType.Pinned));
351 static void MakeObjMovable (List<GCHandle> handles)
353 for (int i = 0; i < handles.Count; ++i) {
354 object o = handles[i].Target;
356 handles[i] = GCHandle.Alloc (o, GCHandleType.Normal);
360 static void ForceMinor () {
361 for (int i = 0; i < 64000; ++i)
365 public void PromotedCwtPointingToYoungStuff () {
366 var cwt = new ConditionalWeakTable <object,object> ();
368 var handles = FillTable3 (cwt);
372 /*Be 100% sure it will be on the young gen*/
374 /*cwt array now will be on old gen*/
379 //Make them non pinned
380 MakeObjMovable (handles);
384 //Force a minor GC - this should cause
393 Assert.IsTrue (cwt.TryGetValue (handles[0].Target, out r1), "#1");
394 Assert.IsTrue (cwt.TryGetValue (handles[1].Target, out r2), "#2");
400 static object _lock1 = new object ();
401 static object _lock2 = new object ();
402 static int reachable = 0;
404 public class FinalizableLink {
405 // The sole purpose of this object is to keep a reference to another object, so it is fine to not use it.
406 #pragma warning disable 414
409 #pragma warning restore 414
410 ConditionalWeakTable <object,object> cwt;
412 public FinalizableLink (int id, object obj, ConditionalWeakTable <object,object> cwt) {
418 ~FinalizableLink () {
421 bool res = cwt.TryGetValue (this, out obj);
425 lock (_lock2) { Monitor.Pulse (_lock2); }
429 static void FillWithFinalizable (ConditionalWeakTable <object,object> cwt)
431 object a = new object ();
432 object b = new FinalizableLink (0, a, cwt);
436 for (int i = 1; i < 20; ++i) {
437 b = new FinalizableLink (i, b, cwt);
443 public void FinalizableObjectsThatRetainDeadKeys ()
445 if (GC.MaxGeneration == 0) /*Boehm doesn't handle ephemerons */
446 Assert.Ignore ("Not working on Boehm.");
448 var cwt = new ConditionalWeakTable <object,object> ();
449 ThreadStart dele = () => { FillWithFinalizable (cwt); };
450 var th = new Thread(dele);
456 Assert.AreEqual (0, reachable, "#1");
461 lock (_lock2) { Monitor.Wait (_lock2, 1000); }
463 Assert.AreEqual (20, reachable, "#1");
467 public void OldGenKeysMakeNewGenObjectsReachable ()
469 if (GC.MaxGeneration == 0) /*Boehm doesn't handle ephemerons */
470 Assert.Ignore ("Not working on Boehm.");
471 ConditionalWeakTable<object, Val> table = new ConditionalWeakTable<object, Val>();
472 List<Key> keys = new List<Key>();
475 // This list references all keys for the duration of the program, so none
476 // should be collected ever.
478 for (int x = 0; x < 1000; x++)
479 keys.Add (new Key () { Foo = x });
481 for (int i = 0; i < 1000; ++i) {
482 // Insert all keys into the ConditionalWeakTable
483 foreach (var key in keys)
484 table.Add (key, new Val () { Foo = key.Foo });
486 // Look up all keys to verify that they are still there
488 foreach (var key in keys)
489 Assert.IsTrue (table.TryGetValue (key, out val), "#1-" + i + "-k-" + key);
491 // Remove all keys from the ConditionalWeakTable
492 foreach (var key in keys)
493 Assert.IsTrue (table.Remove (key), "#2-" + i + "-k-" + key);