public void ResetTest() { ConcurrentObservableSortedCollection <int> subject = new ConcurrentObservableSortedCollection <int>(); // Use a fixed seed for consistency in results Random random = new Random(2); for (int item = 0; item < _itemsPerCollection; ++item) { // Ensure we have some duplicates by picking a random number // less than half the number of items. subject.Add(random.Next(_itemsPerCollection / 2)); } subject.Reset(_testCollections.SelectMany(x => x).ToList()); // Compare test subject with expected result Assert.AreEqual(subject.Count, _sortedCollection.Count); bool itemsEqual = _sortedCollection .Zip(subject, (a, b) => a == b) .All(b => b); Assert.IsTrue(itemsEqual); // Compare collectionView var view = subject.CollectionView; Assert.AreEqual(view.Count, _sortedCollection.Count); bool viewItemsEqual = _sortedCollection .Zip(view, (a, b) => a == b) .All(b => b); Assert.IsTrue(viewItemsEqual); }
public void AddTest() { ConcurrentObservableSortedCollection <int> subject = new ConcurrentObservableSortedCollection <int>(); using (var benchmark = new BenchmarkIt("Adding items to sorted collection")) { // Create test subject // Populate test subject _testCollections.AsParallel().ForAll(collection => { foreach (var item in collection) { subject.Add(item); } }); } // Compare test subject with expected result Assert.AreEqual(subject.Count, _sortedCollection.Count); bool itemsEqual = _sortedCollection .Zip(subject, (a, b) => a == b) .All(b => b); Assert.IsTrue(itemsEqual); // Compare collectionView var view = subject.CollectionView; Assert.AreEqual(view.Count, _sortedCollection.Count); bool viewItemsEqual = _sortedCollection .Zip(view, (a, b) => a == b) .All(b => b); Assert.IsTrue(viewItemsEqual); }
public void Test_ConcurrentObservableSortedCollection_RemoveRange_ByIndex() { var initial = Enumerable.Range(0, 100).ToList(); var startIndex = 50; var removeCount = 40; var collection = new ConcurrentObservableSortedCollection <int>(); collection.AddRange(initial); Assert.AreEqual(100, collection.Count); // Record all the collection changed events List <(object, NotifyCollectionChangedEventArgs)> returnedList = new List <(object, NotifyCollectionChangedEventArgs)>(); collection.CollectionChanged += (s, e) => returnedList.Add((s, e)); collection.RemoveRange(startIndex, removeCount); // Check just one collection changed event was fired Assert.AreEqual(1, returnedList.Count); (var returnedObject, var returnedArgs) = returnedList[0]; Assert.IsNotNull(returnedObject); Assert.IsNotNull(returnedArgs); Assert.AreEqual(returnedObject, collection); Assert.AreEqual(NotifyCollectionChangedAction.Remove, returnedArgs.Action); Assert.AreEqual(startIndex, returnedArgs.OldStartingIndex); Assert.IsNull(returnedArgs.NewItems); Assert.IsNotNull(returnedArgs.OldItems); Assert.AreEqual(removeCount, returnedArgs.OldItems.Count); var toRemove = initial.Skip(startIndex).Take(removeCount).ToList(); Assert.IsTrue(CollectionsAreEqual(toRemove, returnedArgs.OldItems)); }
public void Test_ConcurrentObservableSortedCollection_InsertRange() { var initial = Enumerable.Range(0, 100).ToList(); var toAdd = Enumerable.Range(100, 100).ToList(); var startIndex = 50; var collection = new ConcurrentObservableSortedCollection <int>(); collection.AddRange(initial); Assert.AreEqual(100, collection.Count); // Record all the collection changed events List <(object, NotifyCollectionChangedEventArgs)> returnedList = new List <(object, NotifyCollectionChangedEventArgs)>(); collection.CollectionChanged += (s, e) => returnedList.Add((s, e)); collection.InsertRange(startIndex, toAdd); // Check just one collection changed event was fired Assert.AreEqual(1, returnedList.Count); (var returnedObject, var returnedArgs) = returnedList[0]; Assert.AreEqual(returnedObject, collection); Assert.AreEqual(NotifyCollectionChangedAction.Add, returnedArgs.Action); // Sorted collection doesn't add at the index, so it should be -1 Assert.AreEqual(-1, returnedArgs.NewStartingIndex); Assert.IsNotNull(returnedArgs.NewItems); Assert.IsNull(returnedArgs.OldItems); Assert.AreEqual(toAdd.Count(), returnedArgs.NewItems.Count); Assert.IsTrue(CollectionsAreEqual(toAdd, returnedArgs.NewItems)); }
public void Test_ConcurrentObservableSortedCollection_Clear() { var initial = Enumerable.Range(0, 100).ToList(); var collection = new ConcurrentObservableSortedCollection <int>(); collection.AddRange(initial); Assert.AreEqual(100, collection.Count); // Record all the collection changed events List <(object, NotifyCollectionChangedEventArgs)> returnedList = new List <(object, NotifyCollectionChangedEventArgs)>(); collection.CollectionChanged += (s, e) => returnedList.Add((s, e)); collection.Clear(); // Check just one collection changed event was fired Assert.AreEqual(1, returnedList.Count); (var returnedObject, var returnedArgs) = returnedList[0]; Assert.IsNotNull(returnedObject); Assert.IsNotNull(returnedArgs); Assert.AreEqual(0, collection.Count); Assert.AreEqual(returnedObject, collection); Assert.AreEqual(NotifyCollectionChangedAction.Remove, returnedArgs.Action); Assert.IsNull(returnedArgs.NewItems); Assert.IsNotNull(returnedArgs.OldItems); Assert.AreEqual(initial.Count(), returnedArgs.OldItems.Count); Assert.IsTrue(CollectionsAreEqual(initial, returnedArgs.OldItems)); }
public void IndexTest() { // This test populates the subject with one collection, and then overwrites it with another collection // however as the subject gets sorted on write the 2nd collection won't be equal to the subject at the // end, all we can test for is that it is sorted ConcurrentObservableSortedCollection <int> subject = new ConcurrentObservableSortedCollection <int>(); // Get test collections var sourceCollection1 = _testCollections[0]; var sourceCollection2 = _testCollections[1]; // Check the source collections aren't sorted Assert.IsFalse(IsSorted(sourceCollection1)); Assert.IsFalse(IsSorted(sourceCollection2)); // Check the source collections are different bool sourceCollectionsEqual = sourceCollection1 .Zip(sourceCollection2, (a, b) => a == b) .All(b => b); Assert.IsFalse(sourceCollectionsEqual); // Populate subject subject.AddRange(sourceCollection1); Assert.IsTrue(IsSorted(subject)); // Check the counts are all ok Assert.AreEqual(sourceCollection1.Count, subject.Count); Assert.AreEqual(sourceCollection2.Count, subject.Count); int count = subject.Count; // Overwrite items from the second collection, checking counts all the way for (int i = 0; i < count; ++i) { subject[i] = sourceCollection2[i]; Assert.AreEqual(count, subject.Count); } Assert.IsTrue(IsSorted(subject)); // Now try overwriting with a sorted collection where all // the values are less than what's currently in the collection. // The resulting collection should be the same as the sorted // source collection var sourceCollection2Sorted = sourceCollection2.Select(x => x - _itemsPerCollection).OrderBy(x => x).ToList(); for (int i = 0; i < count; ++i) { subject[i] = sourceCollection2Sorted[i]; Assert.AreEqual(count, subject.Count); } Assert.IsTrue(IsSorted(subject)); bool viewItemsEqual = sourceCollection2Sorted .Zip(subject, (a, b) => a == b) .All(b => b); Assert.IsTrue(viewItemsEqual); }
public void Test_ConcurrentObservableSortedCollection_Reset() { var initial = Enumerable.Range(0, 100).ToList(); var toAdd = Enumerable.Range(100, 100).ToList(); var collection = new ConcurrentObservableSortedCollection <int>(); collection.AddRange(initial); Assert.AreEqual(100, collection.Count); // Record all the collection changed events List <(object, NotifyCollectionChangedEventArgs)> returnedList = new List <(object, NotifyCollectionChangedEventArgs)>(); collection.CollectionChanged += (s, e) => returnedList.Add((s, e)); collection.Reset(toAdd); // Check two collection changed events were fired Assert.AreEqual(2, returnedList.Count); (var returnedObject0, var returnedArgs0) = returnedList[0]; (var returnedObject1, var returnedArgs1) = returnedList[1]; Assert.IsNotNull(returnedObject0); Assert.IsNotNull(returnedArgs0); Assert.IsNotNull(returnedObject1); Assert.IsNotNull(returnedArgs1); Assert.AreEqual(returnedObject0, collection); Assert.AreEqual(returnedObject1, collection); Assert.AreEqual(NotifyCollectionChangedAction.Remove, returnedArgs0.Action); Assert.AreEqual(NotifyCollectionChangedAction.Add, returnedArgs1.Action); Assert.IsNull(returnedArgs0.NewItems); Assert.IsNotNull(returnedArgs0.OldItems); Assert.AreEqual(initial.Count(), returnedArgs0.OldItems.Count); Assert.IsTrue(CollectionsAreEqual(initial, returnedArgs0.OldItems)); Assert.IsNull(returnedArgs1.OldItems); Assert.IsNotNull(returnedArgs1.NewItems); Assert.AreEqual(toAdd.Count(), returnedArgs1.NewItems.Count); Assert.IsTrue(CollectionsAreEqual(toAdd, returnedArgs1.NewItems)); }