-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add CollectionsMarshal.SetCount(list, count) #82146
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e58100b
Add CollectionsMarshal.SetCount(list, count)
MichalPetryka 7a590e1
Update XML doc
MichalPetryka 6056562
Add missing using
MichalPetryka 9ed8241
Fix test
MichalPetryka 28080df
Update CollectionsMarshalTests.cs
MichalPetryka 6755dc2
Update CollectionsMarshal.cs
MichalPetryka 4f3d839
Update CollectionsMarshalTests.cs
MichalPetryka 1b5961d
Update CollectionsMarshalTests.cs
MichalPetryka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 | ||
|---|---|---|---|---|
|
|
@@ -505,5 +505,62 @@ private class IntAsObject | |||
| public int Value; | ||||
| public int Property { get; set; } | ||||
| } | ||||
|
|
||||
| [Fact] | ||||
| public void ListSetCount() | ||||
| { | ||||
| List<int> list = null!; | ||||
| Assert.Throws<NullReferenceException>(() => CollectionsMarshal.SetCount(list, 3)); | ||||
|
|
||||
| Assert.Throws<ArgumentOutOfRangeException>(() => CollectionsMarshal.SetCount(list, -1)); | ||||
|
|
||||
| list = new(); | ||||
| Assert.Throws<ArgumentOutOfRangeException>(() => CollectionsMarshal.SetCount(list, -1)); | ||||
|
|
||||
| CollectionsMarshal.SetCount(list, 5); | ||||
| Assert.Equal(5, list.Count); | ||||
|
|
||||
| list = new() { 1, 2, 3, 4, 5 }; | ||||
| ref int intRef = ref MemoryMarshal.GetReference(CollectionsMarshal.AsSpan(list)); | ||||
| // make sure that size decrease preserves content | ||||
| CollectionsMarshal.SetCount(list, 3); | ||||
| Assert.Equal(3, list.Count); | ||||
| Assert.Throws<ArgumentOutOfRangeException>(() => list[3]); | ||||
| SequenceEquals<int>(CollectionsMarshal.AsSpan(list), new int[] { 1, 2, 3 }); | ||||
| Assert.True(Unsafe.AreSame(ref intRef, ref MemoryMarshal.GetReference(CollectionsMarshal.AsSpan(list)))); | ||||
|
|
||||
| // make sure that size increase preserves content and doesn't clear | ||||
| CollectionsMarshal.SetCount(list, 5); | ||||
| SequenceEquals<int>(CollectionsMarshal.AsSpan(list), new int[] { 1, 2, 3, 4, 5 }); | ||||
| Assert.True(Unsafe.AreSame(ref intRef, ref MemoryMarshal.GetReference(CollectionsMarshal.AsSpan(list)))); | ||||
|
|
||||
| // make sure that reallocations preserve content | ||||
| int newCount = list.Capacity * 2; | ||||
| CollectionsMarshal.SetCount(list, newCount); | ||||
| Assert.Equal(newCount, list.Count); | ||||
| SequenceEquals<int>(CollectionsMarshal.AsSpan(list)[..3], new int[] { 1, 2, 3 }); | ||||
| Assert.True(!Unsafe.AreSame(ref intRef, ref MemoryMarshal.GetReference(CollectionsMarshal.AsSpan(list)))); | ||||
|
|
||||
| List<string> listReference = new() { "a", "b", "c", "d", "e" }; | ||||
| ref string stringRef = ref MemoryMarshal.GetReference(CollectionsMarshal.AsSpan(listReference)); | ||||
| CollectionsMarshal.SetCount(listReference, 3); | ||||
| // verify that reference types aren't cleared | ||||
| SequenceEquals<string>(CollectionsMarshal.AsSpan(listReference), new string[] { "a", "b", "c" }); | ||||
MichalPetryka marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
| Assert.True(Unsafe.AreSame(ref stringRef, ref MemoryMarshal.GetReference(CollectionsMarshal.AsSpan(listReference)))); | ||||
| CollectionsMarshal.SetCount(listReference, 5); | ||||
| // verify that removed reference types are cleared | ||||
| SequenceEquals<string>(CollectionsMarshal.AsSpan(listReference), new string[] { "a", "b", "c", null, null }); | ||||
| Assert.True(Unsafe.AreSame(ref stringRef, ref MemoryMarshal.GetReference(CollectionsMarshal.AsSpan(listReference)))); | ||||
|
|
||||
| static void SequenceEquals<T>(ReadOnlySpan<T> actual, ReadOnlySpan<T> expected) | ||||
| { | ||||
| Assert.Equal(actual.Length, expected.Length); | ||||
|
|
||||
| for (int i = 0; i < actual.Length; i++) | ||||
| { | ||||
| Assert.Equal(actual[i], expected[i]); | ||||
| } | ||||
| } | ||||
|
Comment on lines
+555
to
+563
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's already a helper for this in AssertExtensions:
|
||||
| } | ||||
| } | ||||
| } | ||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.