Skip to content

Commit

Permalink
Refactoring which numeric interface exposes which methods to match la…
Browse files Browse the repository at this point in the history
…test API review decisions (#69582)

* Refactoring which numeric interface exposes which methods to match latest API review decisions

* Refactoring the generic math tests to have a consistent order, allowing coverage to be more easily seen

* Adding test coverage for the APIs that were moved down to INumber and INumberBase

* Adding generic-math tests for System.Numerics.Complex

* Fixing some copy/paste issues that came about from resolving the Int128/UInt128 merge conflicts
  • Loading branch information
tannergooding committed May 20, 2022
1 parent 4955138 commit 8962e24
Show file tree
Hide file tree
Showing 50 changed files with 21,449 additions and 14,179 deletions.
230 changes: 207 additions & 23 deletions src/libraries/Common/tests/System/GenericMathHelpers.cs
Expand Up @@ -42,6 +42,18 @@ public static class BinaryIntegerHelper<TSelf>
public static bool TryWriteBigEndian(TSelf value, Span<byte> destination, out int bytesWritten) => value.TryWriteBigEndian(destination, out bytesWritten);

public static bool TryWriteLittleEndian(TSelf value, Span<byte> destination, out int bytesWritten) => value.TryWriteLittleEndian(destination, out bytesWritten);

public static int WriteBigEndian(TSelf value, byte[] destination) => value.WriteBigEndian(destination);

public static int WriteBigEndian(TSelf value, byte[] destination, int startIndex) => value.WriteBigEndian(destination, startIndex);

public static int WriteBigEndian(TSelf value, Span<byte> destination) => value.WriteBigEndian(destination);

public static int WriteLittleEndian(TSelf value, byte[] destination) => value.WriteLittleEndian(destination);

public static int WriteLittleEndian(TSelf value, byte[] destination, int startIndex) => value.WriteLittleEndian(destination, startIndex);

public static int WriteLittleEndian(TSelf value, Span<byte> destination) => value.WriteLittleEndian(destination);
}

public static class BinaryNumberHelper<TSelf>
Expand Down Expand Up @@ -100,9 +112,39 @@ public static class EqualityOperatorsHelper<TSelf, TOther>
public static bool op_Inequality(TSelf left, TOther right) => left != right;
}

public static class ExponentialFunctionsHelper<TSelf>
where TSelf : IExponentialFunctions<TSelf>
{
public static TSelf Exp(TSelf x) => TSelf.Exp(x);

public static TSelf ExpM1(TSelf x) => TSelf.ExpM1(x);

public static TSelf Exp2(TSelf x) => TSelf.Exp2(x);

public static TSelf Exp2M1(TSelf x) => TSelf.Exp2M1(x);

public static TSelf Exp10(TSelf x) => TSelf.Exp10(x);

public static TSelf Exp10M1(TSelf x) => TSelf.Exp10M1(x);
}

