diff --git a/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs b/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs index f74a2c8c7bef..7dac6ec2f3f7 100644 --- a/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs +++ b/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs @@ -27,6 +27,9 @@ public class ObservableCollection : Collection, INotifyCollectionChanged, [NonSerialized] private int _blockReentrancyCount; + [NonSerialized] + private bool _skipRaisingEvents; + /// /// Initializes a new instance of ObservableCollection that is empty and has default initial capacity. /// @@ -121,9 +124,95 @@ protected override void RemoveItem(int index) base.RemoveItem(index); - OnCountPropertyChanged(); - OnIndexerPropertyChanged(); - OnCollectionChanged(NotifyCollectionChangedAction.Remove, removedItem, index); + if (!_skipRaisingEvents) + { + OnCountPropertyChanged(); + OnIndexerPropertyChanged(); + OnCollectionChanged(NotifyCollectionChangedAction.Remove, removedItem, index); + } + } + + /// + /// Called by base class Collection<T> when a count of items is removed from the list; + /// raises a CollectionChanged event to any listeners. + /// + protected override void RemoveItemsRange(int index, int count) + { + CheckReentrancy(); + + T[] removedItems = null; + + bool ignore = _skipRaisingEvents; + if (!ignore) + { + _skipRaisingEvents = true; + + if (count > 0) + { + removedItems = new T[count]; + for (int i = 0; i < count; i++) + { + removedItems[i] = this[index + i]; + } + } + } + + try + { + base.RemoveItemsRange(index, count); + } + finally + { + if (!ignore) + { + _skipRaisingEvents = false; + } + } + + if (count > 0 && !_skipRaisingEvents) + { + OnCountPropertyChanged(); + OnIndexerPropertyChanged(); + OnCollectionChanged(NotifyCollectionChangedAction.Remove, removedItems, index); + } + } + + /// + /// Called by base class Collection<T> when a collection of items is added to list; + /// raises a CollectionChanged event to any listeners. + /// + protected override void ReplaceItemsRange(int index, int count, IEnumerable collection) + { + CheckReentrancy(); + + _skipRaisingEvents = true; + + T[] itemsToReplace = new T[count - index]; + for (int i = index; i < count; i++) + { + itemsToReplace[i] = this[i]; + } + + try + { + base.ReplaceItemsRange(index, count, collection); + } + finally + { + _skipRaisingEvents = false; + } + + if (!_skipRaisingEvents) + { + IList newItems = collection is IList list ? list : new List(collection); + + if (newItems.Count > 0) + { + OnCountPropertyChanged(); + OnIndexerPropertyChanged(); + OnCollectionChanged(NotifyCollectionChangedAction.Replace, itemsToReplace, newItems, index); + } + } } /// @@ -135,9 +224,51 @@ protected override void InsertItem(int index, T item) CheckReentrancy(); base.InsertItem(index, item); - OnCountPropertyChanged(); - OnIndexerPropertyChanged(); - OnCollectionChanged(NotifyCollectionChangedAction.Add, item, index); + if (!_skipRaisingEvents) + { + OnCountPropertyChanged(); + OnIndexerPropertyChanged(); + OnCollectionChanged(NotifyCollectionChangedAction.Add, item, index); + } + } + + /// + /// Called by base class Collection<T> when a collection of items is added to list; + /// raises a CollectionChanged event to any listeners. + /// + protected override void InsertItemsRange(int index, IEnumerable collection) + { + CheckReentrancy(); + + bool ignore = _skipRaisingEvents; + if (!ignore) + { + _skipRaisingEvents = true; + } + + try + { + base.InsertItemsRange(index, collection); + } + finally + { + if (!ignore) + { + _skipRaisingEvents = false; + } + } + + if (!_skipRaisingEvents) + { + IList newItems = collection is IList list ? list : new List(collection); + + if (newItems.Count > 0) + { + OnCountPropertyChanged(); + OnIndexerPropertyChanged(); + OnCollectionChanged(NotifyCollectionChangedAction.Add, newItems, index); + } + } } /// @@ -265,6 +396,14 @@ private void OnCollectionChanged(NotifyCollectionChangedAction action, object it OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, item, index)); } + /// + /// Helper to raise CollectionChanged event to any listeners + /// + private void OnCollectionChanged(NotifyCollectionChangedAction action, IList items, int index) + { + OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, items, index)); + } + /// /// Helper to raise CollectionChanged event to any listeners /// @@ -281,6 +420,14 @@ private void OnCollectionChanged(NotifyCollectionChangedAction action, object ol OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, newItem, oldItem, index)); } + /// + /// Helper to raise CollectionChanged event to any listeners + /// + private void OnCollectionChanged(NotifyCollectionChangedAction action, IList oldItems, IList newItems, int index) + { + OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, newItems, oldItems, index)); + } + /// /// Helper to raise CollectionChanged event with action == Reset to any listeners /// diff --git a/src/System.ObjectModel/tests/Configurations.props b/src/System.ObjectModel/tests/Configurations.props index 2bc19ff4170e..63b0f12fe5de 100644 --- a/src/System.ObjectModel/tests/Configurations.props +++ b/src/System.ObjectModel/tests/Configurations.props @@ -3,6 +3,8 @@ netstandard; netcoreapp; + uap; + uapaot; \ No newline at end of file diff --git a/src/System.ObjectModel/tests/ObservableCollection/ObservableCollection_MethodTests.netcoreapp.cs b/src/System.ObjectModel/tests/ObservableCollection/ObservableCollection_MethodTests.netcoreapp.cs new file mode 100644 index 000000000000..f15d3c3c463f --- /dev/null +++ b/src/System.ObjectModel/tests/ObservableCollection/ObservableCollection_MethodTests.netcoreapp.cs @@ -0,0 +1,269 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Linq; +using Xunit; + +namespace System.Collections.ObjectModel.Tests +{ + public partial class ObservableCollection_MethodTests + { + [Fact] + public static void InsertRange_NotifyCollectionChanged_Beginning_Test() + { + int[] dataToInsert = new int[] { 1, 2, 3, 4, 5 }; + int[] initialData = new int[] { 10, 11, 12, 13 }; + int eventCounter = 0; + ObservableCollection collection = new ObservableCollection(initialData); + collection.CollectionChanged += (o, e) => eventCounter++; + + collection.InsertRange(0, dataToInsert); + + Assert.Equal(dataToInsert.Length + initialData.Length, collection.Count); + Assert.Equal(1, eventCounter); + + int[] collectionAssertion = collection.ToArray(); + Assert.Equal(dataToInsert, collectionAssertion.AsSpan(0, 5).ToArray()); + Assert.Equal(initialData, collectionAssertion.AsSpan(5).ToArray()); + } + + [Fact] + public static void InsertRange_NotifyCollectionChanged_Middle_Test() + { + int[] dataToInsert = new int[] { 1, 2, 3, 4, 5 }; + int[] initialData = new int[] { 10, 11, 12, 13 }; + int eventCounter = 0; + ObservableCollection collection = new ObservableCollection(initialData); + collection.CollectionChanged += (o, e) => eventCounter++; + + collection.InsertRange(2, dataToInsert); + + Assert.Equal(dataToInsert.Length + initialData.Length, collection.Count); + Assert.Equal(1, eventCounter); + + int[] collectionAssertion = collection.ToArray(); + Assert.Equal(initialData.AsSpan(0, 2).ToArray(), collectionAssertion.AsSpan(0, 2).ToArray()); + Assert.Equal(dataToInsert, collectionAssertion.AsSpan(2, 5).ToArray()); + Assert.Equal(initialData.AsSpan(2, 2).ToArray(), collectionAssertion.AsSpan(7, 2).ToArray()); + } + + [Fact] + public static void InsertRange_NotifyCollectionChanged_End_Test() + { + int[] dataToInsert = new int[] { 1, 2, 3, 4, 5 }; + int[] initialData = new int[] { 10, 11, 12, 13 }; + int eventCounter = 0; + ObservableCollection collection = new ObservableCollection(initialData); + collection.CollectionChanged += (o, e) => eventCounter++; + + collection.InsertRange(4, dataToInsert); + + Assert.Equal(dataToInsert.Length + initialData.Length, collection.Count); + Assert.Equal(1, eventCounter); + + int[] collectionAssertion = collection.ToArray(); + Assert.Equal(initialData, collectionAssertion.AsSpan(0, 4).ToArray()); + Assert.Equal(dataToInsert, collectionAssertion.AsSpan(4).ToArray()); + } + + [Fact] + public static void AddRange_NotifyCollectionChanged_Test() + { + int[] dataToInsert = new int[] { 1, 2, 3, 4, 5 }; + int[] initialData = new int[] { 10, 11, 12, 13 }; + int eventCounter = 0; + ObservableCollection collection = new ObservableCollection(initialData); + collection.CollectionChanged += (o, e) => eventCounter++; + + collection.AddRange(dataToInsert); + + Assert.Equal(dataToInsert.Length + initialData.Length, collection.Count); + Assert.Equal(1, eventCounter); + + int[] collectionAssertion = collection.ToArray(); + Assert.Equal(initialData, collectionAssertion.AsSpan(0, 4).ToArray()); + Assert.Equal(dataToInsert, collectionAssertion.AsSpan(4).ToArray()); + } + + [Fact] + public static void AddRange_NotifyCollectionChanged_EventArgs_Test() + { + int[] dataToAdd = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }; + int[] actualDataAdded = new int[0]; + ObservableCollection collection = new ObservableCollection(); + collection.CollectionChanged += (o, e) => actualDataAdded = e.NewItems.Cast().ToArray(); + + collection.AddRange(dataToAdd); + + Assert.Equal(dataToAdd, actualDataAdded); + } + + [Fact] + public static void InsertRange_NotifyCollectionChanged_EventArgs_Test() + { + int[] dataToAdd = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }; + int[] actualDataAdded = new int[0]; + ObservableCollection collection = new ObservableCollection(); + collection.CollectionChanged += (o, e) => actualDataAdded = e.NewItems.Cast().ToArray(); + + collection.InsertRange(0, dataToAdd); + + Assert.Equal(dataToAdd, actualDataAdded); + } + + [Fact] + public static void InsertRange_NotifyCollectionChanged_EventArgs_Middle_Test() + { + int[] dataToAdd = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }; + int[] actualDataAdded = new int[0]; + ObservableCollection collection = new ObservableCollection(); + for (int i = 0; i < 4; i++) + { + collection.Add(i); + } + + collection.CollectionChanged += (o, e) => actualDataAdded = e.NewItems.Cast().ToArray(); + collection.InsertRange(2, dataToAdd); + + Assert.Equal(dataToAdd, actualDataAdded); + } + + [Fact] + public static void InsertRange_NotifyCollectionChanged_EventArgs_End_Test() + { + int[] dataToAdd = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }; + int[] actualDataAdded = new int[0]; + ObservableCollection collection = new ObservableCollection(); + for (int i = 0; i < 4; i++) + { + collection.Add(i); + } + + collection.CollectionChanged += (o, e) => actualDataAdded = e.NewItems.Cast().ToArray(); + collection.InsertRange(4, dataToAdd); + + Assert.Equal(dataToAdd, actualDataAdded); + } + + [Fact] + public static void RemoveRange_NotifyCollectionChanged_FirstTwo_Test() + { + int[] initialData = new int[] { 10, 11, 12, 13 }; + int itemsToRemove = 2; + int eventCounter = 0; + ObservableCollection collection = new ObservableCollection(initialData); + collection.CollectionChanged += (o, e) => eventCounter++; + + collection.RemoveRange(0, itemsToRemove); + + Assert.Equal(initialData.Length - itemsToRemove, collection.Count); + Assert.Equal(1, eventCounter); + Assert.Equal(initialData.AsSpan(2).ToArray(), collection.ToArray()); + } + + [Fact] + public static void RemoveRange_NotifyCollectionChanged_MiddleTwo_Test() + { + int[] initialData = new int[] { 10, 11, 12, 13 }; + int itemsToRemove = 2; + int eventCounter = 0; + ObservableCollection collection = new ObservableCollection(initialData); + collection.CollectionChanged += (o, e) => eventCounter++; + + collection.RemoveRange(1, itemsToRemove); + + Assert.Equal(initialData.Length - itemsToRemove, collection.Count); + Assert.Equal(1, eventCounter); + Assert.Equal(initialData[0], collection[0]); + Assert.Equal(initialData[3], collection[1]); + } + + [Fact] + public static void RemoveRange_NotifyCollectionChanged_LastTwo_Test() + { + int[] initialData = new int[] { 10, 11, 12, 13 }; + int itemsToRemove = 2; + int eventCounter = 0; + ObservableCollection collection = new ObservableCollection(initialData); + collection.CollectionChanged += (o, e) => eventCounter++; + + collection.RemoveRange(2, itemsToRemove); + + Assert.Equal(initialData.Length - itemsToRemove, collection.Count); + Assert.Equal(1, eventCounter); + Assert.Equal(initialData.AsSpan(0, 2).ToArray(), collection.ToArray()); + } + + [Fact] + public static void RemoveRange_NotifyCollectionChanged_IntMaxValueOverflow_Test() + { + int count = 500; + ObservableCollection collection = new ObservableCollection(); + for (int i = 0; i < count; i++) + { + collection.Add(i); + } + + Assert.Throws(() => collection.RemoveRange(collection.Count - 2, int.MaxValue)); + } + + [Fact] + public static void RemoveRange_NotifyCollectionChanged_EventArgs_IndexOfZero_Test() + { + int[] initialData = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }; + int[] actualDataRemoved = new int[0]; + int numberOfItemsToRemove = 4; + ObservableCollection collection = new ObservableCollection(); + foreach (int item in initialData) + { + collection.Add(item); + } + + collection.CollectionChanged += (o, e) => actualDataRemoved = e.OldItems.Cast().ToArray(); + collection.RemoveRange(0, numberOfItemsToRemove); + + Assert.Equal(initialData.Length - numberOfItemsToRemove, collection.Count); + Assert.Equal(initialData.AsSpan(0, numberOfItemsToRemove).ToArray(), actualDataRemoved); + } + + [Fact] + public static void RemoveRange_NotifyCollectionChanged_EventArgs_IndexMiddle_Test() + { + int[] initialData = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }; + int[] actualDataRemoved = new int[0]; + int numberOfItemsToRemove = 4; + int startIndex = 3; + ObservableCollection collection = new ObservableCollection(); + foreach (int item in initialData) + { + collection.Add(item); + } + + collection.CollectionChanged += (o, e) => actualDataRemoved = e.OldItems.Cast().ToArray(); + collection.RemoveRange(startIndex, numberOfItemsToRemove); + + Assert.Equal(initialData.Length - numberOfItemsToRemove, collection.Count); + Assert.Equal(initialData.AsSpan(startIndex, numberOfItemsToRemove).ToArray(), actualDataRemoved); + } + + [Fact] + public static void ReplaceRange_NotifyCollectionChanged_Test() + { + int[] initialData = new int[] { 10, 11, 12, 13 }; + int[] dataToReplace = new int[] { 3, 8 }; + int eventCounter = 0; + ObservableCollection collection = new ObservableCollection(initialData); + collection.CollectionChanged += (o, e) => eventCounter++; + + collection.ReplaceRange(0, 2, dataToReplace); + + Assert.Equal(initialData.Length, collection.Count); + Assert.Equal(1, eventCounter); + + int[] collectionAssertion = collection.ToArray(); + Assert.Equal(dataToReplace, collectionAssertion.AsSpan(0, 2).ToArray()); + Assert.Equal(initialData.AsSpan(2, 2).ToArray(), collectionAssertion.AsSpan(2, 2).ToArray()); + } + } +} diff --git a/src/System.ObjectModel/tests/ObservableCollection/ObservableCollection_MethodsTest.cs b/src/System.ObjectModel/tests/ObservableCollection/ObservableCollection_MethodsTest.cs index 6f03a97cc190..bb291a98a3e4 100644 --- a/src/System.ObjectModel/tests/ObservableCollection/ObservableCollection_MethodsTest.cs +++ b/src/System.ObjectModel/tests/ObservableCollection/ObservableCollection_MethodsTest.cs @@ -14,7 +14,7 @@ namespace System.Collections.ObjectModel.Tests /// that the CollectionChanged events and eventargs are fired and populated /// properly. /// - public static class PublicMethodsTest + public partial class PublicMethodsTest { /// /// Tests that is possible to Add an item to the collection. diff --git a/src/System.ObjectModel/tests/ObservableCollection/ObservableCollection_SkipCollectionChangedTests.netcoreapp.cs b/src/System.ObjectModel/tests/ObservableCollection/ObservableCollection_SkipCollectionChangedTests.netcoreapp.cs new file mode 100644 index 000000000000..9a9b1c6f2120 --- /dev/null +++ b/src/System.ObjectModel/tests/ObservableCollection/ObservableCollection_SkipCollectionChangedTests.netcoreapp.cs @@ -0,0 +1,126 @@ +using System.Collections.Generic; +using System.Collections.ObjectModel; +using Xunit; + +namespace System.ObjectModel.Tests.ObservableCollection +{ + public class ObservableCollection_SkipCollectionChangedTests + { + [Fact] + public void SkipCollectionChanged_AddRange_Test() + { + int collectionChangedCounter = 0; + NonNullObservableCollection collection = new NonNullObservableCollection(); + collection.Add("1"); + collection.Add("2"); + collection.Add("3"); + collection.CollectionChanged += (s, e) => collectionChangedCounter++; + + Assert.Throws(() => collection.AddRange(new string[1])); + Assert.Equal(0, collectionChangedCounter); + + collection.Add("4"); + Assert.Equal(1, collectionChangedCounter); + } + + [Fact] + public void SkipCollectionChanged_InsertRange_Test() + { + int collectionChangedCounter = 0; + NonNullObservableCollection collection = new NonNullObservableCollection(); + collection.Add("1"); + collection.Add("2"); + collection.Add("3"); + collection.CollectionChanged += (s, e) => collectionChangedCounter++; + + Assert.Throws(() => collection.InsertRange(0, new string[1])); + Assert.Equal(0, collectionChangedCounter); + + collection.Add("4"); + Assert.Equal(1, collectionChangedCounter); + } + + [Fact] + public void SkipCollectionChanged_RemoveRange_Test() + { + int collectionChangedCounter = 0; + NonNullObservableCollection collection = new NonNullObservableCollection(); + collection.Add("1"); + collection.Add("2"); + collection.Add("3"); + collection.CollectionChanged += (s, e) => collectionChangedCounter++; + + collection.RemoveRange(0, 2); + Assert.Equal(1, collectionChangedCounter); + + collection.Add("1"); + Assert.Equal(2, collectionChangedCounter); + } + + [Fact] + public void SkipCollectionChanged_RemoveRange_NoEventsRaised_Test() + { + int collectionChangedCounter = 0; + NonNullObservableCollection collection = new NonNullObservableCollection(); + collection.Add("1"); + collection.Add("2"); + collection.Add("3"); + collection.CollectionChanged += (s, e) => collectionChangedCounter++; + + collection.RemoveRange(0, 0); + + Assert.Equal(0, collectionChangedCounter); + } + + [Fact] + public void SkipCollectionChanged_ReplaceRange_Test() + { + int collectionChangedCounter = 0; + NonNullObservableCollection collection = new NonNullObservableCollection(); + collection.Add("1"); + collection.Add("2"); + collection.Add("3"); + collection.CollectionChanged += (s, e) => collectionChangedCounter++; + + Assert.Throws(() => collection.ReplaceRange(0, 2, new string[1])); + Assert.Equal(0, collectionChangedCounter); + + collection.Add("1"); + Assert.Equal(1, collectionChangedCounter); + } + + [Fact] + public void SkipCollectionChanged_ReplaceRange_Empty_Test() + { + int collectionChangedCounter = 0; + NonNullObservableCollection collection = new NonNullObservableCollection(); + collection.Add("1"); + collection.Add("2"); + collection.Add("3"); + collection.CollectionChanged += (s, e) => collectionChangedCounter++; + + collection.ReplaceRange(0, 0, new string[0]); + Assert.Equal(0, collectionChangedCounter); + + collection.Add("1"); + Assert.Equal(1, collectionChangedCounter); + } + + public class NonNullObservableCollection : ObservableCollection + { + + public NonNullObservableCollection() : base() { } + public NonNullObservableCollection(List list) : base(list) { } + + protected override void InsertItem(int index, T item) + { + if (item == null) + { + throw new ArgumentNullException(); + } + + base.InsertItem(index, item); + } + } + } +} diff --git a/src/System.ObjectModel/tests/System.ObjectModel.Tests.csproj b/src/System.ObjectModel/tests/System.ObjectModel.Tests.csproj index 7682226ac131..89419f88be3d 100644 --- a/src/System.ObjectModel/tests/System.ObjectModel.Tests.csproj +++ b/src/System.ObjectModel/tests/System.ObjectModel.Tests.csproj @@ -22,6 +22,10 @@ Common\System\CollectionsIDictionaryTest.cs + + + + @@ -32,9 +36,6 @@ - - - @@ -43,17 +44,18 @@ + + - Common\System\Runtime\Serialization\Formatters\BinaryFormatterHelpers.cs - \ No newline at end of file + diff --git a/src/System.Runtime/ref/System.Runtime.cs b/src/System.Runtime/ref/System.Runtime.cs index 2646e062652b..694759465fd0 100644 --- a/src/System.Runtime/ref/System.Runtime.cs +++ b/src/System.Runtime/ref/System.Runtime.cs @@ -4034,6 +4034,7 @@ public Collection(System.Collections.Generic.IList list) { } bool System.Collections.IList.IsReadOnly { get { throw null; } } object System.Collections.IList.this[int index] { get { throw null; } set { } } public void Add(T item) { } + public void AddRange(System.Collections.Generic.IEnumerable collection) { } public void Clear() { } protected virtual void ClearItems() { } public bool Contains(T item) { throw null; } @@ -4042,9 +4043,15 @@ public void CopyTo(T[] array, int index) { } public int IndexOf(T item) { throw null; } public void Insert(int index, T item) { } protected virtual void InsertItem(int index, T item) { } - public bool Remove(T item) { throw null; } + protected virtual void InsertItemsRange(int index, System.Collections.Generic.IEnumerable collection) { } + public void InsertRange(int index, System.Collections.Generic.IEnumerable collection) { } + public bool Remove(T item) { throw null; } public void RemoveAt(int index) { } protected virtual void RemoveItem(int index) { } + protected virtual void RemoveItemsRange(int index, int count) { } + public void RemoveRange(int index, int count) { } + protected virtual void ReplaceItemsRange(int index, int count, System.Collections.Generic.IEnumerable collection) { } + public void ReplaceRange(int index, int count, System.Collections.Generic.IEnumerable collection) { } protected virtual void SetItem(int index, T item) { } void System.Collections.ICollection.CopyTo(System.Array array, int index) { } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; } diff --git a/src/System.Runtime/tests/System.Runtime.Tests.csproj b/src/System.Runtime/tests/System.Runtime.Tests.csproj index 812406b043d9..53a45b1ba1de 100644 --- a/src/System.Runtime/tests/System.Runtime.Tests.csproj +++ b/src/System.Runtime/tests/System.Runtime.Tests.csproj @@ -72,6 +72,7 @@ + @@ -155,7 +156,6 @@ - @@ -234,6 +234,7 @@ + @@ -271,7 +272,7 @@ - + diff --git a/src/System.Runtime/tests/System/Collections/ObjectModel/CollectionTests.cs b/src/System.Runtime/tests/System/Collections/ObjectModel/CollectionTests.cs index 4e1fb2f82eb0..0961f2b8812f 100644 --- a/src/System.Runtime/tests/System/Collections/ObjectModel/CollectionTests.cs +++ b/src/System.Runtime/tests/System/Collections/ObjectModel/CollectionTests.cs @@ -1,22 +1,13 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; -using System.Collections; using System.Collections.Generic; -using System.Collections.ObjectModel; using Xunit; namespace System.Collections.ObjectModel.Tests { - /// - /// Since is just a wrapper base class around an , - /// we just verify that the underlying list is what we expect, validate that the calls which - /// we expect are forwarded to the underlying list, and verify that the exceptions we expect - /// are thrown. - /// - public class CollectionTests : CollectionTestBase + public partial class CollectionTests : CollectionTestBase { private static readonly Collection s_empty = new Collection(); diff --git a/src/System.Runtime/tests/System/Collections/ObjectModel/CollectionTests.netcoreapp.cs b/src/System.Runtime/tests/System/Collections/ObjectModel/CollectionTests.netcoreapp.cs new file mode 100644 index 000000000000..940af6773626 --- /dev/null +++ b/src/System.Runtime/tests/System/Collections/ObjectModel/CollectionTests.netcoreapp.cs @@ -0,0 +1,424 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.Linq; +using Xunit; + +namespace System.Collections.ObjectModel.Tests +{ + /// + /// Since is just a wrapper base class around an , + /// we just verify that the underlying list is what we expect, validate that the calls which + /// we expect are forwarded to the underlying list, and verify that the exceptions we expect + /// are thrown. + /// + public partial class CollectionTests : CollectionTestBase + { + [Fact] + public void Collection_AddRange_ToEmpty_Test() + { + int[] expected = new[] { 1, 2, 3, 4 }; + Collection collection = new Collection(); + + collection.AddRange(expected); + + Assert.NotNull(collection); + Assert.Equal(expected.Length, collection.Count); + Assert.Equal(expected, collection.ToArray()); + } + + [Fact] + public void Collection_AddRange_ToExisting_Test() + { + int[] initial = new int[] { 1, 2, 3, 4 }; + int[] dataToInsert = new int[] { 5, 6, 7, 8 }; + Collection collection = new Collection(); + for (int i = 0; i < initial.Length; i++) + collection.Add(initial[i]); + + collection.AddRange(dataToInsert); + + Assert.NotNull(collection); + Assert.Equal(initial.Length + dataToInsert.Length, collection.Count); + + int[] collectionAssertion = collection.ToArray(); + Assert.Equal(initial, collectionAssertion.AsSpan(0, 4).ToArray()); + Assert.Equal(dataToInsert, collectionAssertion.AsSpan(4, 4).ToArray()); + } + + [Fact] + public void Collection_AddRange_Empty_Test() + { + int[] expected = new int[0]; + Collection collection = new Collection(); + + collection.AddRange(expected); + + Assert.NotNull(collection); + Assert.Equal(expected.Length, collection.Count); + } + + [Fact] + public void Collection_AddRange_Null_Test() + { + Collection collection = new Collection(); + Exception ex = Assert.Throws(() => collection.AddRange(null)); + } + + [Fact] + public void Collection_AddRange_ReadOnly_Test() + { + int[] expected = new int[] { 1, 2, 3, 4 }; + int[] baseCollection = new int[] { 5, 6, 7, 8 }; + Collection collection = new Collection(baseCollection); + + Exception ex = Assert.Throws(() => collection.AddRange(expected)); + } + + [Fact] + public void Collection_InsertRange_Beginning_Test() + { + int[] expected = new int[] { 1, 2, 3, 4, 5 }; + int[] originalCollection = new int[] { 10, 11, 12, 13 }; + List baseCollection = new List(originalCollection); + + int expectedLength = expected.Length + originalCollection.Length; + Collection collection = new Collection(baseCollection); + + collection.InsertRange(0, expected); + + Assert.NotNull(collection); + Assert.Equal(expectedLength, collection.Count); + + int[] collectionAssertion = collection.ToArray(); + Assert.Equal(expected, collectionAssertion.AsSpan(0, 5).ToArray()); + Assert.Equal(originalCollection, collectionAssertion.AsSpan(5, 4).ToArray()); + } + + [Fact] + public void Collection_InsertRange_End_Test() + { + int[] expected = new int[] { 1, 2, 3, 4, 5 }; + int[] originalCollection = new int[] { 10, 11, 12, 13 }; + List baseCollection = new List(originalCollection); + + int expectedLength = expected.Length + originalCollection.Length; + Collection collection = new Collection(baseCollection); + + collection.InsertRange(expected.Length - 1, expected); + + Assert.NotNull(collection); + Assert.Equal(expectedLength, collection.Count); + + int[] collectionAssertion = collection.ToArray(); + Assert.Equal(originalCollection, collectionAssertion.AsSpan(0, 4).ToArray()); + Assert.Equal(expected, collectionAssertion.AsSpan(4, 5).ToArray()); + } + + [Fact] + public void Collection_InsertRange_Middle_Test() + { + int[] expected = new int[] { 1, 2, 3, 4, 5 }; + int[] originalCollection = new int[] { 10, 11, 12, 13 }; + List baseCollection = new List(originalCollection); + + int expectedLength = expected.Length + originalCollection.Length; + Collection collection = new Collection(baseCollection); + + collection.InsertRange(2, expected); + + Assert.NotNull(collection); + Assert.Equal(expectedLength, collection.Count); + + int[] collectionAssertion = collection.ToArray(); + Assert.Equal(originalCollection.AsSpan(0, 2).ToArray(), collectionAssertion.AsSpan(0, 2).ToArray()); + Assert.Equal(expected, collectionAssertion.AsSpan(2, 5).ToArray()); + Assert.Equal(originalCollection.AsSpan(2, 2).ToArray(), collectionAssertion.AsSpan(7, 2).ToArray()); + } + + [Fact] + public void Collection_InsertRange_Empty_Test() + { + List baseCollection = new List(new[] { 10, 11, 12, 13 }); + Collection collection = new Collection(baseCollection); + + collection.InsertRange(0, new int[0]); + + Assert.NotNull(collection); + Assert.Equal(baseCollection.Count, collection.Count); + Assert.Equal(baseCollection, collection.ToArray()); + } + + [Fact] + public void Collection_InsertRange_Null_Test() + { + Collection collection = new Collection(); + Exception ex = Assert.Throws(() => collection.InsertRange(0, null)); + } + + [Fact] + public void Collection_InsertRange_ReadOnly_Test() + { + int[] expected = new int[] { 1, 2, 3, 4 }; + int[] baseCollection = new int[] { 5, 6, 7, 8 }; + Collection collection = new Collection(baseCollection); + + Exception ex = Assert.Throws(() => collection.InsertRange(0, expected)); + } + + [Fact] + public void Collection_InsertRange_IndexLessThan0_Test() + { + int[] expected = new int[] { 1, 2, 3, 4 }; + Collection collection = new Collection(); + Exception ex = Assert.Throws(() => collection.InsertRange(-1, expected)); + } + + [Fact] + public void Collection_InsertRange_IndexGreaterThanCount_Test() + { + int[] expected = new int[] { 1, 2, 3, 4 }; + Collection collection = new Collection(); + Exception ex = Assert.Throws(() => collection.InsertRange(10, expected)); + } + + [Fact] + public void Collection_RemoveRange_Overflow_Test() + { + Collection collection = new Collection(); + collection.Add(1); + collection.Add(2); + collection.Add(3); + + Assert.Throws(() => collection.RemoveRange(0, 4)); + } + + [Fact] + public void Collection_RemoveRange_IntMaxValueOverflow_Test() + { + var count = 500; + Collection collection = new Collection(); + for (int i = 0; i < count; i++) + { + collection.Add(i); + } + + Assert.Throws(() => collection.RemoveRange(collection.Count - 2, int.MaxValue)); + } + + [Fact] + public void Collection_RemoveRange_CountIsZero_Test() + { + int[] expected = new int[] { 1, 2, 3, 4, 5 }; + Collection collection = new Collection(); + for (int i = 0; i < expected.Length; i++) + { + collection.Add(expected[i]); + } + + collection.RemoveRange(0, 0); + + Assert.NotNull(collection); + Assert.Equal(expected.Length, collection.Count); + Assert.Equal(expected, collection.ToArray()); + } + + [Fact] + public void Collection_RemoveRange_CountIsLessThanZero_Test() + { + Collection collection = new Collection(); + collection.Add(1); + collection.Add(2); + + Assert.Throws(() => collection.RemoveRange(0, -1)); + } + + [Fact] + public void Collection_RemoveRange_All_Test() + { + Collection collection = new Collection(); + collection.Add(1); + collection.Add(2); + collection.Add(3); + + collection.RemoveRange(0, 3); + + Assert.NotNull(collection); + Assert.Equal(0, collection.Count); + } + + [Fact] + public void Collection_RemoveRange_FirstTwoItems_Test() + { + Collection collection = new Collection(); + collection.Add(1); + collection.Add(2); + collection.Add(3); + + collection.RemoveRange(0, 2); + + Assert.NotNull(collection); + Assert.Equal(1, collection.Count); + Assert.Equal(3, collection[0]); + } + + [Fact] + public void Collection_RemoveRange_LastTwoItems_Test() + { + Collection collection = new Collection(); + collection.Add(1); + collection.Add(2); + collection.Add(3); + + collection.RemoveRange(1, 2); + + Assert.NotNull(collection); + Assert.Equal(1, collection.Count); + Assert.Equal(1, collection[0]); + } + + [Fact] + public void Collection_RemoveRange_ZeroItems_Test() + { + Collection collection = new Collection(); + collection.Add(1); + collection.Add(2); + collection.Add(3); + + collection.RemoveRange(0, 0); + + Assert.NotNull(collection); + Assert.Equal(3, collection.Count); + Assert.Equal(1, collection[0]); + Assert.Equal(2, collection[1]); + Assert.Equal(3, collection[2]); + } + + [Fact] + public void Collection_RemoveRange_IndexLessThanZero_Test() + { + Collection collection = new Collection(); + collection.Add(1); + collection.Add(2); + collection.Add(3); + + AssertExtensions.Throws("index", () => collection.RemoveRange(-1, 3)); + } + + [Fact] + public void Collection_RemoveRange_IndexGreaterThanCollection_Test() + { + Collection collection = new Collection(); + collection.Add(1); + collection.Add(2); + collection.Add(3); + + AssertExtensions.Throws("index", () => collection.RemoveRange(4, 3)); + } + + [Fact] + public void Collection_RemoveRange_ReadOnly_Test() + { + Collection collection = new Collection(new int[] { 1, 2, 3 }); + + Assert.Throws(() => collection.RemoveRange(0, 2)); + } + + [Fact] + public void Collection_ReplaceRange_FirstTwo_Test() + { + int[] initial = new int[] { 1, 2, 3, 4 }; + int[] replace = new int[] { 5, 6, 7, 8 }; + Collection collection = new Collection(); + foreach (var item in initial) + collection.Add(item); + + collection.ReplaceRange(0, 2, replace); + + Assert.NotNull(collection); + Assert.Equal(initial.Length + 2, collection.Count); + + int[] collectionAssertion = collection.ToArray(); + Assert.Equal(replace, collectionAssertion.AsSpan(0, 4).ToArray()); + Assert.Equal(initial.AsSpan(2, 2).ToArray(), collectionAssertion.AsSpan(4, 2).ToArray()); + } + + [Fact] + public void Collection_ReplaceRange_LastTwo_Test() + { + int[] initial = new int[] { 1, 2, 3, 4 }; + int[] replace = new int[] { 5, 6, 7, 8 }; + Collection collection = new Collection(); + foreach (var item in initial) + collection.Add(item); + + collection.ReplaceRange(2, 2, replace); + + Assert.NotNull(collection); + Assert.Equal(initial.Length + 2, collection.Count); + + int[] collectionAssertion = collection.ToArray(); + Assert.Equal(initial.AsSpan(0, 2).ToArray(), collectionAssertion.AsSpan(0, 2).ToArray()); + Assert.Equal(replace.AsSpan(0, 4).ToArray(), collectionAssertion.AsSpan(2, 4).ToArray()); + } + + [Fact] + public void Collection_ReplaceRange_MiddleTwo_Test() + { + int[] initial = new int[] { 1, 2, 3, 4 }; + int[] replace = new int[] { 5, 6, 7, 8 }; + Collection collection = new Collection(); + foreach (var item in initial) + collection.Add(item); + + collection.ReplaceRange(1, 2, replace); + + Assert.NotNull(collection); + Assert.Equal(initial.Length + 2, collection.Count); + + Assert.Equal(initial[0], collection[0]); + Assert.Equal(replace, collection.ToArray().AsSpan(1, 4).ToArray()); + Assert.Equal(initial[3], collection[5]); + } + + [Fact] + public void Collection_ReplaceRange_NullCollection_Test() + { + Collection collection = new Collection(); + collection.Add(1); + collection.Add(2); + + Assert.Throws(() => collection.ReplaceRange(0, 2, null)); + } + + [Fact] + public void Collection_ReplaceRange_ReadOnly_Test() + { + Collection collection = new Collection(new int[] { 1, 2, 3 }); + Assert.Throws(() => collection.ReplaceRange(0, 2, new int[] { 4, 5 })); + } + + [Fact] + public void Collection_ReplaceRange_IndexLessThanZero_Test() + { + Collection collection = new Collection(); + collection.Add(1); + collection.Add(2); + + AssertExtensions.Throws("index", () => collection.ReplaceRange(-2, 2, new int[] { 1, 2 })); + } + + [Fact] + public void Collection_ReplaceRange_IndexGreaterThanCount_Test() + { + Collection collection = new Collection(); + collection.Add(1); + collection.Add(2); + + AssertExtensions.Throws("index", () => collection.ReplaceRange(4, 2, new int[] { 1, 2 })); + } + } +}