-
Couldn't load subscription status.
- Fork 44
Address perf+correctness of StrLen and StrChr #480
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
9df5497
1dcd96d
7149d92
2ab0c15
2f2352e
23e2b61
20dd125
cf488a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| using System.Text; | ||
|
|
||
| namespace Cesium.Runtime.Tests; | ||
|
|
||
| public unsafe class StringFunctionTests | ||
|
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. Let's add some Unicode tests, shall we? 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. Sure, will do |
||
| { | ||
| [Theory] | ||
| [InlineData("Hello\0", 5)] | ||
| [InlineData("Goodbye\0", 7)] | ||
| [InlineData("Hello\0Goodbye\0", 5)] | ||
| [InlineData(" \0", 18)] | ||
| public void StrLen(string input, int expected) | ||
| { | ||
| // TODO: If you are rich enough to procure a 2-4+ GB RAM runner, | ||
| // please update this test to exercise the path where the string | ||
| // length exceeds int.MaxLength of bytes. | ||
| var bytes = Encoding.UTF8.GetBytes(input); | ||
| fixed (byte* str = bytes) | ||
| { | ||
| var actual = StringFunctions.StrLen(str); | ||
|
|
||
| Assert.Equal((nuint)expected, actual); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void StrLen_Null() | ||
| { | ||
| var actual = StringFunctions.StrLen(null); | ||
|
|
||
| Assert.Equal((nuint)0, actual); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("Hello\n", 5)] | ||
| [InlineData("Goodbye\n", 7)] | ||
| [InlineData("Hello\nGoodbye\n", 5)] | ||
| [InlineData(" \n", 18)] | ||
| public void StrChr(string input, int expectedOffset) | ||
| { | ||
| var needle = '\n'; | ||
| var bytes = Encoding.UTF8.GetBytes(input); | ||
| fixed (byte* str = bytes) | ||
| { | ||
| var ptr = StringFunctions.StrChr(str, '\n'); | ||
|
|
||
| Assert.Equal((byte)needle, *ptr); | ||
| Assert.Equal(expectedOffset, (int)(ptr - str)); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void StrChr_NotFound() | ||
| { | ||
| var bytes = Encoding.UTF8.GetBytes("Hello\0"); | ||
| fixed (byte* str = bytes) | ||
| { | ||
| var actual = StringFunctions.StrChr(str, '\n'); | ||
|
|
||
| Assert.True(actual is null); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void StrChr_Null() | ||
| { | ||
| var actual = StringFunctions.StrChr(null, '\0'); | ||
|
|
||
| Assert.True(actual is null); | ||
| } | ||
| } | ||
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.
After some thought, I still want us to support .NET Framework on Windows. Let's re-enable that here, and add a fallback implementation for .NET Standard.