public void RemoveRange_NotSequentialRemove3_FiresRemoveEvent() { var col = new ConcurrentObservableCollection <string>(new[] { "a", "b", "c", "d", "e", "f", "g" }); var received = new List <NotifyCollectionChangedEventArgs>(); col.CollectionChanged += (sender, args) => { if (args.Action == NotifyCollectionChangedAction.Remove) { received.Add(args); } }; col.RemoveRange(new[] { "b", "c", "a", "g" }); received.Count.Should().Be(3); received[0].OldItems.Should().BeEquivalentTo(new[] { "b", "c" }); received[0].OldStartingIndex.Should().Be(1); received[1].OldItems.Should().BeEquivalentTo(new[] { "a" }); received[1].OldStartingIndex.Should().Be(0); received[2].OldItems.Should().BeEquivalentTo(new[] { "g" }); received[2].OldStartingIndex.Should().Be(3); col.Should().BeEquivalentTo("d", "e", "f"); }
public void Write_ReplaceElement_ElementReplaced() { var col = new ConcurrentObservableCollection <string>(new[] { "a", "b", "c" }); col[2] = "z"; col.Should().BeEquivalentTo("a", "b", "z"); }
public void Write_RemoveNotExisting_DoesntFail() { var col = new ConcurrentObservableCollection <string>(new[] { "b", "c", "d" }); col.RemoveRange(new[] { "b", "X" }); col.Should().BeEquivalentTo("c", "d"); }
public void Write_RemoveRange_ElementsRemoved() { var col = new ConcurrentObservableCollection <string>(new[] { "b", "c", "d" }); col.RemoveRange(new[] { "b", "c" }); col.Should().BeEquivalentTo("d"); }
public void Write_InsertElement_ElementInserted() { var col = new ConcurrentObservableCollection <string>(new[] { "a", "b", "c" }); col.Insert(1, "x"); col.Should().BeEquivalentTo("a", "x", "b", "c"); }
public void Write_RemoveElementAtIndex_ElementRemoved() { var col = new ConcurrentObservableCollection <string>(new[] { "a", "b", "c" }); col.RemoveAt(1); col.Should().BeEquivalentTo("a", "c"); }
public void RemoveRange_AcquireRangeToRemoveUsingLinq_RangeRemovedWithoutExceptions() { var col = new ConcurrentObservableCollection <string>(new[] { "a", "b", "c" }); var select = col.Where(c => c.Equals("c")); col.RemoveRange(select); col.Should().BeEquivalentTo("a", "b"); }
public void Write_AddRange_ElementsAdded() { var col = new ConcurrentObservableCollection <string>(); var expected = new[] { "a", "b" }; col.AddRange(expected); col.Should().BeEquivalentTo(expected); }
public void Write_ComplexUpdateOperationFrom2ThreadsAndEnumerationInTheMiddle_CollectionUpdatedProperly() { var array = new int[100]; for (int i = 0; i < 100; i++) { array[i] = i; } var col = new ConcurrentObservableCollection <int>(array); var task1 = new Task(() => { for (int i = 100; i < 200; i++) { _testOutputHelper.WriteLine("Add {0}", i); col.Add(i); } }); var task2 = new Task(() => { for (int i = 0; i < 100; i++) { _testOutputHelper.WriteLine("Remove {0}", i); col.Remove(i); } }); var list = new List <int>(); var task3 = new Task(() => col.ForEach(c => { _testOutputHelper.WriteLine("Enumerating {0}", c); list.Add(c); })); task1.Start(); task2.Start(); task3.Start(); Task.WaitAll(task1, task2, task3); var expected = new int[100]; for (int i = 100; i < 200; i++) { expected[i - 100] = i; } col.Should().BeEquivalentTo(expected, "the collection should be updated properly"); list.Should().NotBeEmpty("the enumeration should find at least one element"); }
public void Write_ComplexOperation_CollectionUpdatedProperly() { var col = new ConcurrentObservableCollection <string>(new[] { "a", "b", "c" }); col.Add("d"); col.Remove("b"); col.Insert(0, "x"); col.AddRange(new[] { "z", "f", "y" }); col.RemoveAt(4); col.RemoveRange(new[] { "y", "c" }); col[2] = "p"; col.Should().BeEquivalentTo("x", "a", "p", "f"); }
public void RemoveRange_AllRemove_FiresResetEvent() { var col = new ConcurrentObservableCollection <string>(new[] { "a", "b", "c", "d", "e", "f" }); int received = 0; col.CollectionChanged += (sender, args) => { if (args.Action == NotifyCollectionChangedAction.Reset) { ++received; } }; col.RemoveRange(new[] { "a", "b", "c", "d", "e", "f" }); received.Should().Be(1); col.Should().BeEmpty(); }
public void RemoveRange_RemoveOneElement_FiresRemoveEvent() { var col = new ConcurrentObservableCollection <string>(new[] { "a", "b", "c", "d", "e", "f", "g" }); var received = new List <string>(); int oldIndex = -1; col.CollectionChanged += (sender, args) => { if (args.Action == NotifyCollectionChangedAction.Remove) { received.AddRange(args.OldItems.OfType <string>()); oldIndex = args.OldStartingIndex; } }; col.RemoveRange(new[] { "d" }); oldIndex.Should().Be(3); received.Should().BeEquivalentTo("d"); col.Should().BeEquivalentTo("a", "b", "c", "e", "f", "g"); }
public void AddRange_5SequentialAdds_CollectionChangeEventsAreReported() { var col = new ConcurrentObservableCollection <string>(new[] { "a" }); var argsList = new List <NotifyCollectionChangedEventArgs>(); col.CollectionChanged += (sender, args) => { argsList.Add(args); }; col.AddRange(new[] { "z1", "f1", "y1" }); col.AddRange(new[] { "z2", "f2", "y2" }); col.AddRange(new[] { "z3", "f3", "y3" }); col.AddRange(new[] { "z4", "f4", "y4" }); col.AddRange(new[] { "z5", "f5", "y5" }); argsList.Count(x => x.Action == NotifyCollectionChangedAction.Add).Should().Be(5); foreach (var args in argsList) { col.Skip(args.NewStartingIndex).Take(args.NewItems.Count).Should().BeEquivalentTo(args.NewItems.OfType <string>()); } col.Should().BeEquivalentTo("a", "z1", "f1", "y1", "z2", "f2", "y2", "z3", "f3", "y3", "z4", "f4", "y4", "z5", "f5", "y5"); }
public void Write_ComplexUpdateOperationFrom2Threads_CollectionUpdatedProperly() { var array = new int[100]; for (int i = 0; i < 100; i++) { array[i] = i; } var col = new ConcurrentObservableCollection <int>(array); var task1 = new Task(() => { for (int i = 100; i < 200; i++) { col.Add(i); } }); var task2 = new Task(() => { for (int i = 0; i < 100; i++) { col.Remove(i); } }); task1.Start(); task2.Start(); Task.WaitAll(task1, task2); var expected = new int[100]; for (int i = 100; i < 200; i++) { expected[i - 100] = i; } col.Should().BeEquivalentTo(expected); }