This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expose/test String.Create span-based method #23872
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,64 @@ public static void Ctor_CharSpan(char[] valueArray, int startIndex, int length, | |
| Assert.Equal(expected, new string(span)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void Create_InvalidArguments_Throw() | ||
| { | ||
| AssertExtensions.Throws<ArgumentNullException>("action", () => string.Create(-1, 0, null)); | ||
| AssertExtensions.Throws<ArgumentOutOfRangeException>("length", () => string.Create(-1, 0, (span, state) => { })); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void Create_Length0_ReturnsEmptyString() | ||
| { | ||
| bool actionInvoked = false; | ||
| Assert.Same(string.Empty, string.Create(0, 0, (span, state) => actionInvoked = true)); | ||
| Assert.False(actionInvoked); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void Create_NullState_Allowed() | ||
| { | ||
| string result = string.Create(1, (object)null, (span, state) => | ||
| { | ||
| span[0] = 'a'; | ||
| Assert.Null(state); | ||
| }); | ||
| Assert.Equal("a", result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void Create_ClearsMemory() | ||
| { | ||
| const int Length = 10; | ||
| string result = string.Create(Length, (object)null, (span, state) => | ||
| { | ||
| for (int i = 0; i < span.Length; i++) | ||
| { | ||
| Assert.Equal('\0', span[i]); | ||
| } | ||
| }); | ||
| Assert.Equal(new string('\0', Length), result); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("a")] | ||
| [InlineData("this is a test")] | ||
| [InlineData("\0\u8001\u8002\ufffd\u1234\ud800\udfff")] | ||
| public static void Create_ReturnsExpectedString(string expected) | ||
| { | ||
| char[] input = expected.ToCharArray(); | ||
| string result = string.Create(input.Length, input, (span, state) => | ||
|
Contributor
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. What's supposed to happen when
Member
Author
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. It should throw. I think in my tired stupor I subconsciously saw "span" and thought "that can't be null" :) |
||
| { | ||
| Assert.Same(input, state); | ||
| for (int i = 0; i < state.Length; i++) | ||
| { | ||
| span[i] = state[i]; | ||
| } | ||
| }); | ||
| Assert.Equal(expected, result); | ||
| } | ||
|
|
||
| [Theory] | ||
| // CurrentCulture | ||
| [InlineData("Hello", "ello", StringComparison.CurrentCulture, true)] | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that I expect anything weird to happen, but seeing the empty action here made me wonder if it's worth having a non-zero length with the empty action. (which should just be equal to
new string((char)0, length)Actually, I guess really it's ensuring that we're using cleared memory, since the other tests set every char position we don't have that assurance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion, @bartonjs. I added a test for it.