Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
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 @@ -164,6 +164,67 @@ public void CopyConstructorExceptions()
AssertExtensions.Throws<ArgumentOutOfRangeException>("capacity", () => new Dictionary<int, int>(new NegativeCountDictionary<int, int>(), EqualityComparer<int>.Default));
}

[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(101)]
public void ICollection_NonGeneric_CopyTo_NonContiguousDictionary(int count)
{
ICollection collection = (ICollection)CreateDictionary(count, k => k.ToString());
KeyValuePair<string, string>[] array = new KeyValuePair<string, string>[count];
collection.CopyTo(array, 0);
int i = 0;
foreach (object obj in collection)
Assert.Equal(array[i++], obj);
}

[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(101)]
public void ICollection_Generic_CopyTo_NonContiguousDictionary(int count)
{
ICollection<KeyValuePair<string, string>> collection = CreateDictionary(count, k => k.ToString());
KeyValuePair<string, string>[] array = new KeyValuePair<string, string>[count];
collection.CopyTo(array, 0);
int i = 0;
foreach (KeyValuePair<string, string> obj in collection)
Assert.Equal(array[i++], obj);
}

[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(101)]
public void IDictionary_Generic_CopyTo_NonContiguousDictionary(int count)
{
IDictionary<string, string> collection = CreateDictionary(count, k => k.ToString());
KeyValuePair<string, string>[] array = new KeyValuePair<string, string>[count];
collection.CopyTo(array, 0);
int i = 0;
foreach (KeyValuePair<string, string> obj in collection)
Assert.Equal(array[i++], obj);
}

[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(101)]
public void CopyTo_NonContiguousDictionary(int count)
{
Dictionary<string, string> collection = (Dictionary<string, string>)CreateDictionary(count, k => k.ToString());
string[] array = new string[count];
collection.Keys.CopyTo(array, 0);
int i = 0;
foreach (KeyValuePair<string, string> obj in collection)
Assert.Equal(array[i++], obj.Key);

collection.Values.CopyTo(array, 0);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally would be separate test for Keys and Values?

i = 0;
foreach (KeyValuePair<string, string> obj in collection)
Assert.Equal(array[i++], obj.Key);
}

[Theory]
[MemberData(nameof(CopyConstructorInt32Data))]
public void CopyConstructorInt32(int size, Func<int, int> keyValueSelector, Func<IDictionary<int, int>, IDictionary<int, int>> dictionarySelector)
Expand Down Expand Up @@ -287,7 +348,10 @@ private static IEnumerable<object[]> GetCopyConstructorData<T>(Func<int, T> keyV

private static IDictionary<T, T> CreateDictionary<T>(int size, Func<int, T> keyValueSelector, IEqualityComparer<T> comparer = null)
{
return Enumerable.Range(1, size).ToDictionary(keyValueSelector, keyValueSelector, comparer);
Dictionary<T, T> dict = Enumerable.Range(0, size + 1).ToDictionary(keyValueSelector, keyValueSelector, comparer);
// Remove first item to reduce Count to size and alter the contiguity of the dictionary
dict.Remove(keyValueSelector(0));
return dict;
}

private sealed class DictionarySubclass<TKey, TValue> : Dictionary<TKey, TValue>
Expand Down