From 2785ef89db5bf33464b3c360ce1095d21668b050 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sat, 27 Apr 2024 07:21:55 -0700 Subject: [PATCH] Revert "Make mutable generic collection interfaces implement read-only collection interfaces (#95830)" This reverts commit a2bd5830ce09cd81d834afd5571bc6c9e5ad879b. --- .../src/System/Array.CoreCLR.cs | 2 +- src/coreclr/vm/array.cpp | 19 +- .../System/Collections/CollectionAsserts.cs | 152 --------------- .../Collections/ICollection.Generic.Tests.cs | 43 ++--- .../Collections/IDictionary.Generic.Tests.cs | 180 +++--------------- .../System/Collections/IList.Generic.Tests.cs | 75 ++++---- .../System/Collections/ISet.Generic.Tests.cs | 53 +++--- .../Generic/CollectionExtensionsTests.cs | 12 -- .../System/Collections/Generic/ICollection.cs | 6 +- .../System/Collections/Generic/IDictionary.cs | 22 +-- .../src/System/Collections/Generic/IList.cs | 6 +- .../src/System/Collections/Generic/ISet.cs | 35 +--- .../System.Runtime/ref/System.Runtime.cs | 49 ++--- 13 files changed, 164 insertions(+), 490 deletions(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs index 74e0739848168..de7b3021c458f 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs @@ -694,7 +694,7 @@ public ArrayInitializeCache(RuntimeType arrayType) // it for type and executes it. // // The "T" will reflect the interface used to invoke the method. The actual runtime "this" will be - // array that is castable to "T[]" (i.e. for primitives and valuetypes, it will be exactly + // array that is castable to "T[]" (i.e. for primitivs and valuetypes, it will be exactly // "T[]" - for orefs, it may be a "U[]" where U derives from T.) //---------------------------------------------------------------------------------------- internal sealed class SZArrayHelper diff --git a/src/coreclr/vm/array.cpp b/src/coreclr/vm/array.cpp index e1e2b31e4df64..ffb6e6def99ce 100644 --- a/src/coreclr/vm/array.cpp +++ b/src/coreclr/vm/array.cpp @@ -1210,14 +1210,29 @@ MethodDesc* GetActualImplementationForArrayGenericIListOrIReadOnlyListMethod(Met } CONTRACTL_END + int slot = pItfcMeth->GetSlot(); + + // We need to pick the right starting method depending on the depth of the inheritance chain + static const BinderMethodID startingMethod[] = { + METHOD__SZARRAYHELPER__GETENUMERATOR, // First method of IEnumerable`1 + METHOD__SZARRAYHELPER__GET_COUNT, // First method of ICollection`1/IReadOnlyCollection`1 + METHOD__SZARRAYHELPER__GET_ITEM // First method of IList`1/IReadOnlyList`1 + }; + // Subtract one for the non-generic IEnumerable that the generic enumerable inherits from unsigned int inheritanceDepth = pItfcMeth->GetMethodTable()->GetNumInterfaces() - 1; + PREFIX_ASSUME(0 <= inheritanceDepth && inheritanceDepth < ARRAY_SIZE(startingMethod)); + + MethodDesc *pGenericImplementor = CoreLibBinder::GetMethod((BinderMethodID)(startingMethod[inheritanceDepth] + slot)); - MethodDesc *pGenericImplementor = MemberLoader::FindMethodByName(g_pSZArrayHelperClass, pItfcMeth->GetName()); + // The most common reason for this assert is that the order of the SZArrayHelper methods in + // corelib.h does not match the order they are implemented on the generic interfaces. + _ASSERTE(pGenericImplementor == MemberLoader::FindMethodByName(g_pSZArrayHelperClass, pItfcMeth->GetName())); // OPTIMIZATION: For any method other than GetEnumerator(), we can safely substitute // "Object" for reference-type theT's. This causes fewer methods to be instantiated. - if (inheritanceDepth != 0 && !theT.IsValueType()) + if (startingMethod[inheritanceDepth] != METHOD__SZARRAYHELPER__GETENUMERATOR && + !theT.IsValueType()) { theT = TypeHandle(g_pObjectClass); } diff --git a/src/libraries/Common/tests/System/Collections/CollectionAsserts.cs b/src/libraries/Common/tests/System/Collections/CollectionAsserts.cs index 1e76d40ebb411..2de26be1737fd 100644 --- a/src/libraries/Common/tests/System/Collections/CollectionAsserts.cs +++ b/src/libraries/Common/tests/System/Collections/CollectionAsserts.cs @@ -1,7 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System; using System.Collections.Generic; using System.Linq; using Xunit; @@ -10,151 +9,6 @@ namespace System.Collections.Tests { internal static class CollectionAsserts { - public static void HasCount(ICollection collection, int count) - { - Assert.Equal(count, collection.Count); -#if !NETFRAMEWORK - IReadOnlyCollection readOnlyCollection = collection; - Assert.Equal(count, readOnlyCollection.Count); -#endif - } - - public static void EqualAt(IList list, int index, T expected) - { - Assert.Equal(expected, list[index]); -#if !NETFRAMEWORK - IReadOnlyList readOnlyList = list; - Assert.Equal(expected, readOnlyList[index]); -#endif - } - - public static void NotEqualAt(IList list, int index, T expected) - { - Assert.NotEqual(expected, list[index]); -#if !NETFRAMEWORK - IReadOnlyList readOnlyList = list; - Assert.NotEqual(expected, readOnlyList[index]); -#endif - } - - public static void ThrowsElementAt(IList list, int index, Type exceptionType) - { - Assert.Throws(exceptionType, () => list[index]); -#if !NETFRAMEWORK - IReadOnlyList readOnlyList = list; - Assert.Throws(exceptionType, () => readOnlyList[index]); -#endif - } - - public static void ElementAtSucceeds(IList list, int index) - { - T result = list[index]; -#if !NETFRAMEWORK - IReadOnlyList readOnlyList = list; - Assert.Equal(result, readOnlyList[index]); -#endif - } - - public static void EqualAt(IDictionary dictionary, TKey key, TValue expected) - { - Assert.Equal(expected, dictionary[key]); -#if !NETFRAMEWORK - IReadOnlyDictionary readOnlyDictionary = dictionary; - Assert.Equal(expected, readOnlyDictionary[key]); -#endif - } - - public static void ContainsKey(IDictionary dictionary, TKey key, bool expected) - { - Assert.Equal(expected, dictionary.ContainsKey(key)); -#if !NETFRAMEWORK - IReadOnlyDictionary readOnlyDictionary = dictionary; - Assert.Equal(expected, readOnlyDictionary.ContainsKey(key)); -#endif - } - - public static void TryGetValue(IDictionary dictionary, TKey key, bool expected, TValue expectedValue = default) - { - Assert.Equal(expected, dictionary.TryGetValue(key, out TValue value)); - if (expected) - { - Assert.Equal(expectedValue, value); - } -#if !NETFRAMEWORK - IReadOnlyDictionary readOnlyDictionary = dictionary; - Assert.Equal(expected, readOnlyDictionary.TryGetValue(key, out value)); - if (expected) - { - Assert.Equal(expectedValue, value); - } -#endif - } - - public static void Contains(ISet set, T expected) - { - Assert.True(set.Contains(expected)); -#if !NETFRAMEWORK - ICollection collection = set; - Assert.True(collection.Contains(expected)); - IReadOnlySet readOnlySet = set; - Assert.True(readOnlySet.Contains(expected)); -#endif - } - - public static void IsProperSubsetOf(ISet set, IEnumerable enumerable, bool expected) - { - Assert.Equal(expected, set.IsProperSubsetOf(enumerable)); -#if !NETFRAMEWORK - IReadOnlySet readOnlySet = set; - Assert.Equal(expected, readOnlySet.IsProperSubsetOf(enumerable)); -#endif - } - - public static void IsProperSupersetOf(ISet set, IEnumerable enumerable, bool expected) - { - Assert.Equal(expected, set.IsProperSupersetOf(enumerable)); -#if !NETFRAMEWORK - IReadOnlySet readOnlySet = set; - Assert.Equal(expected, readOnlySet.IsProperSupersetOf(enumerable)); -#endif - } - - public static void IsSubsetOf(ISet set, IEnumerable enumerable, bool expected) - { - Assert.Equal(expected, set.IsSubsetOf(enumerable)); -#if !NETFRAMEWORK - IReadOnlySet readOnlySet = set; - Assert.Equal(expected, readOnlySet.IsSubsetOf(enumerable)); -#endif - } - - public static void IsSupersetOf(ISet set, IEnumerable enumerable, bool expected) - { - Assert.Equal(expected, set.IsSupersetOf(enumerable)); -#if !NETFRAMEWORK - IReadOnlySet readOnlySet = set; - Assert.Equal(expected, readOnlySet.IsSupersetOf(enumerable)); -#endif - } - - public static void Overlaps(ISet set, IEnumerable enumerable, bool expected) - { - Assert.Equal(expected, set.Overlaps(enumerable)); -#if !NETFRAMEWORK - IReadOnlySet readOnlySet = set; - Assert.Equal(expected, readOnlySet.Overlaps(enumerable)); -#endif - } - - public static void SetEquals(ISet set, IEnumerable enumerable, bool expected) - { - Assert.Equal(expected, set.SetEquals(enumerable)); -#if !NETFRAMEWORK - IReadOnlySet readOnlySet = set; - Assert.Equal(expected, readOnlySet.SetEquals(enumerable)); -#endif - } - public static void Equal(ICollection expected, ICollection actual) { Assert.Equal(expected == null, actual == null); @@ -189,12 +43,6 @@ public static void Equal(ICollection expected, ICollection actual) return; } Assert.Equal(expected.Count, actual.Count); -#if !NETFRAMEWORK - IReadOnlyCollection readOnlyExpected = expected; - Assert.Equal(expected.Count, readOnlyExpected.Count); - IReadOnlyCollection readOnlyActual = actual; - Assert.Equal(actual.Count, readOnlyActual.Count); -#endif IEnumerator e = expected.GetEnumerator(); IEnumerator a = actual.GetEnumerator(); while (e.MoveNext()) diff --git a/src/libraries/Common/tests/System/Collections/ICollection.Generic.Tests.cs b/src/libraries/Common/tests/System/Collections/ICollection.Generic.Tests.cs index 8ae72dea074ab..a94a9308b99ab 100644 --- a/src/libraries/Common/tests/System/Collections/ICollection.Generic.Tests.cs +++ b/src/libraries/Common/tests/System/Collections/ICollection.Generic.Tests.cs @@ -130,7 +130,7 @@ public void ICollection_Generic_IsReadOnly_Validity(int count) public void ICollection_Generic_Count_Validity(int count) { ICollection collection = GenericICollectionFactory(count); - CollectionAsserts.HasCount(collection, count); + Assert.Equal(count, collection.Count); } #endregion @@ -145,7 +145,7 @@ public virtual void ICollection_Generic_Add_DefaultValue(int count) { ICollection collection = GenericICollectionFactory(count); collection.Add(default(T)); - CollectionAsserts.HasCount(collection, count + 1); + Assert.Equal(count + 1, collection.Count); } } @@ -161,7 +161,7 @@ public void ICollection_Generic_Add_InvalidValueToMiddleOfCollection(int count) collection.Add(invalidValue); for (int i = 0; i < count; i++) collection.Add(CreateT(i)); - CollectionAsserts.HasCount(collection, count * 2); + Assert.Equal(count * 2, collection.Count); }); } } @@ -178,7 +178,7 @@ public void ICollection_Generic_Add_InvalidValueToBeginningOfCollection(int coun collection.Add(invalidValue); for (int i = 0; i < count; i++) collection.Add(CreateT(i)); - CollectionAsserts.HasCount(collection, count); + Assert.Equal(count, collection.Count); }); } } @@ -192,9 +192,8 @@ public void ICollection_Generic_Add_InvalidValueToEndOfCollection(int count) Assert.All(InvalidValues, invalidValue => { ICollection collection = GenericICollectionFactory(count); - collection.Add(invalidValue); - CollectionAsserts.HasCount(collection, count); + Assert.Equal(count, collection.Count); }); } } @@ -209,7 +208,7 @@ public void ICollection_Generic_Add_DuplicateValue(int count) T duplicateValue = CreateT(700); collection.Add(duplicateValue); collection.Add(duplicateValue); - CollectionAsserts.HasCount(collection, count + 2); + Assert.Equal(count + 2, collection.Count); } } @@ -222,7 +221,7 @@ public void ICollection_Generic_Add_AfterCallingClear(int count) ICollection collection = GenericICollectionFactory(count); collection.Clear(); AddToCollection(collection, 5); - CollectionAsserts.HasCount(collection, 5); + Assert.Equal(5, collection.Count); } } @@ -262,7 +261,7 @@ public void ICollection_Generic_Add_AfterRemovingAllItems(int count) for (int i = 0; i < count; i++) collection.Remove(collection.ElementAt(0)); collection.Add(CreateT(254)); - CollectionAsserts.HasCount(collection, 1); + Assert.Equal(1, collection.Count); } } @@ -274,7 +273,7 @@ public void ICollection_Generic_Add_ToReadOnlyCollection(int count) { ICollection collection = GenericICollectionFactory(count); Assert.Throws(() => collection.Add(CreateT(0))); - CollectionAsserts.HasCount(collection, count); + Assert.Equal(count, collection.Count); } } @@ -307,12 +306,12 @@ public void ICollection_Generic_Clear(int count) if (IsReadOnly || AddRemoveClear_ThrowsNotSupported) { Assert.Throws(() => collection.Clear()); - CollectionAsserts.HasCount(collection, count); + Assert.Equal(count, collection.Count); } else { collection.Clear(); - CollectionAsserts.HasCount(collection, 0); + Assert.Equal(0, collection.Count); } } @@ -326,14 +325,14 @@ public void ICollection_Generic_Clear_Repeatedly(int count) Assert.Throws(() => collection.Clear()); Assert.Throws(() => collection.Clear()); Assert.Throws(() => collection.Clear()); - CollectionAsserts.HasCount(collection, count); + Assert.Equal(count, collection.Count); } else { collection.Clear(); collection.Clear(); collection.Clear(); - CollectionAsserts.HasCount(collection, 0); + Assert.Equal(0, collection.Count); } } @@ -435,7 +434,7 @@ public void ICollection_Generic_Contains_ValidValueThatExistsTwiceInTheCollectio T item = CreateT(12); collection.Add(item); collection.Add(item); - CollectionAsserts.HasCount(collection, count + 2); + Assert.Equal(count + 2, collection.Count); } } @@ -568,7 +567,7 @@ public void ICollection_Generic_Remove_DefaultValueNotContainedInCollection(int count--; } Assert.False(collection.Remove(value)); - CollectionAsserts.HasCount(collection, count); + Assert.Equal(count, collection.Count); } } @@ -584,7 +583,7 @@ public void ICollection_Generic_Remove_NonDefaultValueNotContainedInCollection(i while (collection.Contains(value) || Enumerable.Contains(InvalidValues, value)) value = CreateT(seed++); Assert.False(collection.Remove(value)); - CollectionAsserts.HasCount(collection, count); + Assert.Equal(count, collection.Count); } } @@ -603,7 +602,7 @@ public virtual void ICollection_Generic_Remove_DefaultValueContainedInCollection count++; } Assert.True(collection.Remove(value)); - CollectionAsserts.HasCount(collection, count - 1); + Assert.Equal(count - 1, collection.Count); } } @@ -622,7 +621,7 @@ public void ICollection_Generic_Remove_NonDefaultValueContainedInCollection(int count++; } Assert.True(collection.Remove(value)); - CollectionAsserts.HasCount(collection, count - 1); + Assert.Equal(count - 1, collection.Count); } } @@ -640,7 +639,7 @@ public void ICollection_Generic_Remove_ValueThatExistsTwiceInCollection(int coun count += 2; Assert.True(collection.Remove(value)); Assert.True(collection.Contains(value)); - CollectionAsserts.HasCount(collection, count - 1); + Assert.Equal(count - 1, collection.Count); } } @@ -655,7 +654,7 @@ public void ICollection_Generic_Remove_EveryValue(int count) { Assert.True(collection.Remove(value)); }); - CollectionAsserts.HasCount(collection, 0); + Assert.Empty(collection); } } @@ -668,7 +667,7 @@ public void ICollection_Generic_Remove_InvalidValue_ThrowsArgumentException(int { Assert.Throws(() => collection.Remove(value)); }); - CollectionAsserts.HasCount(collection, count); + Assert.Equal(count, collection.Count); } [Theory] diff --git a/src/libraries/Common/tests/System/Collections/IDictionary.Generic.Tests.cs b/src/libraries/Common/tests/System/Collections/IDictionary.Generic.Tests.cs index e6311ec89b4fc..df92ab206f668 100644 --- a/src/libraries/Common/tests/System/Collections/IDictionary.Generic.Tests.cs +++ b/src/libraries/Common/tests/System/Collections/IDictionary.Generic.Tests.cs @@ -266,16 +266,12 @@ public void IDictionary_Generic_ItemGet_DefaultKey(int count) if (!DefaultValueAllowed) { Assert.Throws(() => dictionary[default(TKey)]); -#if !NETFRAMEWORK - IReadOnlyDictionary readOnlyDictionary = dictionary; - Assert.Throws(() => readOnlyDictionary[default(TKey)]); -#endif } else { TValue value = CreateTValue(3452); dictionary[default(TKey)] = value; - CollectionAsserts.EqualAt(dictionary, default(TKey), value); + Assert.Equal(value, dictionary[default(TKey)]); } } } @@ -287,10 +283,6 @@ public void IDictionary_Generic_ItemGet_MissingNonDefaultKey_ThrowsKeyNotFoundEx IDictionary dictionary = GenericIDictionaryFactory(count); TKey missingKey = GetNewKey(dictionary); Assert.Throws(() => dictionary[missingKey]); -#if !NETFRAMEWORK - IReadOnlyDictionary readOnlyDictionary = dictionary; - Assert.Throws(() => readOnlyDictionary[missingKey]); -#endif } [Theory] @@ -304,10 +296,6 @@ public void IDictionary_Generic_ItemGet_MissingDefaultKey_ThrowsKeyNotFoundExcep while (dictionary.ContainsKey(missingKey)) dictionary.Remove(missingKey); Assert.Throws(() => dictionary[missingKey]); -#if !NETFRAMEWORK - IReadOnlyDictionary readOnlyDictionary = dictionary; - Assert.Throws(() => readOnlyDictionary[missingKey]); -#endif } } @@ -318,7 +306,7 @@ public void IDictionary_Generic_ItemGet_PresentKeyReturnsCorrectValue(int count) IDictionary dictionary = GenericIDictionaryFactory(count); foreach (KeyValuePair pair in dictionary) { - CollectionAsserts.EqualAt(dictionary, pair.Key, pair.Value); + Assert.Equal(pair.Value, dictionary[pair.Key]); } } @@ -341,7 +329,7 @@ public void IDictionary_Generic_ItemSet_DefaultKey(int count) { TValue value = CreateTValue(3452); dictionary[default(TKey)] = value; - CollectionAsserts.EqualAt(dictionary, default(TKey), value); + Assert.Equal(value, dictionary[default(TKey)]); } } } @@ -367,7 +355,7 @@ public void IDictionary_Generic_ItemSet_AddsNewValueWhenNotPresent(int count) IDictionary dictionary = GenericIDictionaryFactory(count); TKey missingKey = GetNewKey(dictionary); dictionary[missingKey] = CreateTValue(543); - CollectionAsserts.HasCount(dictionary, count + 1); + Assert.Equal(count + 1, dictionary.Count); } } @@ -382,8 +370,8 @@ public void IDictionary_Generic_ItemSet_ReplacesExistingValueWhenPresent(int cou dictionary.Add(existingKey, CreateTValue(5342)); TValue newValue = CreateTValue(1234); dictionary[existingKey] = newValue; - CollectionAsserts.HasCount(dictionary, count + 1); - CollectionAsserts.EqualAt(dictionary, existingKey, newValue); + Assert.Equal(count + 1, dictionary.Count); + Assert.Equal(newValue, dictionary[existingKey]); } } @@ -398,10 +386,6 @@ public void IDictionary_Generic_Keys_ContainsAllCorrectKeys(int count) IDictionary dictionary = GenericIDictionaryFactory(count); IEnumerable expected = dictionary.Select((pair) => pair.Key); Assert.True(expected.SequenceEqual(dictionary.Keys)); -#if !NETFRAMEWORK - IReadOnlyDictionary readOnlyDictionary = dictionary; - Assert.True(expected.SequenceEqual(readOnlyDictionary.Keys)); -#endif } [Theory] @@ -412,10 +396,6 @@ public void IDictionary_Generic_Keys_ModifyingTheDictionaryUpdatesTheCollection( { IDictionary dictionary = GenericIDictionaryFactory(count); ICollection keys = dictionary.Keys; -#if !NETFRAMEWORK - IReadOnlyDictionary readOnlyDictionary = dictionary; - IEnumerable readOnlyKeys = readOnlyDictionary.Keys; -#endif int previousCount = keys.Count; if (count > 0) Assert.NotEmpty(keys); @@ -423,16 +403,10 @@ public void IDictionary_Generic_Keys_ModifyingTheDictionaryUpdatesTheCollection( if (IDictionary_Generic_Keys_Values_ModifyingTheDictionaryUpdatesTheCollection) { Assert.Empty(keys); -#if !NETFRAMEWORK - Assert.Empty(readOnlyKeys); -#endif } else { Assert.Equal(previousCount, keys.Count); -#if !NETFRAMEWORK - Assert.Equal(previousCount, readOnlyKeys.Count()); -#endif } } } @@ -446,20 +420,11 @@ public void IDictionary_Generic_Keys_Enumeration_ParentDictionaryModifiedInvalid IDictionary dictionary = GenericIDictionaryFactory(count); ICollection keys = dictionary.Keys; IEnumerator keysEnum = keys.GetEnumerator(); -#if !NETFRAMEWORK - IReadOnlyDictionary readOnlyDictionary = dictionary; - IEnumerable readOnlyKeys = readOnlyDictionary.Keys; - IEnumerator readOnlyKeysEnum = readOnlyKeys.GetEnumerator(); -#endif dictionary.Add(GetNewKey(dictionary), CreateTValue(3432)); if (count == 0 ? Enumerator_Empty_ModifiedDuringEnumeration_ThrowsInvalidOperationException : IDictionary_Generic_Keys_Values_Enumeration_ThrowsInvalidOperation_WhenParentModified) { Assert.Throws(() => keysEnum.MoveNext()); Assert.Throws(() => keysEnum.Reset()); -#if !NETFRAMEWORK - Assert.Throws(() => readOnlyKeysEnum.MoveNext()); - Assert.Throws(() => readOnlyKeysEnum.Reset()); -#endif } else { @@ -468,13 +433,6 @@ public void IDictionary_Generic_Keys_Enumeration_ParentDictionaryModifiedInvalid _ = keysEnum.Current; } keysEnum.Reset(); -#if !NETFRAMEWORK - if (readOnlyKeysEnum.MoveNext()) - { - _ = readOnlyKeysEnum.Current; - } - readOnlyKeysEnum.Reset(); -#endif } } } @@ -498,25 +456,10 @@ public void IDictionary_Generic_Keys_Enumeration_Reset(int count) IDictionary dictionary = GenericIDictionaryFactory(count); ICollection keys = dictionary.Keys; IEnumerator enumerator = keys.GetEnumerator(); -#if !NETFRAMEWORK - IReadOnlyDictionary readOnlyDictionary = dictionary; - IEnumerable readOnlyKeys = readOnlyDictionary.Keys; - IEnumerator readOnlyEnumerator = readOnlyKeys.GetEnumerator(); -#endif if (IDictionary_Generic_Keys_Values_Enumeration_ResetImplemented) - { enumerator.Reset(); -#if !NETFRAMEWORK - readOnlyEnumerator.Reset(); -#endif - } else - { Assert.Throws(() => enumerator.Reset()); -#if !NETFRAMEWORK - Assert.Throws(() => readOnlyEnumerator.Reset()); -#endif - } } #endregion @@ -530,10 +473,6 @@ public void IDictionary_Generic_Values_ContainsAllCorrectValues(int count) IDictionary dictionary = GenericIDictionaryFactory(count); IEnumerable expected = dictionary.Select((pair) => pair.Value); Assert.True(expected.SequenceEqual(dictionary.Values)); -#if !NETFRAMEWORK - IReadOnlyDictionary readOnlyDictionary = dictionary; - Assert.True(expected.SequenceEqual(readOnlyDictionary.Values)); -#endif } [Theory] @@ -552,10 +491,6 @@ public void IDictionary_Generic_Values_IncludeDuplicatesMultipleTimes(int count) dictionary.Add(missingKey, pair.Value); } Assert.Equal(count * 2, dictionary.Values.Count); -#if !NETFRAMEWORK - IReadOnlyDictionary readOnlyDictionary = dictionary; - Assert.Equal(count * 2, readOnlyDictionary.Values.Count()); -#endif } } @@ -565,18 +500,9 @@ public void IDictionary_Generic_Values_ModifyingTheDictionaryUpdatesTheCollectio { IDictionary dictionary = GenericIDictionaryFactory(count); ICollection values = dictionary.Values; -#if !NETFRAMEWORK - IReadOnlyDictionary readOnlyDictionary = dictionary; - IEnumerable readOnlyValues = readOnlyDictionary.Values; -#endif int previousCount = values.Count; if (count > 0) - { Assert.NotEmpty(values); -#if !NETFRAMEWORK - Assert.NotEmpty(readOnlyValues); -#endif - } if (!IsReadOnly) { @@ -584,16 +510,10 @@ public void IDictionary_Generic_Values_ModifyingTheDictionaryUpdatesTheCollectio if (IDictionary_Generic_Keys_Values_ModifyingTheDictionaryUpdatesTheCollection) { Assert.Empty(values); -#if !NETFRAMEWORK - Assert.Empty(readOnlyValues); -#endif } else { Assert.Equal(previousCount, values.Count); -#if !NETFRAMEWORK - Assert.Equal(previousCount, readOnlyValues.Count()); -#endif } } } @@ -607,20 +527,11 @@ public void IDictionary_Generic_Values_Enumeration_ParentDictionaryModifiedInval IDictionary dictionary = GenericIDictionaryFactory(count); ICollection values = dictionary.Values; IEnumerator valuesEnum = values.GetEnumerator(); -#if !NETFRAMEWORK - IReadOnlyDictionary readOnlyDictionary = dictionary; - IEnumerable readOnlyValues = readOnlyDictionary.Values; - IEnumerator readOnlyValuesEnum = readOnlyValues.GetEnumerator(); -#endif dictionary.Add(GetNewKey(dictionary), CreateTValue(3432)); if (count == 0 ? Enumerator_Empty_ModifiedDuringEnumeration_ThrowsInvalidOperationException : IDictionary_Generic_Keys_Values_Enumeration_ThrowsInvalidOperation_WhenParentModified) { Assert.Throws(() => valuesEnum.MoveNext()); Assert.Throws(() => valuesEnum.Reset()); -#if !NETFRAMEWORK - Assert.Throws(() => readOnlyValuesEnum.MoveNext()); - Assert.Throws(() => readOnlyValuesEnum.Reset()); -#endif } else { @@ -629,13 +540,6 @@ public void IDictionary_Generic_Values_Enumeration_ParentDictionaryModifiedInval _ = valuesEnum.Current; } valuesEnum.Reset(); -#if !NETFRAMEWORK - if (readOnlyValuesEnum.MoveNext()) - { - _ = readOnlyValuesEnum.Current; - } - readOnlyValuesEnum.Reset(); -#endif } } } @@ -659,25 +563,10 @@ public void IDictionary_Generic_Values_Enumeration_Reset(int count) IDictionary dictionary = GenericIDictionaryFactory(count); ICollection values = dictionary.Values; IEnumerator enumerator = values.GetEnumerator(); -#if !NETFRAMEWORK - IReadOnlyDictionary readOnlyDictionary = dictionary; - IEnumerable readOnlyValues = readOnlyDictionary.Values; - IEnumerator readOnlyEnumerator = readOnlyValues.GetEnumerator(); -#endif if (IDictionary_Generic_Keys_Values_Enumeration_ResetImplemented) - { enumerator.Reset(); -#if !NETFRAMEWORK - readOnlyEnumerator.Reset(); -#endif - } else - { Assert.Throws(() => enumerator.Reset()); -#if !NETFRAMEWORK - Assert.Throws(() => readOnlyEnumerator.Reset()); -#endif - } } #endregion @@ -705,8 +594,8 @@ public void IDictionary_Generic_Add_DefaultKey_DefaultValue(int count) if (DefaultValueAllowed && !IsReadOnly) { dictionary.Add(missingKey, value); - CollectionAsserts.HasCount(dictionary, count + 1); - CollectionAsserts.EqualAt(dictionary, missingKey, value); + Assert.Equal(count + 1, dictionary.Count); + Assert.Equal(value, dictionary[missingKey]); } else if (!IsReadOnly) { @@ -724,8 +613,8 @@ public void IDictionary_Generic_Add_DefaultKey_NonDefaultValue(int count) if (DefaultValueAllowed && !IsReadOnly) { dictionary.Add(missingKey, value); - CollectionAsserts.HasCount(dictionary, count + 1); - CollectionAsserts.EqualAt(dictionary, missingKey, value); + Assert.Equal(count + 1, dictionary.Count); + Assert.Equal(value, dictionary[missingKey]); } else if (!IsReadOnly) { @@ -743,8 +632,8 @@ public void IDictionary_Generic_Add_NonDefaultKey_DefaultValue(int count) TKey missingKey = GetNewKey(dictionary); TValue value = default(TValue); dictionary.Add(missingKey, value); - CollectionAsserts.HasCount(dictionary, count + 1); - CollectionAsserts.EqualAt(dictionary, missingKey, value); + Assert.Equal(count + 1, dictionary.Count); + Assert.Equal(value, dictionary[missingKey]); } } @@ -758,8 +647,8 @@ public void IDictionary_Generic_Add_NonDefaultKey_NonDefaultValue(int count) TKey missingKey = GetNewKey(dictionary); TValue value = CreateTValue(1342); dictionary.Add(missingKey, value); - CollectionAsserts.HasCount(dictionary, count + 1); - CollectionAsserts.EqualAt(dictionary, missingKey, value); + Assert.Equal(count + 1, dictionary.Count); + Assert.Equal(value, dictionary[missingKey]); } } @@ -777,10 +666,6 @@ public void IDictionary_Generic_Add_DuplicateValue(int count) dictionary.Add(GetNewKey(dictionary), duplicate); dictionary.Add(GetNewKey(dictionary), duplicate); Assert.Equal(2, dictionary.Values.Count((value) => value.Equals(duplicate))); -#if !NETFRAMEWORK - IReadOnlyDictionary readOnlyDictionary = dictionary; - Assert.Equal(2, readOnlyDictionary.Values.Count((value) => value.Equals(duplicate))); -#endif } } @@ -807,7 +692,7 @@ public void IDictionary_Generic_Add_DistinctValuesWithHashCollisions(int count) if (dictionary != null) { AddToCollection(dictionary, count); - CollectionAsserts.HasCount(dictionary, count); + Assert.Equal(count, dictionary.Count); } } } @@ -824,7 +709,7 @@ public void IDictionary_Generic_ContainsKey_ValidKeyNotContainedInDictionary(int { IDictionary dictionary = GenericIDictionaryFactory(count); TKey missingKey = GetNewKey(dictionary); - CollectionAsserts.ContainsKey(dictionary, missingKey, false); + Assert.False(dictionary.ContainsKey(missingKey)); } } @@ -837,7 +722,7 @@ public void IDictionary_Generic_ContainsKey_ValidKeyContainedInDictionary(int co IDictionary dictionary = GenericIDictionaryFactory(count); TKey missingKey = GetNewKey(dictionary); dictionary.Add(missingKey, CreateTValue(34251)); - CollectionAsserts.ContainsKey(dictionary, missingKey, true); + Assert.True(dictionary.ContainsKey(missingKey)); } } @@ -854,17 +739,13 @@ public void IDictionary_Generic_ContainsKey_DefaultKeyNotContainedInDictionary(i TKey missingKey = default(TKey); while (dictionary.ContainsKey(missingKey)) dictionary.Remove(missingKey); - CollectionAsserts.ContainsKey(dictionary, missingKey, false); + Assert.False(dictionary.ContainsKey(missingKey)); } } else { // throws ArgumentNullException Assert.Throws(() => dictionary.ContainsKey(default(TKey))); -#if !NETFRAMEWORK - IReadOnlyDictionary readOnlyDictionary = dictionary; - Assert.Throws(() => readOnlyDictionary.ContainsKey(default(TKey))); -#endif } } @@ -878,7 +759,7 @@ public void IDictionary_Generic_ContainsKey_DefaultKeyContainedInDictionary(int TKey missingKey = default(TKey); if (!dictionary.ContainsKey(missingKey)) dictionary.Add(missingKey, CreateTValue(5341)); - CollectionAsserts.ContainsKey(dictionary, missingKey, true); + Assert.True(dictionary.ContainsKey(missingKey)); } } @@ -908,7 +789,7 @@ public void IDictionary_Generic_RemoveKey_EveryKey(int count) { Assert.True(dictionary.Remove(key)); }); - CollectionAsserts.HasCount(dictionary, 0); + Assert.Empty(dictionary); } } @@ -921,7 +802,7 @@ public void IDictionary_Generic_RemoveKey_ValidKeyNotContainedInDictionary(int c IDictionary dictionary = GenericIDictionaryFactory(count); TKey missingKey = GetNewKey(dictionary); Assert.False(dictionary.Remove(missingKey)); - CollectionAsserts.HasCount(dictionary, count); + Assert.Equal(count, dictionary.Count); } } @@ -935,7 +816,7 @@ public void IDictionary_Generic_RemoveKey_ValidKeyContainedInDictionary(int coun TKey missingKey = GetNewKey(dictionary); dictionary.Add(missingKey, CreateTValue(34251)); Assert.True(dictionary.Remove(missingKey)); - CollectionAsserts.HasCount(dictionary, count); + Assert.Equal(count, dictionary.Count); } } @@ -1028,7 +909,8 @@ public void IDictionary_Generic_TryGetValue_ValidKeyNotContainedInDictionary(int IDictionary dictionary = GenericIDictionaryFactory(count); TKey missingKey = GetNewKey(dictionary); TValue value = CreateTValue(5123); - CollectionAsserts.TryGetValue(dictionary, missingKey, false); + TValue outValue; + Assert.False(dictionary.TryGetValue(missingKey, out outValue)); } [Theory] @@ -1040,8 +922,10 @@ public void IDictionary_Generic_TryGetValue_ValidKeyContainedInDictionary(int co IDictionary dictionary = GenericIDictionaryFactory(count); TKey missingKey = GetNewKey(dictionary); TValue value = CreateTValue(5123); + TValue outValue; dictionary.TryAdd(missingKey, value); - CollectionAsserts.TryGetValue(dictionary, missingKey, true, value); + Assert.True(dictionary.TryGetValue(missingKey, out outValue)); + Assert.Equal(value, outValue); } } @@ -1058,16 +942,12 @@ public void IDictionary_Generic_TryGetValue_DefaultKeyNotContainedInDictionary(i TKey missingKey = default(TKey); while (dictionary.ContainsKey(missingKey)) dictionary.Remove(missingKey); - CollectionAsserts.TryGetValue(dictionary, missingKey, false); + Assert.False(dictionary.TryGetValue(missingKey, out outValue)); } } else { Assert.Throws(() => dictionary.TryGetValue(default(TKey), out outValue)); -#if !NETFRAMEWORK - IReadOnlyDictionary readOnlyDictionary = dictionary; - Assert.Throws(() => readOnlyDictionary.TryGetValue(default(TKey), out outValue)); -#endif } } @@ -1080,8 +960,10 @@ public void IDictionary_Generic_TryGetValue_DefaultKeyContainedInDictionary(int IDictionary dictionary = GenericIDictionaryFactory(count); TKey missingKey = default(TKey); TValue value = CreateTValue(5123); + TValue outValue; dictionary.TryAdd(missingKey, value); - CollectionAsserts.TryGetValue(dictionary, missingKey, true, value); + Assert.True(dictionary.TryGetValue(missingKey, out outValue)); + Assert.Equal(value, outValue); } } diff --git a/src/libraries/Common/tests/System/Collections/IList.Generic.Tests.cs b/src/libraries/Common/tests/System/Collections/IList.Generic.Tests.cs index 6d66f143f18d6..44f67e0ca24f8 100644 --- a/src/libraries/Common/tests/System/Collections/IList.Generic.Tests.cs +++ b/src/libraries/Common/tests/System/Collections/IList.Generic.Tests.cs @@ -103,8 +103,8 @@ protected override IEnumerable GetModifyEnumerables(ModifyOper public void IList_Generic_ItemGet_NegativeIndex_ThrowsException(int count) { IList list = GenericIListFactory(count); - CollectionAsserts.ThrowsElementAt(list, -1, IList_Generic_Item_InvalidIndex_ThrowType); - CollectionAsserts.ThrowsElementAt(list, int.MinValue, IList_Generic_Item_InvalidIndex_ThrowType); + Assert.Throws(IList_Generic_Item_InvalidIndex_ThrowType, () => list[-1]); + Assert.Throws(IList_Generic_Item_InvalidIndex_ThrowType, () => list[int.MinValue]); } [Theory] @@ -112,8 +112,8 @@ public void IList_Generic_ItemGet_NegativeIndex_ThrowsException(int count) public void IList_Generic_ItemGet_IndexGreaterThanListCount_ThrowsException(int count) { IList list = GenericIListFactory(count); - CollectionAsserts.ThrowsElementAt(list, count, IList_Generic_Item_InvalidIndex_ThrowType); - CollectionAsserts.ThrowsElementAt(list, count + 1, IList_Generic_Item_InvalidIndex_ThrowType); + Assert.Throws(IList_Generic_Item_InvalidIndex_ThrowType, () => list[count]); + Assert.Throws(IList_Generic_Item_InvalidIndex_ThrowType, () => list[count + 1]); } [Theory] @@ -121,7 +121,8 @@ public void IList_Generic_ItemGet_IndexGreaterThanListCount_ThrowsException(int public void IList_Generic_ItemGet_ValidGetWithinListBounds(int count) { IList list = GenericIListFactory(count); - Assert.All(Enumerable.Range(0, count), index => CollectionAsserts.ElementAtSucceeds(list, index)); + T result; + Assert.All(Enumerable.Range(0, count), index => result = list[index]); } #endregion @@ -138,7 +139,7 @@ public void IList_Generic_ItemSet_NegativeIndex_ThrowsException(int count) T validAdd = CreateT(0); Assert.Throws(IList_Generic_Item_InvalidIndex_ThrowType, () => list[-1] = validAdd); Assert.Throws(IList_Generic_Item_InvalidIndex_ThrowType, () => list[int.MinValue] = validAdd); - CollectionAsserts.HasCount(list, count); + Assert.Equal(count, list.Count); } } @@ -152,7 +153,7 @@ public void IList_Generic_ItemSet_IndexGreaterThanListCount_ThrowsException(int T validAdd = CreateT(0); Assert.Throws(IList_Generic_Item_InvalidIndex_ThrowType, () => list[count] = validAdd); Assert.Throws(IList_Generic_Item_InvalidIndex_ThrowType, () => list[count + 1] = validAdd); - CollectionAsserts.HasCount(list, count); + Assert.Equal(count, list.Count); } } @@ -165,7 +166,7 @@ public void IList_Generic_ItemSet_OnReadOnlyList(int count) IList list = GenericIListFactory(count); T before = list[count / 2]; Assert.Throws(() => list[count / 2] = CreateT(321432)); - CollectionAsserts.EqualAt(list, count / 2, before); + Assert.Equal(before, list[count / 2]); } } @@ -178,7 +179,7 @@ public void IList_Generic_ItemSet_FirstItemToNonDefaultValue(int count) IList list = GenericIListFactory(count); T value = CreateT(123452); list[0] = value; - CollectionAsserts.EqualAt(list, 0, value); + Assert.Equal(value, list[0]); } } @@ -192,12 +193,12 @@ public void IList_Generic_ItemSet_FirstItemToDefaultValue(int count) if (DefaultValueAllowed) { list[0] = default(T); - CollectionAsserts.EqualAt(list, 0, default(T)); + Assert.Equal(default(T), list[0]); } else { Assert.Throws(() => list[0] = default(T)); - CollectionAsserts.NotEqualAt(list, 0, default(T)); + Assert.NotEqual(default(T), list[0]); } } } @@ -212,7 +213,7 @@ public void IList_Generic_ItemSet_LastItemToNonDefaultValue(int count) T value = CreateT(123452); int lastIndex = count > 0 ? count - 1 : 0; list[lastIndex] = value; - CollectionAsserts.EqualAt(list, lastIndex, value); + Assert.Equal(value, list[lastIndex]); } } @@ -227,12 +228,12 @@ public void IList_Generic_ItemSet_LastItemToDefaultValue(int count) if (DefaultValueAllowed) { list[lastIndex] = default(T); - CollectionAsserts.EqualAt(list, lastIndex, default(T)); + Assert.Equal(default(T), list[lastIndex]); } else { Assert.Throws(() => list[lastIndex] = default(T)); - CollectionAsserts.NotEqualAt(list, lastIndex, default(T)); + Assert.NotEqual(default(T), list[lastIndex]); } } } @@ -247,8 +248,8 @@ public void IList_Generic_ItemSet_DuplicateValues(int count) T value = CreateT(123452); list[0] = value; list[1] = value; - CollectionAsserts.EqualAt(list, 0, value); - CollectionAsserts.EqualAt(list, 1, value); + Assert.Equal(value, list[0]); + Assert.Equal(value, list[1]); } } @@ -395,7 +396,7 @@ public void IList_Generic_Insert_NegativeIndex_ThrowsArgumentOutOfRangeException T validAdd = CreateT(0); Assert.Throws(() => list.Insert(-1, validAdd)); Assert.Throws(() => list.Insert(int.MinValue, validAdd)); - CollectionAsserts.HasCount(list, count); + Assert.Equal(count, list.Count); } } @@ -408,8 +409,8 @@ public void IList_Generic_Insert_IndexGreaterThanListCount_Appends(int count) IList list = GenericIListFactory(count); T validAdd = CreateT(12350); list.Insert(count, validAdd); - CollectionAsserts.HasCount(list, count + 1); - CollectionAsserts.EqualAt(list, count, validAdd); + Assert.Equal(count + 1, list.Count); + Assert.Equal(validAdd, list[count]); } } @@ -421,7 +422,7 @@ public void IList_Generic_Insert_ToReadOnlyList(int count) { IList list = GenericIListFactory(count); Assert.Throws(() => list.Insert(count / 2, CreateT(321432))); - CollectionAsserts.HasCount(list, count); + Assert.Equal(count, list.Count); } } @@ -434,8 +435,8 @@ public void IList_Generic_Insert_FirstItemToNonDefaultValue(int count) IList list = GenericIListFactory(count); T value = CreateT(123452); list.Insert(0, value); - CollectionAsserts.EqualAt(list, 0, value); - CollectionAsserts.HasCount(list, count + 1); + Assert.Equal(value, list[0]); + Assert.Equal(count + 1, list.Count); } } @@ -448,8 +449,8 @@ public void IList_Generic_Insert_FirstItemToDefaultValue(int count) IList list = GenericIListFactory(count); T value = default(T); list.Insert(0, value); - CollectionAsserts.EqualAt(list, 0, value); - CollectionAsserts.HasCount(list, count + 1); + Assert.Equal(value, list[0]); + Assert.Equal(count + 1, list.Count); } } @@ -463,8 +464,8 @@ public void IList_Generic_Insert_LastItemToNonDefaultValue(int count) T value = CreateT(123452); int lastIndex = count > 0 ? count - 1 : 0; list.Insert(lastIndex, value); - CollectionAsserts.EqualAt(list, lastIndex, value); - CollectionAsserts.HasCount(list, count + 1); + Assert.Equal(value, list[lastIndex]); + Assert.Equal(count + 1, list.Count); } } @@ -478,8 +479,8 @@ public void IList_Generic_Insert_LastItemToDefaultValue(int count) T value = default(T); int lastIndex = count > 0 ? count - 1 : 0; list.Insert(lastIndex, value); - CollectionAsserts.EqualAt(list, lastIndex, value); - CollectionAsserts.HasCount(list, count + 1); + Assert.Equal(value, list[lastIndex]); + Assert.Equal(count + 1, list.Count); } } @@ -499,9 +500,9 @@ public void IList_Generic_Insert_DuplicateValues(int count) { list.Insert(0, value); list.Insert(1, value); - CollectionAsserts.EqualAt(list, 0, value); - CollectionAsserts.EqualAt(list, 1, value); - CollectionAsserts.HasCount(list, count + 2); + Assert.Equal(value, list[0]); + Assert.Equal(value, list[1]); + Assert.Equal(count + 2, list.Count); } } } @@ -534,7 +535,7 @@ public void IList_Generic_RemoveAt_NegativeIndex_ThrowsArgumentOutOfRangeExcepti T validAdd = CreateT(0); Assert.Throws(() => list.RemoveAt(-1)); Assert.Throws(() => list.RemoveAt(int.MinValue)); - CollectionAsserts.HasCount(list, count); + Assert.Equal(count, list.Count); } } @@ -548,7 +549,7 @@ public void IList_Generic_RemoveAt_IndexGreaterThanListCount_ThrowsArgumentOutOf T validAdd = CreateT(0); Assert.Throws(() => list.RemoveAt(count)); Assert.Throws(() => list.RemoveAt(count + 1)); - CollectionAsserts.HasCount(list, count); + Assert.Equal(count, list.Count); } } @@ -560,7 +561,7 @@ public void IList_Generic_RemoveAt_OnReadOnlyList(int count) { IList list = GenericIListFactory(count); Assert.Throws(() => list.RemoveAt(count / 2)); - CollectionAsserts.HasCount(list, count); + Assert.Equal(count, list.Count); } } @@ -571,11 +572,11 @@ public void IList_Generic_RemoveAt_AllValidIndices(int count) if (!IsReadOnly && !AddRemoveClear_ThrowsNotSupported) { IList list = GenericIListFactory(count); - CollectionAsserts.HasCount(list, count); + Assert.Equal(count, list.Count); Assert.All(Enumerable.Range(0, count).Reverse(), index => { list.RemoveAt(index); - CollectionAsserts.HasCount(list, index); + Assert.Equal(index, list.Count); }); } } @@ -590,7 +591,7 @@ public void IList_Generic_RemoveAt_ZeroMultipleTimes(int count) Assert.All(Enumerable.Range(0, count), index => { list.RemoveAt(0); - CollectionAsserts.HasCount(list, count - index - 1); + Assert.Equal(count - index - 1, list.Count); }); } } diff --git a/src/libraries/Common/tests/System/Collections/ISet.Generic.Tests.cs b/src/libraries/Common/tests/System/Collections/ISet.Generic.Tests.cs index 350faab6f44cb..303001e8bd144 100644 --- a/src/libraries/Common/tests/System/Collections/ISet.Generic.Tests.cs +++ b/src/libraries/Common/tests/System/Collections/ISet.Generic.Tests.cs @@ -84,8 +84,8 @@ public void ICollection_Generic_Add_ReturnValue(int count) Assert.True(set.Add(newValue)); if (!DuplicateValuesAllowed) Assert.False(set.Add(newValue)); - CollectionAsserts.HasCount(set, count + 1); - CollectionAsserts.Contains(set, newValue); + Assert.Equal(count + 1, set.Count); + Assert.True(set.Contains(newValue)); } } @@ -104,7 +104,7 @@ public void ICollection_Generic_Add_DuplicateValue_DoesNothing(int count) duplicateValue = CreateT(seed++); collection.Add(duplicateValue); collection.Add(duplicateValue); - CollectionAsserts.HasCount(collection, count + 1); + Assert.Equal(count + 1, collection.Count); } } } @@ -118,7 +118,7 @@ private void Validate_ExceptWith(ISet set, IEnumerable enumerable) if (set.Count == 0 || enumerable == set) { set.ExceptWith(enumerable); - CollectionAsserts.HasCount(set, 0); + Assert.Equal(0, set.Count); } else { @@ -126,7 +126,7 @@ private void Validate_ExceptWith(ISet set, IEnumerable enumerable) foreach (T element in enumerable) expected.Remove(element); set.ExceptWith(enumerable); - CollectionAsserts.HasCount(set, expected.Count); + Assert.Equal(expected.Count, set.Count); Assert.True(expected.SetEquals(set)); } } @@ -136,7 +136,7 @@ private void Validate_IntersectWith(ISet set, IEnumerable enumerable) if (set.Count == 0 || Enumerable.Count(enumerable) == 0) { set.IntersectWith(enumerable); - CollectionAsserts.HasCount(set, 0); + Assert.Equal(0, set.Count); } else if (set == enumerable) { @@ -152,7 +152,7 @@ private void Validate_IntersectWith(ISet set, IEnumerable enumerable) if (enumerable.Contains(value, comparer)) expected.Add(value); set.IntersectWith(enumerable); - CollectionAsserts.HasCount(set, expected.Count); + Assert.Equal(expected.Count, set.Count); Assert.True(expected.SetEquals(set)); } } @@ -178,7 +178,7 @@ private void Validate_IsProperSubsetOf(ISet set, IEnumerable enumerable) break; } } - CollectionAsserts.IsProperSubsetOf(set, enumerable, !setContainsValueNotInEnumerable && enumerableContainsValueNotInSet); + Assert.Equal(!setContainsValueNotInEnumerable && enumerableContainsValueNotInSet, set.IsProperSubsetOf(enumerable)); } private void Validate_IsProperSupersetOf(ISet set, IEnumerable enumerable) @@ -203,7 +203,7 @@ private void Validate_IsProperSupersetOf(ISet set, IEnumerable enumerable) } } isProperSuperset = isProperSuperset && setContainsElementsNotInEnumerable; - CollectionAsserts.IsProperSupersetOf(set, enumerable, isProperSuperset); + Assert.Equal(isProperSuperset, set.IsProperSupersetOf(enumerable)); } private void Validate_IsSubsetOf(ISet set, IEnumerable enumerable) @@ -212,10 +212,10 @@ private void Validate_IsSubsetOf(ISet set, IEnumerable enumerable) foreach (T value in set) if (!enumerable.Contains(value, comparer)) { - CollectionAsserts.IsSubsetOf(set, enumerable, false); + Assert.False(set.IsSubsetOf(enumerable)); return; } - CollectionAsserts.IsSubsetOf(set, enumerable, true); + Assert.True(set.IsSubsetOf(enumerable)); } private void Validate_IsSupersetOf(ISet set, IEnumerable enumerable) @@ -224,10 +224,10 @@ private void Validate_IsSupersetOf(ISet set, IEnumerable enumerable) foreach (T value in enumerable) if (!set.Contains(value, comparer)) { - CollectionAsserts.IsSupersetOf(set, enumerable, false); + Assert.False(set.IsSupersetOf(enumerable)); return; } - CollectionAsserts.IsSupersetOf(set, enumerable, true); + Assert.True(set.IsSupersetOf(enumerable)); } private void Validate_Overlaps(ISet set, IEnumerable enumerable) @@ -237,11 +237,11 @@ private void Validate_Overlaps(ISet set, IEnumerable enumerable) { if (set.Contains(value, comparer)) { - CollectionAsserts.Overlaps(set, enumerable, true); + Assert.True(set.Overlaps(enumerable)); return; } } - CollectionAsserts.Overlaps(set, enumerable, false); + Assert.False(set.Overlaps(enumerable)); } private void Validate_SetEquals(ISet set, IEnumerable enumerable) @@ -251,7 +251,7 @@ private void Validate_SetEquals(ISet set, IEnumerable enumerable) { if (!enumerable.Contains(value, comparer)) { - CollectionAsserts.SetEquals(set, enumerable, false); + Assert.False(set.SetEquals(enumerable)); return; } } @@ -259,11 +259,11 @@ private void Validate_SetEquals(ISet set, IEnumerable enumerable) { if (!set.Contains(value, comparer)) { - CollectionAsserts.SetEquals(set, enumerable, false); + Assert.False(set.SetEquals(enumerable)); return; } } - CollectionAsserts.SetEquals(set, enumerable, true); + Assert.True(set.SetEquals(enumerable)); } private void Validate_SymmetricExceptWith(ISet set, IEnumerable enumerable) @@ -277,7 +277,7 @@ private void Validate_SymmetricExceptWith(ISet set, IEnumerable enumerable if (!enumerable.Contains(element, comparer)) expected.Add(element); set.SymmetricExceptWith(enumerable); - CollectionAsserts.HasCount(set, expected.Count); + Assert.Equal(expected.Count, set.Count); Assert.True(expected.SetEquals(set)); } @@ -289,7 +289,7 @@ private void Validate_UnionWith(ISet set, IEnumerable enumerable) if (!set.Contains(element, comparer)) expected.Add(element); set.UnionWith(enumerable); - CollectionAsserts.HasCount(set, expected.Count); + Assert.Equal(expected.Count, set.Count); Assert.True(expected.SetEquals(set)); } @@ -308,15 +308,6 @@ public void ISet_Generic_NullEnumerableArgument(int count) Assert.Throws(() => set.IsSupersetOf(null)); Assert.Throws(() => set.Overlaps(null)); Assert.Throws(() => set.SetEquals(null)); -#if !NETFRAMEWORK - IReadOnlySet readOnlySet = set; - Assert.Throws(() => readOnlySet.IsProperSubsetOf(null)); - Assert.Throws(() => readOnlySet.IsProperSupersetOf(null)); - Assert.Throws(() => readOnlySet.IsSubsetOf(null)); - Assert.Throws(() => readOnlySet.IsSupersetOf(null)); - Assert.Throws(() => readOnlySet.Overlaps(null)); - Assert.Throws(() => readOnlySet.SetEquals(null)); -#endif if (!IsReadOnly) { Assert.Throws(() => set.ExceptWith(null)); @@ -511,7 +502,7 @@ public void ISet_Generic_Overlaps_Itself(int setLength) public void ISet_Generic_SetEquals_Itself(int setLength) { ISet set = GenericISetFactory(setLength); - CollectionAsserts.SetEquals(set, set, true); + Assert.True(set.SetEquals(set)); } [Theory] @@ -669,7 +660,7 @@ public void ISet_Generic_SymmetricExceptWith_AfterRemovingElements(EnumerableTyp if (!enumerable.Contains(element, comparer)) expected.Add(element); set.SymmetricExceptWith(enumerable); - CollectionAsserts.HasCount(set, expected.Count); + Assert.Equal(expected.Count, set.Count); Assert.True(expected.SetEquals(set)); } } diff --git a/src/libraries/System.Collections/tests/Generic/CollectionExtensionsTests.cs b/src/libraries/System.Collections/tests/Generic/CollectionExtensionsTests.cs index 6a5b458232fc5..213d9d6faca99 100644 --- a/src/libraries/System.Collections/tests/Generic/CollectionExtensionsTests.cs +++ b/src/libraries/System.Collections/tests/Generic/CollectionExtensionsTests.cs @@ -61,10 +61,6 @@ public void TryAdd_KeyDoesntExistInIDictionary_ReturnsTrue() IDictionary dictionary = new SortedDictionary(); Assert.True(dictionary.TryAdd("key", "value")); Assert.Equal("value", dictionary["key"]); -#if !NETFRAMEWORK - IReadOnlyDictionary readOnlyDictionary = dictionary; - Assert.Equal("value", readOnlyDictionary["key"]); -#endif } [Fact] @@ -73,10 +69,6 @@ public void TryAdd_KeyExistsInIDictionary_ReturnsFalse() IDictionary dictionary = new SortedDictionary() { ["key"] = "value" }; Assert.False(dictionary.TryAdd("key", "value2")); Assert.Equal("value", dictionary["key"]); -#if !NETFRAMEWORK - IReadOnlyDictionary readOnlyDictionary = dictionary; - Assert.Equal("value", readOnlyDictionary["key"]); -#endif } [Fact] @@ -104,10 +96,6 @@ public void Remove_KeyExistsInIDictionary_ReturnsTrue() Assert.True(dictionary.Remove("key", out var value)); Assert.Equal("value", value); Assert.Throws(() => dictionary["key"]); -#if !NETFRAMEWORK - IReadOnlyDictionary readOnlyDictionary = dictionary; - Assert.Throws(() => readOnlyDictionary["key"]); -#endif } [Fact] diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ICollection.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ICollection.cs index 24cf81efb2b44..1c1095f8a5cf7 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ICollection.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ICollection.cs @@ -9,9 +9,9 @@ namespace System.Collections.Generic { // Base interface for all collections, defining enumerators, size, and // synchronization methods. - public interface ICollection : IReadOnlyCollection + public interface ICollection : IEnumerable { - new int Count + int Count { #if MONO [DynamicDependency(nameof(Array.InternalArray__ICollection_get_Count), typeof(Array))] @@ -53,7 +53,5 @@ bool IsReadOnly [DynamicDependency(nameof(Array.InternalArray__ICollection_Remove) + "``1", typeof(Array))] #endif bool Remove(T item); - - int IReadOnlyCollection.Count => Count; } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IDictionary.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IDictionary.cs index 7e3e30d8db092..56a03106c205b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IDictionary.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IDictionary.cs @@ -9,32 +9,32 @@ namespace System.Collections.Generic // Keys can be any non-null object. Values can be any object. // You can look up a value in an IDictionary via the default indexed // property, Items. - public interface IDictionary : ICollection>, IReadOnlyDictionary + public interface IDictionary : ICollection> { // Interfaces are not serializable // The Item property provides methods to read and edit entries // in the Dictionary. - new TValue this[TKey key] + TValue this[TKey key] { get; set; } // Returns a collections of the keys in this dictionary. - new ICollection Keys + ICollection Keys { get; } // Returns a collections of the values in this dictionary. - new ICollection Values + ICollection Values { get; } // Returns whether this dictionary contains a particular key. // - new bool ContainsKey(TKey key); + bool ContainsKey(TKey key); // Adds a key-value pair to the dictionary. // @@ -44,16 +44,6 @@ public interface IDictionary : ICollection.this[TKey key] => this[key]; - - IEnumerable IReadOnlyDictionary.Keys => Keys; - - IEnumerable IReadOnlyDictionary.Values => Values; - - bool IReadOnlyDictionary.ContainsKey(TKey key) => ContainsKey(key); - - bool IReadOnlyDictionary.TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) => TryGetValue(key, out value); + bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value); } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IList.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IList.cs index ec8098c3ff7ea..f45ae823daf65 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IList.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IList.cs @@ -10,10 +10,10 @@ namespace System.Collections.Generic // An IList is an ordered collection of objects. The exact ordering // is up to the implementation of the list, ranging from a sorted // order to insertion order. - public interface IList : ICollection, IReadOnlyList + public interface IList : ICollection { // The Item property provides methods to read and edit entries in the List. - new T this[int index] + T this[int index] { #if MONO [DynamicDependency(nameof(Array.InternalArray__get_Item) + "``1", typeof(Array))] @@ -46,7 +46,5 @@ public interface IList : ICollection, IReadOnlyList [DynamicDependency(nameof(Array.InternalArray__RemoveAt), typeof(Array))] #endif void RemoveAt(int index); - - T IReadOnlyList.this[int index] => this[index]; } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ISet.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ISet.cs index ce2e8e7c5f4ff..cee05d198cda0 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ISet.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ISet.cs @@ -8,7 +8,7 @@ namespace System.Collections.Generic /// by some comparer. It also supports basic set operations such as Union, Intersection, /// Complement and Exclusive Complement. /// - public interface ISet : ICollection, IReadOnlySet + public interface ISet : ICollection { //Add ITEM to the set, return true if added, false if duplicate new bool Add(T item); @@ -26,42 +26,21 @@ public interface ISet : ICollection, IReadOnlySet void SymmetricExceptWith(IEnumerable other); //Check if this set is a subset of other - new bool IsSubsetOf(IEnumerable other); + bool IsSubsetOf(IEnumerable other); //Check if this set is a superset of other - new bool IsSupersetOf(IEnumerable other); + bool IsSupersetOf(IEnumerable other); //Check if this set is a subset of other, but not the same as it - new bool IsProperSupersetOf(IEnumerable other); + bool IsProperSupersetOf(IEnumerable other); //Check if this set is a superset of other, but not the same as it - new bool IsProperSubsetOf(IEnumerable other); + bool IsProperSubsetOf(IEnumerable other); //Check if this set has any elements in common with other - new bool Overlaps(IEnumerable other); + bool Overlaps(IEnumerable other); //Check if this set contains the same and only the same elements as other - new bool SetEquals(IEnumerable other); - - /// - /// Determines if the set contains a specific item - /// - /// The item to check if the set contains. - /// if found; otherwise . - new bool Contains(T item) => ((ICollection)this).Contains(item); - - bool IReadOnlySet.IsSubsetOf(IEnumerable other) => IsSubsetOf(other); - - bool IReadOnlySet.IsSupersetOf(IEnumerable other) => IsSupersetOf(other); - - bool IReadOnlySet.IsProperSupersetOf(IEnumerable other) => IsProperSupersetOf(other); - - bool IReadOnlySet.IsProperSubsetOf(IEnumerable other) => IsProperSubsetOf(other); - - bool IReadOnlySet.Overlaps(IEnumerable other) => Overlaps(other); - - bool IReadOnlySet.SetEquals(IEnumerable other) => SetEquals(other); - - bool IReadOnlySet.Contains(T value) => ((ICollection)this).Contains(value); + bool SetEquals(IEnumerable other); } } diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index c2f175247dc9b..a3f2a4b07e2f4 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -7803,35 +7803,29 @@ public partial interface IAsyncEnumerator : System.IAsyncDisposable T Current { get; } System.Threading.Tasks.ValueTask MoveNextAsync(); } - public partial interface ICollection : System.Collections.Generic.IEnumerable, System.Collections.IEnumerable, System.Collections.Generic.IReadOnlyCollection + public partial interface ICollection : System.Collections.Generic.IEnumerable, System.Collections.IEnumerable { - new int Count { get; } + int Count { get; } bool IsReadOnly { get; } void Add(T item); void Clear(); bool Contains(T item); void CopyTo(T[] array, int arrayIndex); bool Remove(T item); - int System.Collections.Generic.IReadOnlyCollection.Count => Count; } public partial interface IComparer { int Compare(T? x, T? y); } - public partial interface IDictionary : System.Collections.Generic.ICollection>, System.Collections.Generic.IEnumerable>, System.Collections.IEnumerable, System.Collections.Generic.IReadOnlyDictionary, System.Collections.Generic.IReadOnlyCollection> + public partial interface IDictionary : System.Collections.Generic.ICollection>, System.Collections.Generic.IEnumerable>, System.Collections.IEnumerable { - new TValue this[TKey key] { get; set; } - new System.Collections.Generic.ICollection Keys { get; } - new System.Collections.Generic.ICollection Values { get; } + TValue this[TKey key] { get; set; } + System.Collections.Generic.ICollection Keys { get; } + System.Collections.Generic.ICollection Values { get; } void Add(TKey key, TValue value); - new bool ContainsKey(TKey key); + bool ContainsKey(TKey key); bool Remove(TKey key); - new bool TryGetValue(TKey key, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(false)] out TValue value); - TValue System.Collections.Generic.IReadOnlyDictionary.this[TKey key] => this[key]; - System.Collections.Generic.IEnumerable System.Collections.Generic.IReadOnlyDictionary.Keys => Keys; - System.Collections.Generic.IEnumerable System.Collections.Generic.IReadOnlyDictionary.Values => Values; - bool System.Collections.Generic.IReadOnlyDictionary.ContainsKey(TKey key) => ContainsKey(key); - bool System.Collections.Generic.IReadOnlyDictionary.TryGetValue(TKey key, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(false)] out TValue value) => TryGetValue(key, out value); + bool TryGetValue(TKey key, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(false)] out TValue value); } public partial interface IEnumerable : System.Collections.IEnumerable { @@ -7846,13 +7840,12 @@ public partial interface IEqualityComparer bool Equals(T? x, T? y); int GetHashCode([System.Diagnostics.CodeAnalysis.DisallowNullAttribute] T obj); } - public partial interface IList : System.Collections.Generic.ICollection, System.Collections.Generic.IEnumerable, System.Collections.IEnumerable, System.Collections.Generic.IReadOnlyList, System.Collections.Generic.IReadOnlyCollection + public partial interface IList : System.Collections.Generic.ICollection, System.Collections.Generic.IEnumerable, System.Collections.IEnumerable { - new T this[int index] { get; set; } + T this[int index] { get; set; } int IndexOf(T item); void Insert(int index, T item); void RemoveAt(int index); - T System.Collections.Generic.IReadOnlyList.this[int index] => this[index]; } public partial interface IReadOnlyCollection : System.Collections.Generic.IEnumerable, System.Collections.IEnumerable { @@ -7880,27 +7873,19 @@ public partial interface IReadOnlySet : System.Collections.Generic.IEnumerabl bool Overlaps(System.Collections.Generic.IEnumerable other); bool SetEquals(System.Collections.Generic.IEnumerable other); } - public partial interface ISet : System.Collections.Generic.ICollection, System.Collections.Generic.IEnumerable, System.Collections.IEnumerable, System.Collections.Generic.IReadOnlySet, System.Collections.Generic.IReadOnlyCollection + public partial interface ISet : System.Collections.Generic.ICollection, System.Collections.Generic.IEnumerable, System.Collections.IEnumerable { new bool Add(T item); void ExceptWith(System.Collections.Generic.IEnumerable other); void IntersectWith(System.Collections.Generic.IEnumerable other); - new bool IsProperSubsetOf(System.Collections.Generic.IEnumerable other); - new bool IsProperSupersetOf(System.Collections.Generic.IEnumerable other); - new bool IsSubsetOf(System.Collections.Generic.IEnumerable other); - new bool IsSupersetOf(System.Collections.Generic.IEnumerable other); - new bool Overlaps(System.Collections.Generic.IEnumerable other); - new bool SetEquals(System.Collections.Generic.IEnumerable other); + bool IsProperSubsetOf(System.Collections.Generic.IEnumerable other); + bool IsProperSupersetOf(System.Collections.Generic.IEnumerable other); + bool IsSubsetOf(System.Collections.Generic.IEnumerable other); + bool IsSupersetOf(System.Collections.Generic.IEnumerable other); + bool Overlaps(System.Collections.Generic.IEnumerable other); + bool SetEquals(System.Collections.Generic.IEnumerable other); void SymmetricExceptWith(System.Collections.Generic.IEnumerable other); void UnionWith(System.Collections.Generic.IEnumerable other); - new bool Contains(T item) => ((ICollection)this).Contains(item); - bool System.Collections.Generic.IReadOnlySet.Contains(T item) => ((ICollection)this).Contains(item); - bool System.Collections.Generic.IReadOnlySet.IsProperSubsetOf(System.Collections.Generic.IEnumerable other) => IsProperSubsetOf(other); - bool System.Collections.Generic.IReadOnlySet.IsProperSupersetOf(System.Collections.Generic.IEnumerable other) => IsProperSupersetOf(other); - bool System.Collections.Generic.IReadOnlySet.IsSubsetOf(System.Collections.Generic.IEnumerable other) => IsSubsetOf(other); - bool System.Collections.Generic.IReadOnlySet.IsSupersetOf(System.Collections.Generic.IEnumerable other) => IsSupersetOf(other); - bool System.Collections.Generic.IReadOnlySet.Overlaps(System.Collections.Generic.IEnumerable other) => Overlaps(other); - bool System.Collections.Generic.IReadOnlySet.SetEquals(System.Collections.Generic.IEnumerable other) => SetEquals(other); } public partial class KeyNotFoundException : System.SystemException {