-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue-44801 EnsureCapacity Apis For List Stack Queue (#47149)
* issue-44801 Initial commit: public List.EnsureCapacity. * Create Stack.EnsureCapacity(). * issue-44801 Fix comment for Stack. * issue-44801 Fix comment: Move temp MaxArrayLength into near usage scope for Stack. * issue-44801 Fix comment for List. * issue-44801 Create EnsureCapacity for Queue. * issue-44801 Create tests for List.EnsureCapacity. * issue-44801 Create tests for Stack.EnsureCapacity. * issue-44801 Create tests for Queue.EnsureCapacity. * issue-44801 Update version if updating internal buffer. * issue-44801 Update test cases to verify invalidating enumeration (List, Stack, Queue). * issue-44801 Improve code change for List: 1. Avoid incrementing version number twice in one Insert (or Add) method call; 2. Avoid more capacity check for Insert (or Add) method. * issue-44801 Fix comments: consider integer overflow; refactor methods. * Update src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs Fix comment: update xml doc. Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com> * ensure same algorithm is used for all resize operations * revert array length check in List.EnsureCapacityCore * remove GrowFactor constant * skip large capacity tests on mono. * Fix overflow handling when capacity < MaxArrayLength; add clarifying comments Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
- Loading branch information
1 parent
0db5b46
commit 28be257
Showing
8 changed files
with
432 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/libraries/System.Collections/tests/Generic/List/List.Generic.Tests.EnsureCapacity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Generic; | ||
using Xunit; | ||
|
||
namespace System.Collections.Tests | ||
{ | ||
/// <summary> | ||
/// Contains tests that ensure the correctness of the List class. | ||
/// </summary> | ||
public abstract partial class List_Generic_Tests<T> : IList_Generic_Tests<T> | ||
{ | ||
[Theory] | ||
[MemberData(nameof(ValidCollectionSizes))] | ||
public void EnsureCapacity_RequestingLargerCapacity_DoesInvalidateEnumeration(int count) | ||
{ | ||
List<T> list = GenericListFactory(count); | ||
IEnumerator<T> copiedListEnumerator = new List<T>(list).GetEnumerator(); | ||
IEnumerator<T> enumerator = list.GetEnumerator(); | ||
var capacity = list.Capacity; | ||
|
||
list.EnsureCapacity(capacity + 1); | ||
|
||
Assert.Throws<InvalidOperationException>(() => enumerator.MoveNext()); | ||
} | ||
|
||
[Fact] | ||
public void EnsureCapacity_NotInitialized_RequestedZero_ReturnsZero() | ||
{ | ||
var list = new List<T>(); | ||
Assert.Equal(0, list.EnsureCapacity(0)); | ||
Assert.Equal(0, list.Capacity); | ||
} | ||
|
||
[Fact] | ||
public void EnsureCapacity_NegativeCapacityRequested_Throws() | ||
{ | ||
var list = new List<T>(); | ||
AssertExtensions.Throws<ArgumentOutOfRangeException>("capacity", () => list.EnsureCapacity(-1)); | ||
} | ||
|
||
const int MaxArraySize = 0X7FEFFFFF; | ||
|
||
[Theory] | ||
[InlineData(5, MaxArraySize + 1)] | ||
[InlineData(1, int.MaxValue)] | ||
[SkipOnMono("mono forces no restrictions on array size.")] | ||
public void EnsureCapacity_LargeCapacity_Throws(int count, int requestCapacity) | ||
{ | ||
List<T> list = GenericListFactory(count); | ||
Assert.Throws<OutOfMemoryException>(() => list.EnsureCapacity(requestCapacity)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(5)] | ||
public void EnsureCapacity_RequestedCapacitySmallerThanOrEqualToCurrent_CapacityUnchanged(int currentCapacity) | ||
{ | ||
var list = new List<T>(currentCapacity); | ||
|
||
for (int requestCapacity = 0; requestCapacity <= currentCapacity; requestCapacity++) | ||
{ | ||
Assert.Equal(currentCapacity, list.EnsureCapacity(requestCapacity)); | ||
Assert.Equal(currentCapacity, list.Capacity); | ||
} | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(ValidCollectionSizes))] | ||
public void EnsureCapacity_RequestedCapacitySmallerThanOrEqualToCount_CapacityUnchanged(int count) | ||
{ | ||
List<T> list = GenericListFactory(count); | ||
var currentCapacity = list.Capacity; | ||
|
||
for (int requestCapacity = 0; requestCapacity <= count; requestCapacity++) | ||
{ | ||
Assert.Equal(currentCapacity, list.EnsureCapacity(requestCapacity)); | ||
Assert.Equal(currentCapacity, list.Capacity); | ||
} | ||
} | ||
|
||
[Theory] | ||
[InlineData(0)] | ||
[InlineData(1)] | ||
[InlineData(5)] | ||
public void EnsureCapacity_CapacityIsAtLeastTheRequested(int count) | ||
{ | ||
List<T> list = GenericListFactory(count); | ||
|
||
int currentCapacity = list.Capacity; | ||
int requestCapacity = currentCapacity + 1; | ||
int newCapacity = list.EnsureCapacity(requestCapacity); | ||
Assert.InRange(newCapacity, requestCapacity, int.MaxValue); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(ValidCollectionSizes))] | ||
public void EnsureCapacity_RequestingLargerCapacity_DoesNotImpactListContent(int count) | ||
{ | ||
List<T> list = GenericListFactory(count); | ||
var copiedList = new List<T>(list); | ||
|
||
list.EnsureCapacity(list.Capacity + 1); | ||
Assert.Equal(copiedList, list); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.