public static class FloatingPointHelper<TSelf>
where TSelf : IFloatingPoint<TSelf>
{
public static TSelf Ceiling(TSelf x) => TSelf.Ceiling(x);

public static TSelf Floor(TSelf x) => TSelf.Floor(x);

public static TSelf Round(TSelf x) => TSelf.Round(x);

public static TSelf Round(TSelf x, int digits) => TSelf.Round(x, digits);

public static TSelf Round(TSelf x, MidpointRounding mode) => TSelf.Round(x, mode);

public static TSelf Round(TSelf x, int digits, MidpointRounding mode) => TSelf.Round(x, digits, mode);

public static TSelf Truncate(TSelf x) => TSelf.Truncate(x);

public static int GetExponentByteCount(TSelf value) => value.GetExponentByteCount();

public static int GetExponentShortestBitLength(TSelf value) => value.GetExponentShortestBitLength();
Expand All @@ -118,6 +160,82 @@ public static class FloatingPointHelper<TSelf>
public static bool TryWriteSignificandBigEndian(TSelf value, Span<byte> destination, out int bytesWritten) => value.TryWriteSignificandBigEndian(destination, out bytesWritten);

public static bool TryWriteSignificandLittleEndian(TSelf value, Span<byte> destination, out int bytesWritten) => value.TryWriteSignificandLittleEndian(destination, out bytesWritten);

public static int WriteExponentBigEndian(TSelf value, byte[] destination) => value.WriteExponentBigEndian(destination);

public static int WriteExponentBigEndian(TSelf value, byte[] destination, int startIndex) => value.WriteExponentBigEndian(destination, startIndex);

public static int WriteExponentBigEndian(TSelf value, Span<byte> destination) => value.WriteExponentBigEndian(destination);

public static int WriteExponentLittleEndian(TSelf value, byte[] destination) => value.WriteExponentLittleEndian(destination);

public static int WriteExponentLittleEndian(TSelf value, byte[] destination, int startIndex) => value.WriteExponentLittleEndian(destination, startIndex);

public static int WriteExponentLittleEndian(TSelf value, Span<byte> destination) => value.WriteExponentLittleEndian(destination);

public static int WriteSignificandBigEndian(TSelf value, byte[] destination) => value.WriteSignificandBigEndian(destination);

public static int WriteSignificandBigEndian(TSelf value, byte[] destination, int startIndex) => value.WriteSignificandBigEndian(destination, startIndex);

public static int WriteSignificandBigEndian(TSelf value, Span<byte> destination) => value.WriteSignificandBigEndian(destination);

public static int WriteSignificandLittleEndian(TSelf value, byte[] destination) => value.WriteSignificandLittleEndian(destination);

public static int WriteSignificandLittleEndian(TSelf value, byte[] destination, int startIndex) => value.WriteSignificandLittleEndian(destination, startIndex);

public static int WriteSignificandLittleEndian(TSelf value, Span<byte> destination) => value.WriteSignificandLittleEndian(destination);
}

public static class FloatingPointIeee754Helper<TSelf>
where TSelf : IFloatingPointIeee754<TSelf>
{
public static TSelf E => TSelf.E;

public static TSelf Epsilon => TSelf.Epsilon;

public static TSelf NaN => TSelf.NaN;

public static TSelf NegativeInfinity => TSelf.NegativeInfinity;

public static TSelf NegativeZero => TSelf.NegativeZero;

public static TSelf Pi => TSelf.Pi;

public static TSelf PositiveInfinity => TSelf.PositiveInfinity;

public static TSelf Tau => TSelf.Tau;

public static TSelf BitDecrement(TSelf x) => TSelf.BitDecrement(x);

public static TSelf BitIncrement(TSelf x) => TSelf.BitIncrement(x);

public static TSelf FusedMultiplyAdd(TSelf left, TSelf right, TSelf addend) => TSelf.FusedMultiplyAdd(left, right, addend);

public static TSelf Ieee754Remainder(TSelf left, TSelf right) => TSelf.Ieee754Remainder(left, right);

public static int ILogB(TSelf x) => TSelf.ILogB(x);

public static TSelf ReciprocalEstimate(TSelf x) => TSelf.ReciprocalEstimate(x);

public static TSelf ReciprocalSqrtEstimate(TSelf x) => TSelf.ReciprocalSqrtEstimate(x);

public static TSelf ScaleB(TSelf x, int n) => TSelf.ScaleB(x, n);
}

public static class HyperbolicFunctionsHelper<TSelf>
where TSelf : IHyperbolicFunctions<TSelf>
{
public static TSelf Acosh(TSelf x) => TSelf.Acosh(x);

public static TSelf Asinh(TSelf x) => TSelf.Asinh(x);

public static TSelf Atanh(TSelf x) => TSelf.Atanh(x);

public static TSelf Cosh(TSelf x) => TSelf.Cosh(x);

public static TSelf Sinh(TSelf x) => TSelf.Sinh(x);

public static TSelf Tanh(TSelf x) => TSelf.Tanh(x);
}

