Skip to content
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

Merged
merged 2 commits into from
Jul 11, 2021

Conversation

tannergooding
Copy link
Member

This adds more tests covering the generic math feature for integer types.

CC. @pgovind, @stephentoub, @jeffhandley

@dotnet-issue-labeler
Copy link

Note regarding the new-api-needs-documentation label:

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.

@dotnet-issue-labeler
Copy link

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)
Copy link
Member

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?

Copy link
Member Author

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay for tests

Comment on lines +757 to +776
[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);
}
Copy link
Member

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:

Suggested change
[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);
}

Copy link
Member Author

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why delete these?

Copy link
Member Author

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.

Copy link
Member

@stephentoub stephentoub left a 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.

@lambdageek
Copy link
Member

mono interp failure looks relevant
* Assertion: should not be reached at /__w/1/s/src/mono/mono/mini/interp/transform.c:5328 is an unexpected STACK_TYPE_ in a CEE_CONV_U instruction /cc @BrzVlad

@BrzVlad
Copy link
Member

BrzVlad commented Jul 9, 2021

mono interp failures should have been fixed in #55418

@tannergooding
Copy link
Member Author

Rebased to pick up fixes.

[Fact]
public static void MinTest()
{
Assert.Equal((byte)0x00, NumberHelper<byte>.Min((byte)0x00, (byte)1));
Copy link
Contributor

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

Copy link
Contributor

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

Copy link
Contributor

@pgovind pgovind left a 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 :))

@ghost ghost locked as resolved and limited conversation to collaborators Aug 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants