diff --git a/src/System.Collections/ref/System.Collections.cs b/src/System.Collections/ref/System.Collections.cs index 4b0599c1a43d..81db6619e614 100644 --- a/src/System.Collections/ref/System.Collections.cs +++ b/src/System.Collections/ref/System.Collections.cs @@ -94,6 +94,7 @@ public void Clear() { } public virtual void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } public virtual void OnDeserialization(object sender) { } public bool Remove(TKey key) { throw null; } + public bool Remove(TKey key, out TValue value) { throw null; } public bool TryAdd(TKey key, TValue value) { throw null; } void System.Collections.Generic.ICollection>.Add(System.Collections.Generic.KeyValuePair keyValuePair) { } bool System.Collections.Generic.ICollection>.Contains(System.Collections.Generic.KeyValuePair keyValuePair) { throw null; } diff --git a/src/System.Collections/tests/Generic/Dictionary/Dictionary.Generic.Tests.netcoreapp.cs b/src/System.Collections/tests/Generic/Dictionary/Dictionary.Generic.Tests.netcoreapp.cs new file mode 100644 index 000000000000..991dc5b8aec3 --- /dev/null +++ b/src/System.Collections/tests/Generic/Dictionary/Dictionary.Generic.Tests.netcoreapp.cs @@ -0,0 +1,88 @@ +// 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.Collections.Generic; +using System.Linq; +using Xunit; + +namespace System.Collections.Tests +{ + /// + /// Contains tests that ensure the correctness of the Dictionary class. + /// + public abstract partial class Dictionary_Generic_Tests : IDictionary_Generic_Tests + { + #region Remove(TKey) + + [Theory] + [MemberData(nameof(ValidCollectionSizes))] + public void Dictionary_Generic_RemoveKey_ValidKeyNotContainedInDictionary(int count) + { + Dictionary dictionary = (Dictionary)GenericIDictionaryFactory(count); + TValue value; + TKey missingKey = GetNewKey(dictionary); + + Assert.False(dictionary.Remove(missingKey, out value)); + Assert.Equal(count, dictionary.Count); + Assert.Equal(default(TValue), value); + } + + [Theory] + [MemberData(nameof(ValidCollectionSizes))] + public void Dictionary_Generic_RemoveKey_ValidKeyContainedInDictionary(int count) + { + Dictionary dictionary = (Dictionary)GenericIDictionaryFactory(count); + TKey missingKey = GetNewKey(dictionary); + TValue outValue; + TValue inValue = CreateTValue(count); + + dictionary.Add(missingKey, inValue); + Assert.True(dictionary.Remove(missingKey, out outValue)); + Assert.Equal(count, dictionary.Count); + Assert.Equal(inValue, outValue); + Assert.False(dictionary.TryGetValue(missingKey, out outValue)); + } + + [Theory] + [MemberData(nameof(ValidCollectionSizes))] + public void Dictionary_Generic_RemoveKey_DefaultKeyNotContainedInDictionary(int count) + { + Dictionary dictionary = (Dictionary)GenericIDictionaryFactory(count); + TValue outValue; + + if (DefaultValueAllowed) + { + TKey missingKey = default(TKey); + while (dictionary.ContainsKey(missingKey)) + dictionary.Remove(missingKey); + Assert.False(dictionary.Remove(missingKey, out outValue)); + Assert.Equal(default(TValue), outValue); + } + else + { + TValue initValue = CreateTValue(count); + outValue = initValue; + Assert.Throws(() => dictionary.Remove(default(TKey), out outValue)); + Assert.Equal(initValue, outValue); + } + } + + [Theory] + [MemberData(nameof(ValidCollectionSizes))] + public void Dictionary_Generic_RemoveKey_DefaultKeyContainedInDictionary(int count) + { + if (DefaultValueAllowed) + { + Dictionary dictionary = (Dictionary)(GenericIDictionaryFactory(count)); + TKey missingKey = default(TKey); + TValue value; + + dictionary.TryAdd(missingKey, default(TValue)); + Assert.True(dictionary.Remove(missingKey, out value)); + } + } + + #endregion + } +} diff --git a/src/System.Collections/tests/System.Collections.Tests.csproj b/src/System.Collections/tests/System.Collections.Tests.csproj index 4ba0a2c65044..d820abc79a0d 100644 --- a/src/System.Collections/tests/System.Collections.Tests.csproj +++ b/src/System.Collections/tests/System.Collections.Tests.csproj @@ -75,6 +75,7 @@ Common\System\Collections\DictionaryExtensions.cs + @@ -160,4 +161,4 @@ - + \ No newline at end of file