public void removeWhere_numElements(int numElementsToRemove) { var collection = new MyObservableCollection <int>(); List <int> removedItems = new List <int>(); collection.ItemDetached += (sender, oldItem, index) => { removedItems.Add(oldItem); }; int maxCount = 30; collection.AddRange(Enumerable.Range(0, maxCount)); int initCount = collection.Count; int multiple = 3; collection.RemoveWhere(val => { int remainder; int div = Math.DivRem(val, multiple, out remainder); return(remainder == 0); }, numElementsToRemove); Assert.IsTrue(collection.Count == initCount - numElementsToRemove); var xRemovedItems = Enumerable.Range(0, numElementsToRemove) .Select(val => val * multiple) .ToArray(); Assert.AreEqual(removedItems.Count, xRemovedItems.Length); Assert.IsTrue(!removedItems.Except(xRemovedItems).Any()); Assert.IsTrue(!xRemovedItems.Except(removedItems).Any()); Enumerable.Range(0, maxCount) .Except(xRemovedItems) .ForEach(val => Assert.IsTrue(collection.Contains(val))); xRemovedItems.ForEach(val => Assert.IsFalse(collection.Contains(val))); }
public void AddRangeTest2() { var collection = new MyObservableCollection <int>(); Enumerable.Range(10, 15).ForEach(val => collection.Add(val)); int initCount = collection.Count; int attachedCount = 0; collection.ItemAttached += (sender, newItem, index) => { Assert.IsTrue(ReferenceEquals(sender, collection)); attachedCount++; }; int addedCount = 0; collection.ItemAdded += (sender, newItem, index) => { Assert.IsTrue(ReferenceEquals(sender, collection)); addedCount++; }; var newItems = Enumerable.Range(0, 5).ToArray(); collection.AddRange(newItems); Assert.AreEqual(attachedCount, newItems.Length); Assert.AreEqual(addedCount, newItems.Length); Assert.AreEqual(newItems.Length, collection.Count - initCount); foreach (var item in newItems) { Assert.IsTrue(collection.Contains(item)); } }
public void AddRangeTest() { var collection = new MyObservableCollection <int>(); int attachedCount = 0; collection.ItemAttached += (sender, newItem, index) => { Assert.IsTrue(ReferenceEquals(sender, collection)); attachedCount++; }; int addedCount = 0; collection.ItemAdded += (sender, newItem, index) => { Assert.IsTrue(ReferenceEquals(sender, collection)); addedCount++; }; var newItems = Enumerable.Range(0, 5).ToArray(); collection.AddRange(newItems); Assert.AreEqual(attachedCount, newItems.Length); Assert.AreEqual(addedCount, newItems.Length); Assert.AreEqual(newItems.Length, collection.Count); foreach (var item in newItems) { Assert.IsTrue(collection.Contains(item)); } bool exceptionThrow = false; try { collection.AddRange(null); } catch (Exception) { exceptionThrow = true; } Assert.IsFalse(exceptionThrow); }