From 7ef94b0afbe185d104af262a397abde21a87aee5 Mon Sep 17 00:00:00 2001 From: WinCPP Date: Sun, 9 Apr 2017 11:10:52 +0530 Subject: [PATCH 1/3] Issue #15622 New overload Dictionary.Remove --- .../ref/System.Collections.cs | 1 + .../Dictionary/Dictionary.Generic.Tests.cs | 71 +++++++++++++++++++ 2 files changed, 72 insertions(+) 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.cs b/src/System.Collections/tests/Generic/Dictionary/Dictionary.Generic.Tests.cs index 3298856b6c91..13e8d3653585 100644 --- a/src/System.Collections/tests/Generic/Dictionary/Dictionary.Generic.Tests.cs +++ b/src/System.Collections/tests/Generic/Dictionary/Dictionary.Generic.Tests.cs @@ -154,5 +154,76 @@ public void IReadOnlyDictionary_Generic_Values_ContainsAllCorrectValues(int coun } #endregion + + #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 + { + outValue = CreateTValue(count); + Assert.Throws(() => dictionary.Remove(default(TKey), out outValue)); + Assert.Equal(default(TValue), 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 } } From 7c8de871268e8fe7b994b490a70cad255763de51 Mon Sep 17 00:00:00 2001 From: WinCPP Date: Wed, 12 Apr 2017 00:06:19 +0530 Subject: [PATCH 2/3] Issue #15622 New overload Dictionary.Remove --- .../tests/Generic/Dictionary/Dictionary.Generic.Tests.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/System.Collections/tests/Generic/Dictionary/Dictionary.Generic.Tests.cs b/src/System.Collections/tests/Generic/Dictionary/Dictionary.Generic.Tests.cs index 13e8d3653585..c51c0c3a23bc 100644 --- a/src/System.Collections/tests/Generic/Dictionary/Dictionary.Generic.Tests.cs +++ b/src/System.Collections/tests/Generic/Dictionary/Dictionary.Generic.Tests.cs @@ -203,9 +203,10 @@ public void Dictionary_Generic_RemoveKey_DefaultKeyNotContainedInDictionary(int } else { - outValue = CreateTValue(count); + TValue initValue = CreateTValue(count); + outValue = initValue; Assert.Throws(() => dictionary.Remove(default(TKey), out outValue)); - Assert.Equal(default(TValue), outValue); + Assert.Equal(initValue, outValue); } } From 4402509677c39753a85119151aa3cdc87d20cf87 Mon Sep 17 00:00:00 2001 From: WinCPP Date: Fri, 14 Apr 2017 15:28:41 +0530 Subject: [PATCH 3/3] Limit tests for Remove(K, out V) to netcoreapp --- .../Dictionary/Dictionary.Generic.Tests.cs | 72 --------------- .../Dictionary.Generic.Tests.netcoreapp.cs | 88 +++++++++++++++++++ .../tests/System.Collections.Tests.csproj | 3 +- 3 files changed, 90 insertions(+), 73 deletions(-) create mode 100644 src/System.Collections/tests/Generic/Dictionary/Dictionary.Generic.Tests.netcoreapp.cs diff --git a/src/System.Collections/tests/Generic/Dictionary/Dictionary.Generic.Tests.cs b/src/System.Collections/tests/Generic/Dictionary/Dictionary.Generic.Tests.cs index c51c0c3a23bc..3298856b6c91 100644 --- a/src/System.Collections/tests/Generic/Dictionary/Dictionary.Generic.Tests.cs +++ b/src/System.Collections/tests/Generic/Dictionary/Dictionary.Generic.Tests.cs @@ -154,77 +154,5 @@ public void IReadOnlyDictionary_Generic_Values_ContainsAllCorrectValues(int coun } #endregion - - #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/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