public void Remove_ObjectShouldGetRemoved() { var obj = new object(); var wrc = new WeakReferenceCollection<object>(); wrc.Add(obj); Assert.AreEqual(1, wrc.AliveItemsToList().Count, "Should have 1 item!"); wrc.Remove(obj); Assert.AreEqual(0, wrc.AliveItemsToList().Count, "Should have 0 item!"); }
public void AliveItemsToList_ShouldReturnAllAliveItems() { var list = new object[] { new object(), new object(), new object() }; var wrc = new WeakReferenceCollection<object>(); var obj1 = new object(); wrc.Add(obj1); foreach (object obj in list) { wrc.Add(obj); } var obj2 = new object(); wrc.Add(obj2); Assert.AreEqual(list.Length + 2, wrc.AliveItemsToList().Count, "Should have same number of items!"); obj1 = obj2 = null; GC.Collect(); GC.WaitForPendingFinalizers(); var aliveItems = wrc.AliveItemsToList(); Assert.AreEqual(list.Length, aliveItems.Count, "Should have 2 less items!"); Assert.AreEqual(list[0], aliveItems[0]); Assert.AreEqual(list[1], aliveItems[1]); Assert.AreEqual(list[2], aliveItems[2]); }
public void AliveItemsToList_ShouldReturnAllItems() { var list = new object[] {new object(), new object(), new object()}; var wrc = new WeakReferenceCollection<object>(); foreach (object obj in list) { wrc.Add(obj); } Assert.AreEqual(list.Length, wrc.AliveItemsToList().Count, "Should have same number of items!"); }
private void UpdateImportIndex() { var partsToIndex = _partsToIndex.AliveItemsToList(); _partsToIndex.Clear(); List <PartManager?> partsToUnindex = _partsToUnindex.AliveItemsToList() !; _partsToUnindex.Clear(); if (partsToIndex.Count == 0 && partsToUnindex.Count == 0) { return; } foreach (var partManager in partsToIndex) { var index = partsToUnindex.IndexOf(partManager); // If the same part is being added and removed we can ignore both if (index >= 0) { partsToUnindex[index] = null; } else { AddIndexEntries(partManager); } } foreach (var partManager in partsToUnindex) { if (partManager != null) { RemoveIndexEntries(partManager); } } }
public void AliveItemsToList_ShouldReturnEmpty() { var wrc = new WeakReferenceCollection<object>(); Assert.AreEqual(0, wrc.AliveItemsToList().Count, "Should have 0 items!"); }