-
-
Notifications
You must be signed in to change notification settings - Fork 4
Add polyfills for Random.NextInt64(...), Random.NextSingle(...), Random.GetItems(...), Random.Shuffle(...), Random.GetHexString(...), Random.GetString(...)
#60
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 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9be1c49
Add polyfills for `Random.NextInt64(...)`, `Random.NextSingle(...)`, …
Tyrrrz 658f503
Update PolyShim/Net80/Random.cs
Tyrrrz 1d44d01
Update PolyShim/Net100/Random.cs
Tyrrrz 92f5f64
Update PolyShim/Net60/Random.cs
Tyrrrz 738c4ce
asd
Tyrrrz 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| using System; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace PolyShim.Tests.Net100; | ||
|
|
||
| public class RandomTests | ||
| { | ||
| [Fact] | ||
| public void GetHexString_Test() | ||
| { | ||
| // Arrange | ||
| var random = new Random(0); | ||
|
|
||
| // Act & assert | ||
| for (var i = 0; i < 100; i++) | ||
| { | ||
| random.GetHexString(16).Should().MatchRegex("^[0-9A-F]{16}$"); | ||
| random.GetHexString(16, true).Should().MatchRegex("^[0-9a-f]{16}$"); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetString_Array_Test() | ||
| { | ||
| // Arrange | ||
| var random = new Random(0); | ||
| var choices = | ||
| "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".ToCharArray(); | ||
|
|
||
| // Act | ||
| for (var i = 0; i < 100; i++) | ||
| { | ||
| var str = random.GetString(choices, 16); | ||
|
|
||
| // Assert | ||
| str.Length.Should().Be(16); | ||
|
|
||
| foreach (var ch in str) | ||
| { | ||
| choices.Should().Contain(ch); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetString_Span_Test() | ||
| { | ||
| // Arrange | ||
| var random = new Random(0); | ||
| var choices = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".AsSpan(); | ||
|
|
||
| // Act | ||
| for (var i = 0; i < 100; i++) | ||
| { | ||
| var str = random.GetString(choices, 16); | ||
|
|
||
| // Assert | ||
| str.Length.Should().Be(16); | ||
|
|
||
| foreach (var ch in str) | ||
| { | ||
| choices.ToArray().Should().Contain(ch); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| using System; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace PolyShim.Tests.Net80; | ||
|
|
||
| public class RandomTests | ||
| { | ||
| [Fact] | ||
| public void GetItems_Test() | ||
| { | ||
| // Arrange | ||
| var random = new Random(0); | ||
| var choices = new[] { 1, 2, 3, 4, 5 }; | ||
|
|
||
| // Act | ||
| for (var i = 0; i < 100; i++) | ||
| { | ||
| var items = random.GetItems(choices, 3); | ||
|
|
||
| // Assert | ||
| items.Should().HaveCount(3); | ||
|
|
||
| foreach (var item in items) | ||
| { | ||
| choices.Should().Contain(item); | ||
| } | ||
| } | ||
| } | ||
Tyrrrz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| [Fact] | ||
| public void Shuffle_Test() | ||
| { | ||
| // Arrange | ||
| var random = new Random(0); | ||
| var originalItems = new[] { 1, 2, 3, 4, 5 }; | ||
|
|
||
| // Act | ||
| for (var i = 0; i < 100; i++) | ||
| { | ||
| var items = (int[])originalItems.Clone(); | ||
| random.Shuffle(items); | ||
|
|
||
| // Assert | ||
| items.Should().HaveCount(originalItems.Length); | ||
| items.Should().BeEquivalentTo(originalItems); | ||
|
|
||
| foreach (var item in items) | ||
| { | ||
| originalItems.Should().Contain(item); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| #if (NETCOREAPP && !NET10_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD) | ||
| #nullable enable | ||
| // ReSharper disable RedundantUsingDirective | ||
| // ReSharper disable CheckNamespace | ||
| // ReSharper disable InconsistentNaming | ||
| // ReSharper disable PartialTypeWithSinglePart | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| internal static partial class PolyfillExtensions | ||
| { | ||
| extension(Random random) | ||
| { | ||
| // https://learn.microsoft.com/dotnet/api/system.random.gethexstring#system-random-gethexstring(system-int32-system-boolean) | ||
| public string GetHexString(int stringLength, bool lowercase = false) | ||
| { | ||
| var bytes = new byte[(stringLength + 1) / 2]; | ||
| random.NextBytes(bytes); | ||
|
|
||
| return lowercase ? Convert.ToHexStringLower(bytes) : Convert.ToHexString(bytes); | ||
Tyrrrz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Signature-compatible replacement for GetString(ReadOnlySpan<char>, int) | ||
| // https://learn.microsoft.com/dotnet/api/system.random.getstring | ||
| public string GetString(char[] choices, int length) | ||
| { | ||
| var buffer = new StringBuilder(); | ||
|
|
||
| for (var i = 0; i < length; i++) | ||
| { | ||
| var index = random.Next(choices.Length); | ||
| buffer.Append(choices[index]); | ||
| } | ||
|
|
||
| return buffer.ToString(); | ||
| } | ||
|
|
||
| #if FEATURE_MEMORY | ||
| // https://learn.microsoft.com/dotnet/api/system.random.getstring | ||
| public string GetString(ReadOnlySpan<char> choices, int length) => | ||
| random.GetString(choices.ToArray(), length); | ||
| #endif | ||
| } | ||
| } | ||
| #endif | ||
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 |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #if (NETCOREAPP && !NET8_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD) | ||
| #nullable enable | ||
| // ReSharper disable RedundantUsingDirective | ||
| // ReSharper disable CheckNamespace | ||
| // ReSharper disable InconsistentNaming | ||
| // ReSharper disable PartialTypeWithSinglePart | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
|
|
||
| internal static partial class PolyfillExtensions | ||
| { | ||
| extension(Random random) | ||
| { | ||
| // https://learn.microsoft.com/dotnet/api/system.random.getitems#system-random-getitems-1(-0()-system-int32) | ||
| public T[] GetItems<T>(T[] choices, int length) | ||
| { | ||
| var result = new T[length]; | ||
| var selectedIndices = new HashSet<int>(); | ||
|
|
||
| while (selectedIndices.Count < length) | ||
| { | ||
| var index = random.Next(choices.Length); | ||
| if (selectedIndices.Add(index)) | ||
| { | ||
| result[selectedIndices.Count - 1] = choices[index]; | ||
| } | ||
| } | ||
|
|
||
Tyrrrz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return result; | ||
| } | ||
|
|
||
| // https://learn.microsoft.com/dotnet/api/system.random.shuffle#system-random-shuffle-1(-0()) | ||
| public void Shuffle<T>(T[] items) | ||
| { | ||
| for (var i = items.Length - 1; i > 0; i--) | ||
| { | ||
| var j = random.Next(i + 1); | ||
| (items[i], items[j]) = (items[j], items[i]); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| #endif | ||
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
Oops, something went wrong.
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.