Skip to content

Fix AdaptiveCapacityDictionary assert with 11 elements #34313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 14, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public void Setup()

_smallCapDict = new AdaptiveCapacityDictionary<string, string>(capacity: 1, StringComparer.OrdinalIgnoreCase);
_smallCapDictTen = new AdaptiveCapacityDictionary<string, string>(capacity: 10, StringComparer.OrdinalIgnoreCase);
_filledSmallDictionary = new AdaptiveCapacityDictionary<string, string>(_tenValues, capacity: 10, StringComparer.OrdinalIgnoreCase);
_filledSmallDictionary = new AdaptiveCapacityDictionary<string, string>(capacity: 10, StringComparer.OrdinalIgnoreCase);
foreach (var a in _tenValues)
{
_filledSmallDictionary[a.Key] = a.Value;
}

_dict = new Dictionary<string, string>(1, StringComparer.OrdinalIgnoreCase);
_dictTen = new Dictionary<string, string>(10, StringComparer.OrdinalIgnoreCase);
Expand Down
4 changes: 3 additions & 1 deletion src/Http/Http/src/Internal/RequestCookieCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ internal static RequestCookieCollection ParseInternal(StringValues values, bool
{
return Empty;
}
var collection = new RequestCookieCollection(values.Count);

// Do not set the collection capacity based on StringValues.Count, the Cookie header is supposed to be a single combined value.
var collection = new RequestCookieCollection();
var store = collection.Store!;

if (CookieHeaderParserShared.TryParseValues(values, store, enableCookieNameEncoding, supportsMultipleValues: true))
Expand Down
8 changes: 8 additions & 0 deletions src/Http/Http/test/RequestCookiesCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,13 @@ public void AppContextSwitchUnEscapesKeysAndValues(string input, string expected
Assert.Equal(expectedKey, cookies.Keys.Single());
Assert.Equal(expectedValue, cookies[expectedKey]);
}

[Fact]
public void ParseManyCookies()
{
var cookies = RequestCookieCollection.Parse(new StringValues(new[] { "a=a", "b=b", "c=c", "d=d", "e=e", "f=f", "g=g", "h=h", "i=i", "j=j", "k=k", "l=l" }));

Assert.Equal(12, cookies.Count);
}
}
}
23 changes: 0 additions & 23 deletions src/Shared/Dictionary/AdaptiveCapacityDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,6 @@ public AdaptiveCapacityDictionary(int capacity, IEqualityComparer<TKey> comparer
else
{
_dictionaryStorage = new Dictionary<TKey, TValue>(capacity);
_arrayStorage = Array.Empty<KeyValuePair<TKey, TValue>>();
}
}

/// <summary>
/// Creates a <see cref="AdaptiveCapacityDictionary{TKey, TValue}"/> initialized with the specified <paramref name="values"/>.
/// </summary>
/// <param name="values">An object to initialize the dictionary. The value can be of type
/// <see cref="IDictionary{TKey, TValue}"/> or <see cref="IReadOnlyDictionary{TKey, TValue}"/>
/// or an object with public properties as key-value pairs.
/// </param>
/// <remarks>This constructor is unoptimized and primarily used for tests.</remarks>
/// <param name="comparer">Equality comparison.</param>
/// <param name="capacity">Initial capacity.</param>
internal AdaptiveCapacityDictionary(IEnumerable<KeyValuePair<TKey, TValue>> values, int capacity, IEqualityComparer<TKey> comparer)
{
_comparer = comparer ?? EqualityComparer<TKey>.Default;

_arrayStorage = new KeyValuePair<TKey, TValue>[capacity];

foreach (var kvp in values)
{
Add(kvp.Key, kvp.Value);
}
}

Expand Down
85 changes: 13 additions & 72 deletions src/Shared/test/Shared.Tests/AdaptiveCapacityDictionaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Microsoft.AspNetCore.Internal;
using Microsoft.AspNetCore.Testing;
Expand Down Expand Up @@ -38,66 +39,23 @@ public void CreateFromNull()
Assert.Null(dict._dictionaryStorage);
}

