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 if (GC.MaxGeneration == 0) /*Boehm doesn't handle ephemerons */
185 var cwt = new ConditionalWeakTable <object,object> ();
186 List<object> keepAlive;
187 List<WeakReference> keys;
188 FillStuff (cwt, out keepAlive, out keys);
192 Assert.IsTrue (keys [0].IsAlive, "r0");
193 Assert.IsFalse (keys [1].IsAlive, "r1");
194 Assert.IsTrue (keys [2].IsAlive, "r2");
197 Assert.IsTrue (cwt.TryGetValue (keepAlive [0], out res), "ka0");
198 Assert.IsTrue (res is Link, "ka1");
200 Link link = res as Link;
201 Assert.IsTrue (cwt.TryGetValue (link.obj, out res), "ka2");
202 Assert.AreEqual ("str0", res, "ka3");
206 static List<WeakReference> FillWithNetwork (ConditionalWeakTable <object,object> cwt)
209 object[] keys = new object [K];
210 for (int i = 0; i < K; ++i)
211 keys[i] = new object ();
213 Random rand = new Random ();
215 /*produce a complex enough network of links*/
216 for (int i = 0; i < K; ++i)
217 cwt.Add (keys [i], new Link (keys [rand.Next (K)]));
219 var res = new List<WeakReference> ();
221 for (int i = 0; i < 10; ++i)
222 res.Add (new WeakReference (keys [rand.Next (K)]));
223 Array.Clear (keys, 0, keys.Length);
229 public void InsertStress () {
230 if (GC.MaxGeneration == 0) /*Boehm doesn't handle ephemerons */
232 var cwt = new ConditionalWeakTable <object,object> ();
234 var a = new object ();
235 var b = new object ();
237 cwt.Add (a, new object ());
238 cwt.Add (b, new object ());
240 List<WeakReference> res = null;
241 ThreadStart dele = () => { res = FillWithNetwork (cwt); };
242 var th = new Thread(dele);
249 for (int i = 0; i < res.Count; ++i)
250 Assert.IsFalse (res [i].IsAlive, "#r" + i);
253 static List<WeakReference> FillWithNetwork2 (ConditionalWeakTable <object,object>[] cwt) {
255 cwt[0] = new ConditionalWeakTable <object,object> ();
256 var res = FillWithNetwork (cwt[0]);
261 static void ForcePromotion () {
262 var o = new object[64000];
264 for (int i = 0; i < 64000; ++i)
268 static List<object> FillReachable (ConditionalWeakTable <object,object>[] cwt)
270 var res = new List<object> ();
271 for (int i = 0; i < 10; ++i) {
272 res.Add (new object ());
273 cwt[0].Add (res [i], i);
280 public void OldGenStress () {
281 if (GC.MaxGeneration == 0) /*Boehm doesn't handle ephemerons */
283 var cwt = new ConditionalWeakTable <object,object>[1];
284 List<object> k = null;
285 List<WeakReference> res, res2;
288 ThreadStart dele = () => {
289 res = FillWithNetwork2 (cwt);
291 k = FillReachable (cwt);
292 res2 = FillWithNetwork2 (cwt);
295 var th = new Thread(dele);
301 for (int i = 0; i < res.Count; ++i)
302 Assert.IsFalse (res [i].IsAlive, "#r0-" + i);
303 for (int i = 0; i < res2.Count; ++i)
304 Assert.IsFalse (res2 [i].IsAlive, "#r1-" + i);
306 for (int i = 0; i < k.Count; ++i) {
308 Assert.IsTrue (cwt[0].TryGetValue (k [i], out val), "k0-" + i);
309 Assert.AreEqual (i, val, "k1-" + i);
314 static List<GCHandle> FillTable3 (ConditionalWeakTable <object,object> cwt) {
315 var handles = new List<GCHandle> ();
322 handles.Add (GCHandle.Alloc (a, GCHandleType.Pinned));
323 handles.Add (GCHandle.Alloc (b, GCHandleType.Pinned));
324 handles.Add (GCHandle.Alloc (k1, GCHandleType.Pinned));
325 handles.Add (GCHandle.Alloc (k2, GCHandleType.Pinned));
334 static void MakeObjMovable (List<GCHandle> handles)
336 for (int i = 0; i < handles.Count; ++i) {
337 object o = handles[i].Target;
339 handles[i] = GCHandle.Alloc (o, GCHandleType.Normal);
343 static void ForceMinor () {
344 for (int i = 0; i < 64000; ++i)
348 public void PromotedCwtPointingToYoungStuff () {
349 var cwt = new ConditionalWeakTable <object,object> ();
351 var handles = FillTable3 (cwt);
355 /*Be 100% sure it will be on the young gen*/
357 /*cwt array now will be on old gen*/
362 //Make them non pinned
363 MakeObjMovable (handles);
367 //Force a minor GC - this should cause
376 Assert.IsTrue (cwt.TryGetValue (handles[0].Target, out r1), "#1");
377 Assert.IsTrue (cwt.TryGetValue (handles[1].Target, out r2), "#2");
383 static object _lock1 = new object ();
384 static object _lock2 = new object ();
385 static int reachable = 0;
387 public class FinalizableLink {
389 ConditionalWeakTable <object,object> cwt;
392 public FinalizableLink (int id, object obj, ConditionalWeakTable <object,object> cwt) {
398 ~FinalizableLink () {
401 bool res = cwt.TryGetValue (this, out obj);
405 lock (_lock2) { Monitor.Pulse (_lock2); }
409 static void FillWithFinalizable (ConditionalWeakTable <object,object> cwt)
411 object a = new object ();
412 object b = new FinalizableLink (0, a, cwt);
416 for (int i = 1; i < 20; ++i) {
417 b = new FinalizableLink (i, b, cwt);
423 public void FinalizableObjectsThatRetainDeadKeys ()
425 if (GC.MaxGeneration == 0) /*Boehm doesn't handle ephemerons */
428 var cwt = new ConditionalWeakTable <object,object> ();
429 ThreadStart dele = () => { FillWithFinalizable (cwt); };
430 var th = new Thread(dele);
436 Assert.AreEqual (0, reachable, "#1");
441 lock (_lock2) { Monitor.Wait (_lock2, 1000); }
443 Assert.AreEqual (20, reachable, "#1");