Skip to content
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 @@ -154,13 +154,17 @@ public readonly void CopyTo(Span<KeyValuePair<string, object?>> tags)
/// <param name="array">The one-dimensional Array that is the destination of the elements copied from <see cref="T:System.Diagnostics.TagList" />. The Array must have zero-based indexing.</param>
/// <param name="arrayIndex">The zero-based index in <paramref name="array" /> at which copying begins.</param>
/// <exception cref="T:System.ArgumentNullException"> <paramref name="array" /> is null.</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException"> <paramref name="arrayIndex " /> is less than 0 or greater that or equal the <paramref name="array" /> length.</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException"> <paramref name="arrayIndex" /> is less than 0 or greater than the <paramref name="array" /> length.</exception>
public readonly void CopyTo(KeyValuePair<string, object?>[] array, int arrayIndex)
{
ArgumentNullException.ThrowIfNull(array);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual((uint)arrayIndex, (uint)array.Length, nameof(arrayIndex));
ArgumentOutOfRangeException.ThrowIfGreaterThan((uint)arrayIndex, (uint)array.Length, nameof(arrayIndex));

CopyTo(array.AsSpan(arrayIndex));
if (_tagsCount > 0)
{
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual((uint)arrayIndex, (uint)array.Length, nameof(arrayIndex));
CopyTo(array.AsSpan(arrayIndex));
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,25 @@ public readonly void CopyTo(Span<KeyValuePair<string, object?>> tags)
/// <param name="array">The one-dimensional Array that is the destination of the elements copied from <see cref="T:System.Diagnostics.TagList" />. The Array must have zero-based indexing.</param>
/// <param name="arrayIndex">The zero-based index in <paramref name="array" /> at which copying begins.</param>
/// <exception cref="T:System.ArgumentNullException"> <paramref name="array" /> is null.</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException"> <paramref name="arrayIndex " /> is less than 0 or greater that or equal the <paramref name="array" /> length.</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException"> <paramref name="arrayIndex" /> is less than 0 or greater than the <paramref name="array" /> length.</exception>
public readonly void CopyTo(KeyValuePair<string, object?>[] array, int arrayIndex)
{
ArgumentNullException.ThrowIfNull(array);

if ((uint)arrayIndex >= array.Length)
if ((uint)arrayIndex > (uint)array.Length)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
}

CopyTo(array.AsSpan(arrayIndex));
if (_tagsCount > 0)
{
if ((uint)arrayIndex >= (uint)array.Length)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
}

CopyTo(array.AsSpan(arrayIndex));
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Xunit;
using System.Collections.Generic;
using System.Linq;

namespace System.Diagnostics.Tests
{
Expand Down Expand Up @@ -82,6 +83,9 @@ public void TestCopyTo()
array = new KeyValuePair<string, object?>[tagList.Count];
tagList.CopyTo(array);
ValidateTags(tagList, array);
array = new KeyValuePair<string, object?>[tagList.Count];
((ICollection<KeyValuePair<string, object?>>)tagList).CopyTo(array, 0);
ValidateTags(tagList, array);
}
}

Expand Down Expand Up @@ -277,6 +281,31 @@ public void TestIndex(int count)
}
}

[Fact]
public void TestEmptyTagListCopyTo()
{
TagList emptyList = new TagList();
Assert.Equal(0, emptyList.Count);

KeyValuePair<string, object?>[] emptyArray = Array.Empty<KeyValuePair<string, object?>>();
emptyList.CopyTo(emptyArray, 0);
emptyList.CopyTo(emptyArray.AsSpan());
emptyList.CopyTo(emptyArray);

KeyValuePair<string, object?>[] nonEmptyArray = new KeyValuePair<string, object?>[3];
emptyList.CopyTo(nonEmptyArray, 0);
emptyList.CopyTo(nonEmptyArray, 2);
emptyList.CopyTo(nonEmptyArray, 3);
emptyList.CopyTo(nonEmptyArray.AsSpan());
emptyList.CopyTo(nonEmptyArray);

Assert.Throws<ArgumentOutOfRangeException>(() => emptyList.CopyTo(nonEmptyArray, -1));
Assert.Throws<ArgumentOutOfRangeException>(() => emptyList.CopyTo(nonEmptyArray, 4));

KeyValuePair<string, object?>[] result = emptyList.ToArray();
Assert.Empty(result);
}

[Fact]
public void TestNegativeCases()
{
Expand Down
Loading