public void TestWeakSet() { var ws = new WeakSet <object>(); var o = new Object(); ws.Add(o); ws.Add(o); Assert.AreEqual(1, ws.Count); foreach (var item in ws) { Assert.AreEqual(item, o); } ws.Remove(o); Assert.AreEqual(0, ws.Count); AddObject(ws); AddObject(ws); Assert.AreEqual(2, ws.Count); GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); int iC = 0; foreach (var item in ws) { iC++; } Assert.AreEqual(0, iC); Assert.AreEqual(2, ws.Count); ws.WeakCleanup(); Assert.AreEqual(0, ws.Count); }
public void TestWeakSet() { var ws = new WeakSet<object>(); var o = new Object(); ws.Add(o); ws.Add(o); Assert.AreEqual(1, ws.Count); foreach (var item in ws) Assert.AreEqual(item, o); ws.Remove(o); Assert.AreEqual(0, ws.Count); AddObject(ws); AddObject(ws); Assert.AreEqual(2, ws.Count); GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); int iC = 0; foreach (var item in ws) iC++; Assert.AreEqual(0, iC); Assert.AreEqual(2, ws.Count); ws.WeakCleanup(); Assert.AreEqual(0, ws.Count); }