Skip to content

Commit 9857fe0

Browse files
No-op ConcurrentStack.PushRange(T[]) if empty (#62126)
* No-op ConcurrentStack.PushRange(T[]) if empty Resolves #62121. * Add more bounds checks Assert that ArgumentOutOfRangeException is thrown if specifying indexes for an empty array with PushRange(T[], int, int). * Review feedback Allow out-of-bounds index on empty array if count is zero. * Add extra test case Add a further test case for a non-empty array but with a count parameter value of zero.
1 parent 8f87ac7 commit 9857fe0

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentStack.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ public void PushRange(T[] items, int startIndex, int count)
349349
if (count == 0)
350350
return;
351351

352-
353352
Node head, tail;
354353
head = tail = new Node(items[startIndex]);
355354
for (int i = startIndex + 1; i < startIndex + count; i++)
@@ -410,7 +409,7 @@ private static void ValidatePushPopRangeInput(T[] items, int startIndex, int cou
410409
throw new ArgumentOutOfRangeException(nameof(count), SR.ConcurrentStack_PushPopRange_CountOutOfRange);
411410
}
412411
int length = items.Length;
413-
if (startIndex >= length || startIndex < 0)
412+
if (startIndex > length || startIndex < 0)
414413
{
415414
throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ConcurrentStack_PushPopRange_StartOutOfRange);
416415
}

src/libraries/System.Collections.Concurrent/tests/ConcurrentStackTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,37 @@ public void PushRange_NoItems_NothingAdded()
6363
Assert.True(s.IsEmpty);
6464
}
6565

66+
[Fact]
67+
public void PushRange_NoItems_NothingAdded_EmptyArrayWithRangeSpecified()
68+
{
69+
var s = new ConcurrentStack<int>();
70+
Assert.True(s.IsEmpty);
71+
72+
s.PushRange(new int[0], 0, 0);
73+
Assert.True(s.IsEmpty);
74+
}
75+
76+
[Fact]
77+
public void PushRange_NoItems_NothingAdded_NonEmptyArrayWithZeroCountSpecified()
78+
{
79+
var s = new ConcurrentStack<int>();
80+
Assert.True(s.IsEmpty);
81+
82+
int[] arr = new int[2];
83+
s.PushRange(arr, arr.Length, 0);
84+
Assert.True(s.IsEmpty);
85+
}
86+
87+
[Fact]
88+
public void PushRange_NoItems_NothingAdded_EmptyArrayNoRangeSpecified()
89+
{
90+
var s = new ConcurrentStack<int>();
91+
Assert.True(s.IsEmpty);
92+
93+
s.PushRange(new int[0]);
94+
Assert.True(s.IsEmpty);
95+
}
96+
6697
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
6798
[InlineData(8, 10)]
6899
[InlineData(16, 100)]
@@ -134,6 +165,7 @@ public void PushRange_InvalidArguments_Throws()
134165
Assert.Throws<ArgumentOutOfRangeException>(() => stack.PushRange(new int[1], 0, -1));
135166
Assert.Throws<ArgumentOutOfRangeException>(() => stack.PushRange(new int[1], -1, 1));
136167
Assert.Throws<ArgumentOutOfRangeException>(() => stack.PushRange(new int[1], 2, 1));
168+
AssertExtensions.Throws<ArgumentException>(null, () => stack.PushRange(new int[0], 0, 1));
137169
AssertExtensions.Throws<ArgumentException>(null, () => stack.PushRange(new int[1], 0, 10));
138170
}
139171

0 commit comments

Comments
 (0)