public static class IncrementOperatorsHelper<TSelf>
Expand All @@ -128,6 +246,24 @@ public static class IncrementOperatorsHelper<TSelf>
public static TSelf op_CheckedIncrement(TSelf value) => checked(++value);
}

public static class LogarithmicFunctionsHelper<TSelf>
where TSelf : ILogarithmicFunctions<TSelf>
{
public static TSelf Log(TSelf x) => TSelf.Log(x);

public static TSelf Log(TSelf x, TSelf newBase) => TSelf.Log(x, newBase);

public static TSelf LogP1(TSelf x) => TSelf.LogP1(x);

public static TSelf Log2(TSelf x) => TSelf.Log2(x);

public static TSelf Log2P1(TSelf x) => TSelf.Log2P1(x);

public static TSelf Log10(TSelf x) => TSelf.Log10(x);

public static TSelf Log10P1(TSelf x) => TSelf.Log10P1(x);
}

public static class MinMaxValueHelper<TSelf>
where TSelf : IMinMaxValue<TSelf>
{
Expand Down Expand Up @@ -162,58 +298,72 @@ public static class NumberBaseHelper<TSelf>
public static TSelf One => TSelf.One;

public static TSelf Zero => TSelf.Zero;
}

public static class NumberHelper<TSelf>
where TSelf : INumber<TSelf>
{
public static TSelf Abs(TSelf value) => TSelf.Abs(value);

public static TSelf Clamp(TSelf value, TSelf min, TSelf max) => TSelf.Clamp(value, min, max);

public static TSelf CopySign(TSelf value, TSelf sign) => TSelf.CopySign(value, sign);

public static TSelf CreateChecked<TOther>(TOther value)
where TOther : INumber<TOther> => TSelf.CreateChecked(value);
where TOther : INumberBase<TOther> => TSelf.CreateChecked(value);

public static TSelf CreateSaturating<TOther>(TOther value)
where TOther : INumber<TOther> => TSelf.CreateSaturating(value);
where TOther : INumberBase<TOther> => TSelf.CreateSaturating(value);

public static TSelf CreateTruncating<TOther>(TOther value)
where TOther : INumber<TOther> => TSelf.CreateTruncating(value);
where TOther : INumberBase<TOther> => TSelf.CreateTruncating(value);

public static bool IsFinite(TSelf value) => TSelf.IsFinite(value);

public static bool IsInfinity(TSelf value) => TSelf.IsInfinity(value);

public static bool IsNaN(TSelf value) => TSelf.IsNaN(value);

public static bool IsNegative(TSelf value) => TSelf.IsNegative(value);

public static TSelf Max(TSelf x, TSelf y) => TSelf.Max(x, y);
public static bool IsNegativeInfinity(TSelf value) => TSelf.IsNegativeInfinity(value);

public static bool IsNormal(TSelf value) => TSelf.IsNormal(value);

public static bool IsPositiveInfinity(TSelf value) => TSelf.IsPositiveInfinity(value);

public static bool IsSubnormal(TSelf value) => TSelf.IsSubnormal(value);

public static TSelf MaxMagnitude(TSelf x, TSelf y) => TSelf.MaxMagnitude(x, y);

public static TSelf Min(TSelf x, TSelf y) => TSelf.Min(x, y);
public static TSelf MaxMagnitudeNumber(TSelf x, TSelf y) => TSelf.MaxMagnitudeNumber(x, y);

public static TSelf MinMagnitude(TSelf x, TSelf y) => TSelf.MinMagnitude(x, y);

public static TSelf Parse(string s, IFormatProvider provider) => TSelf.Parse(s, provider);
public static TSelf MinMagnitudeNumber(TSelf x, TSelf y) => TSelf.MinMagnitudeNumber(x, y);

public static TSelf Parse(string s, NumberStyles style, IFormatProvider provider) => TSelf.Parse(s, style, provider);

public static TSelf Parse(ReadOnlySpan<char> s, IFormatProvider provider) => TSelf.Parse(s, provider);

public static TSelf Parse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider) => TSelf.Parse(s, style, provider);

