-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Adding more tests for the generic math feature #55377
Conversation
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label. |
@@ -313,11 +313,11 @@ static byte IBinaryInteger<byte>.PopCount(byte value) | |||
=> (byte)BitOperations.PopCount(value); | |||
|
|||
[RequiresPreviewFeatures] | |||
static byte IBinaryInteger<byte>.RotateLeft(byte value, byte rotateAmount) | |||
static byte IBinaryInteger<byte>.RotateLeft(byte value, int rotateAmount) |
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.
This was a mistake in the interface definition?
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.
Yesn't.
Ideally both rotate
and shift
operators would take TSelf
on the RHS. However, shift
operators require dotnet/csharplang#4666 to go in and so Rotate
should match until it does.
@@ -318,19 +318,19 @@ object IConvertible.ToType(Type type, IFormatProvider? provider) | |||
|
|||
[RequiresPreviewFeatures] | |||
static sbyte IBinaryInteger<sbyte>.LeadingZeroCount(sbyte value) | |||
=> (sbyte)(BitOperations.LeadingZeroCount((byte)value) - 16); | |||
=> (sbyte)(BitOperations.LeadingZeroCount((byte)value) - 24); |
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.
Yay for tests
[Fact] | ||
public static void TryCreateFromCharTest() | ||
{ | ||
byte result; | ||
|
||
Assert.True(NumberHelper<byte>.TryCreate<char>((char)0x0000, out result)); | ||
Assert.Equal((byte)0x00, result); | ||
|
||
Assert.True(NumberHelper<byte>.TryCreate<char>((char)0x0001, out result)); | ||
Assert.Equal((byte)0x01, result); | ||
|
||
Assert.False(NumberHelper<byte>.TryCreate<char>((char)0x7FFF, out result)); | ||
Assert.Equal((byte)0x00, result); | ||
|
||
Assert.False(NumberHelper<byte>.TryCreate<char>((char)0x8000, out result)); | ||
Assert.Equal((byte)0x00, result); | ||
|
||
Assert.False(NumberHelper<byte>.TryCreate<char>((char)0xFFFF, out result)); | ||
Assert.Equal((byte)0x00, result); | ||
} |
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.
Did you consider using theories to tighten up these tests and avoid the duplication? e.g. I prefer the conciseness and clarity of this:
[Fact] | |
public static void TryCreateFromCharTest() | |
{ | |
byte result; | |
Assert.True(NumberHelper<byte>.TryCreate<char>((char)0x0000, out result)); | |
Assert.Equal((byte)0x00, result); | |
Assert.True(NumberHelper<byte>.TryCreate<char>((char)0x0001, out result)); | |
Assert.Equal((byte)0x01, result); | |
Assert.False(NumberHelper<byte>.TryCreate<char>((char)0x7FFF, out result)); | |
Assert.Equal((byte)0x00, result); | |
Assert.False(NumberHelper<byte>.TryCreate<char>((char)0x8000, out result)); | |
Assert.Equal((byte)0x00, result); | |
Assert.False(NumberHelper<byte>.TryCreate<char>((char)0xFFFF, out result)); | |
Assert.Equal((byte)0x00, result); | |
} | |
[Theory] | |
[InlineData(0x0000, 0x00)] | |
[InlineData(0x0001, 0x01)] | |
[InlineData(0x7FFF, 0x00)] | |
[InlineData(0x8000, 0x00)] | |
[InlineData(0xFFFF, 0x00)] | |
public static void TryCreateFromCharTest(int input, int expected) | |
{ | |
Assert.True(NumberHelper<byte>.TryCreate<char>((char)input, out byte result)); | |
Assert.Equal((byte)expected, result); | |
} |
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.
I'll log an issue for this as a low hanging fruit to make these easier to manage and add new cases for in the future.
return TResult.Create(sum) / TResult.Create(values.Count()); | ||
} | ||
|
||
public static TResult StandardDeviation<TSelf, TResult>(IEnumerable<TSelf> values) |
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.
Why delete these?
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.
Accident. Must have deleted these by mistake when adding in the other tests.
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.
Spot checked and left a few comments. Thanks.
mono interp failure looks relevant |
mono interp failures should have been fixed in #55418 |
Rebased to pick up fixes. |
fe4fc18
to
0420af8
Compare
[Fact] | ||
public static void MinTest() | ||
{ | ||
Assert.Equal((byte)0x00, NumberHelper<byte>.Min((byte)0x00, (byte)1)); |
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.
I feel like Theory would be easier here, but it's a nit, so I'm not too bothered. I also don't know how you generated these. If you used a tt file, I'm sure writing a Fact
was easier
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.
Oh never mind, I saw your reply to Toub
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.
I checked a few places and everything looked good. (I know it's already merged :))
This adds more tests covering the generic math feature for integer types.
CC. @pgovind, @stephentoub, @jeffhandley