Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/System.Collections/ref/System.Collections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<System.Collections.Generic.KeyValuePair<TKey, TValue>>.Add(System.Collections.Generic.KeyValuePair<TKey, TValue> keyValuePair) { }
bool System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey, TValue>>.Contains(System.Collections.Generic.KeyValuePair<TKey, TValue> keyValuePair) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Contains tests that ensure the correctness of the Dictionary class.
/// </summary>
public abstract partial class Dictionary_Generic_Tests<TKey, TValue> : IDictionary_Generic_Tests<TKey, TValue>
{
#region Remove(TKey)

[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void Dictionary_Generic_RemoveKey_ValidKeyNotContainedInDictionary(int count)
{
Dictionary<TKey, TValue> dictionary = (Dictionary<TKey, TValue>)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<TKey, TValue> dictionary = (Dictionary<TKey, TValue>)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<TKey, TValue> dictionary = (Dictionary<TKey, TValue>)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<ArgumentNullException>(() => 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<TKey, TValue> dictionary = (Dictionary<TKey, TValue>)(GenericIDictionaryFactory(count));
TKey missingKey = default(TKey);
TValue value;

dictionary.TryAdd(missingKey, default(TValue));
Assert.True(dictionary.Remove(missingKey, out value));
}
}

#endregion
}
}
3 changes: 2 additions & 1 deletion src/System.Collections/tests/System.Collections.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<Link>Common\System\Collections\DictionaryExtensions.cs</Link>
</Compile>
<!-- Generic tests -->
<Compile Include="Generic\Dictionary\Dictionary.Generic.Tests.netcoreapp.cs" Condition="'$(TargetGroup)' == 'netcoreapp'"/>
<Compile Include="Generic\SortedSet\SortedSet.TreeSubSet.Tests.cs" />
<Compile Include="StructuralComparisonsTests.cs" />
<Compile Include="BitArray\BitArray_CtorTests.cs" />
Expand Down Expand Up @@ -160,4 +161,4 @@
</Compile>
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>
</Project>