public static int Sign(TSelf value) => TSelf.Sign(value);

public static bool TryCreate<TOther>(TOther value, out TSelf result)
where TOther : INumber<TOther> => TSelf.TryCreate(value, out result);

public static bool TryParse(string s, IFormatProvider provider, out TSelf result) => TSelf.TryParse(s, provider, out result);
where TOther : INumberBase<TOther> => TSelf.TryCreate(value, out result);

public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out TSelf result) => TSelf.TryParse(s, style, provider, out result);

public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out TSelf result) => TSelf.TryParse(s, provider, out result);

public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out TSelf result) => TSelf.TryParse(s, style, provider, out result);
}

public static class NumberHelper<TSelf>
where TSelf : INumber<TSelf>
{
public static TSelf Clamp(TSelf value, TSelf min, TSelf max) => TSelf.Clamp(value, min, max);

public static TSelf CopySign(TSelf value, TSelf sign) => TSelf.CopySign(value, sign);

public static TSelf Max(TSelf x, TSelf y) => TSelf.Max(x, y);

public static TSelf MaxNumber(TSelf x, TSelf y) => TSelf.MaxNumber(x, y);

public static TSelf Min(TSelf x, TSelf y) => TSelf.Min(x, y);

public static TSelf MinNumber(TSelf x, TSelf y) => TSelf.MinNumber(x, y);

public static int Sign(TSelf value) => TSelf.Sign(value);
}

public static class ParsableHelper<TSelf>
where TSelf : IParsable<TSelf>
{
Expand All @@ -222,6 +372,20 @@ public static class ParsableHelper<TSelf>
public static bool TryParse(string s, IFormatProvider provider, out TSelf result) => TSelf.TryParse(s, provider, out result);
}

public static class PowerFunctionsHelper<TSelf>
where TSelf : IPowerFunctions<TSelf>
{
public static TSelf Pow(TSelf x, TSelf y) => TSelf.Pow(x, y);
}

public static class RootFunctionsHelper<TSelf>
where TSelf : IRootFunctions<TSelf>
{
public static TSelf Cbrt(TSelf x) => TSelf.Cbrt(x);

public static TSelf Sqrt(TSelf x) => TSelf.Sqrt(x);
}

public static class ShiftOperatorsHelper<TSelf, TResult>
where TSelf : IShiftOperators<TSelf, TResult>
{
Expand Down Expand Up @@ -254,6 +418,26 @@ public static class SubtractionOperatorsHelper<TSelf, TOther, TResult>
public static TResult op_CheckedSubtraction(TSelf left, TOther right) => checked(left - right);
}

public static class TrigonometricFunctionsHelper<TSelf>
where TSelf : ITrigonometricFunctions<TSelf>
{
public static TSelf Acos(TSelf x) => TSelf.Acos(x);

public static TSelf Asin(TSelf x) => TSelf.Asin(x);

public static TSelf Atan(TSelf x) => TSelf.Atan(x);

public static TSelf Atan2(TSelf y, TSelf x) => TSelf.Atan2(y, x);

public static TSelf Cos(TSelf x) => TSelf.Cos(x);

public static TSelf Sin(TSelf x) => TSelf.Sin(x);

public static (TSelf Sin, TSelf Cos) SinCos(TSelf x) => TSelf.SinCos(x);

public static TSelf Tan(TSelf x) => TSelf.Tan(x);
}

public static class UnaryNegationOperatorsHelper<TSelf, TResult>
where TSelf : IUnaryNegationOperators<TSelf, TResult>
{
Expand Down

0 comments on commit 8962e24

Please sign in to comment.