public static KeyValuePair<string, object>[] IEnumerableKeyValuePairData
{
get
{
return new[]
{
new KeyValuePair<string, object?>("Name", "James"),
new KeyValuePair<string, object?>("Age", 30),
new KeyValuePair<string, object?>("Address", new Address() { City = "Redmond", State = "WA" })
};
}
}

public static KeyValuePair<string, string>[] IEnumerableStringValuePairData
{
get
{
return new[]
{
new KeyValuePair<string, string>("First Name", "James"),
new KeyValuePair<string, string>("Last Name", "Henrik"),
new KeyValuePair<string, string>("Middle Name", "Bob")
};
}
}

[Fact]
public void CreateFromIEnumerableKeyValuePair_CopiesValues()
public void CreateWithCapacityOverDefaultLimit()
{
// Arrange & Act
var dict = new AdaptiveCapacityDictionary<string, object?>(IEnumerableKeyValuePairData, capacity: IEnumerableKeyValuePairData.Length, EqualityComparer<string>.Default);
// The default threshold between array and dictionary is 10. If we created one over that limit it should go directly to a dictionary.
var dict = new AdaptiveCapacityDictionary<string, string>(capacity: 12, StringComparer.OrdinalIgnoreCase);

// Assert
Assert.IsType<KeyValuePair<string, object?>[]>(dict._arrayStorage);
Assert.Collection(
dict.OrderBy(kvp => kvp.Key),
kvp =>
{
Assert.Equal("Address", kvp.Key);
var address = Assert.IsType<Address>(kvp.Value);
Assert.Equal("Redmond", address.City);
Assert.Equal("WA", address.State);
},
kvp => { Assert.Equal("Age", kvp.Key); Assert.Equal(30, kvp.Value); },
kvp => { Assert.Equal("Name", kvp.Key); Assert.Equal("James", kvp.Value); });
}
Assert.Null(dict._arrayStorage);
Assert.NotNull(dict._dictionaryStorage);

[Fact]
public void CreateFromIEnumerableStringValuePair_CopiesValues()
{
// Arrange & Act
var dict = new AdaptiveCapacityDictionary<string, string>(IEnumerableStringValuePairData, capacity: 3, StringComparer.OrdinalIgnoreCase);
for (var i = 0; i < 12; i++)
{
dict[i.ToString(CultureInfo.InvariantCulture)] = i.ToString(CultureInfo.InvariantCulture);
}

// Assert
Assert.IsType<KeyValuePair<string, string>[]>(dict._arrayStorage);
Assert.Collection(
dict.OrderBy(kvp => kvp.Key),
kvp => { Assert.Equal("First Name", kvp.Key); Assert.Equal("James", kvp.Value); },
kvp => { Assert.Equal("Last Name", kvp.Key); Assert.Equal("Henrik", kvp.Value); },
kvp => { Assert.Equal("Middle Name", kvp.Key); Assert.Equal("Bob", kvp.Value); });
Assert.Null(dict._arrayStorage);
Assert.NotNull(dict._dictionaryStorage);
Assert.Equal(12, dict.Count);
}

[Fact]
Expand All @@ -114,23 +72,6 @@ public void CreateFromIEnumerableKeyValuePair_ThrowsExceptionForDuplicateKey()
$"An element with the key 'Name' already exists in the {nameof(AdaptiveCapacityDictionary<string, object?>)}.");
}

[Fact]
public void CreateFromIEnumerableStringValuePair_ThrowsExceptionForDuplicateKey()
{
// Arrange
var values = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("name", "Billy"),
new KeyValuePair<string, string>("Name", "Joey"),
};

// Act & Assert
ExceptionAssert.ThrowsArgument(
() => new AdaptiveCapacityDictionary<string, string>(values, capacity: 3, StringComparer.OrdinalIgnoreCase),
"key",
$"An element with the key 'Name' already exists in the {nameof(AdaptiveCapacityDictionary<string, object>)}.");
}

[Fact]
public void Comparer_IsOrdinalIgnoreCase()
{
Expand Down