Skip to content
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

No-op ConcurrentStack.PushRange(T[]) if empty #62126

Merged
merged 4 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -316,6 +316,11 @@ public void PushRange(T[] items)
{
throw new ArgumentNullException(nameof(items));
}

// No op if the length is zero
if (items.Length == 0)
martincostello marked this conversation as resolved.
Show resolved Hide resolved
return;

PushRange(items, 0, items.Length);
}

Expand Down Expand Up @@ -349,7 +354,6 @@ public void PushRange(T[] items, int startIndex, int count)
if (count == 0)
return;


Node head, tail;
head = tail = new Node(items[startIndex]);
for (int i = startIndex + 1; i < startIndex + count; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ public void PushRange_NoItems_NothingAdded()
Assert.True(s.IsEmpty);
}

[Fact]
public void PushRange_NoItems_NothingAdded_NoRangeSpecified()
{
var s = new ConcurrentStack<int>();
Assert.True(s.IsEmpty);

s.PushRange(new int[0]);
Assert.True(s.IsEmpty);
}
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[InlineData(8, 10)]
[InlineData(16, 100)]
Expand Down Expand Up @@ -131,6 +141,8 @@ public void PushRange_InvalidArguments_Throws()
var stack = new ConcurrentStack<int>();
Assert.Throws<ArgumentNullException>(() => stack.PushRange(null));
Assert.Throws<ArgumentNullException>(() => stack.PushRange(null, 0, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => stack.PushRange(new int[0], 0, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => stack.PushRange(new int[0], 0, 1));
Assert.Throws<ArgumentOutOfRangeException>(() => stack.PushRange(new int[1], 0, -1));
Assert.Throws<ArgumentOutOfRangeException>(() => stack.PushRange(new int[1], -1, 1));
Assert.Throws<ArgumentOutOfRangeException>(() => stack.PushRange(new int[1], 2, 1));
Expand Down