Skip to content

Commit 8458629

Browse files
committed
Tests
1 parent 183287f commit 8458629

File tree

3 files changed

+1515
-9
lines changed

3 files changed

+1515
-9
lines changed

src/Shared/Dictionary/SmallCapacityDictionary.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal class SmallCapacityDictionary<TKey, TValue> : IDictionary<TKey, TValue>
1919

2020
internal KeyValuePair<TKey, TValue>[] _arrayStorage;
2121
private int _count;
22-
private Dictionary<TKey, TValue>? _backup;
22+
internal Dictionary<TKey, TValue>? _backup;
2323
private IEqualityComparer<TKey> _comparer;
2424

2525
/// <summary>
@@ -28,23 +28,25 @@ internal class SmallCapacityDictionary<TKey, TValue> : IDictionary<TKey, TValue>
2828
/// </summary>
2929
/// <param name="items">The items array.</param>
3030
/// <returns>A new <see cref="SmallCapacityDictionary{TKey, TValue}"/>.</returns>
31-
public static SmallCapacityDictionary<TKey, TValue> FromArray(KeyValuePair<TKey, TValue>[] items)
31+
public static SmallCapacityDictionary<TKey, TValue> FromArray(KeyValuePair<TKey, TValue>[] items, IEqualityComparer<TKey>? comparer = null)
3232
{
3333
if (items == null)
3434
{
3535
throw new ArgumentNullException(nameof(items));
3636
}
3737

38+
comparer = comparer ?? EqualityComparer<TKey>.Default;
39+
3840
if (items.Length > DefaultArrayThreshold)
3941
{
4042
// Don't use dictionary for large arrays.
41-
var dict = new Dictionary<TKey, TValue>();
43+
var dict = new Dictionary<TKey, TValue>(comparer);
4244
foreach (var item in items)
4345
{
4446
dict[item.Key] = item.Value;
4547
}
4648

47-
return new SmallCapacityDictionary<TKey, TValue>()
49+
return new SmallCapacityDictionary<TKey, TValue>(comparer)
4850
{
4951
_backup = dict
5052
};
@@ -328,7 +330,7 @@ public void Add(TKey key, TValue value)
328330

329331
if (ContainsKeyArray(key))
330332
{
331-
throw new ArgumentException($"An element with the key '{nameof(key)}' already exists in the {nameof(SmallCapacityDictionary<TKey, TValue>)}.");
333+
throw new ArgumentException($"An element with the key '{key}' already exists in the {nameof(SmallCapacityDictionary<TKey, TValue>)}.", nameof(key));
332334
}
333335

334336
_arrayStorage[_count] = new KeyValuePair<TKey, TValue>(key, value);
@@ -618,7 +620,7 @@ private void EnsureCapacitySlow(int capacity)
618620
{
619621
if (_arrayStorage.Length < capacity)
620622
{
621-
if (capacity < DefaultArrayThreshold)
623+
if (capacity > DefaultArrayThreshold)
622624
{
623625
_backup = new Dictionary<TKey, TValue>(capacity);
624626
foreach (var item in _arrayStorage)
@@ -747,14 +749,16 @@ public void Dispose()
747749
public bool MoveNext()
748750
{
749751
var dictionary = _dictionary;
752+
if (dictionary._arrayStorage.Length >= _index)
753+
{
754+
return false;
755+
}
750756

751-
// The uncommon case is that the propertyStorage is in use
752757
Current = dictionary._arrayStorage[_index];
753758
_index++;
754759
return true;
755760
}
756761

757-
758762
/// <inheritdoc />
759763
public void Reset()
760764
{

src/Shared/test/Shared.Tests/Microsoft.AspNetCore.Shared.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks>
@@ -27,6 +27,7 @@
2727
<Compile Include="$(SharedSourceRoot)ServerInfrastructure\**\*.cs" Link="Shared\ServerInfrastructure\%(Filename)%(Extension)"/>
2828
<Compile Include="$(SharedSourceRoot)ValueTaskExtensions\**\*.cs" Link="Shared\ValueTaskExtensions\%(Filename)%(Extension)" />
2929
<Compile Include="$(SharedSourceRoot)StackTrace\StackFrame\**\*.cs" Link="Shared\StackTrace\%(Filename)%(Extension)"/>
30+
<Compile Include="$(SharedSourceRoot)Dictionary\**\*.cs" Link="Shared\Dictionary\%(Filename)%(Extension)"/>
3031
<Compile Include="$(SharedSourceRoot)TypeNameHelper\StackFrame\**\*.cs" Link="Shared\TypeNameHelper\%(Filename)%(Extension)"/>
3132
<Compile Include="$(SharedSourceRoot)ValueStopwatch\**\*.cs" Link="Shared\ValueStopwatch\%(Filename)%(Extension)"/>
3233
<Compile Include="$(SharedSourceRoot)WebEncoders\**\*.cs" Link="Shared\WebEncoders\%(Filename)%(Extension)"/>

0 commit comments

Comments
 (0)