diff --git a/src/libraries/Common/tests/System/GenericMathHelpers.cs b/src/libraries/Common/tests/System/GenericMathHelpers.cs index a02f8a4e162a6..76c9eb33ffb45 100644 --- a/src/libraries/Common/tests/System/GenericMathHelpers.cs +++ b/src/libraries/Common/tests/System/GenericMathHelpers.cs @@ -25,6 +25,43 @@ public static class BinaryIntegerHelper { public static (TSelf Quotient, TSelf Remainder) DivRem(TSelf left, TSelf right) => TSelf.DivRem(left, right); + public static (TSelf Quotient, TSelf Remainder) DivRem(TSelf left, TSelf right, DivisionRounding mode) => TSelf.DivRem(left, right, mode); + + public static TSelf Divide(TSelf left, TSelf right, DivisionRounding mode) => TSelf.Divide(left, right, mode); + + public static TSelf Remainder(TSelf left, TSelf right, DivisionRounding mode) => TSelf.Remainder(left, right, mode); + + public static TSelf DivideRoundingCorrection(TSelf left, TSelf right, DivisionRounding mode) => mode switch + { + _ when left % right == TSelf.Zero => TSelf.Zero, + DivisionRounding.Truncate => TSelf.Zero, + DivisionRounding.Floor when right > TSelf.Zero ^ left > TSelf.Zero => -TSelf.One, + DivisionRounding.Ceiling when right > TSelf.Zero ^ left < TSelf.Zero => TSelf.One, + DivisionRounding.AwayFromZero => (right > TSelf.Zero ^ left > TSelf.Zero) ? -TSelf.One : TSelf.One, + DivisionRounding.Euclidean when left < TSelf.Zero => (right > TSelf.Zero) ? -TSelf.One : TSelf.One, + _ => TSelf.Zero, + }; + + public static TSelf RemainderRoundingCorrection(TSelf left, TSelf right, DivisionRounding mode) => mode switch + { + _ when left % right == TSelf.Zero => TSelf.Zero, + DivisionRounding.Truncate => TSelf.Zero, + DivisionRounding.Floor when right > TSelf.Zero ^ left > TSelf.Zero => right, + DivisionRounding.Ceiling when right > TSelf.Zero ^ left < TSelf.Zero => -right, + DivisionRounding.AwayFromZero => (right > TSelf.Zero ^ left > TSelf.Zero) ? right : -right, + DivisionRounding.Euclidean when left < TSelf.Zero => (right > TSelf.Zero) ? right : -right, + _ => TSelf.Zero, + }; + + public static (TSelf Quotient, TSelf Remainder) DivRemExpected(TSelf left, TSelf right, DivisionRounding mode) => + (left / right + DivideRoundingCorrection(left, right, mode), left % right + RemainderRoundingCorrection(left, right, mode)); + + public static TSelf DivideExpected(TSelf left, TSelf right, DivisionRounding mode) + => left / right + DivideRoundingCorrection(left, right, mode); + + public static TSelf RemainderExpected(TSelf left, TSelf right, DivisionRounding mode) + => left % right + RemainderRoundingCorrection(left, right, mode); + public static TSelf LeadingZeroCount(TSelf value) => TSelf.LeadingZeroCount(value); public static TSelf PopCount(TSelf value) => TSelf.PopCount(value); diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index d920dc227a376..35cd227bad5d5 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -2767,6 +2767,7 @@ + @@ -2807,4 +2808,4 @@ - \ No newline at end of file + diff --git a/src/libraries/System.Private.CoreLib/src/System/Numerics/DivisionRounding.cs b/src/libraries/System.Private.CoreLib/src/System/Numerics/DivisionRounding.cs new file mode 100644 index 0000000000000..087c519f0b8e2 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Numerics/DivisionRounding.cs @@ -0,0 +1,48 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Numerics +{ + /// + /// Specifies the rounding strategy to use when performing division. + /// + public enum DivisionRounding + { + /// + /// Truncated division (rounding toward zero) — round the division result towards zero. + /// + Truncate = 0, + // Graph for truncated division with positive divisor https://www.wolframalpha.com/input?i=Plot%5B%7BIntegerPart%5Bn%5D%2C+n+-+IntegerPart%5Bn%5D%7D%2C+%7Bn%2C+-3%2C+3%7D%5D + // Graph for truncated division with negative divisor https://www.wolframalpha.com/input?i=Plot%5B%7BIntegerPart%5B-n%5D%2C+n+%2B+IntegerPart%5B-n%5D%7D%2C+%7Bn%2C+-3%2C+3%7D%5D + + /// + /// Floor division (rounding down) — round the division result down to the next lower integer. + /// + Floor = 1, + // Graph for floor division with positive divisor https://www.wolframalpha.com/input?i=Plot%5B%7BFloor%5Bn%5D%2C+n+-+Floor%5Bn%5D%7D%2C+%7Bn%2C+-3%2C+3%7D%5D + // Graph for floor division with negative divisor https://www.wolframalpha.com/input?i=Plot%5B%7BFloor%5B-n%5D%2C+n+%2B+Floor%5B-n%5D%7D%2C+%7Bn%2C+-3%2C+3%7D%5D + + /// + /// Ceiling division (rounding up) — round the division result up to the next higher integer. + /// + Ceiling = 2, + // Graph for ceiling division with positive divisor https://www.wolframalpha.com/input?i=Plot%5B%7BCeiling%5Bn%5D%2C+n+-+Ceiling%5Bn%5D%7D%2C+%7Bn%2C+-3%2C+3%7D%5D + // Graph for ceiling division with negative divisor https://www.wolframalpha.com/input?i=Plot%5B%7BCeiling%5B-n%5D%2C+n+%2B+Ceiling%5B-n%5D%7D%2C+%7Bn%2C+-3%2C+3%7D%5D + + /// + /// AwayFromZero division (rounding away zero — round the division result away from zero to the nearest integer. + /// + AwayFromZero = 3, + // Graph for AwayFromZero division with positive divisor https://www.wolframalpha.com/input?i=Plot%5B%7BIntegerPart%5Bn%5D+%2B+Sign%5Bn%5D%2C+n+-+IntegerPart%5Bn%5D+-+Sign%5Bn%5D%7D%2C+%7Bn%2C+-3%2C+3%7D%5D + // Graph for AwayFromZero division with negative divisor https://www.wolframalpha.com/input?i=Plot%5B%7BIntegerPart%5B-n%5D+%2B+Sign%5B-n%5D%2C+n+%2B+IntegerPart%5B-n%5D+%2B+Sign%5B-n%5D%7D%2C+%7Bn%2C+-3%2C+3%7D%5D + + /// + /// Euclidean division ensures a non-negative remainder: + /// for positive divisor — round the division result down to the next lower integer (rounding down); + /// for negative divisor — round the division result up to the next higher integer (rounding up); + /// + Euclidean = 4, + // Graph for Euclidean division with positive divisor https://www.wolframalpha.com/input?i=Plot%5B%7BFloor%5Bn%5D%2C+n+-+Floor%5Bn%5D%7D%2C+%7Bn%2C+-3%2C+3%7D%5D + // Graph for Euclidean division with negative divisor https://www.wolframalpha.com/input?i=Plot%5B%7BCeiling%5B-n%5D%2C+n+%2B+Ceiling%5B-n%5D%7D%2C+%7Bn%2C+-3%2C+3%7D%5D + } +} diff --git a/src/libraries/System.Private.CoreLib/src/System/Numerics/IBinaryInteger.cs b/src/libraries/System.Private.CoreLib/src/System/Numerics/IBinaryInteger.cs index a150b61e2c40e..32ae2d59c9aac 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Numerics/IBinaryInteger.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Numerics/IBinaryInteger.cs @@ -20,6 +20,207 @@ static virtual (TSelf Quotient, TSelf Remainder) DivRem(TSelf left, TSelf right) return (quotient, (left - (quotient * right))); } + /// Computes the quotient and remainder of two values using the specified division rounding mode. + /// The value which divides. + /// The value which divides . + /// The mode. + /// The quotient and remainder of divided-by with the specified division rounding mode. + static virtual (TSelf Quotient, TSelf Remainder) DivRem(TSelf left, TSelf right, DivisionRounding mode) + { + (TSelf quotient, TSelf remainder) = TSelf.DivRem(left, right); + + if (TSelf.IsZero(remainder)) + return (quotient, remainder); + + switch (mode) + { + case DivisionRounding.Truncate: + break; + case DivisionRounding.Floor: + if (TSelf.IsPositive(left) != TSelf.IsPositive(right)) + { + quotient--; + remainder += right; + } + break; + case DivisionRounding.Ceiling: + { + if (TSelf.IsPositive(left) == TSelf.IsPositive(right)) + { + quotient++; + remainder -= right; + } + break; + } + case DivisionRounding.AwayFromZero: + { + if (TSelf.IsPositive(left) != TSelf.IsPositive(right)) + { + quotient--; + remainder += right; + } + else + { + quotient++; + remainder -= right; + } + break; + } + case DivisionRounding.Euclidean: + { + if (TSelf.IsNegative(left)) + { + if (TSelf.IsPositive(right)) + { + quotient--; + remainder += right; + } + else + { + quotient++; + remainder -= right; + } + } + break; + } + default: + ThrowHelper.ThrowArgumentException_InvalidEnumValue(mode); + break; + } + + return (quotient, remainder); + } + + /// Computes the quotient of two values using the specified division rounding mode. + /// The value which divides. + /// The value which divides . + /// The mode. + /// The quotient of divided-by with the specified division rounding mode. + static virtual TSelf Divide(TSelf left, TSelf right, DivisionRounding mode) + { + (TSelf quotient, TSelf remainder) = TSelf.DivRem(left, right); + + if (TSelf.IsZero(remainder)) + return quotient; + + switch (mode) + { + case DivisionRounding.Truncate: + break; + case DivisionRounding.Floor: + if (TSelf.IsPositive(left) != TSelf.IsPositive(right)) + { + quotient--; + } + break; + case DivisionRounding.Ceiling: + { + if (TSelf.IsPositive(left) == TSelf.IsPositive(right)) + { + quotient++; + } + break; + } + case DivisionRounding.AwayFromZero: + { + if (TSelf.IsPositive(left) != TSelf.IsPositive(right)) + { + quotient--; + } + else + { + quotient++; + } + break; + } + case DivisionRounding.Euclidean: + { + if (TSelf.IsNegative(left)) + { + if (TSelf.IsPositive(right)) + { + quotient--; + } + else + { + quotient++; + } + } + break; + } + default: + ThrowHelper.ThrowArgumentException_InvalidEnumValue(mode); + break; + } + + return quotient; + } + + /// Computes the remainder of two values using the specified division rounding mode. + /// The value which divides. + /// The value which divides . + /// The mode. + /// The remainder of divided-by with the specified division rounding mode. + static virtual TSelf Remainder(TSelf left, TSelf right, DivisionRounding mode) + { + TSelf remainder = left % right; + + if (TSelf.IsZero(remainder)) + return remainder; + + switch (mode) + { + case DivisionRounding.Truncate: + break; + case DivisionRounding.Floor: + if (TSelf.IsPositive(left) != TSelf.IsPositive(right)) + { + remainder += right; + } + break; + case DivisionRounding.Ceiling: + { + if (TSelf.IsPositive(left) == TSelf.IsPositive(right)) + { + remainder -= right; + } + break; + } + case DivisionRounding.AwayFromZero: + { + if (TSelf.IsPositive(left) != TSelf.IsPositive(right)) + { + remainder += right; + } + else + { + remainder -= right; + } + break; + } + case DivisionRounding.Euclidean: + { + if (TSelf.IsNegative(left)) + { + if (TSelf.IsPositive(right)) + { + remainder += right; + } + else + { + remainder -= right; + } + } + break; + } + default: + ThrowHelper.ThrowArgumentException_InvalidEnumValue(mode); + break; + } + + return remainder; + } + /// Computes the number of leading zero bits in a value. /// The value whose leading zero bits are to be counted. /// The number of leading zero bits in . diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index b5ecc14826bfa..8ff30d3f440f8 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -11142,6 +11142,14 @@ public static partial class BitOperations [System.CLSCompliantAttribute(false)] public static int TrailingZeroCount(nuint value) { throw null; } } + public enum DivisionRounding + { + Truncate = 0, + Floor = 1, + Ceiling = 2, + AwayFromZero = 3, + Euclidean = 4, + } public partial interface IAdditionOperators where TSelf : System.Numerics.IAdditionOperators? { static abstract TResult operator +(TSelf left, TOther right); @@ -11156,7 +11164,10 @@ public partial interface IBinaryFloatingPointIeee754 : System.IComparable } public partial interface IBinaryInteger : System.IComparable, System.IComparable, System.IEquatable, System.IFormattable, System.IParsable, System.ISpanFormattable, System.ISpanParsable, System.Numerics.IAdditionOperators, System.Numerics.IAdditiveIdentity, System.Numerics.IBinaryNumber, System.Numerics.IBitwiseOperators, System.Numerics.IComparisonOperators, System.Numerics.IDecrementOperators, System.Numerics.IDivisionOperators, System.Numerics.IEqualityOperators, System.Numerics.IIncrementOperators, System.Numerics.IModulusOperators, System.Numerics.IMultiplicativeIdentity, System.Numerics.IMultiplyOperators, System.Numerics.INumber, System.Numerics.INumberBase, System.Numerics.IShiftOperators, System.Numerics.ISubtractionOperators, System.Numerics.IUnaryNegationOperators, System.Numerics.IUnaryPlusOperators where TSelf : System.Numerics.IBinaryInteger? { + static virtual TSelf Divide(TSelf left, TSelf right, System.Numerics.DivisionRounding mode) { throw null; } static virtual (TSelf Quotient, TSelf Remainder) DivRem(TSelf left, TSelf right) { throw null; } + static virtual (TSelf Quotient, TSelf Remainder) DivRem(TSelf left, TSelf right, System.Numerics.DivisionRounding mode) { throw null; } + static virtual TSelf Remainder(TSelf left, TSelf right, System.Numerics.DivisionRounding mode) { throw null; } int GetByteCount(); int GetShortestBitLength(); static virtual TSelf LeadingZeroCount(TSelf value) { throw null; } diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/ByteTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/ByteTests.GenericMath.cs index 348795de8b82e..5fb58d6b7d084 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/ByteTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/ByteTests.GenericMath.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Globalization; +using System.Numerics; using System.Runtime.InteropServices; using Xunit; @@ -51,11 +52,122 @@ public static void AdditiveIdentityTest() [Fact] public static void DivRemTest() { - Assert.Equal(((byte)0x00, (byte)0x00), BinaryIntegerHelper.DivRem((byte)0x00, (byte)2)); - Assert.Equal(((byte)0x00, (byte)0x01), BinaryIntegerHelper.DivRem((byte)0x01, (byte)2)); - Assert.Equal(((byte)0x3F, (byte)0x01), BinaryIntegerHelper.DivRem((byte)0x7F, (byte)2)); - Assert.Equal(((byte)0x40, (byte)0x00), BinaryIntegerHelper.DivRem((byte)0x80, (byte)2)); - Assert.Equal(((byte)0x7F, (byte)0x01), BinaryIntegerHelper.DivRem((byte)0xFF, (byte)2)); + Assert.Equal(((byte)0x00, (byte)0x00), BinaryIntegerHelper.DivRem(0x00, 0x02)); + Assert.Equal(((byte)0x00, (byte)0x01), BinaryIntegerHelper.DivRem(0x01, 0x02)); + Assert.Equal(((byte)0x01, (byte)0x00), BinaryIntegerHelper.DivRem(0x02, 0x02)); + Assert.Equal(((byte)0x3F, (byte)0x00), BinaryIntegerHelper.DivRem(0x7E, 0x02)); + Assert.Equal(((byte)0x3F, (byte)0x01), BinaryIntegerHelper.DivRem(0x7F, 0x02)); + Assert.Equal(((byte)0x40, (byte)0x00), BinaryIntegerHelper.DivRem(0x80, 0x02)); + Assert.Equal(((byte)0x40, (byte)0x01), BinaryIntegerHelper.DivRem(0x81, 0x02)); + Assert.Equal(((byte)0x7F, (byte)0x00), BinaryIntegerHelper.DivRem(0xFE, 0x02)); + Assert.Equal(((byte)0x7F, (byte)0x01), BinaryIntegerHelper.DivRem(0xFF, 0x02)); + + Assert.Equal(((byte)0x00, (byte)0x00), BinaryIntegerHelper.DivRem(0x00, 0xFE)); + Assert.Equal(((byte)0x00, (byte)0x01), BinaryIntegerHelper.DivRem(0x01, 0xFE)); + Assert.Equal(((byte)0x00, (byte)0x02), BinaryIntegerHelper.DivRem(0x02, 0xFE)); + Assert.Equal(((byte)0x00, (byte)0x7E), BinaryIntegerHelper.DivRem(0x7E, 0xFE)); + Assert.Equal(((byte)0x00, (byte)0x7F), BinaryIntegerHelper.DivRem(0x7F, 0xFE)); + Assert.Equal(((byte)0x00, (byte)0x80), BinaryIntegerHelper.DivRem(0x80, 0xFE)); + Assert.Equal(((byte)0x00, (byte)0x81), BinaryIntegerHelper.DivRem(0x81, 0xFE)); + Assert.Equal(((byte)0x01, (byte)0x00), BinaryIntegerHelper.DivRem(0xFE, 0xFE)); + Assert.Equal(((byte)0x01, (byte)0x01), BinaryIntegerHelper.DivRem(0xFF, 0xFE)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem(0x00, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(0x01, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(0xFF, 0)); + } + + [Fact] + public static void DivRemModeTest() + { + foreach (var mode in (DivisionRounding[]) Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x00, 0x02, mode), BinaryIntegerHelper.DivRem(0x00, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x01, 0x02, mode), BinaryIntegerHelper.DivRem(0x01, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x02, 0x02, mode), BinaryIntegerHelper.DivRem(0x02, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x7E, 0x02, mode), BinaryIntegerHelper.DivRem(0x7E, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x7F, 0x02, mode), BinaryIntegerHelper.DivRem(0x7F, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x80, 0x02, mode), BinaryIntegerHelper.DivRem(0x80, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x81, 0x02, mode), BinaryIntegerHelper.DivRem(0x81, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0xFE, 0x02, mode), BinaryIntegerHelper.DivRem(0xFE, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0xFF, 0x02, mode), BinaryIntegerHelper.DivRem(0xFF, 0x02, mode)); + + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x00, 0xFE, mode), BinaryIntegerHelper.DivRem(0x00, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x01, 0xFE, mode), BinaryIntegerHelper.DivRem(0x01, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x02, 0xFE, mode), BinaryIntegerHelper.DivRem(0x02, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x7E, 0xFE, mode), BinaryIntegerHelper.DivRem(0x7E, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x7F, 0xFE, mode), BinaryIntegerHelper.DivRem(0x7F, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x80, 0xFE, mode), BinaryIntegerHelper.DivRem(0x80, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x81, 0xFE, mode), BinaryIntegerHelper.DivRem(0x81, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0xFE, 0xFE, mode), BinaryIntegerHelper.DivRem(0xFE, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0xFF, 0xFE, mode), BinaryIntegerHelper.DivRem(0xFF, 0xFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem(0x00, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(0x01, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(0xFF, 0, mode)); + } + } + + [Fact] + public static void DivideModeTest() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x00, 0x02, mode), BinaryIntegerHelper.Divide(0x00, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x01, 0x02, mode), BinaryIntegerHelper.Divide(0x01, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x02, 0x02, mode), BinaryIntegerHelper.Divide(0x02, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x7E, 0x02, mode), BinaryIntegerHelper.Divide(0x7E, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x7F, 0x02, mode), BinaryIntegerHelper.Divide(0x7F, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x80, 0x02, mode), BinaryIntegerHelper.Divide(0x80, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x81, 0x02, mode), BinaryIntegerHelper.Divide(0x81, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0xFE, 0x02, mode), BinaryIntegerHelper.Divide(0xFE, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0xFF, 0x02, mode), BinaryIntegerHelper.Divide(0xFF, 0x02, mode)); + + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x00, 0xFE, mode), BinaryIntegerHelper.Divide(0x00, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x01, 0xFE, mode), BinaryIntegerHelper.Divide(0x01, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x02, 0xFE, mode), BinaryIntegerHelper.Divide(0x02, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x7E, 0xFE, mode), BinaryIntegerHelper.Divide(0x7E, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x7F, 0xFE, mode), BinaryIntegerHelper.Divide(0x7F, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x80, 0xFE, mode), BinaryIntegerHelper.Divide(0x80, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x81, 0xFE, mode), BinaryIntegerHelper.Divide(0x81, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0xFE, 0xFE, mode), BinaryIntegerHelper.Divide(0xFE, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0xFF, 0xFE, mode), BinaryIntegerHelper.Divide(0xFF, 0xFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Divide(0x00, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide(0x01, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide(0xFF, 0, mode)); + } + } + + [Fact] + public static void RemainderModeTest() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x00, 0x02, mode), BinaryIntegerHelper.Remainder(0x00, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x01, 0x02, mode), BinaryIntegerHelper.Remainder(0x01, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x02, 0x02, mode), BinaryIntegerHelper.Remainder(0x02, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x7E, 0x02, mode), BinaryIntegerHelper.Remainder(0x7E, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x7F, 0x02, mode), BinaryIntegerHelper.Remainder(0x7F, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x80, 0x02, mode), BinaryIntegerHelper.Remainder(0x80, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x81, 0x02, mode), BinaryIntegerHelper.Remainder(0x81, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0xFE, 0x02, mode), BinaryIntegerHelper.Remainder(0xFE, 0x02, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0xFF, 0x02, mode), BinaryIntegerHelper.Remainder(0xFF, 0x02, mode)); + + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x00, 0xFE, mode), BinaryIntegerHelper.Remainder(0x00, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x01, 0xFE, mode), BinaryIntegerHelper.Remainder(0x01, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x02, 0xFE, mode), BinaryIntegerHelper.Remainder(0x02, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x7E, 0xFE, mode), BinaryIntegerHelper.Remainder(0x7E, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x7F, 0xFE, mode), BinaryIntegerHelper.Remainder(0x7F, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x80, 0xFE, mode), BinaryIntegerHelper.Remainder(0x80, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x81, 0xFE, mode), BinaryIntegerHelper.Remainder(0x81, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0xFE, 0xFE, mode), BinaryIntegerHelper.Remainder(0xFE, 0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0xFF, 0xFE, mode), BinaryIntegerHelper.Remainder(0xFF, 0xFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Remainder(0x00, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder(0x01, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder(0xFF, 0, mode)); + } } [Fact] diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/CharTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/CharTests.GenericMath.cs index f1ef4f9bc1960..4dd73a0d29249 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/CharTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/CharTests.GenericMath.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Numerics; using System.Runtime.InteropServices; using Xunit; @@ -50,11 +51,122 @@ public static void AdditiveIdentityTest() [Fact] public static void DivRemTest() { - Assert.Equal(((char)0x0000, (char)0x0000), BinaryIntegerHelper.DivRem((char)0x0000, (char)2)); - Assert.Equal(((char)0x0000, (char)0x0001), BinaryIntegerHelper.DivRem((char)0x0001, (char)2)); - Assert.Equal(((char)0x3FFF, (char)0x0001), BinaryIntegerHelper.DivRem((char)0x7FFF, (char)2)); - Assert.Equal(((char)0x4000, (char)0x0000), BinaryIntegerHelper.DivRem((char)0x8000, (char)2)); - Assert.Equal(((char)0x7FFF, (char)0x0001), BinaryIntegerHelper.DivRem((char)0xFFFF, (char)2)); + Assert.Equal(((char)0x0000, (char)0x0000), BinaryIntegerHelper.DivRem((char)0x0000, (char)0x0002)); + Assert.Equal(((char)0x0000, (char)0x0001), BinaryIntegerHelper.DivRem((char)0x0001, (char)0x0002)); + Assert.Equal(((char)0x0001, (char)0x0000), BinaryIntegerHelper.DivRem((char)0x0002, (char)0x0002)); + Assert.Equal(((char)0x3FFF, (char)0x0000), BinaryIntegerHelper.DivRem((char)0x7FFE, (char)0x0002)); + Assert.Equal(((char)0x3FFF, (char)0x0001), BinaryIntegerHelper.DivRem((char)0x7FFF, (char)0x0002)); + Assert.Equal(((char)0x4000, (char)0x0000), BinaryIntegerHelper.DivRem((char)0x8000, (char)0x0002)); + Assert.Equal(((char)0x4000, (char)0x0001), BinaryIntegerHelper.DivRem((char)0x8001, (char)0x0002)); + Assert.Equal(((char)0x7FFF, (char)0x0000), BinaryIntegerHelper.DivRem((char)0xFFFE, (char)0x0002)); + Assert.Equal(((char)0x7FFF, (char)0x0001), BinaryIntegerHelper.DivRem((char)0xFFFF, (char)0x0002)); + + Assert.Equal(((char)0x0000, (char)0x0000), BinaryIntegerHelper.DivRem((char)0x0000, (char)0xFFFE)); + Assert.Equal(((char)0x0000, (char)0x0001), BinaryIntegerHelper.DivRem((char)0x0001, (char)0xFFFE)); + Assert.Equal(((char)0x0000, (char)0x0002), BinaryIntegerHelper.DivRem((char)0x0002, (char)0xFFFE)); + Assert.Equal(((char)0x0000, (char)0x7FFE), BinaryIntegerHelper.DivRem((char)0x7FFE, (char)0xFFFE)); + Assert.Equal(((char)0x0000, (char)0x7FFF), BinaryIntegerHelper.DivRem((char)0x7FFF, (char)0xFFFE)); + Assert.Equal(((char)0x0000, (char)0x8000), BinaryIntegerHelper.DivRem((char)0x8000, (char)0xFFFE)); + Assert.Equal(((char)0x0000, (char)0x8001), BinaryIntegerHelper.DivRem((char)0x8001, (char)0xFFFE)); + Assert.Equal(((char)0x0001, (char)0x0000), BinaryIntegerHelper.DivRem((char)0xFFFE, (char)0xFFFE)); + Assert.Equal(((char)0x0001, (char)0x0001), BinaryIntegerHelper.DivRem((char)0xFFFF, (char)0xFFFE)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem((char)0x0000, (char)0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((char)0x0001, (char)0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((char)0xFFFF, (char)0)); + } + + [Fact] + public static void DivRemModeTest() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivRemExpected((char)0x0000, (char)0x0002, mode), BinaryIntegerHelper.DivRem((char)0x0000, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((char)0x0001, (char)0x0002, mode), BinaryIntegerHelper.DivRem((char)0x0001, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((char)0x0002, (char)0x0002, mode), BinaryIntegerHelper.DivRem((char)0x0002, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((char)0x7FFE, (char)0x0002, mode), BinaryIntegerHelper.DivRem((char)0x7FFE, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((char)0x7FFF, (char)0x0002, mode), BinaryIntegerHelper.DivRem((char)0x7FFF, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((char)0x8000, (char)0x0002, mode), BinaryIntegerHelper.DivRem((char)0x8000, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((char)0x8001, (char)0x0002, mode), BinaryIntegerHelper.DivRem((char)0x8001, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((char)0xFFFE, (char)0x0002, mode), BinaryIntegerHelper.DivRem((char)0xFFFE, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((char)0xFFFF, (char)0x0002, mode), BinaryIntegerHelper.DivRem((char)0xFFFF, (char)0x0002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivRemExpected((char)0x0000, (char)0xFFFE, mode), BinaryIntegerHelper.DivRem((char)0x0000, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((char)0x0001, (char)0xFFFE, mode), BinaryIntegerHelper.DivRem((char)0x0001, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((char)0x0002, (char)0xFFFE, mode), BinaryIntegerHelper.DivRem((char)0x0002, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((char)0x7FFE, (char)0xFFFE, mode), BinaryIntegerHelper.DivRem((char)0x7FFE, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((char)0x7FFF, (char)0xFFFE, mode), BinaryIntegerHelper.DivRem((char)0x7FFF, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((char)0x8000, (char)0xFFFE, mode), BinaryIntegerHelper.DivRem((char)0x8000, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((char)0x8001, (char)0xFFFE, mode), BinaryIntegerHelper.DivRem((char)0x8001, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((char)0xFFFE, (char)0xFFFE, mode), BinaryIntegerHelper.DivRem((char)0xFFFE, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((char)0xFFFF, (char)0xFFFE, mode), BinaryIntegerHelper.DivRem((char)0xFFFF, (char)0xFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem((char)0x0000, (char)0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((char)0x0001, (char)0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((char)0xFFFF, (char)0, mode)); + } + } + + [Fact] + public static void DivideModeTest() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivideExpected((char)0x0000, (char)0x0002, mode), BinaryIntegerHelper.Divide((char)0x0000, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((char)0x0001, (char)0x0002, mode), BinaryIntegerHelper.Divide((char)0x0001, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((char)0x0002, (char)0x0002, mode), BinaryIntegerHelper.Divide((char)0x0002, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((char)0x7FFE, (char)0x0002, mode), BinaryIntegerHelper.Divide((char)0x7FFE, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((char)0x7FFF, (char)0x0002, mode), BinaryIntegerHelper.Divide((char)0x7FFF, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((char)0x8000, (char)0x0002, mode), BinaryIntegerHelper.Divide((char)0x8000, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((char)0x8001, (char)0x0002, mode), BinaryIntegerHelper.Divide((char)0x8001, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((char)0xFFFE, (char)0x0002, mode), BinaryIntegerHelper.Divide((char)0xFFFE, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((char)0xFFFF, (char)0x0002, mode), BinaryIntegerHelper.Divide((char)0xFFFF, (char)0x0002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivideExpected((char)0x0000, (char)0xFFFE, mode), BinaryIntegerHelper.Divide((char)0x0000, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((char)0x0001, (char)0xFFFE, mode), BinaryIntegerHelper.Divide((char)0x0001, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((char)0x0002, (char)0xFFFE, mode), BinaryIntegerHelper.Divide((char)0x0002, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((char)0x7FFE, (char)0xFFFE, mode), BinaryIntegerHelper.Divide((char)0x7FFE, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((char)0x7FFF, (char)0xFFFE, mode), BinaryIntegerHelper.Divide((char)0x7FFF, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((char)0x8000, (char)0xFFFE, mode), BinaryIntegerHelper.Divide((char)0x8000, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((char)0x8001, (char)0xFFFE, mode), BinaryIntegerHelper.Divide((char)0x8001, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((char)0xFFFE, (char)0xFFFE, mode), BinaryIntegerHelper.Divide((char)0xFFFE, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((char)0xFFFF, (char)0xFFFE, mode), BinaryIntegerHelper.Divide((char)0xFFFF, (char)0xFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Divide((char)0x0000, (char)0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide((char)0x0001, (char)0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide((char)0xFFFF, (char)0, mode)); + } + } + + [Fact] + public static void RemainderModeTest() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.RemainderExpected((char)0x0000, (char)0x0002, mode), BinaryIntegerHelper.Remainder((char)0x0000, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((char)0x0001, (char)0x0002, mode), BinaryIntegerHelper.Remainder((char)0x0001, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((char)0x0002, (char)0x0002, mode), BinaryIntegerHelper.Remainder((char)0x0002, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((char)0x7FFE, (char)0x0002, mode), BinaryIntegerHelper.Remainder((char)0x7FFE, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((char)0x7FFF, (char)0x0002, mode), BinaryIntegerHelper.Remainder((char)0x7FFF, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((char)0x8000, (char)0x0002, mode), BinaryIntegerHelper.Remainder((char)0x8000, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((char)0x8001, (char)0x0002, mode), BinaryIntegerHelper.Remainder((char)0x8001, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((char)0xFFFE, (char)0x0002, mode), BinaryIntegerHelper.Remainder((char)0xFFFE, (char)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((char)0xFFFF, (char)0x0002, mode), BinaryIntegerHelper.Remainder((char)0xFFFF, (char)0x0002, mode)); + + Assert.Equal(BinaryIntegerHelper.RemainderExpected((char)0x0000, (char)0xFFFE, mode), BinaryIntegerHelper.Remainder((char)0x0000, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((char)0x0001, (char)0xFFFE, mode), BinaryIntegerHelper.Remainder((char)0x0001, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((char)0x0002, (char)0xFFFE, mode), BinaryIntegerHelper.Remainder((char)0x0002, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((char)0x7FFE, (char)0xFFFE, mode), BinaryIntegerHelper.Remainder((char)0x7FFE, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((char)0x7FFF, (char)0xFFFE, mode), BinaryIntegerHelper.Remainder((char)0x7FFF, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((char)0x8000, (char)0xFFFE, mode), BinaryIntegerHelper.Remainder((char)0x8000, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((char)0x8001, (char)0xFFFE, mode), BinaryIntegerHelper.Remainder((char)0x8001, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((char)0xFFFE, (char)0xFFFE, mode), BinaryIntegerHelper.Remainder((char)0xFFFE, (char)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((char)0xFFFF, (char)0xFFFE, mode), BinaryIntegerHelper.Remainder((char)0xFFFF, (char)0xFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Remainder((char)0x0000, (char)0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder((char)0x0001, (char)0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder((char)0xFFFF, (char)0, mode)); + } } [Fact] diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int128Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int128Tests.GenericMath.cs index 66de69ce2eec7..6b3a9bc643b37 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int128Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int128Tests.GenericMath.cs @@ -1,7 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Globalization; +using System.Numerics; using System.Runtime.InteropServices; using Xunit; @@ -101,11 +101,122 @@ public static void AdditiveIdentityTest() [Fact] public static void DivRemTest() { - Assert.Equal((Zero, Zero), BinaryIntegerHelper.DivRem(Zero, 2)); - Assert.Equal((Zero, One), BinaryIntegerHelper.DivRem(One, 2)); - Assert.Equal((new Int128(0x3FFF_FFFF_FFFF_FFFF, 0xFFFF_FFFF_FFFF_FFFF), One), BinaryIntegerHelper.DivRem(MaxValue, 2)); - Assert.Equal((new Int128(0xC000_0000_0000_0000, 0x0000_0000_0000_0000), Zero), BinaryIntegerHelper.DivRem(MinValue, 2)); - Assert.Equal((Zero, NegativeOne), BinaryIntegerHelper.DivRem(NegativeOne, 2)); + Assert.Equal((Zero, Zero), BinaryIntegerHelper.DivRem(Zero, Two)); + Assert.Equal((Zero, One), BinaryIntegerHelper.DivRem(One, Two)); + Assert.Equal((One, Zero), BinaryIntegerHelper.DivRem(Two, Two)); + Assert.Equal((new Int128(0x3FFF_FFFF_FFFF_FFFF, 0xFFFF_FFFF_FFFF_FFFF), Zero), BinaryIntegerHelper.DivRem(MaxValueMinusOne, Two)); + Assert.Equal((new Int128(0x3FFF_FFFF_FFFF_FFFF, 0xFFFF_FFFF_FFFF_FFFF), One), BinaryIntegerHelper.DivRem(MaxValue, Two)); + Assert.Equal((new Int128(0xC000_0000_0000_0000, 0x0000_0000_0000_0000), Zero), BinaryIntegerHelper.DivRem(MinValue, Two)); + Assert.Equal((new Int128(0xC000_0000_0000_0000, 0x0000_0000_0000_0001), NegativeOne), BinaryIntegerHelper.DivRem(MinValuePlusOne, Two)); + Assert.Equal((NegativeOne, Zero), BinaryIntegerHelper.DivRem(NegativeTwo, Two)); + Assert.Equal((Zero, NegativeOne), BinaryIntegerHelper.DivRem(NegativeOne, Two)); + + Assert.Equal((Zero, Zero), BinaryIntegerHelper.DivRem(Zero, NegativeTwo)); + Assert.Equal((Zero, One), BinaryIntegerHelper.DivRem(One, NegativeTwo)); + Assert.Equal((NegativeOne, Zero), BinaryIntegerHelper.DivRem(Two, NegativeTwo)); + Assert.Equal((new Int128(0xC000_0000_0000_0000, 0x0000_0000_0000_0001), Zero), BinaryIntegerHelper.DivRem(MaxValueMinusOne, NegativeTwo)); + Assert.Equal((new Int128(0xC000_0000_0000_0000, 0x0000_0000_0000_0001), One), BinaryIntegerHelper.DivRem(MaxValue, NegativeTwo)); + Assert.Equal((new Int128(0x4000_0000_0000_0000, 0x0000_0000_0000_0000), Zero), BinaryIntegerHelper.DivRem(MinValue, NegativeTwo)); + Assert.Equal((new Int128(0x3FFF_FFFF_FFFF_FFFF, 0xFFFF_FFFF_FFFF_FFFF), NegativeOne), BinaryIntegerHelper.DivRem(MinValuePlusOne, NegativeTwo)); + Assert.Equal((One, Zero), BinaryIntegerHelper.DivRem(NegativeTwo, NegativeTwo)); + Assert.Equal((Zero, NegativeOne), BinaryIntegerHelper.DivRem(NegativeOne, NegativeTwo)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem(Zero, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(One, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(NegativeOne, 0)); + } + + [Fact] + public static void DivRemModeTest() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivRemExpected(Zero, Two, mode), BinaryIntegerHelper.DivRem(Zero, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(One, Two, mode), BinaryIntegerHelper.DivRem(One, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(Two, Two, mode), BinaryIntegerHelper.DivRem(Two, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(MaxValueMinusOne, Two, mode), BinaryIntegerHelper.DivRem(MaxValueMinusOne, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(MaxValue, Two, mode), BinaryIntegerHelper.DivRem(MaxValue, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(MinValue, Two, mode), BinaryIntegerHelper.DivRem(MinValue, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(MinValuePlusOne, Two, mode), BinaryIntegerHelper.DivRem(MinValuePlusOne, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(NegativeTwo, Two, mode), BinaryIntegerHelper.DivRem(NegativeTwo, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(NegativeOne, Two, mode), BinaryIntegerHelper.DivRem(NegativeOne, Two, mode)); + + Assert.Equal(BinaryIntegerHelper.DivRemExpected(Zero, NegativeTwo, mode), BinaryIntegerHelper.DivRem(Zero, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(One, NegativeTwo, mode), BinaryIntegerHelper.DivRem(One, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(Two, NegativeTwo, mode), BinaryIntegerHelper.DivRem(Two, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(MaxValueMinusOne, NegativeTwo, mode), BinaryIntegerHelper.DivRem(MaxValueMinusOne, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(MaxValue, NegativeTwo, mode), BinaryIntegerHelper.DivRem(MaxValue, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(MinValue, NegativeTwo, mode), BinaryIntegerHelper.DivRem(MinValue, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(MinValuePlusOne, NegativeTwo, mode), BinaryIntegerHelper.DivRem(MinValuePlusOne, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(NegativeTwo, NegativeTwo, mode), BinaryIntegerHelper.DivRem(NegativeTwo, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(NegativeOne, NegativeTwo, mode), BinaryIntegerHelper.DivRem(NegativeOne, NegativeTwo, mode)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem(Zero, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(One, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(NegativeOne, 0, mode)); + } + } + + [Fact] + public static void DivideModeTest() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivideExpected(Zero, Two, mode), BinaryIntegerHelper.Divide(Zero, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(One, Two, mode), BinaryIntegerHelper.Divide(One, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(Two, Two, mode), BinaryIntegerHelper.Divide(Two, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(MaxValueMinusOne, Two, mode), BinaryIntegerHelper.Divide(MaxValueMinusOne, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(MaxValue, Two, mode), BinaryIntegerHelper.Divide(MaxValue, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(MinValue, Two, mode), BinaryIntegerHelper.Divide(MinValue, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(MinValuePlusOne, Two, mode), BinaryIntegerHelper.Divide(MinValuePlusOne, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(NegativeTwo, Two, mode), BinaryIntegerHelper.Divide(NegativeTwo, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(NegativeOne, Two, mode), BinaryIntegerHelper.Divide(NegativeOne, Two, mode)); + + Assert.Equal(BinaryIntegerHelper.DivideExpected(Zero, NegativeTwo, mode), BinaryIntegerHelper.Divide(Zero, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(One, NegativeTwo, mode), BinaryIntegerHelper.Divide(One, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(Two, NegativeTwo, mode), BinaryIntegerHelper.Divide(Two, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(MaxValueMinusOne, NegativeTwo, mode), BinaryIntegerHelper.Divide(MaxValueMinusOne, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(MaxValue, NegativeTwo, mode), BinaryIntegerHelper.Divide(MaxValue, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(MinValue, NegativeTwo, mode), BinaryIntegerHelper.Divide(MinValue, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(MinValuePlusOne, NegativeTwo, mode), BinaryIntegerHelper.Divide(MinValuePlusOne, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(NegativeTwo, NegativeTwo, mode), BinaryIntegerHelper.Divide(NegativeTwo, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(NegativeOne, NegativeTwo, mode), BinaryIntegerHelper.Divide(NegativeOne, NegativeTwo, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Divide(Zero, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide(One, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide(NegativeOne, 0, mode)); + } + } + + [Fact] + public static void RemainderModeTest() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.RemainderExpected(Zero, Two, mode), BinaryIntegerHelper.Remainder(Zero, Two, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(One, Two, mode), BinaryIntegerHelper.Remainder(One, Two, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(Two, Two, mode), BinaryIntegerHelper.Remainder(Two, Two, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(MaxValueMinusOne, Two, mode), BinaryIntegerHelper.Remainder(MaxValueMinusOne, Two, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(MaxValue, Two, mode), BinaryIntegerHelper.Remainder(MaxValue, Two, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(MinValue, Two, mode), BinaryIntegerHelper.Remainder(MinValue, Two, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(MinValuePlusOne, Two, mode), BinaryIntegerHelper.Remainder(MinValuePlusOne, Two, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(NegativeTwo, Two, mode), BinaryIntegerHelper.Remainder(NegativeTwo, Two, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(NegativeOne, Two, mode), BinaryIntegerHelper.Remainder(NegativeOne, Two, mode)); + + Assert.Equal(BinaryIntegerHelper.RemainderExpected(Zero, NegativeTwo, mode), BinaryIntegerHelper.Remainder(Zero, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(One, NegativeTwo, mode), BinaryIntegerHelper.Remainder(One, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(Two, NegativeTwo, mode), BinaryIntegerHelper.Remainder(Two, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(MaxValueMinusOne, NegativeTwo, mode), BinaryIntegerHelper.Remainder(MaxValueMinusOne, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(MaxValue, NegativeTwo, mode), BinaryIntegerHelper.Remainder(MaxValue, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(MinValue, NegativeTwo, mode), BinaryIntegerHelper.Remainder(MinValue, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(MinValuePlusOne, NegativeTwo, mode), BinaryIntegerHelper.Remainder(MinValuePlusOne, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(NegativeTwo, NegativeTwo, mode), BinaryIntegerHelper.Remainder(NegativeTwo, NegativeTwo, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(NegativeOne, NegativeTwo, mode), BinaryIntegerHelper.Remainder(NegativeOne, NegativeTwo, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Remainder(Zero, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder(One, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder(NegativeOne, 0, mode)); + } } [Fact] diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int16Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int16Tests.GenericMath.cs index 5e1a78029f01e..8aa51d363da03 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int16Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int16Tests.GenericMath.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Globalization; +using System.Numerics; using System.Runtime.InteropServices; using Xunit; @@ -51,11 +52,134 @@ public static void AdditiveIdentityTest() [Fact] public static void DivRemTest() { - Assert.Equal(((short)0x0000, (short)0x0000), BinaryIntegerHelper.DivRem((short)0x0000, (short)2)); - Assert.Equal(((short)0x0000, (short)0x0001), BinaryIntegerHelper.DivRem((short)0x0001, (short)2)); - Assert.Equal(((short)0x3FFF, (short)0x0001), BinaryIntegerHelper.DivRem((short)0x7FFF, (short)2)); - Assert.Equal((unchecked((short)0xC000), (short)0x0000), BinaryIntegerHelper.DivRem(unchecked((short)0x8000), (short)2)); - Assert.Equal(((short)0x0000, unchecked((short)0xFFFF)), BinaryIntegerHelper.DivRem(unchecked((short)0xFFFF), (short)2)); + unchecked + { + Assert.Equal(((short)0x0000, (short)0x0000), BinaryIntegerHelper.DivRem((short)0x0000, (short)0x0002)); + Assert.Equal(((short)0x0000, (short)0x0001), BinaryIntegerHelper.DivRem((short)0x0001, (short)0x0002)); + Assert.Equal(((short)0x0001, (short)0x0000), BinaryIntegerHelper.DivRem((short)0x0002, (short)0x0002)); + Assert.Equal(((short)0x3FFF, (short)0x0000), BinaryIntegerHelper.DivRem((short)0x7FFE, (short)0x0002)); + Assert.Equal(((short)0x3FFF, (short)0x0001), BinaryIntegerHelper.DivRem((short)0x7FFF, (short)0x0002)); + Assert.Equal(((short)0xC000, (short)0x0000), BinaryIntegerHelper.DivRem((short)0x8000, (short)0x0002)); + Assert.Equal(((short)0xC001, (short)0xFFFF), BinaryIntegerHelper.DivRem((short)0x8001, (short)0x0002)); + Assert.Equal(((short)0xFFFF, (short)0x0000), BinaryIntegerHelper.DivRem((short)0xFFFE, (short)0x0002)); + Assert.Equal(((short)0x0000, (short)0xFFFF), BinaryIntegerHelper.DivRem((short)0xFFFF, (short)0x0002)); + + Assert.Equal(((short)0x0000, (short)0x0000), BinaryIntegerHelper.DivRem((short)0x0000, (short)0xFFFE)); + Assert.Equal(((short)0x0000, (short)0x0001), BinaryIntegerHelper.DivRem((short)0x0001, (short)0xFFFE)); + Assert.Equal(((short)0xFFFF, (short)0x0000), BinaryIntegerHelper.DivRem((short)0x0002, (short)0xFFFE)); + Assert.Equal(((short)0xC001, (short)0x0000), BinaryIntegerHelper.DivRem((short)0x7FFE, (short)0xFFFE)); + Assert.Equal(((short)0xC001, (short)0x0001), BinaryIntegerHelper.DivRem((short)0x7FFF, (short)0xFFFE)); + Assert.Equal(((short)0x4000, (short)0x0000), BinaryIntegerHelper.DivRem((short)0x8000, (short)0xFFFE)); + Assert.Equal(((short)0x3FFF, (short)0xFFFF), BinaryIntegerHelper.DivRem((short)0x8001, (short)0xFFFE)); + Assert.Equal(((short)0x0001, (short)0x0000), BinaryIntegerHelper.DivRem((short)0xFFFE, (short)0xFFFE)); + Assert.Equal(((short)0x0000, (short)0xFFFF), BinaryIntegerHelper.DivRem((short)0xFFFF, (short)0xFFFE)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem((short)0x0000, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((short)0x0001, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((short)0xFFFF, 0)); + } + } + + [Fact] + public static void DivRemModeTest() + { + unchecked + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivRemExpected((short)0x0000, (short)0x0002, mode), BinaryIntegerHelper.DivRem((short)0x0000, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((short)0x0001, (short)0x0002, mode), BinaryIntegerHelper.DivRem((short)0x0001, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((short)0x0002, (short)0x0002, mode), BinaryIntegerHelper.DivRem((short)0x0002, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((short)0x7FFE, (short)0x0002, mode), BinaryIntegerHelper.DivRem((short)0x7FFE, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((short)0x7FFF, (short)0x0002, mode), BinaryIntegerHelper.DivRem((short)0x7FFF, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((short)0x8000, (short)0x0002, mode), BinaryIntegerHelper.DivRem((short)0x8000, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((short)0x8001, (short)0x0002, mode), BinaryIntegerHelper.DivRem((short)0x8001, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((short)0xFFFE, (short)0x0002, mode), BinaryIntegerHelper.DivRem((short)0xFFFE, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((short)0xFFFF, (short)0x0002, mode), BinaryIntegerHelper.DivRem((short)0xFFFF, (short)0x0002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivRemExpected((short)0x0000, (short)0xFFFE, mode), BinaryIntegerHelper.DivRem((short)0x0000, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((short)0x0001, (short)0xFFFE, mode), BinaryIntegerHelper.DivRem((short)0x0001, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((short)0x0002, (short)0xFFFE, mode), BinaryIntegerHelper.DivRem((short)0x0002, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((short)0x7FFE, (short)0xFFFE, mode), BinaryIntegerHelper.DivRem((short)0x7FFE, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((short)0x7FFF, (short)0xFFFE, mode), BinaryIntegerHelper.DivRem((short)0x7FFF, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((short)0x8000, (short)0xFFFE, mode), BinaryIntegerHelper.DivRem((short)0x8000, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((short)0x8001, (short)0xFFFE, mode), BinaryIntegerHelper.DivRem((short)0x8001, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((short)0xFFFE, (short)0xFFFE, mode), BinaryIntegerHelper.DivRem((short)0xFFFE, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((short)0xFFFF, (short)0xFFFE, mode), BinaryIntegerHelper.DivRem((short)0xFFFF, (short)0xFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem((short)0x0000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((short)0x0001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((short)0xFFFF, 0, mode)); + } + } + } + + [Fact] + public static void DivideModeTest() + { + unchecked + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivideExpected((short)0x0000, (short)0x0002, mode), BinaryIntegerHelper.Divide((short)0x0000, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((short)0x0001, (short)0x0002, mode), BinaryIntegerHelper.Divide((short)0x0001, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((short)0x0002, (short)0x0002, mode), BinaryIntegerHelper.Divide((short)0x0002, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((short)0x7FFE, (short)0x0002, mode), BinaryIntegerHelper.Divide((short)0x7FFE, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((short)0x7FFF, (short)0x0002, mode), BinaryIntegerHelper.Divide((short)0x7FFF, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((short)0x8000, (short)0x0002, mode), BinaryIntegerHelper.Divide((short)0x8000, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((short)0x8001, (short)0x0002, mode), BinaryIntegerHelper.Divide((short)0x8001, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((short)0xFFFE, (short)0x0002, mode), BinaryIntegerHelper.Divide((short)0xFFFE, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((short)0xFFFF, (short)0x0002, mode), BinaryIntegerHelper.Divide((short)0xFFFF, (short)0x0002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivideExpected((short)0x0000, (short)0xFFFE, mode), BinaryIntegerHelper.Divide((short)0x0000, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((short)0x0001, (short)0xFFFE, mode), BinaryIntegerHelper.Divide((short)0x0001, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((short)0x0002, (short)0xFFFE, mode), BinaryIntegerHelper.Divide((short)0x0002, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((short)0x7FFE, (short)0xFFFE, mode), BinaryIntegerHelper.Divide((short)0x7FFE, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((short)0x7FFF, (short)0xFFFE, mode), BinaryIntegerHelper.Divide((short)0x7FFF, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((short)0x8000, (short)0xFFFE, mode), BinaryIntegerHelper.Divide((short)0x8000, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((short)0x8001, (short)0xFFFE, mode), BinaryIntegerHelper.Divide((short)0x8001, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((short)0xFFFE, (short)0xFFFE, mode), BinaryIntegerHelper.Divide((short)0xFFFE, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((short)0xFFFF, (short)0xFFFE, mode), BinaryIntegerHelper.Divide((short)0xFFFF, (short)0xFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Divide((short)0x0000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide((short)0x0001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide((short)0xFFFF, 0, mode)); + } + } + } + + [Fact] + public static void RemainderModeTest() + { + unchecked + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.RemainderExpected((short)0x0000, (short)0x0002, mode), BinaryIntegerHelper.Remainder((short)0x0000, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((short)0x0001, (short)0x0002, mode), BinaryIntegerHelper.Remainder((short)0x0001, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((short)0x0002, (short)0x0002, mode), BinaryIntegerHelper.Remainder((short)0x0002, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((short)0x7FFE, (short)0x0002, mode), BinaryIntegerHelper.Remainder((short)0x7FFE, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((short)0x7FFF, (short)0x0002, mode), BinaryIntegerHelper.Remainder((short)0x7FFF, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((short)0x8000, (short)0x0002, mode), BinaryIntegerHelper.Remainder((short)0x8000, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((short)0x8001, (short)0x0002, mode), BinaryIntegerHelper.Remainder((short)0x8001, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((short)0xFFFE, (short)0x0002, mode), BinaryIntegerHelper.Remainder((short)0xFFFE, (short)0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((short)0xFFFF, (short)0x0002, mode), BinaryIntegerHelper.Remainder((short)0xFFFF, (short)0x0002, mode)); + + Assert.Equal(BinaryIntegerHelper.RemainderExpected((short)0x0000, (short)0xFFFE, mode), BinaryIntegerHelper.Remainder((short)0x0000, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((short)0x0001, (short)0xFFFE, mode), BinaryIntegerHelper.Remainder((short)0x0001, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((short)0x0002, (short)0xFFFE, mode), BinaryIntegerHelper.Remainder((short)0x0002, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((short)0x7FFE, (short)0xFFFE, mode), BinaryIntegerHelper.Remainder((short)0x7FFE, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((short)0x7FFF, (short)0xFFFE, mode), BinaryIntegerHelper.Remainder((short)0x7FFF, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((short)0x8000, (short)0xFFFE, mode), BinaryIntegerHelper.Remainder((short)0x8000, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((short)0x8001, (short)0xFFFE, mode), BinaryIntegerHelper.Remainder((short)0x8001, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((short)0xFFFE, (short)0xFFFE, mode), BinaryIntegerHelper.Remainder((short)0xFFFE, (short)0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((short)0xFFFF, (short)0xFFFE, mode), BinaryIntegerHelper.Remainder((short)0xFFFF, (short)0xFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Remainder((short)0x0000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder((short)0x0001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder((short)0xFFFF, 0, mode)); + } + } } [Fact] diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int32Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int32Tests.GenericMath.cs index e915e1d58df63..5d98e93a7084a 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int32Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int32Tests.GenericMath.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Globalization; +using System.Numerics; using System.Runtime.InteropServices; using Xunit; @@ -51,11 +52,134 @@ public static void AdditiveIdentityTest() [Fact] public static void DivRemTest() { - Assert.Equal(((int)0x00000000, (int)0x00000000), BinaryIntegerHelper.DivRem((int)0x00000000, 2)); - Assert.Equal(((int)0x00000000, (int)0x00000001), BinaryIntegerHelper.DivRem((int)0x00000001, 2)); - Assert.Equal(((int)0x3FFFFFFF, (int)0x00000001), BinaryIntegerHelper.DivRem((int)0x7FFFFFFF, 2)); - Assert.Equal((unchecked((int)0xC0000000), (int)0x00000000), BinaryIntegerHelper.DivRem(unchecked((int)0x80000000), 2)); - Assert.Equal(((int)0x00000000, unchecked((int)0xFFFFFFFF)), BinaryIntegerHelper.DivRem(unchecked((int)0xFFFFFFFF), 2)); + unchecked + { + Assert.Equal(((int)0x00000000, (int)0x00000000), BinaryIntegerHelper.DivRem((int)0x00000000, (int)0x00000002)); + Assert.Equal(((int)0x00000000, (int)0x00000001), BinaryIntegerHelper.DivRem((int)0x00000001, (int)0x00000002)); + Assert.Equal(((int)0x00000001, (int)0x00000000), BinaryIntegerHelper.DivRem((int)0x00000002, (int)0x00000002)); + Assert.Equal(((int)0x3FFFFFFF, (int)0x00000000), BinaryIntegerHelper.DivRem((int)0x7FFFFFFE, (int)0x00000002)); + Assert.Equal(((int)0x3FFFFFFF, (int)0x00000001), BinaryIntegerHelper.DivRem((int)0x7FFFFFFF, (int)0x00000002)); + Assert.Equal(((int)0xC0000000, (int)0x00000000), BinaryIntegerHelper.DivRem((int)0x80000000, (int)0x00000002)); + Assert.Equal(((int)0xC0000001, (int)0xFFFFFFFF), BinaryIntegerHelper.DivRem((int)0x80000001, (int)0x00000002)); + Assert.Equal(((int)0xFFFFFFFF, (int)0x00000000), BinaryIntegerHelper.DivRem((int)0xFFFFFFFE, (int)0x00000002)); + Assert.Equal(((int)0x00000000, (int)0xFFFFFFFF), BinaryIntegerHelper.DivRem((int)0xFFFFFFFF, (int)0x00000002)); + + Assert.Equal(((int)0x00000000, (int)0x00000000), BinaryIntegerHelper.DivRem((int)0x00000000, (int)0xFFFFFFFE)); + Assert.Equal(((int)0x00000000, (int)0x00000001), BinaryIntegerHelper.DivRem((int)0x00000001, (int)0xFFFFFFFE)); + Assert.Equal(((int)0xFFFFFFFF, (int)0x00000000), BinaryIntegerHelper.DivRem((int)0x00000002, (int)0xFFFFFFFE)); + Assert.Equal(((int)0xC0000001, (int)0x00000000), BinaryIntegerHelper.DivRem((int)0x7FFFFFFE, (int)0xFFFFFFFE)); + Assert.Equal(((int)0xC0000001, (int)0x00000001), BinaryIntegerHelper.DivRem((int)0x7FFFFFFF, (int)0xFFFFFFFE)); + Assert.Equal(((int)0x40000000, (int)0x00000000), BinaryIntegerHelper.DivRem((int)0x80000000, (int)0xFFFFFFFE)); + Assert.Equal(((int)0x3FFFFFFF, (int)0xFFFFFFFF), BinaryIntegerHelper.DivRem((int)0x80000001, (int)0xFFFFFFFE)); + Assert.Equal(((int)0x00000001, (int)0x00000000), BinaryIntegerHelper.DivRem((int)0xFFFFFFFE, (int)0xFFFFFFFE)); + Assert.Equal(((int)0x00000000, (int)0xFFFFFFFF), BinaryIntegerHelper.DivRem((int)0xFFFFFFFF, (int)0xFFFFFFFE)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem((int)0x00000000, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((int)0x00000001, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((int)0xFFFFFFFF, 0)); + } + } + + [Fact] + public static void DivRemModeTest() + { + unchecked + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivRemExpected((int)0x00000000, (int)0x00000002, mode), BinaryIntegerHelper.DivRem((int)0x00000000, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((int)0x00000001, (int)0x00000002, mode), BinaryIntegerHelper.DivRem((int)0x00000001, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((int)0x00000002, (int)0x00000002, mode), BinaryIntegerHelper.DivRem((int)0x00000002, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((int)0x7FFFFFFE, (int)0x00000002, mode), BinaryIntegerHelper.DivRem((int)0x7FFFFFFE, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((int)0x7FFFFFFF, (int)0x00000002, mode), BinaryIntegerHelper.DivRem((int)0x7FFFFFFF, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((int)0x80000000, (int)0x00000002, mode), BinaryIntegerHelper.DivRem((int)0x80000000, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((int)0x80000001, (int)0x00000002, mode), BinaryIntegerHelper.DivRem((int)0x80000001, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((int)0xFFFFFFFE, (int)0x00000002, mode), BinaryIntegerHelper.DivRem((int)0xFFFFFFFE, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((int)0xFFFFFFFF, (int)0x00000002, mode), BinaryIntegerHelper.DivRem((int)0xFFFFFFFF, (int)0x00000002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivRemExpected((int)0x00000000, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((int)0x00000000, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((int)0x00000001, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((int)0x00000001, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((int)0x00000002, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((int)0x00000002, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((int)0x7FFFFFFE, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((int)0x7FFFFFFE, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((int)0x7FFFFFFF, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((int)0x7FFFFFFF, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((int)0x80000000, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((int)0x80000000, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((int)0x80000001, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((int)0x80000001, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((int)0xFFFFFFFE, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((int)0xFFFFFFFE, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((int)0xFFFFFFFF, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((int)0xFFFFFFFF, (int)0xFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem((int)0x00000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((int)0x00000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((int)0xFFFFFFFF, 0, mode)); + } + } + } + + [Fact] + public static void DivideModeTest() + { + unchecked + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivideExpected((int)0x00000000, (int)0x00000002, mode), BinaryIntegerHelper.Divide((int)0x00000000, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((int)0x00000001, (int)0x00000002, mode), BinaryIntegerHelper.Divide((int)0x00000001, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((int)0x00000002, (int)0x00000002, mode), BinaryIntegerHelper.Divide((int)0x00000002, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((int)0x7FFFFFFE, (int)0x00000002, mode), BinaryIntegerHelper.Divide((int)0x7FFFFFFE, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((int)0x7FFFFFFF, (int)0x00000002, mode), BinaryIntegerHelper.Divide((int)0x7FFFFFFF, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((int)0x80000000, (int)0x00000002, mode), BinaryIntegerHelper.Divide((int)0x80000000, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((int)0x80000001, (int)0x00000002, mode), BinaryIntegerHelper.Divide((int)0x80000001, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((int)0xFFFFFFFE, (int)0x00000002, mode), BinaryIntegerHelper.Divide((int)0xFFFFFFFE, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((int)0xFFFFFFFF, (int)0x00000002, mode), BinaryIntegerHelper.Divide((int)0xFFFFFFFF, (int)0x00000002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivideExpected((int)0x00000000, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((int)0x00000000, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((int)0x00000001, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((int)0x00000001, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((int)0x00000002, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((int)0x00000002, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((int)0x7FFFFFFE, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((int)0x7FFFFFFE, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((int)0x7FFFFFFF, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((int)0x7FFFFFFF, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((int)0x80000000, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((int)0x80000000, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((int)0x80000001, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((int)0x80000001, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((int)0xFFFFFFFE, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((int)0xFFFFFFFE, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((int)0xFFFFFFFF, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((int)0xFFFFFFFF, (int)0xFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Divide((int)0x00000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide((int)0x00000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide((int)0xFFFFFFFF, 0, mode)); + } + } + } + + [Fact] + public static void RemainderModeTest() + { + unchecked + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.RemainderExpected((int)0x00000000, (int)0x00000002, mode), BinaryIntegerHelper.Remainder((int)0x00000000, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((int)0x00000001, (int)0x00000002, mode), BinaryIntegerHelper.Remainder((int)0x00000001, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((int)0x00000002, (int)0x00000002, mode), BinaryIntegerHelper.Remainder((int)0x00000002, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((int)0x7FFFFFFE, (int)0x00000002, mode), BinaryIntegerHelper.Remainder((int)0x7FFFFFFE, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((int)0x7FFFFFFF, (int)0x00000002, mode), BinaryIntegerHelper.Remainder((int)0x7FFFFFFF, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((int)0x80000000, (int)0x00000002, mode), BinaryIntegerHelper.Remainder((int)0x80000000, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((int)0x80000001, (int)0x00000002, mode), BinaryIntegerHelper.Remainder((int)0x80000001, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((int)0xFFFFFFFE, (int)0x00000002, mode), BinaryIntegerHelper.Remainder((int)0xFFFFFFFE, (int)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((int)0xFFFFFFFF, (int)0x00000002, mode), BinaryIntegerHelper.Remainder((int)0xFFFFFFFF, (int)0x00000002, mode)); + + Assert.Equal(BinaryIntegerHelper.RemainderExpected((int)0x00000000, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((int)0x00000000, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((int)0x00000001, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((int)0x00000001, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((int)0x00000002, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((int)0x00000002, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((int)0x7FFFFFFE, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((int)0x7FFFFFFE, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((int)0x7FFFFFFF, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((int)0x7FFFFFFF, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((int)0x80000000, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((int)0x80000000, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((int)0x80000001, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((int)0x80000001, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((int)0xFFFFFFFE, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((int)0xFFFFFFFE, (int)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((int)0xFFFFFFFF, (int)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((int)0xFFFFFFFF, (int)0xFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Remainder((int)0x00000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder((int)0x00000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder((int)0xFFFFFFFF, 0, mode)); + } + } } [Fact] diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int64Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int64Tests.GenericMath.cs index aa8d7893f73c4..46561b3023260 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int64Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int64Tests.GenericMath.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Globalization; +using System.Numerics; using System.Runtime.InteropServices; using Xunit; @@ -51,11 +52,134 @@ public static void AdditiveIdentityTest() [Fact] public static void DivRemTest() { - Assert.Equal(((long)0x0000000000000000, (long)0x0000000000000000), BinaryIntegerHelper.DivRem((long)0x0000000000000000, 2)); - Assert.Equal(((long)0x0000000000000000, (long)0x0000000000000001), BinaryIntegerHelper.DivRem((long)0x0000000000000001, 2)); - Assert.Equal(((long)0x3FFFFFFFFFFFFFFF, (long)0x0000000000000001), BinaryIntegerHelper.DivRem((long)0x7FFFFFFFFFFFFFFF, 2)); - Assert.Equal((unchecked((long)0xC000000000000000), (long)0x0000000000000000), BinaryIntegerHelper.DivRem(unchecked((long)0x8000000000000000), 2)); - Assert.Equal(((long)0x0000000000000000, unchecked((long)0xFFFFFFFFFFFFFFFF)), BinaryIntegerHelper.DivRem(unchecked((long)0xFFFFFFFFFFFFFFFF), 2)); + unchecked + { + Assert.Equal(((long)0x0000000000000000, (long)0x0000000000000000), BinaryIntegerHelper.DivRem((long)0x0000000000000000, (long)0x0000000000000002)); + Assert.Equal(((long)0x0000000000000000, (long)0x0000000000000001), BinaryIntegerHelper.DivRem((long)0x0000000000000001, (long)0x0000000000000002)); + Assert.Equal(((long)0x0000000000000001, (long)0x0000000000000000), BinaryIntegerHelper.DivRem((long)0x0000000000000002, (long)0x0000000000000002)); + Assert.Equal(((long)0x3FFFFFFFFFFFFFFF, (long)0x0000000000000000), BinaryIntegerHelper.DivRem((long)0x7FFFFFFFFFFFFFFE, (long)0x0000000000000002)); + Assert.Equal(((long)0x3FFFFFFFFFFFFFFF, (long)0x0000000000000001), BinaryIntegerHelper.DivRem((long)0x7FFFFFFFFFFFFFFF, (long)0x0000000000000002)); + Assert.Equal(((long)0xC000000000000000, (long)0x0000000000000000), BinaryIntegerHelper.DivRem((long)0x8000000000000000, (long)0x0000000000000002)); + Assert.Equal(((long)0xC000000000000001, (long)0xFFFFFFFFFFFFFFFF), BinaryIntegerHelper.DivRem((long)0x8000000000000001, (long)0x0000000000000002)); + Assert.Equal(((long)0xFFFFFFFFFFFFFFFF, (long)0x0000000000000000), BinaryIntegerHelper.DivRem((long)0xFFFFFFFFFFFFFFFE, (long)0x0000000000000002)); + Assert.Equal(((long)0x0000000000000000, (long)0xFFFFFFFFFFFFFFFF), BinaryIntegerHelper.DivRem((long)0xFFFFFFFFFFFFFFFF, (long)0x0000000000000002)); + + Assert.Equal(((long)0x0000000000000000, (long)0x0000000000000000), BinaryIntegerHelper.DivRem((long)0x0000000000000000, (long)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((long)0x0000000000000000, (long)0x0000000000000001), BinaryIntegerHelper.DivRem((long)0x0000000000000001, (long)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((long)0xFFFFFFFFFFFFFFFF, (long)0x0000000000000000), BinaryIntegerHelper.DivRem((long)0x0000000000000002, (long)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((long)0xC000000000000001, (long)0x0000000000000000), BinaryIntegerHelper.DivRem((long)0x7FFFFFFFFFFFFFFE, (long)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((long)0xC000000000000001, (long)0x0000000000000001), BinaryIntegerHelper.DivRem((long)0x7FFFFFFFFFFFFFFF, (long)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((long)0x4000000000000000, (long)0x0000000000000000), BinaryIntegerHelper.DivRem((long)0x8000000000000000, (long)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((long)0x3FFFFFFFFFFFFFFF, (long)0xFFFFFFFFFFFFFFFF), BinaryIntegerHelper.DivRem((long)0x8000000000000001, (long)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((long)0x0000000000000001, (long)0x0000000000000000), BinaryIntegerHelper.DivRem((long)0xFFFFFFFFFFFFFFFE, (long)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((long)0x0000000000000000, (long)0xFFFFFFFFFFFFFFFF), BinaryIntegerHelper.DivRem((long)0xFFFFFFFFFFFFFFFF, (long)0xFFFFFFFFFFFFFFFE)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem((long)0x0000000000000000, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((long)0x0000000000000001, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((long)0xFFFFFFFFFFFFFFFF, 0)); + } + } + + [Fact] + public static void DivRemModeTest() + { + unchecked + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivRemExpected((long)0x0000000000000000, (long)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((long)0x0000000000000000, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((long)0x0000000000000001, (long)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((long)0x0000000000000001, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((long)0x0000000000000002, (long)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((long)0x0000000000000002, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((long)0x7FFFFFFFFFFFFFFE, (long)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((long)0x7FFFFFFFFFFFFFFE, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((long)0x7FFFFFFFFFFFFFFF, (long)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((long)0x7FFFFFFFFFFFFFFF, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((long)0x8000000000000000, (long)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((long)0x8000000000000000, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((long)0x8000000000000001, (long)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((long)0x8000000000000001, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((long)0xFFFFFFFFFFFFFFFE, (long)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((long)0xFFFFFFFFFFFFFFFE, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((long)0xFFFFFFFFFFFFFFFF, (long)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((long)0xFFFFFFFFFFFFFFFF, (long)0x0000000000000002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivRemExpected((long)0x0000000000000000, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((long)0x0000000000000000, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((long)0x0000000000000001, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((long)0x0000000000000001, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((long)0x0000000000000002, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((long)0x0000000000000002, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((long)0x7FFFFFFFFFFFFFFE, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((long)0x7FFFFFFFFFFFFFFE, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((long)0x7FFFFFFFFFFFFFFF, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((long)0x7FFFFFFFFFFFFFFF, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((long)0x8000000000000000, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((long)0x8000000000000000, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((long)0x8000000000000001, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((long)0x8000000000000001, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((long)0xFFFFFFFFFFFFFFFE, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((long)0xFFFFFFFFFFFFFFFE, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((long)0xFFFFFFFFFFFFFFFF, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((long)0xFFFFFFFFFFFFFFFF, (long)0xFFFFFFFFFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.DivRemExpected((long)0x0000000000000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRemExpected((long)0x0000000000000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRemExpected((long)0xFFFFFFFFFFFFFFFF, 0, mode)); + } + } + } + + [Fact] + public static void DivideModeTest() + { + unchecked + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivideExpected((long)0x0000000000000000, (long)0x0000000000000002, mode), BinaryIntegerHelper.Divide((long)0x0000000000000000, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((long)0x0000000000000001, (long)0x0000000000000002, mode), BinaryIntegerHelper.Divide((long)0x0000000000000001, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((long)0x0000000000000002, (long)0x0000000000000002, mode), BinaryIntegerHelper.Divide((long)0x0000000000000002, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((long)0x7FFFFFFFFFFFFFFE, (long)0x0000000000000002, mode), BinaryIntegerHelper.Divide((long)0x7FFFFFFFFFFFFFFE, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((long)0x7FFFFFFFFFFFFFFF, (long)0x0000000000000002, mode), BinaryIntegerHelper.Divide((long)0x7FFFFFFFFFFFFFFF, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((long)0x8000000000000000, (long)0x0000000000000002, mode), BinaryIntegerHelper.Divide((long)0x8000000000000000, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((long)0x8000000000000001, (long)0x0000000000000002, mode), BinaryIntegerHelper.Divide((long)0x8000000000000001, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((long)0xFFFFFFFFFFFFFFFE, (long)0x0000000000000002, mode), BinaryIntegerHelper.Divide((long)0xFFFFFFFFFFFFFFFE, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((long)0xFFFFFFFFFFFFFFFF, (long)0x0000000000000002, mode), BinaryIntegerHelper.Divide((long)0xFFFFFFFFFFFFFFFF, (long)0x0000000000000002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivideExpected((long)0x0000000000000000, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((long)0x0000000000000000, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((long)0x0000000000000001, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((long)0x0000000000000001, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((long)0x0000000000000002, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((long)0x0000000000000002, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((long)0x7FFFFFFFFFFFFFFE, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((long)0x7FFFFFFFFFFFFFFE, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((long)0x7FFFFFFFFFFFFFFF, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((long)0x7FFFFFFFFFFFFFFF, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((long)0x8000000000000000, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((long)0x8000000000000000, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((long)0x8000000000000001, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((long)0x8000000000000001, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((long)0xFFFFFFFFFFFFFFFE, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((long)0xFFFFFFFFFFFFFFFE, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((long)0xFFFFFFFFFFFFFFFF, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((long)0xFFFFFFFFFFFFFFFF, (long)0xFFFFFFFFFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.DivideExpected((long)0x0000000000000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivideExpected((long)0x0000000000000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivideExpected((long)0xFFFFFFFFFFFFFFFF, 0, mode)); + } + } + } + + [Fact] + public static void RemainderModeTest() + { + unchecked + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.RemainderExpected((long)0x0000000000000000, (long)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((long)0x0000000000000000, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((long)0x0000000000000001, (long)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((long)0x0000000000000001, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((long)0x0000000000000002, (long)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((long)0x0000000000000002, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((long)0x7FFFFFFFFFFFFFFE, (long)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((long)0x7FFFFFFFFFFFFFFE, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((long)0x7FFFFFFFFFFFFFFF, (long)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((long)0x7FFFFFFFFFFFFFFF, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((long)0x8000000000000000, (long)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((long)0x8000000000000000, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((long)0x8000000000000001, (long)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((long)0x8000000000000001, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((long)0xFFFFFFFFFFFFFFFE, (long)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((long)0xFFFFFFFFFFFFFFFE, (long)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((long)0xFFFFFFFFFFFFFFFF, (long)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((long)0xFFFFFFFFFFFFFFFF, (long)0x0000000000000002, mode)); + + Assert.Equal(BinaryIntegerHelper.RemainderExpected((long)0x0000000000000000, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((long)0x0000000000000000, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((long)0x0000000000000001, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((long)0x0000000000000001, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((long)0x0000000000000002, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((long)0x0000000000000002, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((long)0x7FFFFFFFFFFFFFFE, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((long)0x7FFFFFFFFFFFFFFE, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((long)0x7FFFFFFFFFFFFFFF, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((long)0x7FFFFFFFFFFFFFFF, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((long)0x8000000000000000, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((long)0x8000000000000000, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((long)0x8000000000000001, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((long)0x8000000000000001, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((long)0xFFFFFFFFFFFFFFFE, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((long)0xFFFFFFFFFFFFFFFE, (long)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((long)0xFFFFFFFFFFFFFFFF, (long)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((long)0xFFFFFFFFFFFFFFFF, (long)0xFFFFFFFFFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.RemainderExpected((long)0x0000000000000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.RemainderExpected((long)0x0000000000000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.RemainderExpected((long)0xFFFFFFFFFFFFFFFF, 0, mode)); + } + } } [Fact] diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/IntPtrTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/IntPtrTests.GenericMath.cs index 4e4d6c59fbf88..18ab5ad3f276a 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/IntPtrTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/IntPtrTests.GenericMath.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Globalization; +using System.Numerics; using System.Runtime.InteropServices; using Xunit; @@ -74,21 +75,250 @@ public static void AdditiveIdentityTest() [Fact] public static void DivRemTest() { - if (Environment.Is64BitProcess) - { - Assert.Equal((unchecked((nint)0x0000000000000000), unchecked((nint)0x0000000000000000)), BinaryIntegerHelper.DivRem(unchecked((nint)0x0000000000000000), (nint)2)); - Assert.Equal((unchecked((nint)0x0000000000000000), unchecked((nint)0x0000000000000001)), BinaryIntegerHelper.DivRem(unchecked((nint)0x0000000000000001), (nint)2)); - Assert.Equal((unchecked((nint)0x3FFFFFFFFFFFFFFF), unchecked((nint)0x0000000000000001)), BinaryIntegerHelper.DivRem(unchecked((nint)0x7FFFFFFFFFFFFFFF), (nint)2)); - Assert.Equal((unchecked((nint)0xC000000000000000), unchecked((nint)0x0000000000000000)), BinaryIntegerHelper.DivRem(unchecked((nint)0x8000000000000000), (nint)2)); - Assert.Equal((unchecked((nint)0x0000000000000000), unchecked((nint)0xFFFFFFFFFFFFFFFF)), BinaryIntegerHelper.DivRem(unchecked((nint)0xFFFFFFFFFFFFFFFF), (nint)2)); - } - else - { - Assert.Equal(((nint)0x00000000, (nint)0x00000000), BinaryIntegerHelper.DivRem((nint)0x00000000, (nint)2)); - Assert.Equal(((nint)0x00000000, (nint)0x00000001), BinaryIntegerHelper.DivRem((nint)0x00000001, (nint)2)); - Assert.Equal(((nint)0x3FFFFFFF, (nint)0x00000001), BinaryIntegerHelper.DivRem((nint)0x7FFFFFFF, (nint)2)); - Assert.Equal((unchecked((nint)0xC0000000), (nint)0x00000000), BinaryIntegerHelper.DivRem(unchecked((nint)0x80000000), (nint)2)); - Assert.Equal(((nint)0x00000000, unchecked((nint)0xFFFFFFFF)), BinaryIntegerHelper.DivRem(unchecked((nint)0xFFFFFFFF), (nint)2)); + unchecked + { + if (Environment.Is64BitProcess) + { + Assert.Equal(((nint)0x0000000000000000, (nint)0x0000000000000000), BinaryIntegerHelper.DivRem((nint)0x0000000000000000, (nint)0x0000000000000002)); + Assert.Equal(((nint)0x0000000000000000, (nint)0x0000000000000001), BinaryIntegerHelper.DivRem((nint)0x0000000000000001, (nint)0x0000000000000002)); + Assert.Equal(((nint)0x0000000000000001, (nint)0x0000000000000000), BinaryIntegerHelper.DivRem((nint)0x0000000000000002, (nint)0x0000000000000002)); + Assert.Equal(((nint)0x3FFFFFFFFFFFFFFF, (nint)0x0000000000000000), BinaryIntegerHelper.DivRem((nint)0x7FFFFFFFFFFFFFFE, (nint)0x0000000000000002)); + Assert.Equal(((nint)0x3FFFFFFFFFFFFFFF, (nint)0x0000000000000001), BinaryIntegerHelper.DivRem((nint)0x7FFFFFFFFFFFFFFF, (nint)0x0000000000000002)); + Assert.Equal(((nint)0xC000000000000000, (nint)0x0000000000000000), BinaryIntegerHelper.DivRem((nint)0x8000000000000000, (nint)0x0000000000000002)); + Assert.Equal(((nint)0xC000000000000001, (nint)0xFFFFFFFFFFFFFFFF), BinaryIntegerHelper.DivRem((nint)0x8000000000000001, (nint)0x0000000000000002)); + Assert.Equal(((nint)0xFFFFFFFFFFFFFFFF, (nint)0x0000000000000000), BinaryIntegerHelper.DivRem((nint)0xFFFFFFFFFFFFFFFE, (nint)0x0000000000000002)); + Assert.Equal(((nint)0x0000000000000000, (nint)0xFFFFFFFFFFFFFFFF), BinaryIntegerHelper.DivRem((nint)0xFFFFFFFFFFFFFFFF, (nint)0x0000000000000002)); + + Assert.Equal(((nint)0x0000000000000000, (nint)0x0000000000000000), BinaryIntegerHelper.DivRem((nint)0x0000000000000000, (nint)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((nint)0x0000000000000000, (nint)0x0000000000000001), BinaryIntegerHelper.DivRem((nint)0x0000000000000001, (nint)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((nint)0xFFFFFFFFFFFFFFFF, (nint)0x0000000000000000), BinaryIntegerHelper.DivRem((nint)0x0000000000000002, (nint)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((nint)0xC000000000000001, (nint)0x0000000000000000), BinaryIntegerHelper.DivRem((nint)0x7FFFFFFFFFFFFFFE, (nint)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((nint)0xC000000000000001, (nint)0x0000000000000001), BinaryIntegerHelper.DivRem((nint)0x7FFFFFFFFFFFFFFF, (nint)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((nint)0x4000000000000000, (nint)0x0000000000000000), BinaryIntegerHelper.DivRem((nint)0x8000000000000000, (nint)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((nint)0x3FFFFFFFFFFFFFFF, (nint)0xFFFFFFFFFFFFFFFF), BinaryIntegerHelper.DivRem((nint)0x8000000000000001, (nint)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((nint)0x0000000000000001, (nint)0x0000000000000000), BinaryIntegerHelper.DivRem((nint)0xFFFFFFFFFFFFFFFE, (nint)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((nint)0x0000000000000000, (nint)0xFFFFFFFFFFFFFFFF), BinaryIntegerHelper.DivRem((nint)0xFFFFFFFFFFFFFFFF, (nint)0xFFFFFFFFFFFFFFFE)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem((nint)0x0000000000000000, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((nint)0x0000000000000001, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((nint)0xFFFFFFFFFFFFFFFF, 0)); + } + else + { + Assert.Equal(((nint)0x00000000, (nint)0x00000000), BinaryIntegerHelper.DivRem((nint)0x00000000, (nint)0x00000002)); + Assert.Equal(((nint)0x00000000, (nint)0x00000001), BinaryIntegerHelper.DivRem((nint)0x00000001, (nint)0x00000002)); + Assert.Equal(((nint)0x00000001, (nint)0x00000000), BinaryIntegerHelper.DivRem((nint)0x00000002, (nint)0x00000002)); + Assert.Equal(((nint)0x3FFFFFFF, (nint)0x00000000), BinaryIntegerHelper.DivRem((nint)0x7FFFFFFE, (nint)0x00000002)); + Assert.Equal(((nint)0x3FFFFFFF, (nint)0x00000001), BinaryIntegerHelper.DivRem((nint)0x7FFFFFFF, (nint)0x00000002)); + Assert.Equal(((nint)0xC0000000, (nint)0x00000000), BinaryIntegerHelper.DivRem((nint)0x80000000, (nint)0x00000002)); + Assert.Equal(((nint)0xC0000001, (nint)0xFFFFFFFF), BinaryIntegerHelper.DivRem((nint)0x80000001, (nint)0x00000002)); + Assert.Equal(((nint)0xFFFFFFFF, (nint)0x00000000), BinaryIntegerHelper.DivRem((nint)0xFFFFFFFE, (nint)0x00000002)); + Assert.Equal(((nint)0x00000000, (nint)0xFFFFFFFF), BinaryIntegerHelper.DivRem((nint)0xFFFFFFFF, (nint)0x00000002)); + + Assert.Equal(((nint)0x00000000, (nint)0x00000000), BinaryIntegerHelper.DivRem((nint)0x00000000, (nint)0xFFFFFFFE)); + Assert.Equal(((nint)0x00000000, (nint)0x00000001), BinaryIntegerHelper.DivRem((nint)0x00000001, (nint)0xFFFFFFFE)); + Assert.Equal(((nint)0xFFFFFFFF, (nint)0x00000000), BinaryIntegerHelper.DivRem((nint)0x00000002, (nint)0xFFFFFFFE)); + Assert.Equal(((nint)0xC0000001, (nint)0x00000000), BinaryIntegerHelper.DivRem((nint)0x7FFFFFFE, (nint)0xFFFFFFFE)); + Assert.Equal(((nint)0xC0000001, (nint)0x00000001), BinaryIntegerHelper.DivRem((nint)0x7FFFFFFF, (nint)0xFFFFFFFE)); + Assert.Equal(((nint)0x40000000, (nint)0x00000000), BinaryIntegerHelper.DivRem((nint)0x80000000, (nint)0xFFFFFFFE)); + Assert.Equal(((nint)0x3FFFFFFF, (nint)0xFFFFFFFF), BinaryIntegerHelper.DivRem((nint)0x80000001, (nint)0xFFFFFFFE)); + Assert.Equal(((nint)0x00000001, (nint)0x00000000), BinaryIntegerHelper.DivRem((nint)0xFFFFFFFE, (nint)0xFFFFFFFE)); + Assert.Equal(((nint)0x00000000, (nint)0xFFFFFFFF), BinaryIntegerHelper.DivRem((nint)0xFFFFFFFF, (nint)0xFFFFFFFE)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem((nint)0x00000000, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((nint)0x00000001, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((nint)0xFFFFFFFF, 0)); + } + } + } + + [Fact] + public static void DivRemModeTest() + { + unchecked + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + if (Environment.Is64BitProcess) + { + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x0000000000000000, (nint)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((nint)0x0000000000000000, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x0000000000000001, (nint)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((nint)0x0000000000000001, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x0000000000000002, (nint)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((nint)0x0000000000000002, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x7FFFFFFFFFFFFFFE, (nint)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((nint)0x7FFFFFFFFFFFFFFE, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x7FFFFFFFFFFFFFFF, (nint)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((nint)0x7FFFFFFFFFFFFFFF, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x8000000000000000, (nint)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((nint)0x8000000000000000, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x8000000000000001, (nint)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((nint)0x8000000000000001, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0xFFFFFFFFFFFFFFFE, (nint)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((nint)0xFFFFFFFFFFFFFFFE, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0xFFFFFFFFFFFFFFFF, (nint)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((nint)0xFFFFFFFFFFFFFFFF, (nint)0x0000000000000002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x0000000000000000, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nint)0x0000000000000000, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x0000000000000001, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nint)0x0000000000000001, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x0000000000000002, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nint)0x0000000000000002, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x7FFFFFFFFFFFFFFE, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nint)0x7FFFFFFFFFFFFFFE, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x7FFFFFFFFFFFFFFF, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nint)0x7FFFFFFFFFFFFFFF, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x8000000000000000, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nint)0x8000000000000000, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x8000000000000001, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nint)0x8000000000000001, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0xFFFFFFFFFFFFFFFE, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nint)0xFFFFFFFFFFFFFFFE, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0xFFFFFFFFFFFFFFFF, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nint)0xFFFFFFFFFFFFFFFF, (nint)0xFFFFFFFFFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.DivRemExpected((nint)0x0000000000000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRemExpected((nint)0x0000000000000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRemExpected((nint)0xFFFFFFFFFFFFFFFF, 0, mode)); + } + else + { + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x00000000, (nint)0x00000002, mode), BinaryIntegerHelper.DivRem((nint)0x00000000, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x00000001, (nint)0x00000002, mode), BinaryIntegerHelper.DivRem((nint)0x00000001, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x00000002, (nint)0x00000002, mode), BinaryIntegerHelper.DivRem((nint)0x00000002, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x7FFFFFFE, (nint)0x00000002, mode), BinaryIntegerHelper.DivRem((nint)0x7FFFFFFE, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x7FFFFFFF, (nint)0x00000002, mode), BinaryIntegerHelper.DivRem((nint)0x7FFFFFFF, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x80000000, (nint)0x00000002, mode), BinaryIntegerHelper.DivRem((nint)0x80000000, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x80000001, (nint)0x00000002, mode), BinaryIntegerHelper.DivRem((nint)0x80000001, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0xFFFFFFFE, (nint)0x00000002, mode), BinaryIntegerHelper.DivRem((nint)0xFFFFFFFE, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0xFFFFFFFF, (nint)0x00000002, mode), BinaryIntegerHelper.DivRem((nint)0xFFFFFFFF, (nint)0x00000002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x00000000, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nint)0x00000000, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x00000001, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nint)0x00000001, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x00000002, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nint)0x00000002, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x7FFFFFFE, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nint)0x7FFFFFFE, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x7FFFFFFF, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nint)0x7FFFFFFF, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x80000000, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nint)0x80000000, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0x80000001, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nint)0x80000001, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0xFFFFFFFE, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nint)0xFFFFFFFE, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nint)0xFFFFFFFF, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nint)0xFFFFFFFF, (nint)0xFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem((nint)0x00000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((nint)0x00000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((nint)0xFFFFFFFF, 0, mode)); + + } + } + } + } + + [Fact] + public static void DivideModeTest() + { + unchecked + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + if (Environment.Is64BitProcess) + { + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x0000000000000000, (nint)0x0000000000000002, mode), BinaryIntegerHelper.Divide((nint)0x0000000000000000, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x0000000000000001, (nint)0x0000000000000002, mode), BinaryIntegerHelper.Divide((nint)0x0000000000000001, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x0000000000000002, (nint)0x0000000000000002, mode), BinaryIntegerHelper.Divide((nint)0x0000000000000002, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x7FFFFFFFFFFFFFFE, (nint)0x0000000000000002, mode), BinaryIntegerHelper.Divide((nint)0x7FFFFFFFFFFFFFFE, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x7FFFFFFFFFFFFFFF, (nint)0x0000000000000002, mode), BinaryIntegerHelper.Divide((nint)0x7FFFFFFFFFFFFFFF, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x8000000000000000, (nint)0x0000000000000002, mode), BinaryIntegerHelper.Divide((nint)0x8000000000000000, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x8000000000000001, (nint)0x0000000000000002, mode), BinaryIntegerHelper.Divide((nint)0x8000000000000001, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0xFFFFFFFFFFFFFFFE, (nint)0x0000000000000002, mode), BinaryIntegerHelper.Divide((nint)0xFFFFFFFFFFFFFFFE, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0xFFFFFFFFFFFFFFFF, (nint)0x0000000000000002, mode), BinaryIntegerHelper.Divide((nint)0xFFFFFFFFFFFFFFFF, (nint)0x0000000000000002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x0000000000000000, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((nint)0x0000000000000000, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x0000000000000001, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((nint)0x0000000000000001, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x0000000000000002, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((nint)0x0000000000000002, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x7FFFFFFFFFFFFFFE, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((nint)0x7FFFFFFFFFFFFFFE, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x7FFFFFFFFFFFFFFF, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((nint)0x7FFFFFFFFFFFFFFF, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x8000000000000000, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((nint)0x8000000000000000, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x8000000000000001, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((nint)0x8000000000000001, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0xFFFFFFFFFFFFFFFE, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((nint)0xFFFFFFFFFFFFFFFE, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0xFFFFFFFFFFFFFFFF, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((nint)0xFFFFFFFFFFFFFFFF, (nint)0xFFFFFFFFFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.DivideExpected((nint)0x0000000000000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivideExpected((nint)0x0000000000000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivideExpected((nint)0xFFFFFFFFFFFFFFFF, 0, mode)); + } + else + { + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x00000000, (nint)0x00000002, mode), BinaryIntegerHelper.Divide((nint)0x00000000, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x00000001, (nint)0x00000002, mode), BinaryIntegerHelper.Divide((nint)0x00000001, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x00000002, (nint)0x00000002, mode), BinaryIntegerHelper.Divide((nint)0x00000002, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x7FFFFFFE, (nint)0x00000002, mode), BinaryIntegerHelper.Divide((nint)0x7FFFFFFE, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x7FFFFFFF, (nint)0x00000002, mode), BinaryIntegerHelper.Divide((nint)0x7FFFFFFF, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x80000000, (nint)0x00000002, mode), BinaryIntegerHelper.Divide((nint)0x80000000, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x80000001, (nint)0x00000002, mode), BinaryIntegerHelper.Divide((nint)0x80000001, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0xFFFFFFFE, (nint)0x00000002, mode), BinaryIntegerHelper.Divide((nint)0xFFFFFFFE, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0xFFFFFFFF, (nint)0x00000002, mode), BinaryIntegerHelper.Divide((nint)0xFFFFFFFF, (nint)0x00000002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x00000000, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((nint)0x00000000, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x00000001, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((nint)0x00000001, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x00000002, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((nint)0x00000002, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x7FFFFFFE, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((nint)0x7FFFFFFE, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x7FFFFFFF, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((nint)0x7FFFFFFF, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x80000000, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((nint)0x80000000, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0x80000001, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((nint)0x80000001, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0xFFFFFFFE, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((nint)0xFFFFFFFE, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nint)0xFFFFFFFF, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((nint)0xFFFFFFFF, (nint)0xFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Divide((nint)0x00000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide((nint)0x00000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide((nint)0xFFFFFFFF, 0, mode)); + } + } + } + } + + [Fact] + public static void RemainderModeTest() + { + unchecked + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + if (Environment.Is64BitProcess) + { + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x0000000000000000, (nint)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((nint)0x0000000000000000, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x0000000000000001, (nint)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((nint)0x0000000000000001, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x0000000000000002, (nint)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((nint)0x0000000000000002, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x7FFFFFFFFFFFFFFE, (nint)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((nint)0x7FFFFFFFFFFFFFFE, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x7FFFFFFFFFFFFFFF, (nint)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((nint)0x7FFFFFFFFFFFFFFF, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x8000000000000000, (nint)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((nint)0x8000000000000000, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x8000000000000001, (nint)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((nint)0x8000000000000001, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0xFFFFFFFFFFFFFFFE, (nint)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((nint)0xFFFFFFFFFFFFFFFE, (nint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0xFFFFFFFFFFFFFFFF, (nint)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((nint)0xFFFFFFFFFFFFFFFF, (nint)0x0000000000000002, mode)); + + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x0000000000000000, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nint)0x0000000000000000, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x0000000000000001, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nint)0x0000000000000001, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x0000000000000002, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nint)0x0000000000000002, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x7FFFFFFFFFFFFFFE, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nint)0x7FFFFFFFFFFFFFFE, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x7FFFFFFFFFFFFFFF, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nint)0x7FFFFFFFFFFFFFFF, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x8000000000000000, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nint)0x8000000000000000, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x8000000000000001, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nint)0x8000000000000001, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0xFFFFFFFFFFFFFFFE, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nint)0xFFFFFFFFFFFFFFFE, (nint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0xFFFFFFFFFFFFFFFF, (nint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nint)0xFFFFFFFFFFFFFFFF, (nint)0xFFFFFFFFFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.RemainderExpected((nint)0x0000000000000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.RemainderExpected((nint)0x0000000000000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.RemainderExpected((nint)0xFFFFFFFFFFFFFFFF, 0, mode)); + } + else + { + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x00000000, (nint)0x00000002, mode), BinaryIntegerHelper.Remainder((nint)0x00000000, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x00000001, (nint)0x00000002, mode), BinaryIntegerHelper.Remainder((nint)0x00000001, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x00000002, (nint)0x00000002, mode), BinaryIntegerHelper.Remainder((nint)0x00000002, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x7FFFFFFE, (nint)0x00000002, mode), BinaryIntegerHelper.Remainder((nint)0x7FFFFFFE, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x7FFFFFFF, (nint)0x00000002, mode), BinaryIntegerHelper.Remainder((nint)0x7FFFFFFF, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x80000000, (nint)0x00000002, mode), BinaryIntegerHelper.Remainder((nint)0x80000000, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x80000001, (nint)0x00000002, mode), BinaryIntegerHelper.Remainder((nint)0x80000001, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0xFFFFFFFE, (nint)0x00000002, mode), BinaryIntegerHelper.Remainder((nint)0xFFFFFFFE, (nint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0xFFFFFFFF, (nint)0x00000002, mode), BinaryIntegerHelper.Remainder((nint)0xFFFFFFFF, (nint)0x00000002, mode)); + + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x00000000, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nint)0x00000000, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x00000001, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nint)0x00000001, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x00000002, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nint)0x00000002, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x7FFFFFFE, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nint)0x7FFFFFFE, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x7FFFFFFF, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nint)0x7FFFFFFF, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x80000000, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nint)0x80000000, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0x80000001, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nint)0x80000001, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0xFFFFFFFE, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nint)0xFFFFFFFE, (nint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nint)0xFFFFFFFF, (nint)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nint)0xFFFFFFFF, (nint)0xFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Remainder((nint)0x00000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder((nint)0x00000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder((nint)0xFFFFFFFF, 0, mode)); + } + } } } diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Numerics/DimTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Numerics/DimTests.GenericMath.cs index 0da59d597f7b1..a42cdd65f8e67 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Numerics/DimTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Numerics/DimTests.GenericMath.cs @@ -45,21 +45,255 @@ public static void AllBitsSetUInt32Test() [Fact] public static void DivRemInt32Test() { - Assert.Equal(((BinaryIntegerWrapper)0x00000000, (BinaryIntegerWrapper)0x00000000), BinaryIntegerHelper>.DivRem((int)0x00000000, 2)); - Assert.Equal(((BinaryIntegerWrapper)0x00000000, (BinaryIntegerWrapper)0x00000001), BinaryIntegerHelper>.DivRem((int)0x00000001, 2)); - Assert.Equal(((BinaryIntegerWrapper)0x3FFFFFFF, (BinaryIntegerWrapper)0x00000001), BinaryIntegerHelper>.DivRem((int)0x7FFFFFFF, 2)); - Assert.Equal(((BinaryIntegerWrapper)unchecked((int)0xC0000000), (BinaryIntegerWrapper)0x00000000), BinaryIntegerHelper>.DivRem(unchecked((int)0x80000000), 2)); - Assert.Equal(((BinaryIntegerWrapper)0x00000000, (BinaryIntegerWrapper)unchecked((int)0xFFFFFFFF)), BinaryIntegerHelper>.DivRem(unchecked((int)0xFFFFFFFF), 2)); + unchecked + { + Assert.Equal(((BinaryIntegerWrapper)(int)0x00000000, (BinaryIntegerWrapper)(int)0x00000000), BinaryIntegerHelper>.DivRem((int)0x00000000, 2)); + Assert.Equal(((BinaryIntegerWrapper)(int)0x00000000, (BinaryIntegerWrapper)(int)0x00000001), BinaryIntegerHelper>.DivRem((int)0x00000001, 2)); + Assert.Equal(((BinaryIntegerWrapper)(int)0x00000001, (BinaryIntegerWrapper)(int)0x00000000), BinaryIntegerHelper>.DivRem((int)0x00000002, 2)); + Assert.Equal(((BinaryIntegerWrapper)(int)0x3FFFFFFF, (BinaryIntegerWrapper)(int)0x00000000), BinaryIntegerHelper>.DivRem((int)0x7FFFFFFE, 2)); + Assert.Equal(((BinaryIntegerWrapper)(int)0x3FFFFFFF, (BinaryIntegerWrapper)(int)0x00000001), BinaryIntegerHelper>.DivRem((int)0x7FFFFFFF, 2)); + Assert.Equal(((BinaryIntegerWrapper)(int)0xC0000000, (BinaryIntegerWrapper)(int)0x00000000), BinaryIntegerHelper>.DivRem((int)0x80000000, 2)); + Assert.Equal(((BinaryIntegerWrapper)(int)0xC0000001, (BinaryIntegerWrapper)(int)0xFFFFFFFF), BinaryIntegerHelper>.DivRem((int)0x80000001, 2)); + Assert.Equal(((BinaryIntegerWrapper)(int)0xFFFFFFFF, (BinaryIntegerWrapper)(int)0x00000000), BinaryIntegerHelper>.DivRem((int)0xFFFFFFFE, 2)); + Assert.Equal(((BinaryIntegerWrapper)(int)0x00000000, (BinaryIntegerWrapper)(int)0xFFFFFFFF), BinaryIntegerHelper>.DivRem((int)0xFFFFFFFF, 2)); + + Assert.Equal(((BinaryIntegerWrapper)(int)0x00000000, (BinaryIntegerWrapper)(int)0x00000000), BinaryIntegerHelper>.DivRem((int)0x00000000, -2)); + Assert.Equal(((BinaryIntegerWrapper)(int)0x00000000, (BinaryIntegerWrapper)(int)0x00000001), BinaryIntegerHelper>.DivRem((int)0x00000001, -2)); + Assert.Equal(((BinaryIntegerWrapper)(int)0xFFFFFFFF, (BinaryIntegerWrapper)(int)0x00000000), BinaryIntegerHelper>.DivRem((int)0x00000002, -2)); + Assert.Equal(((BinaryIntegerWrapper)(int)0xC0000001, (BinaryIntegerWrapper)(int)0x00000000), BinaryIntegerHelper>.DivRem((int)0x7FFFFFFE, -2)); + Assert.Equal(((BinaryIntegerWrapper)(int)0xC0000001, (BinaryIntegerWrapper)(int)0x00000001), BinaryIntegerHelper>.DivRem((int)0x7FFFFFFF, -2)); + Assert.Equal(((BinaryIntegerWrapper)(int)0x40000000, (BinaryIntegerWrapper)(int)0x00000000), BinaryIntegerHelper>.DivRem((int)0x80000000, -2)); + Assert.Equal(((BinaryIntegerWrapper)(int)0x3FFFFFFF, (BinaryIntegerWrapper)(int)0xFFFFFFFF), BinaryIntegerHelper>.DivRem((int)0x80000001, -2)); + Assert.Equal(((BinaryIntegerWrapper)(int)0x00000001, (BinaryIntegerWrapper)(int)0x00000000), BinaryIntegerHelper>.DivRem((int)0xFFFFFFFE, -2)); + Assert.Equal(((BinaryIntegerWrapper)(int)0x00000000, (BinaryIntegerWrapper)(int)0xFFFFFFFF), BinaryIntegerHelper>.DivRem((int)0xFFFFFFFF, -2)); + + Assert.Throws(() => BinaryIntegerHelper>.DivRem((int)0x00000000, 0)); + Assert.Throws(() => BinaryIntegerHelper>.DivRem((int)0x00000001, 0)); + Assert.Throws(() => BinaryIntegerHelper>.DivRem((int)0xFFFFFFFF, 0)); + } + } + + [Fact] + public static void DivRemModeInt32Test() + { + unchecked + { + foreach (var mode in (DivisionRounding[]) Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper>.DivRemExpected((int)0x00000000, 2, mode), BinaryIntegerHelper>.DivRem((int)0x00000000, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected((int)0x00000001, 2, mode), BinaryIntegerHelper>.DivRem((int)0x00000001, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected((int)0x00000002, 2, mode), BinaryIntegerHelper>.DivRem((int)0x00000002, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected((int)0x7FFFFFFE, 2, mode), BinaryIntegerHelper>.DivRem((int)0x7FFFFFFE, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected((int)0x7FFFFFFF, 2, mode), BinaryIntegerHelper>.DivRem((int)0x7FFFFFFF, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected((int)0x80000000, 2, mode), BinaryIntegerHelper>.DivRem((int)0x80000000, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected((int)0x80000001, 2, mode), BinaryIntegerHelper>.DivRem((int)0x80000001, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected((int)0xFFFFFFFE, 2, mode), BinaryIntegerHelper>.DivRem((int)0xFFFFFFFE, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected((int)0xFFFFFFFF, 2, mode), BinaryIntegerHelper>.DivRem((int)0xFFFFFFFF, 2, mode)); + + Assert.Equal(BinaryIntegerHelper>.DivRemExpected((int)0x00000000, -2, mode), BinaryIntegerHelper>.DivRem((int)0x00000000, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected((int)0x00000001, -2, mode), BinaryIntegerHelper>.DivRem((int)0x00000001, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected((int)0x00000002, -2, mode), BinaryIntegerHelper>.DivRem((int)0x00000002, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected((int)0x7FFFFFFE, -2, mode), BinaryIntegerHelper>.DivRem((int)0x7FFFFFFE, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected((int)0x7FFFFFFF, -2, mode), BinaryIntegerHelper>.DivRem((int)0x7FFFFFFF, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected((int)0x80000000, -2, mode), BinaryIntegerHelper>.DivRem((int)0x80000000, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected((int)0x80000001, -2, mode), BinaryIntegerHelper>.DivRem((int)0x80000001, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected((int)0xFFFFFFFE, -2, mode), BinaryIntegerHelper>.DivRem((int)0xFFFFFFFE, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected((int)0xFFFFFFFF, -2, mode), BinaryIntegerHelper>.DivRem((int)0xFFFFFFFF, -2, mode)); + + Assert.Throws(() => BinaryIntegerHelper>.DivRem((int)0x00000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper>.DivRem((int)0x00000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper>.DivRem((int)0xFFFFFFFF, 0, mode)); + } + } + } + + [Fact] + public static void DivideModeInt32Test() + { + unchecked + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper>.DivideExpected((int)0x00000000, 2, mode), BinaryIntegerHelper>.Divide((int)0x00000000, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected((int)0x00000001, 2, mode), BinaryIntegerHelper>.Divide((int)0x00000001, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected((int)0x00000002, 2, mode), BinaryIntegerHelper>.Divide((int)0x00000002, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected((int)0x7FFFFFFE, 2, mode), BinaryIntegerHelper>.Divide((int)0x7FFFFFFE, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected((int)0x7FFFFFFF, 2, mode), BinaryIntegerHelper>.Divide((int)0x7FFFFFFF, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected((int)0x80000000, 2, mode), BinaryIntegerHelper>.Divide((int)0x80000000, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected((int)0x80000001, 2, mode), BinaryIntegerHelper>.Divide((int)0x80000001, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected((int)0xFFFFFFFE, 2, mode), BinaryIntegerHelper>.Divide((int)0xFFFFFFFE, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected((int)0xFFFFFFFF, 2, mode), BinaryIntegerHelper>.Divide((int)0xFFFFFFFF, 2, mode)); + + Assert.Equal(BinaryIntegerHelper>.DivideExpected((int)0x00000000, -2, mode), BinaryIntegerHelper>.Divide((int)0x00000000, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected((int)0x00000001, -2, mode), BinaryIntegerHelper>.Divide((int)0x00000001, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected((int)0x00000002, -2, mode), BinaryIntegerHelper>.Divide((int)0x00000002, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected((int)0x7FFFFFFE, -2, mode), BinaryIntegerHelper>.Divide((int)0x7FFFFFFE, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected((int)0x7FFFFFFF, -2, mode), BinaryIntegerHelper>.Divide((int)0x7FFFFFFF, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected((int)0x80000000, -2, mode), BinaryIntegerHelper>.Divide((int)0x80000000, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected((int)0x80000001, -2, mode), BinaryIntegerHelper>.Divide((int)0x80000001, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected((int)0xFFFFFFFE, -2, mode), BinaryIntegerHelper>.Divide((int)0xFFFFFFFE, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected((int)0xFFFFFFFF, -2, mode), BinaryIntegerHelper>.Divide((int)0xFFFFFFFF, -2, mode)); + + Assert.Throws(() => BinaryIntegerHelper>.Divide((int)0x00000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper>.Divide((int)0x00000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper>.Divide((int)0xFFFFFFFF, 0, mode)); + } + } + } + + [Fact] + public static void RemainderModeInt32Test() + { + unchecked + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper>.RemainderExpected((int)0x00000000, 2, mode), BinaryIntegerHelper>.Remainder((int)0x00000000, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected((int)0x00000001, 2, mode), BinaryIntegerHelper>.Remainder((int)0x00000001, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected((int)0x00000002, 2, mode), BinaryIntegerHelper>.Remainder((int)0x00000002, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected((int)0x7FFFFFFE, 2, mode), BinaryIntegerHelper>.Remainder((int)0x7FFFFFFE, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected((int)0x7FFFFFFF, 2, mode), BinaryIntegerHelper>.Remainder((int)0x7FFFFFFF, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected((int)0x80000000, 2, mode), BinaryIntegerHelper>.Remainder((int)0x80000000, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected((int)0x80000001, 2, mode), BinaryIntegerHelper>.Remainder((int)0x80000001, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected((int)0xFFFFFFFE, 2, mode), BinaryIntegerHelper>.Remainder((int)0xFFFFFFFE, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected((int)0xFFFFFFFF, 2, mode), BinaryIntegerHelper>.Remainder((int)0xFFFFFFFF, 2, mode)); + + Assert.Equal(BinaryIntegerHelper>.RemainderExpected((int)0x00000000, -2, mode), BinaryIntegerHelper>.Remainder((int)0x00000000, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected((int)0x00000001, -2, mode), BinaryIntegerHelper>.Remainder((int)0x00000001, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected((int)0x00000002, -2, mode), BinaryIntegerHelper>.Remainder((int)0x00000002, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected((int)0x7FFFFFFE, -2, mode), BinaryIntegerHelper>.Remainder((int)0x7FFFFFFE, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected((int)0x7FFFFFFF, -2, mode), BinaryIntegerHelper>.Remainder((int)0x7FFFFFFF, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected((int)0x80000000, -2, mode), BinaryIntegerHelper>.Remainder((int)0x80000000, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected((int)0x80000001, -2, mode), BinaryIntegerHelper>.Remainder((int)0x80000001, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected((int)0xFFFFFFFE, -2, mode), BinaryIntegerHelper>.Remainder((int)0xFFFFFFFE, -2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected((int)0xFFFFFFFF, -2, mode), BinaryIntegerHelper>.Remainder((int)0xFFFFFFFF, -2, mode)); + + Assert.Throws(() => BinaryIntegerHelper>.Remainder((int)0x00000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper>.Remainder((int)0x00000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper>.Remainder((int)0xFFFFFFFF, 0, mode)); + } + } } [Fact] public static void DivRemUInt32Test() { - Assert.Equal(((BinaryIntegerWrapper)0x00000000, (BinaryIntegerWrapper)0x00000000), BinaryIntegerHelper>.DivRem((uint)0x00000000, 2)); - Assert.Equal(((BinaryIntegerWrapper)0x00000000, (BinaryIntegerWrapper)0x00000001), BinaryIntegerHelper>.DivRem((uint)0x00000001, 2)); - Assert.Equal(((BinaryIntegerWrapper)0x3FFFFFFF, (BinaryIntegerWrapper)0x00000001), BinaryIntegerHelper>.DivRem((uint)0x7FFFFFFF, 2)); - Assert.Equal(((BinaryIntegerWrapper)0x40000000, (BinaryIntegerWrapper)0x00000000), BinaryIntegerHelper>.DivRem((uint)0x80000000, 2)); - Assert.Equal(((BinaryIntegerWrapper)0x7FFFFFFF, (BinaryIntegerWrapper)0x00000001), BinaryIntegerHelper>.DivRem((uint)0xFFFFFFFF, 2)); + Assert.Equal(((BinaryIntegerWrapper)0x00000000, (BinaryIntegerWrapper)0x00000000), BinaryIntegerHelper>.DivRem(0x00000000, 2)); + Assert.Equal(((BinaryIntegerWrapper)0x00000000, (BinaryIntegerWrapper)0x00000001), BinaryIntegerHelper>.DivRem(0x00000001, 2)); + Assert.Equal(((BinaryIntegerWrapper)0x00000001, (BinaryIntegerWrapper)0x00000000), BinaryIntegerHelper>.DivRem(0x00000002, 2)); + Assert.Equal(((BinaryIntegerWrapper)0x3FFFFFFF, (BinaryIntegerWrapper)0x00000000), BinaryIntegerHelper>.DivRem(0x7FFFFFFE, 2)); + Assert.Equal(((BinaryIntegerWrapper)0x3FFFFFFF, (BinaryIntegerWrapper)0x00000001), BinaryIntegerHelper>.DivRem(0x7FFFFFFF, 2)); + Assert.Equal(((BinaryIntegerWrapper)0x40000000, (BinaryIntegerWrapper)0x00000000), BinaryIntegerHelper>.DivRem(0x80000000, 2)); + Assert.Equal(((BinaryIntegerWrapper)0x40000000, (BinaryIntegerWrapper)0x00000001), BinaryIntegerHelper>.DivRem(0x80000001, 2)); + Assert.Equal(((BinaryIntegerWrapper)0x7FFFFFFF, (BinaryIntegerWrapper)0x00000000), BinaryIntegerHelper>.DivRem(0xFFFFFFFE, 2)); + Assert.Equal(((BinaryIntegerWrapper)0x7FFFFFFF, (BinaryIntegerWrapper)0x00000001), BinaryIntegerHelper>.DivRem(0xFFFFFFFF, 2)); + + Assert.Equal(((BinaryIntegerWrapper)0x00000000, (BinaryIntegerWrapper)0x00000000), BinaryIntegerHelper>.DivRem(0x00000000, 0xFFFFFFFE)); + Assert.Equal(((BinaryIntegerWrapper)0x00000000, (BinaryIntegerWrapper)0x00000001), BinaryIntegerHelper>.DivRem(0x00000001, 0xFFFFFFFE)); + Assert.Equal(((BinaryIntegerWrapper)0x00000000, (BinaryIntegerWrapper)0x00000002), BinaryIntegerHelper>.DivRem(0x00000002, 0xFFFFFFFE)); + Assert.Equal(((BinaryIntegerWrapper)0x00000000, (BinaryIntegerWrapper)0x7FFFFFFE), BinaryIntegerHelper>.DivRem(0x7FFFFFFE, 0xFFFFFFFE)); + Assert.Equal(((BinaryIntegerWrapper)0x00000000, (BinaryIntegerWrapper)0x7FFFFFFF), BinaryIntegerHelper>.DivRem(0x7FFFFFFF, 0xFFFFFFFE)); + Assert.Equal(((BinaryIntegerWrapper)0x00000000, (BinaryIntegerWrapper)0x80000000), BinaryIntegerHelper>.DivRem(0x80000000, 0xFFFFFFFE)); + Assert.Equal(((BinaryIntegerWrapper)0x00000000, (BinaryIntegerWrapper)0x80000001), BinaryIntegerHelper>.DivRem(0x80000001, 0xFFFFFFFE)); + Assert.Equal(((BinaryIntegerWrapper)0x00000001, (BinaryIntegerWrapper)0x00000000), BinaryIntegerHelper>.DivRem(0xFFFFFFFE, 0xFFFFFFFE)); + Assert.Equal(((BinaryIntegerWrapper)0x00000001, (BinaryIntegerWrapper)0x00000001), BinaryIntegerHelper>.DivRem(0xFFFFFFFF, 0xFFFFFFFE)); + + Assert.Throws(() => BinaryIntegerHelper>.DivRem(0x00000000, 0)); + Assert.Throws(() => BinaryIntegerHelper>.DivRem(0x00000001, 0)); + Assert.Throws(() => BinaryIntegerHelper>.DivRem(0xFFFFFFFF, 0)); + } + + [Fact] + public static void DivRemModeUInt32Test() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper>.DivRemExpected(0x00000000, 2, mode), BinaryIntegerHelper>.DivRem(0x00000000, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected(0x00000001, 2, mode), BinaryIntegerHelper>.DivRem(0x00000001, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected(0x00000002, 2, mode), BinaryIntegerHelper>.DivRem(0x00000002, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected(0x7FFFFFFE, 2, mode), BinaryIntegerHelper>.DivRem(0x7FFFFFFE, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected(0x7FFFFFFF, 2, mode), BinaryIntegerHelper>.DivRem(0x7FFFFFFF, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected(0x80000000, 2, mode), BinaryIntegerHelper>.DivRem(0x80000000, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected(0x80000001, 2, mode), BinaryIntegerHelper>.DivRem(0x80000001, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected(0xFFFFFFFE, 2, mode), BinaryIntegerHelper>.DivRem(0xFFFFFFFE, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected(0xFFFFFFFF, 2, mode), BinaryIntegerHelper>.DivRem(0xFFFFFFFF, 2, mode)); + + Assert.Equal(BinaryIntegerHelper>.DivRemExpected(0x00000000, 0xFFFFFFFE, mode), BinaryIntegerHelper>.DivRem(0x00000000, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected(0x00000001, 0xFFFFFFFE, mode), BinaryIntegerHelper>.DivRem(0x00000001, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected(0x00000002, 0xFFFFFFFE, mode), BinaryIntegerHelper>.DivRem(0x00000002, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected(0x7FFFFFFE, 0xFFFFFFFE, mode), BinaryIntegerHelper>.DivRem(0x7FFFFFFE, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected(0x7FFFFFFF, 0xFFFFFFFE, mode), BinaryIntegerHelper>.DivRem(0x7FFFFFFF, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected(0x80000000, 0xFFFFFFFE, mode), BinaryIntegerHelper>.DivRem(0x80000000, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected(0x80000001, 0xFFFFFFFE, mode), BinaryIntegerHelper>.DivRem(0x80000001, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected(0xFFFFFFFE, 0xFFFFFFFE, mode), BinaryIntegerHelper>.DivRem(0xFFFFFFFE, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.DivRemExpected(0xFFFFFFFF, 0xFFFFFFFE, mode), BinaryIntegerHelper>.DivRem(0xFFFFFFFF, 0xFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper>.DivRem(0x00000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper>.DivRem(0x00000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper>.DivRem(0xFFFFFFFF, 0, mode)); + } + } + + [Fact] + public static void DivideModeUInt32Test() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper>.DivideExpected(0x00000000, 2, mode), BinaryIntegerHelper>.Divide(0x00000000, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected(0x00000001, 2, mode), BinaryIntegerHelper>.Divide(0x00000001, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected(0x00000002, 2, mode), BinaryIntegerHelper>.Divide(0x00000002, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected(0x7FFFFFFE, 2, mode), BinaryIntegerHelper>.Divide(0x7FFFFFFE, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected(0x7FFFFFFF, 2, mode), BinaryIntegerHelper>.Divide(0x7FFFFFFF, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected(0x80000000, 2, mode), BinaryIntegerHelper>.Divide(0x80000000, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected(0x80000001, 2, mode), BinaryIntegerHelper>.Divide(0x80000001, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected(0xFFFFFFFE, 2, mode), BinaryIntegerHelper>.Divide(0xFFFFFFFE, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected(0xFFFFFFFF, 2, mode), BinaryIntegerHelper>.Divide(0xFFFFFFFF, 2, mode)); + + Assert.Equal(BinaryIntegerHelper>.DivideExpected(0x00000000, 0xFFFFFFFE, mode), BinaryIntegerHelper>.Divide(0x00000000, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected(0x00000001, 0xFFFFFFFE, mode), BinaryIntegerHelper>.Divide(0x00000001, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected(0x00000002, 0xFFFFFFFE, mode), BinaryIntegerHelper>.Divide(0x00000002, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected(0x7FFFFFFE, 0xFFFFFFFE, mode), BinaryIntegerHelper>.Divide(0x7FFFFFFE, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected(0x7FFFFFFF, 0xFFFFFFFE, mode), BinaryIntegerHelper>.Divide(0x7FFFFFFF, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected(0x80000000, 0xFFFFFFFE, mode), BinaryIntegerHelper>.Divide(0x80000000, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected(0x80000001, 0xFFFFFFFE, mode), BinaryIntegerHelper>.Divide(0x80000001, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected(0xFFFFFFFE, 0xFFFFFFFE, mode), BinaryIntegerHelper>.Divide(0xFFFFFFFE, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.DivideExpected(0xFFFFFFFF, 0xFFFFFFFE, mode), BinaryIntegerHelper>.Divide(0xFFFFFFFF, 0xFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper>.Divide(0x00000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper>.Divide(0x00000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper>.Divide(0xFFFFFFFF, 0, mode)); + } + } + + [Fact] + public static void RemainderModeUInt32Test() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper>.RemainderExpected(0x00000000, 2, mode), BinaryIntegerHelper>.Remainder(0x00000000, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected(0x00000001, 2, mode), BinaryIntegerHelper>.Remainder(0x00000001, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected(0x00000002, 2, mode), BinaryIntegerHelper>.Remainder(0x00000002, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected(0x7FFFFFFE, 2, mode), BinaryIntegerHelper>.Remainder(0x7FFFFFFE, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected(0x7FFFFFFF, 2, mode), BinaryIntegerHelper>.Remainder(0x7FFFFFFF, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected(0x80000000, 2, mode), BinaryIntegerHelper>.Remainder(0x80000000, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected(0x80000001, 2, mode), BinaryIntegerHelper>.Remainder(0x80000001, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected(0xFFFFFFFE, 2, mode), BinaryIntegerHelper>.Remainder(0xFFFFFFFE, 2, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected(0xFFFFFFFF, 2, mode), BinaryIntegerHelper>.Remainder(0xFFFFFFFF, 2, mode)); + + Assert.Equal(BinaryIntegerHelper>.RemainderExpected(0x00000000, 0xFFFFFFFE, mode), BinaryIntegerHelper>.Remainder(0x00000000, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected(0x00000001, 0xFFFFFFFE, mode), BinaryIntegerHelper>.Remainder(0x00000001, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected(0x00000002, 0xFFFFFFFE, mode), BinaryIntegerHelper>.Remainder(0x00000002, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected(0x7FFFFFFE, 0xFFFFFFFE, mode), BinaryIntegerHelper>.Remainder(0x7FFFFFFE, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected(0x7FFFFFFF, 0xFFFFFFFE, mode), BinaryIntegerHelper>.Remainder(0x7FFFFFFF, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected(0x80000000, 0xFFFFFFFE, mode), BinaryIntegerHelper>.Remainder(0x80000000, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected(0x80000001, 0xFFFFFFFE, mode), BinaryIntegerHelper>.Remainder(0x80000001, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected(0xFFFFFFFE, 0xFFFFFFFE, mode), BinaryIntegerHelper>.Remainder(0xFFFFFFFE, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper>.RemainderExpected(0xFFFFFFFF, 0xFFFFFFFE, mode), BinaryIntegerHelper>.Remainder(0xFFFFFFFF, 0xFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper>.Remainder(0x00000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper>.Remainder(0x00000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper>.Remainder(0xFFFFFFFF, 0, mode)); + } } [Fact] diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/SByteTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/SByteTests.GenericMath.cs index 80b566a8473d0..f5add4d9edd06 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/SByteTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/SByteTests.GenericMath.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Globalization; +using System.Numerics; using System.Runtime.InteropServices; using Xunit; @@ -51,11 +52,134 @@ public static void AdditiveIdentityTest() [Fact] public static void DivRemTest() { - Assert.Equal(((sbyte)0x00, (sbyte)0x00), BinaryIntegerHelper.DivRem((sbyte)0x00, (sbyte)2)); - Assert.Equal(((sbyte)0x00, (sbyte)0x01), BinaryIntegerHelper.DivRem((sbyte)0x01, (sbyte)2)); - Assert.Equal(((sbyte)0x3F, (sbyte)0x01), BinaryIntegerHelper.DivRem((sbyte)0x7F, (sbyte)2)); - Assert.Equal((unchecked((sbyte)0xC0), (sbyte)0x00), BinaryIntegerHelper.DivRem(unchecked((sbyte)0x80), (sbyte)2)); - Assert.Equal(((sbyte)0x00, unchecked((sbyte)0xFF)), BinaryIntegerHelper.DivRem(unchecked((sbyte)0xFF), (sbyte)2)); + unchecked + { + Assert.Equal(((sbyte)0x00, (sbyte)0x00), BinaryIntegerHelper.DivRem((sbyte)0x00, (sbyte)0x02)); + Assert.Equal(((sbyte)0x00, (sbyte)0x01), BinaryIntegerHelper.DivRem((sbyte)0x01, (sbyte)0x02)); + Assert.Equal(((sbyte)0x01, (sbyte)0x00), BinaryIntegerHelper.DivRem((sbyte)0x02, (sbyte)0x02)); + Assert.Equal(((sbyte)0x3F, (sbyte)0x00), BinaryIntegerHelper.DivRem((sbyte)0x7E, (sbyte)0x02)); + Assert.Equal(((sbyte)0x3F, (sbyte)0x01), BinaryIntegerHelper.DivRem((sbyte)0x7F, (sbyte)0x02)); + Assert.Equal(((sbyte)0xC0, (sbyte)0x00), BinaryIntegerHelper.DivRem((sbyte)0x80, (sbyte)0x02)); + Assert.Equal(((sbyte)0xC1, (sbyte)0xFF), BinaryIntegerHelper.DivRem((sbyte)0x81, (sbyte)0x02)); + Assert.Equal(((sbyte)0xFF, (sbyte)0x00), BinaryIntegerHelper.DivRem((sbyte)0xFE, (sbyte)0x02)); + Assert.Equal(((sbyte)0x00, (sbyte)0xFF), BinaryIntegerHelper.DivRem((sbyte)0xFF, (sbyte)0x02)); + + Assert.Equal(((sbyte)0x00, (sbyte)0x00), BinaryIntegerHelper.DivRem((sbyte)0x00, (sbyte)0xFE)); + Assert.Equal(((sbyte)0x00, (sbyte)0x01), BinaryIntegerHelper.DivRem((sbyte)0x01, (sbyte)0xFE)); + Assert.Equal(((sbyte)0xFF, (sbyte)0x00), BinaryIntegerHelper.DivRem((sbyte)0x02, (sbyte)0xFE)); + Assert.Equal(((sbyte)0xC1, (sbyte)0x00), BinaryIntegerHelper.DivRem((sbyte)0x7E, (sbyte)0xFE)); + Assert.Equal(((sbyte)0xC1, (sbyte)0x01), BinaryIntegerHelper.DivRem((sbyte)0x7F, (sbyte)0xFE)); + Assert.Equal(((sbyte)0x40, (sbyte)0x00), BinaryIntegerHelper.DivRem((sbyte)0x80, (sbyte)0xFE)); + Assert.Equal(((sbyte)0x3F, (sbyte)0xFF), BinaryIntegerHelper.DivRem((sbyte)0x81, (sbyte)0xFE)); + Assert.Equal(((sbyte)0x01, (sbyte)0x00), BinaryIntegerHelper.DivRem((sbyte)0xFE, (sbyte)0xFE)); + Assert.Equal(((sbyte)0x00, (sbyte)0xFF), BinaryIntegerHelper.DivRem((sbyte)0xFF, (sbyte)0xFE)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem((sbyte)0x00, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((sbyte)0x01, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((sbyte)0xFF, 0)); + } + } + + [Fact] + public static void DivRemModeTest() + { + unchecked + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivRemExpected((sbyte)0x00, (sbyte)0x02, mode), BinaryIntegerHelper.DivRem((sbyte)0x00, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((sbyte)0x01, (sbyte)0x02, mode), BinaryIntegerHelper.DivRem((sbyte)0x01, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((sbyte)0x02, (sbyte)0x02, mode), BinaryIntegerHelper.DivRem((sbyte)0x02, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((sbyte)0x7E, (sbyte)0x02, mode), BinaryIntegerHelper.DivRem((sbyte)0x7E, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((sbyte)0x7F, (sbyte)0x02, mode), BinaryIntegerHelper.DivRem((sbyte)0x7F, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((sbyte)0x80, (sbyte)0x02, mode), BinaryIntegerHelper.DivRem((sbyte)0x80, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((sbyte)0x81, (sbyte)0x02, mode), BinaryIntegerHelper.DivRem((sbyte)0x81, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((sbyte)0xFE, (sbyte)0x02, mode), BinaryIntegerHelper.DivRem((sbyte)0xFE, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((sbyte)0xFF, (sbyte)0x02, mode), BinaryIntegerHelper.DivRem((sbyte)0xFF, (sbyte)0x02, mode)); + + Assert.Equal(BinaryIntegerHelper.DivRemExpected((sbyte)0x00, (sbyte)0xFE, mode), BinaryIntegerHelper.DivRem((sbyte)0x00, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((sbyte)0x01, (sbyte)0xFE, mode), BinaryIntegerHelper.DivRem((sbyte)0x01, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((sbyte)0x02, (sbyte)0xFE, mode), BinaryIntegerHelper.DivRem((sbyte)0x02, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((sbyte)0x7E, (sbyte)0xFE, mode), BinaryIntegerHelper.DivRem((sbyte)0x7E, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((sbyte)0x7F, (sbyte)0xFE, mode), BinaryIntegerHelper.DivRem((sbyte)0x7F, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((sbyte)0x80, (sbyte)0xFE, mode), BinaryIntegerHelper.DivRem((sbyte)0x80, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((sbyte)0x81, (sbyte)0xFE, mode), BinaryIntegerHelper.DivRem((sbyte)0x81, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((sbyte)0xFE, (sbyte)0xFE, mode), BinaryIntegerHelper.DivRem((sbyte)0xFE, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((sbyte)0xFF, (sbyte)0xFE, mode), BinaryIntegerHelper.DivRem((sbyte)0xFF, (sbyte)0xFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem((sbyte)0x00, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((sbyte)0x01, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((sbyte)0xFF, 0, mode)); + } + } + } + + [Fact] + public static void DivideModeTest() + { + unchecked + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivideExpected((sbyte)0x00, (sbyte)0x02, mode), BinaryIntegerHelper.Divide((sbyte)0x00, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((sbyte)0x01, (sbyte)0x02, mode), BinaryIntegerHelper.Divide((sbyte)0x01, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((sbyte)0x02, (sbyte)0x02, mode), BinaryIntegerHelper.Divide((sbyte)0x02, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((sbyte)0x7E, (sbyte)0x02, mode), BinaryIntegerHelper.Divide((sbyte)0x7E, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((sbyte)0x7F, (sbyte)0x02, mode), BinaryIntegerHelper.Divide((sbyte)0x7F, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((sbyte)0x80, (sbyte)0x02, mode), BinaryIntegerHelper.Divide((sbyte)0x80, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((sbyte)0x81, (sbyte)0x02, mode), BinaryIntegerHelper.Divide((sbyte)0x81, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((sbyte)0xFE, (sbyte)0x02, mode), BinaryIntegerHelper.Divide((sbyte)0xFE, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((sbyte)0xFF, (sbyte)0x02, mode), BinaryIntegerHelper.Divide((sbyte)0xFF, (sbyte)0x02, mode)); + + Assert.Equal(BinaryIntegerHelper.DivideExpected((sbyte)0x00, (sbyte)0xFE, mode), BinaryIntegerHelper.Divide((sbyte)0x00, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((sbyte)0x01, (sbyte)0xFE, mode), BinaryIntegerHelper.Divide((sbyte)0x01, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((sbyte)0x02, (sbyte)0xFE, mode), BinaryIntegerHelper.Divide((sbyte)0x02, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((sbyte)0x7E, (sbyte)0xFE, mode), BinaryIntegerHelper.Divide((sbyte)0x7E, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((sbyte)0x7F, (sbyte)0xFE, mode), BinaryIntegerHelper.Divide((sbyte)0x7F, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((sbyte)0x80, (sbyte)0xFE, mode), BinaryIntegerHelper.Divide((sbyte)0x80, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((sbyte)0x81, (sbyte)0xFE, mode), BinaryIntegerHelper.Divide((sbyte)0x81, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((sbyte)0xFE, (sbyte)0xFE, mode), BinaryIntegerHelper.Divide((sbyte)0xFE, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((sbyte)0xFF, (sbyte)0xFE, mode), BinaryIntegerHelper.Divide((sbyte)0xFF, (sbyte)0xFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Divide((sbyte)0x00, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide((sbyte)0x01, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide((sbyte)0xFF, 0, mode)); + } + } + } + + [Fact] + public static void RemainderModeTest() + { + unchecked + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.RemainderExpected((sbyte)0x00, (sbyte)0x02, mode), BinaryIntegerHelper.Remainder((sbyte)0x00, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((sbyte)0x01, (sbyte)0x02, mode), BinaryIntegerHelper.Remainder((sbyte)0x01, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((sbyte)0x02, (sbyte)0x02, mode), BinaryIntegerHelper.Remainder((sbyte)0x02, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((sbyte)0x7E, (sbyte)0x02, mode), BinaryIntegerHelper.Remainder((sbyte)0x7E, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((sbyte)0x7F, (sbyte)0x02, mode), BinaryIntegerHelper.Remainder((sbyte)0x7F, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((sbyte)0x80, (sbyte)0x02, mode), BinaryIntegerHelper.Remainder((sbyte)0x80, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((sbyte)0x81, (sbyte)0x02, mode), BinaryIntegerHelper.Remainder((sbyte)0x81, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((sbyte)0xFE, (sbyte)0x02, mode), BinaryIntegerHelper.Remainder((sbyte)0xFE, (sbyte)0x02, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((sbyte)0xFF, (sbyte)0x02, mode), BinaryIntegerHelper.Remainder((sbyte)0xFF, (sbyte)0x02, mode)); + + Assert.Equal(BinaryIntegerHelper.RemainderExpected((sbyte)0x00, (sbyte)0xFE, mode), BinaryIntegerHelper.Remainder((sbyte)0x00, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((sbyte)0x01, (sbyte)0xFE, mode), BinaryIntegerHelper.Remainder((sbyte)0x01, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((sbyte)0x02, (sbyte)0xFE, mode), BinaryIntegerHelper.Remainder((sbyte)0x02, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((sbyte)0x7E, (sbyte)0xFE, mode), BinaryIntegerHelper.Remainder((sbyte)0x7E, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((sbyte)0x7F, (sbyte)0xFE, mode), BinaryIntegerHelper.Remainder((sbyte)0x7F, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((sbyte)0x80, (sbyte)0xFE, mode), BinaryIntegerHelper.Remainder((sbyte)0x80, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((sbyte)0x81, (sbyte)0xFE, mode), BinaryIntegerHelper.Remainder((sbyte)0x81, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((sbyte)0xFE, (sbyte)0xFE, mode), BinaryIntegerHelper.Remainder((sbyte)0xFE, (sbyte)0xFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((sbyte)0xFF, (sbyte)0xFE, mode), BinaryIntegerHelper.Remainder((sbyte)0xFF, (sbyte)0xFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Remainder((sbyte)0x00, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder((sbyte)0x01, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder((sbyte)0xFF, 0, mode)); + } + } } [Fact] diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt128Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt128Tests.GenericMath.cs index d8366cf6542d7..8492d36367bc0 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt128Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt128Tests.GenericMath.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Globalization; +using System.Numerics; using System.Runtime.InteropServices; using Xunit; @@ -101,11 +102,122 @@ public static void AdditiveIdentityTest() [Fact] public static void DivRemTest() { - Assert.Equal((Zero, Zero), BinaryIntegerHelper.DivRem(Zero, 2U)); - Assert.Equal((Zero, One), BinaryIntegerHelper.DivRem(One, 2U)); - Assert.Equal((new UInt128(0x3FFF_FFFF_FFFF_FFFF, 0xFFFF_FFFF_FFFF_FFFF), One), BinaryIntegerHelper.DivRem(Int128MaxValue, 2U)); - Assert.Equal((new UInt128(0x4000_0000_0000_0000, 0x0000_0000_0000_0000), Zero), BinaryIntegerHelper.DivRem(Int128MaxValuePlusOne, 2U)); - Assert.Equal((Int128MaxValue, One), BinaryIntegerHelper.DivRem(MaxValue, 2U)); + Assert.Equal((Zero, Zero), BinaryIntegerHelper.DivRem(Zero, Two)); + Assert.Equal((Zero, One), BinaryIntegerHelper.DivRem(One, Two)); + Assert.Equal((One, Zero), BinaryIntegerHelper.DivRem(Two, Two)); + Assert.Equal((new UInt128(0x3FFF_FFFF_FFFF_FFFF, 0xFFFF_FFFF_FFFF_FFFF), Zero), BinaryIntegerHelper.DivRem(Int128MaxValueMinusOne, Two)); + Assert.Equal((new UInt128(0x3FFF_FFFF_FFFF_FFFF, 0xFFFF_FFFF_FFFF_FFFF), One), BinaryIntegerHelper.DivRem(Int128MaxValue, Two)); + Assert.Equal((new UInt128(0x4000_0000_0000_0000, 0x0000_0000_0000_0000), Zero), BinaryIntegerHelper.DivRem(Int128MaxValuePlusOne, Two)); + Assert.Equal((new UInt128(0x4000_0000_0000_0000, 0x0000_0000_0000_0000), One), BinaryIntegerHelper.DivRem(Int128MaxValuePlusTwo, Two)); + Assert.Equal((Int128MaxValue, Zero), BinaryIntegerHelper.DivRem(MaxValueMinusOne, Two)); + Assert.Equal((Int128MaxValue, One), BinaryIntegerHelper.DivRem(MaxValue, Two)); + + Assert.Equal((Zero, Zero), BinaryIntegerHelper.DivRem(Zero, MaxValueMinusOne)); + Assert.Equal((Zero, One), BinaryIntegerHelper.DivRem(One, MaxValueMinusOne)); + Assert.Equal((Zero, Two), BinaryIntegerHelper.DivRem(Two, MaxValueMinusOne)); + Assert.Equal((Zero, Int128MaxValueMinusOne), BinaryIntegerHelper.DivRem(Int128MaxValueMinusOne, MaxValueMinusOne)); + Assert.Equal((Zero, Int128MaxValue), BinaryIntegerHelper.DivRem(Int128MaxValue, MaxValueMinusOne)); + Assert.Equal((Zero, Int128MaxValuePlusOne), BinaryIntegerHelper.DivRem(Int128MaxValuePlusOne, MaxValueMinusOne)); + Assert.Equal((Zero, Int128MaxValuePlusTwo), BinaryIntegerHelper.DivRem(Int128MaxValuePlusTwo, MaxValueMinusOne)); + Assert.Equal((One, Zero), BinaryIntegerHelper.DivRem(MaxValueMinusOne, MaxValueMinusOne)); + Assert.Equal((One, One), BinaryIntegerHelper.DivRem(MaxValue, MaxValueMinusOne)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem(Zero, Zero)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(One, Zero)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(MaxValue, Zero)); + } + + [Fact] + public static void DivRemModeTest() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivRemExpected(Zero, Two, mode), BinaryIntegerHelper.DivRem(Zero, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(One, Two, mode), BinaryIntegerHelper.DivRem(One, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(Two, Two, mode), BinaryIntegerHelper.DivRem(Two, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(Int128MaxValueMinusOne, Two, mode), BinaryIntegerHelper.DivRem(Int128MaxValueMinusOne, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(Int128MaxValue, Two, mode), BinaryIntegerHelper.DivRem(Int128MaxValue, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(Int128MaxValuePlusOne, Two, mode), BinaryIntegerHelper.DivRem(Int128MaxValuePlusOne, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(Int128MaxValuePlusTwo, Two, mode), BinaryIntegerHelper.DivRem(Int128MaxValuePlusTwo, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(MaxValueMinusOne, Two, mode), BinaryIntegerHelper.DivRem(MaxValueMinusOne, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(MaxValue, Two, mode), BinaryIntegerHelper.DivRem(MaxValue, Two, mode)); + + Assert.Equal(BinaryIntegerHelper.DivRemExpected(Zero, MaxValueMinusOne, mode), BinaryIntegerHelper.DivRem(Zero, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(One, MaxValueMinusOne, mode), BinaryIntegerHelper.DivRem(One, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(Two, MaxValueMinusOne, mode), BinaryIntegerHelper.DivRem(Two, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(Int128MaxValueMinusOne, MaxValueMinusOne, mode), BinaryIntegerHelper.DivRem(Int128MaxValueMinusOne, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(Int128MaxValue, MaxValueMinusOne, mode), BinaryIntegerHelper.DivRem(Int128MaxValue, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(Int128MaxValuePlusOne, MaxValueMinusOne, mode), BinaryIntegerHelper.DivRem(Int128MaxValuePlusOne, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(Int128MaxValuePlusTwo, MaxValueMinusOne, mode), BinaryIntegerHelper.DivRem(Int128MaxValuePlusTwo, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(MaxValueMinusOne, MaxValueMinusOne, mode), BinaryIntegerHelper.DivRem(MaxValueMinusOne, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(MaxValue, MaxValueMinusOne, mode), BinaryIntegerHelper.DivRem(MaxValue, MaxValueMinusOne, mode)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem(Zero, Zero, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(One, Zero, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(MaxValue, Zero, mode)); + } + } + + [Fact] + public static void DivideModeTest() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivideExpected(Zero, Two, mode), BinaryIntegerHelper.Divide(Zero, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(One, Two, mode), BinaryIntegerHelper.Divide(One, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(Two, Two, mode), BinaryIntegerHelper.Divide(Two, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(Int128MaxValueMinusOne, Two, mode), BinaryIntegerHelper.Divide(Int128MaxValueMinusOne, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(Int128MaxValue, Two, mode), BinaryIntegerHelper.Divide(Int128MaxValue, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(Int128MaxValuePlusOne, Two, mode), BinaryIntegerHelper.Divide(Int128MaxValuePlusOne, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(Int128MaxValuePlusTwo, Two, mode), BinaryIntegerHelper.Divide(Int128MaxValuePlusTwo, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(MaxValueMinusOne, Two, mode), BinaryIntegerHelper.Divide(MaxValueMinusOne, Two, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(MaxValue, Two, mode), BinaryIntegerHelper.Divide(MaxValue, Two, mode)); + + Assert.Equal(BinaryIntegerHelper.DivideExpected(Zero, MaxValueMinusOne, mode), BinaryIntegerHelper.Divide(Zero, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(One, MaxValueMinusOne, mode), BinaryIntegerHelper.Divide(One, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(Two, MaxValueMinusOne, mode), BinaryIntegerHelper.Divide(Two, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(Int128MaxValueMinusOne, MaxValueMinusOne, mode), BinaryIntegerHelper.Divide(Int128MaxValueMinusOne, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(Int128MaxValue, MaxValueMinusOne, mode), BinaryIntegerHelper.Divide(Int128MaxValue, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(Int128MaxValuePlusOne, MaxValueMinusOne, mode), BinaryIntegerHelper.Divide(Int128MaxValuePlusOne, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(Int128MaxValuePlusTwo, MaxValueMinusOne, mode), BinaryIntegerHelper.Divide(Int128MaxValuePlusTwo, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(MaxValueMinusOne, MaxValueMinusOne, mode), BinaryIntegerHelper.Divide(MaxValueMinusOne, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(MaxValue, MaxValueMinusOne, mode), BinaryIntegerHelper.Divide(MaxValue, MaxValueMinusOne, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Divide(Zero, Zero, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide(One, Zero, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide(MaxValue, Zero, mode)); + } + } + + [Fact] + public static void RemainderModeTest() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.RemainderExpected(Zero, Two, mode), BinaryIntegerHelper.Remainder(Zero, Two, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(One, Two, mode), BinaryIntegerHelper.Remainder(One, Two, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(Two, Two, mode), BinaryIntegerHelper.Remainder(Two, Two, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(Int128MaxValueMinusOne, Two, mode), BinaryIntegerHelper.Remainder(Int128MaxValueMinusOne, Two, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(Int128MaxValue, Two, mode), BinaryIntegerHelper.Remainder(Int128MaxValue, Two, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(Int128MaxValuePlusOne, Two, mode), BinaryIntegerHelper.Remainder(Int128MaxValuePlusOne, Two, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(Int128MaxValuePlusTwo, Two, mode), BinaryIntegerHelper.Remainder(Int128MaxValuePlusTwo, Two, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(MaxValueMinusOne, Two, mode), BinaryIntegerHelper.Remainder(MaxValueMinusOne, Two, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(MaxValue, Two, mode), BinaryIntegerHelper.Remainder(MaxValue, Two, mode)); + + Assert.Equal(BinaryIntegerHelper.RemainderExpected(Zero, MaxValueMinusOne, mode), BinaryIntegerHelper.Remainder(Zero, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(One, MaxValueMinusOne, mode), BinaryIntegerHelper.Remainder(One, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(Two, MaxValueMinusOne, mode), BinaryIntegerHelper.Remainder(Two, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(Int128MaxValueMinusOne, MaxValueMinusOne, mode), BinaryIntegerHelper.Remainder(Int128MaxValueMinusOne, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(Int128MaxValue, MaxValueMinusOne, mode), BinaryIntegerHelper.Remainder(Int128MaxValue, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(Int128MaxValuePlusOne, MaxValueMinusOne, mode), BinaryIntegerHelper.Remainder(Int128MaxValuePlusOne, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(Int128MaxValuePlusTwo, MaxValueMinusOne, mode), BinaryIntegerHelper.Remainder(Int128MaxValuePlusTwo, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(MaxValueMinusOne, MaxValueMinusOne, mode), BinaryIntegerHelper.Remainder(MaxValueMinusOne, MaxValueMinusOne, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(MaxValue, MaxValueMinusOne, mode), BinaryIntegerHelper.Remainder(MaxValue, MaxValueMinusOne, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Remainder(Zero, Zero, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder(One, Zero, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder(MaxValue, Zero, mode)); + } } [Fact] diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt16Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt16Tests.GenericMath.cs index 7aa8954dbc193..34d03a2fcc79b 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt16Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt16Tests.GenericMath.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Globalization; +using System.Numerics; using System.Runtime.InteropServices; using Xunit; @@ -51,11 +52,122 @@ public static void AdditiveIdentityTest() [Fact] public static void DivRemTest() { - Assert.Equal(((ushort)0x0000, (ushort)0x0000), BinaryIntegerHelper.DivRem((ushort)0x0000, (ushort)2)); - Assert.Equal(((ushort)0x0000, (ushort)0x0001), BinaryIntegerHelper.DivRem((ushort)0x0001, (ushort)2)); - Assert.Equal(((ushort)0x3FFF, (ushort)0x0001), BinaryIntegerHelper.DivRem((ushort)0x7FFF, (ushort)2)); - Assert.Equal(((ushort)0x4000, (ushort)0x0000), BinaryIntegerHelper.DivRem((ushort)0x8000, (ushort)2)); - Assert.Equal(((ushort)0x7FFF, (ushort)0x0001), BinaryIntegerHelper.DivRem((ushort)0xFFFF, (ushort)2)); + Assert.Equal(((ushort)0x0000, (ushort)0x0000), BinaryIntegerHelper.DivRem(0x0000, 0x0002)); + Assert.Equal(((ushort)0x0000, (ushort)0x0001), BinaryIntegerHelper.DivRem(0x0001, 0x0002)); + Assert.Equal(((ushort)0x0001, (ushort)0x0000), BinaryIntegerHelper.DivRem(0x0002, 0x0002)); + Assert.Equal(((ushort)0x3FFF, (ushort)0x0000), BinaryIntegerHelper.DivRem(0x7FFE, 0x0002)); + Assert.Equal(((ushort)0x3FFF, (ushort)0x0001), BinaryIntegerHelper.DivRem(0x7FFF, 0x0002)); + Assert.Equal(((ushort)0x4000, (ushort)0x0000), BinaryIntegerHelper.DivRem(0x8000, 0x0002)); + Assert.Equal(((ushort)0x4000, (ushort)0x0001), BinaryIntegerHelper.DivRem(0x8001, 0x0002)); + Assert.Equal(((ushort)0x7FFF, (ushort)0x0000), BinaryIntegerHelper.DivRem(0xFFFE, 0x0002)); + Assert.Equal(((ushort)0x7FFF, (ushort)0x0001), BinaryIntegerHelper.DivRem(0xFFFF, 0x0002)); + + Assert.Equal(((ushort)0x0000, (ushort)0x0000), BinaryIntegerHelper.DivRem(0x0000, 0xFFFE)); + Assert.Equal(((ushort)0x0000, (ushort)0x0001), BinaryIntegerHelper.DivRem(0x0001, 0xFFFE)); + Assert.Equal(((ushort)0x0000, (ushort)0x0002), BinaryIntegerHelper.DivRem(0x0002, 0xFFFE)); + Assert.Equal(((ushort)0x0000, (ushort)0x7FFE), BinaryIntegerHelper.DivRem(0x7FFE, 0xFFFE)); + Assert.Equal(((ushort)0x0000, (ushort)0x7FFF), BinaryIntegerHelper.DivRem(0x7FFF, 0xFFFE)); + Assert.Equal(((ushort)0x0000, (ushort)0x8000), BinaryIntegerHelper.DivRem(0x8000, 0xFFFE)); + Assert.Equal(((ushort)0x0000, (ushort)0x8001), BinaryIntegerHelper.DivRem(0x8001, 0xFFFE)); + Assert.Equal(((ushort)0x0001, (ushort)0x0000), BinaryIntegerHelper.DivRem(0xFFFE, 0xFFFE)); + Assert.Equal(((ushort)0x0001, (ushort)0x0001), BinaryIntegerHelper.DivRem(0xFFFF, 0xFFFE)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem(0x0000, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(0x0001, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(0xFFFF, 0)); + } + + [Fact] + public static void DivRemModeTest() + { + foreach (var mode in (DivisionRounding[]) Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x0000, 0x0002, mode), BinaryIntegerHelper.DivRem(0x0000, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x0001, 0x0002, mode), BinaryIntegerHelper.DivRem(0x0001, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x0002, 0x0002, mode), BinaryIntegerHelper.DivRem(0x0002, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x7FFE, 0x0002, mode), BinaryIntegerHelper.DivRem(0x7FFE, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x7FFF, 0x0002, mode), BinaryIntegerHelper.DivRem(0x7FFF, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x8000, 0x0002, mode), BinaryIntegerHelper.DivRem(0x8000, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x8001, 0x0002, mode), BinaryIntegerHelper.DivRem(0x8001, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0xFFFE, 0x0002, mode), BinaryIntegerHelper.DivRem(0xFFFE, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0xFFFF, 0x0002, mode), BinaryIntegerHelper.DivRem(0xFFFF, 0x0002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x0000, 0xFFFE, mode), BinaryIntegerHelper.DivRem(0x0000, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x0001, 0xFFFE, mode), BinaryIntegerHelper.DivRem(0x0001, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x0002, 0xFFFE, mode), BinaryIntegerHelper.DivRem(0x0002, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x7FFE, 0xFFFE, mode), BinaryIntegerHelper.DivRem(0x7FFE, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x7FFF, 0xFFFE, mode), BinaryIntegerHelper.DivRem(0x7FFF, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x8000, 0xFFFE, mode), BinaryIntegerHelper.DivRem(0x8000, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x8001, 0xFFFE, mode), BinaryIntegerHelper.DivRem(0x8001, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0xFFFE, 0xFFFE, mode), BinaryIntegerHelper.DivRem(0xFFFE, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0xFFFF, 0xFFFE, mode), BinaryIntegerHelper.DivRem(0xFFFF, 0xFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem(0x0000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(0x0001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(0xFFFF, 0, mode)); + } + } + + [Fact] + public static void DivideModeTest() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x0000, 0x0002, mode), BinaryIntegerHelper.Divide(0x0000, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x0001, 0x0002, mode), BinaryIntegerHelper.Divide(0x0001, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x0002, 0x0002, mode), BinaryIntegerHelper.Divide(0x0002, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x7FFE, 0x0002, mode), BinaryIntegerHelper.Divide(0x7FFE, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x7FFF, 0x0002, mode), BinaryIntegerHelper.Divide(0x7FFF, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x8000, 0x0002, mode), BinaryIntegerHelper.Divide(0x8000, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x8001, 0x0002, mode), BinaryIntegerHelper.Divide(0x8001, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0xFFFE, 0x0002, mode), BinaryIntegerHelper.Divide(0xFFFE, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0xFFFF, 0x0002, mode), BinaryIntegerHelper.Divide(0xFFFF, 0x0002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x0000, 0xFFFE, mode), BinaryIntegerHelper.Divide(0x0000, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x0001, 0xFFFE, mode), BinaryIntegerHelper.Divide(0x0001, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x0002, 0xFFFE, mode), BinaryIntegerHelper.Divide(0x0002, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x7FFE, 0xFFFE, mode), BinaryIntegerHelper.Divide(0x7FFE, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x7FFF, 0xFFFE, mode), BinaryIntegerHelper.Divide(0x7FFF, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x8000, 0xFFFE, mode), BinaryIntegerHelper.Divide(0x8000, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x8001, 0xFFFE, mode), BinaryIntegerHelper.Divide(0x8001, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0xFFFE, 0xFFFE, mode), BinaryIntegerHelper.Divide(0xFFFE, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0xFFFF, 0xFFFE, mode), BinaryIntegerHelper.Divide(0xFFFF, 0xFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Divide(0x0000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide(0x0001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide(0xFFFF, 0, mode)); + } + } + + [Fact] + public static void RemainderModeTest() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x0000, 0x0002, mode), BinaryIntegerHelper.Remainder(0x0000, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x0001, 0x0002, mode), BinaryIntegerHelper.Remainder(0x0001, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x0002, 0x0002, mode), BinaryIntegerHelper.Remainder(0x0002, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x7FFE, 0x0002, mode), BinaryIntegerHelper.Remainder(0x7FFE, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x7FFF, 0x0002, mode), BinaryIntegerHelper.Remainder(0x7FFF, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x8000, 0x0002, mode), BinaryIntegerHelper.Remainder(0x8000, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x8001, 0x0002, mode), BinaryIntegerHelper.Remainder(0x8001, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0xFFFE, 0x0002, mode), BinaryIntegerHelper.Remainder(0xFFFE, 0x0002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0xFFFF, 0x0002, mode), BinaryIntegerHelper.Remainder(0xFFFF, 0x0002, mode)); + + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x0000, 0xFFFE, mode), BinaryIntegerHelper.Remainder(0x0000, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x0001, 0xFFFE, mode), BinaryIntegerHelper.Remainder(0x0001, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x0002, 0xFFFE, mode), BinaryIntegerHelper.Remainder(0x0002, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x7FFE, 0xFFFE, mode), BinaryIntegerHelper.Remainder(0x7FFE, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x7FFF, 0xFFFE, mode), BinaryIntegerHelper.Remainder(0x7FFF, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x8000, 0xFFFE, mode), BinaryIntegerHelper.Remainder(0x8000, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x8001, 0xFFFE, mode), BinaryIntegerHelper.Remainder(0x8001, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0xFFFE, 0xFFFE, mode), BinaryIntegerHelper.Remainder(0xFFFE, 0xFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0xFFFF, 0xFFFE, mode), BinaryIntegerHelper.Remainder(0xFFFF, 0xFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Remainder(0x0000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder(0x0001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder(0xFFFF, 0, mode)); + } } [Fact] diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt32Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt32Tests.GenericMath.cs index 04de00647e55f..b6ea65ed19898 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt32Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt32Tests.GenericMath.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Globalization; +using System.Numerics; using System.Runtime.InteropServices; using Xunit; @@ -52,11 +53,122 @@ public static void AdditiveIdentityTest() [Fact] public static void DivRemTest() { - Assert.Equal(((uint)0x00000000, (uint)0x00000000), BinaryIntegerHelper.DivRem((uint)0x00000000, 2)); - Assert.Equal(((uint)0x00000000, (uint)0x00000001), BinaryIntegerHelper.DivRem((uint)0x00000001, 2)); - Assert.Equal(((uint)0x3FFFFFFF, (uint)0x00000001), BinaryIntegerHelper.DivRem((uint)0x7FFFFFFF, 2)); - Assert.Equal(((uint)0x40000000, (uint)0x00000000), BinaryIntegerHelper.DivRem((uint)0x80000000, 2)); - Assert.Equal(((uint)0x7FFFFFFF, (uint)0x00000001), BinaryIntegerHelper.DivRem((uint)0xFFFFFFFF, 2)); + Assert.Equal(((uint)0x00000000, (uint)0x00000000), BinaryIntegerHelper.DivRem(0x00000000, 0x00000002)); + Assert.Equal(((uint)0x00000000, (uint)0x00000001), BinaryIntegerHelper.DivRem(0x00000001, 0x00000002)); + Assert.Equal(((uint)0x00000001, (uint)0x00000000), BinaryIntegerHelper.DivRem(0x00000002, 0x00000002)); + Assert.Equal(((uint)0x3FFFFFFF, (uint)0x00000000), BinaryIntegerHelper.DivRem(0x7FFFFFFE, 0x00000002)); + Assert.Equal(((uint)0x3FFFFFFF, (uint)0x00000001), BinaryIntegerHelper.DivRem(0x7FFFFFFF, 0x00000002)); + Assert.Equal(((uint)0x40000000, (uint)0x00000000), BinaryIntegerHelper.DivRem(0x80000000, 0x00000002)); + Assert.Equal(((uint)0x40000000, (uint)0x00000001), BinaryIntegerHelper.DivRem(0x80000001, 0x00000002)); + Assert.Equal(((uint)0x7FFFFFFF, (uint)0x00000000), BinaryIntegerHelper.DivRem(0xFFFFFFFE, 0x00000002)); + Assert.Equal(((uint)0x7FFFFFFF, (uint)0x00000001), BinaryIntegerHelper.DivRem(0xFFFFFFFF, 0x00000002)); + + Assert.Equal(((uint)0x00000000, (uint)0x00000000), BinaryIntegerHelper.DivRem(0x00000000, 0xFFFFFFFE)); + Assert.Equal(((uint)0x00000000, (uint)0x00000001), BinaryIntegerHelper.DivRem(0x00000001, 0xFFFFFFFE)); + Assert.Equal(((uint)0x00000000, (uint)0x00000002), BinaryIntegerHelper.DivRem(0x00000002, 0xFFFFFFFE)); + Assert.Equal(((uint)0x00000000, (uint)0x7FFFFFFE), BinaryIntegerHelper.DivRem(0x7FFFFFFE, 0xFFFFFFFE)); + Assert.Equal(((uint)0x00000000, (uint)0x7FFFFFFF), BinaryIntegerHelper.DivRem(0x7FFFFFFF, 0xFFFFFFFE)); + Assert.Equal(((uint)0x00000000, (uint)0x80000000), BinaryIntegerHelper.DivRem(0x80000000, 0xFFFFFFFE)); + Assert.Equal(((uint)0x00000000, (uint)0x80000001), BinaryIntegerHelper.DivRem(0x80000001, 0xFFFFFFFE)); + Assert.Equal(((uint)0x00000001, (uint)0x00000000), BinaryIntegerHelper.DivRem(0xFFFFFFFE, 0xFFFFFFFE)); + Assert.Equal(((uint)0x00000001, (uint)0x00000001), BinaryIntegerHelper.DivRem(0xFFFFFFFF, 0xFFFFFFFE)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem(0x00000000, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(0x00000001, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(0xFFFFFFFF, 0)); + } + + [Fact] + public static void DivRemModeTest() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x00000000, 0x00000002, mode), BinaryIntegerHelper.DivRem(0x00000000, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x00000001, 0x00000002, mode), BinaryIntegerHelper.DivRem(0x00000001, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x00000002, 0x00000002, mode), BinaryIntegerHelper.DivRem(0x00000002, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x7FFFFFFE, 0x00000002, mode), BinaryIntegerHelper.DivRem(0x7FFFFFFE, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x7FFFFFFF, 0x00000002, mode), BinaryIntegerHelper.DivRem(0x7FFFFFFF, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x80000000, 0x00000002, mode), BinaryIntegerHelper.DivRem(0x80000000, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x80000001, 0x00000002, mode), BinaryIntegerHelper.DivRem(0x80000001, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0xFFFFFFFE, 0x00000002, mode), BinaryIntegerHelper.DivRem(0xFFFFFFFE, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0xFFFFFFFF, 0x00000002, mode), BinaryIntegerHelper.DivRem(0xFFFFFFFF, 0x00000002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x00000000, 0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem(0x00000000, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x00000001, 0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem(0x00000001, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x00000002, 0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem(0x00000002, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x7FFFFFFE, 0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem(0x7FFFFFFE, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x7FFFFFFF, 0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem(0x7FFFFFFF, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x80000000, 0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem(0x80000000, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x80000001, 0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem(0x80000001, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0xFFFFFFFE, 0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem(0xFFFFFFFE, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0xFFFFFFFF, 0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem(0xFFFFFFFF, 0xFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem(0x00000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(0x00000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(0xFFFFFFFF, 0, mode)); + } + } + + [Fact] + public static void DivideModeTest() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x00000000, 0x00000002, mode), BinaryIntegerHelper.Divide(0x00000000, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x00000001, 0x00000002, mode), BinaryIntegerHelper.Divide(0x00000001, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x00000002, 0x00000002, mode), BinaryIntegerHelper.Divide(0x00000002, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x7FFFFFFE, 0x00000002, mode), BinaryIntegerHelper.Divide(0x7FFFFFFE, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x7FFFFFFF, 0x00000002, mode), BinaryIntegerHelper.Divide(0x7FFFFFFF, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x80000000, 0x00000002, mode), BinaryIntegerHelper.Divide(0x80000000, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x80000001, 0x00000002, mode), BinaryIntegerHelper.Divide(0x80000001, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0xFFFFFFFE, 0x00000002, mode), BinaryIntegerHelper.Divide(0xFFFFFFFE, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0xFFFFFFFF, 0x00000002, mode), BinaryIntegerHelper.Divide(0xFFFFFFFF, 0x00000002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x00000000, 0xFFFFFFFE, mode), BinaryIntegerHelper.Divide(0x00000000, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x00000001, 0xFFFFFFFE, mode), BinaryIntegerHelper.Divide(0x00000001, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x00000002, 0xFFFFFFFE, mode), BinaryIntegerHelper.Divide(0x00000002, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x7FFFFFFE, 0xFFFFFFFE, mode), BinaryIntegerHelper.Divide(0x7FFFFFFE, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x7FFFFFFF, 0xFFFFFFFE, mode), BinaryIntegerHelper.Divide(0x7FFFFFFF, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x80000000, 0xFFFFFFFE, mode), BinaryIntegerHelper.Divide(0x80000000, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x80000001, 0xFFFFFFFE, mode), BinaryIntegerHelper.Divide(0x80000001, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0xFFFFFFFE, 0xFFFFFFFE, mode), BinaryIntegerHelper.Divide(0xFFFFFFFE, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0xFFFFFFFF, 0xFFFFFFFE, mode), BinaryIntegerHelper.Divide(0xFFFFFFFF, 0xFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Divide(0x00000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide(0x00000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide(0xFFFFFFFF, 0, mode)); + } + } + + [Fact] + public static void RemainderModeTest() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x00000000, 0x00000002, mode), BinaryIntegerHelper.Remainder(0x00000000, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x00000001, 0x00000002, mode), BinaryIntegerHelper.Remainder(0x00000001, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x00000002, 0x00000002, mode), BinaryIntegerHelper.Remainder(0x00000002, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x7FFFFFFE, 0x00000002, mode), BinaryIntegerHelper.Remainder(0x7FFFFFFE, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x7FFFFFFF, 0x00000002, mode), BinaryIntegerHelper.Remainder(0x7FFFFFFF, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x80000000, 0x00000002, mode), BinaryIntegerHelper.Remainder(0x80000000, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x80000001, 0x00000002, mode), BinaryIntegerHelper.Remainder(0x80000001, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0xFFFFFFFE, 0x00000002, mode), BinaryIntegerHelper.Remainder(0xFFFFFFFE, 0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0xFFFFFFFF, 0x00000002, mode), BinaryIntegerHelper.Remainder(0xFFFFFFFF, 0x00000002, mode)); + + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x00000000, 0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder(0x00000000, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x00000001, 0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder(0x00000001, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x00000002, 0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder(0x00000002, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x7FFFFFFE, 0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder(0x7FFFFFFE, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x7FFFFFFF, 0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder(0x7FFFFFFF, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x80000000, 0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder(0x80000000, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x80000001, 0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder(0x80000001, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0xFFFFFFFE, 0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder(0xFFFFFFFE, 0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0xFFFFFFFF, 0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder(0xFFFFFFFF, 0xFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Remainder(0x00000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder(0x00000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder(0xFFFFFFFF, 0, mode)); + } } [Fact] diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt64Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt64Tests.GenericMath.cs index c2dd6490190d4..87ed615d6ca5e 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt64Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt64Tests.GenericMath.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Globalization; +using System.Numerics; using System.Runtime.InteropServices; using Xunit; @@ -59,11 +60,122 @@ public static void AdditiveIdentityTest() [Fact] public static void DivRemTest() { - Assert.Equal(((ulong)0x0000000000000000, (ulong)0x0000000000000000), BinaryIntegerHelper.DivRem((ulong)0x0000000000000000, 2)); - Assert.Equal(((ulong)0x0000000000000000, (ulong)0x0000000000000001), BinaryIntegerHelper.DivRem((ulong)0x0000000000000001, 2)); - Assert.Equal(((ulong)0x3FFFFFFFFFFFFFFF, (ulong)0x0000000000000001), BinaryIntegerHelper.DivRem((ulong)0x7FFFFFFFFFFFFFFF, 2)); - Assert.Equal(((ulong)0x4000000000000000, (ulong)0x0000000000000000), BinaryIntegerHelper.DivRem((ulong)0x8000000000000000, 2)); - Assert.Equal(((ulong)0x7FFFFFFFFFFFFFFF, (ulong)0x0000000000000001), BinaryIntegerHelper.DivRem((ulong)0xFFFFFFFFFFFFFFFF, 2)); + Assert.Equal(((ulong)0x0000000000000000, (ulong)0x0000000000000000), BinaryIntegerHelper.DivRem(0x0000000000000000, 0x0000000000000002)); + Assert.Equal(((ulong)0x0000000000000000, (ulong)0x0000000000000001), BinaryIntegerHelper.DivRem(0x0000000000000001, 0x0000000000000002)); + Assert.Equal(((ulong)0x0000000000000001, (ulong)0x0000000000000000), BinaryIntegerHelper.DivRem(0x0000000000000002, 0x0000000000000002)); + Assert.Equal(((ulong)0x3FFFFFFFFFFFFFFF, (ulong)0x0000000000000000), BinaryIntegerHelper.DivRem(0x7FFFFFFFFFFFFFFE, 0x0000000000000002)); + Assert.Equal(((ulong)0x3FFFFFFFFFFFFFFF, (ulong)0x0000000000000001), BinaryIntegerHelper.DivRem(0x7FFFFFFFFFFFFFFF, 0x0000000000000002)); + Assert.Equal(((ulong)0x4000000000000000, (ulong)0x0000000000000000), BinaryIntegerHelper.DivRem(0x8000000000000000, 0x0000000000000002)); + Assert.Equal(((ulong)0x4000000000000000, (ulong)0x0000000000000001), BinaryIntegerHelper.DivRem(0x8000000000000001, 0x0000000000000002)); + Assert.Equal(((ulong)0x7FFFFFFFFFFFFFFF, (ulong)0x0000000000000000), BinaryIntegerHelper.DivRem(0xFFFFFFFFFFFFFFFE, 0x0000000000000002)); + Assert.Equal(((ulong)0x7FFFFFFFFFFFFFFF, (ulong)0x0000000000000001), BinaryIntegerHelper.DivRem(0xFFFFFFFFFFFFFFFF, 0x0000000000000002)); + + Assert.Equal(((ulong)0x0000000000000000, (ulong)0x0000000000000000), BinaryIntegerHelper.DivRem(0x0000000000000000, 0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((ulong)0x0000000000000000, (ulong)0x0000000000000001), BinaryIntegerHelper.DivRem(0x0000000000000001, 0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((ulong)0x0000000000000000, (ulong)0x0000000000000002), BinaryIntegerHelper.DivRem(0x0000000000000002, 0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((ulong)0x0000000000000000, (ulong)0x7FFFFFFFFFFFFFFE), BinaryIntegerHelper.DivRem(0x7FFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((ulong)0x0000000000000000, (ulong)0x7FFFFFFFFFFFFFFF), BinaryIntegerHelper.DivRem(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((ulong)0x0000000000000000, (ulong)0x8000000000000000), BinaryIntegerHelper.DivRem(0x8000000000000000, 0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((ulong)0x0000000000000000, (ulong)0x8000000000000001), BinaryIntegerHelper.DivRem(0x8000000000000001, 0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((ulong)0x0000000000000001, (ulong)0x0000000000000000), BinaryIntegerHelper.DivRem(0xFFFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((ulong)0x0000000000000001, (ulong)0x0000000000000001), BinaryIntegerHelper.DivRem(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFE)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem(0x0000000000000000, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(0x0000000000000001, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(0xFFFFFFFFFFFFFFFF, 0)); + } + + [Fact] + public static void DivRemModeTest() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x0000000000000000, 0x0000000000000002, mode), BinaryIntegerHelper.DivRem(0x0000000000000000, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x0000000000000001, 0x0000000000000002, mode), BinaryIntegerHelper.DivRem(0x0000000000000001, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x0000000000000002, 0x0000000000000002, mode), BinaryIntegerHelper.DivRem(0x0000000000000002, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x7FFFFFFFFFFFFFFE, 0x0000000000000002, mode), BinaryIntegerHelper.DivRem(0x7FFFFFFFFFFFFFFE, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x7FFFFFFFFFFFFFFF, 0x0000000000000002, mode), BinaryIntegerHelper.DivRem(0x7FFFFFFFFFFFFFFF, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x8000000000000000, 0x0000000000000002, mode), BinaryIntegerHelper.DivRem(0x8000000000000000, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x8000000000000001, 0x0000000000000002, mode), BinaryIntegerHelper.DivRem(0x8000000000000001, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0xFFFFFFFFFFFFFFFE, 0x0000000000000002, mode), BinaryIntegerHelper.DivRem(0xFFFFFFFFFFFFFFFE, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0xFFFFFFFFFFFFFFFF, 0x0000000000000002, mode), BinaryIntegerHelper.DivRem(0xFFFFFFFFFFFFFFFF, 0x0000000000000002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x0000000000000000, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem(0x0000000000000000, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x0000000000000001, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem(0x0000000000000001, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x0000000000000002, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem(0x0000000000000002, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x7FFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem(0x7FFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x8000000000000000, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem(0x8000000000000000, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0x8000000000000001, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem(0x8000000000000001, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0xFFFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem(0xFFFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem(0x0000000000000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(0x0000000000000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem(0xFFFFFFFFFFFFFFFF, 0, mode)); + } + } + + [Fact] + public static void DivideModeTest() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x0000000000000000, 0x0000000000000002, mode), BinaryIntegerHelper.Divide(0x0000000000000000, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x0000000000000001, 0x0000000000000002, mode), BinaryIntegerHelper.Divide(0x0000000000000001, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x0000000000000002, 0x0000000000000002, mode), BinaryIntegerHelper.Divide(0x0000000000000002, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x7FFFFFFFFFFFFFFE, 0x0000000000000002, mode), BinaryIntegerHelper.Divide(0x7FFFFFFFFFFFFFFE, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x7FFFFFFFFFFFFFFF, 0x0000000000000002, mode), BinaryIntegerHelper.Divide(0x7FFFFFFFFFFFFFFF, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x8000000000000000, 0x0000000000000002, mode), BinaryIntegerHelper.Divide(0x8000000000000000, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x8000000000000001, 0x0000000000000002, mode), BinaryIntegerHelper.Divide(0x8000000000000001, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0xFFFFFFFFFFFFFFFE, 0x0000000000000002, mode), BinaryIntegerHelper.Divide(0xFFFFFFFFFFFFFFFE, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0xFFFFFFFFFFFFFFFF, 0x0000000000000002, mode), BinaryIntegerHelper.Divide(0xFFFFFFFFFFFFFFFF, 0x0000000000000002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x0000000000000000, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide(0x0000000000000000, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x0000000000000001, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide(0x0000000000000001, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x0000000000000002, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide(0x0000000000000002, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x7FFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide(0x7FFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x8000000000000000, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide(0x8000000000000000, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0x8000000000000001, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide(0x8000000000000001, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0xFFFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide(0xFFFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Divide(0x0000000000000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide(0x0000000000000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide(0xFFFFFFFFFFFFFFFF, 0, mode)); + } + } + + [Fact] + public static void RemainderModeTest() + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x0000000000000000, 0x0000000000000002, mode), BinaryIntegerHelper.Remainder(0x0000000000000000, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x0000000000000001, 0x0000000000000002, mode), BinaryIntegerHelper.Remainder(0x0000000000000001, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x0000000000000002, 0x0000000000000002, mode), BinaryIntegerHelper.Remainder(0x0000000000000002, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x7FFFFFFFFFFFFFFE, 0x0000000000000002, mode), BinaryIntegerHelper.Remainder(0x7FFFFFFFFFFFFFFE, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x7FFFFFFFFFFFFFFF, 0x0000000000000002, mode), BinaryIntegerHelper.Remainder(0x7FFFFFFFFFFFFFFF, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x8000000000000000, 0x0000000000000002, mode), BinaryIntegerHelper.Remainder(0x8000000000000000, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x8000000000000001, 0x0000000000000002, mode), BinaryIntegerHelper.Remainder(0x8000000000000001, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0xFFFFFFFFFFFFFFFE, 0x0000000000000002, mode), BinaryIntegerHelper.Remainder(0xFFFFFFFFFFFFFFFE, 0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0xFFFFFFFFFFFFFFFF, 0x0000000000000002, mode), BinaryIntegerHelper.Remainder(0xFFFFFFFFFFFFFFFF, 0x0000000000000002, mode)); + + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x0000000000000000, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder(0x0000000000000000, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x0000000000000001, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder(0x0000000000000001, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x0000000000000002, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder(0x0000000000000002, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x7FFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder(0x7FFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x8000000000000000, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder(0x8000000000000000, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0x8000000000000001, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder(0x8000000000000001, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0xFFFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder(0xFFFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Remainder(0x0000000000000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder(0x0000000000000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder(0xFFFFFFFFFFFFFFFF, 0, mode)); + } } [Fact] diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UIntPtrTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UIntPtrTests.GenericMath.cs index 02d4365c107f8..b67413444cbf8 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UIntPtrTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UIntPtrTests.GenericMath.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Globalization; +using System.Numerics; using System.Runtime.InteropServices; using Xunit; @@ -76,19 +77,256 @@ public static void DivRemTest() { if (Environment.Is64BitProcess) { - Assert.Equal((unchecked((nuint)0x0000000000000000), unchecked((nuint)0x0000000000000000)), BinaryIntegerHelper.DivRem(unchecked((nuint)0x0000000000000000), (nuint)2)); - Assert.Equal((unchecked((nuint)0x0000000000000000), unchecked((nuint)0x0000000000000001)), BinaryIntegerHelper.DivRem(unchecked((nuint)0x0000000000000001), (nuint)2)); - Assert.Equal((unchecked((nuint)0x3FFFFFFFFFFFFFFF), unchecked((nuint)0x0000000000000001)), BinaryIntegerHelper.DivRem(unchecked((nuint)0x7FFFFFFFFFFFFFFF), (nuint)2)); - Assert.Equal((unchecked((nuint)0x4000000000000000), unchecked((nuint)0x0000000000000000)), BinaryIntegerHelper.DivRem(unchecked((nuint)0x8000000000000000), (nuint)2)); - Assert.Equal((unchecked((nuint)0x7FFFFFFFFFFFFFFF), unchecked((nuint)0x0000000000000001)), BinaryIntegerHelper.DivRem(unchecked((nuint)0xFFFFFFFFFFFFFFFF), (nuint)2)); - } - else - { - Assert.Equal(((nuint)0x00000000, (nuint)0x00000000), BinaryIntegerHelper.DivRem((nuint)0x00000000, (nuint)2)); - Assert.Equal(((nuint)0x00000000, (nuint)0x00000001), BinaryIntegerHelper.DivRem((nuint)0x00000001, (nuint)2)); - Assert.Equal(((nuint)0x3FFFFFFF, (nuint)0x00000001), BinaryIntegerHelper.DivRem((nuint)0x7FFFFFFF, (nuint)2)); - Assert.Equal(((nuint)0x40000000, (nuint)0x00000000), BinaryIntegerHelper.DivRem((nuint)0x80000000, (nuint)2)); - Assert.Equal(((nuint)0x7FFFFFFF, (nuint)0x00000001), BinaryIntegerHelper.DivRem((nuint)0xFFFFFFFF, (nuint)2)); + unchecked + { + Assert.Equal(((nuint)0x0000000000000000, (nuint)0x0000000000000000), BinaryIntegerHelper.DivRem((nuint)0x0000000000000000, (nuint)0x0000000000000002)); + Assert.Equal(((nuint)0x0000000000000000, (nuint)0x0000000000000001), BinaryIntegerHelper.DivRem((nuint)0x0000000000000001, (nuint)0x0000000000000002)); + Assert.Equal(((nuint)0x0000000000000001, (nuint)0x0000000000000000), BinaryIntegerHelper.DivRem((nuint)0x0000000000000002, (nuint)0x0000000000000002)); + Assert.Equal(((nuint)0x3FFFFFFFFFFFFFFF, (nuint)0x0000000000000000), BinaryIntegerHelper.DivRem((nuint)0x7FFFFFFFFFFFFFFE, (nuint)0x0000000000000002)); + Assert.Equal(((nuint)0x3FFFFFFFFFFFFFFF, (nuint)0x0000000000000001), BinaryIntegerHelper.DivRem((nuint)0x7FFFFFFFFFFFFFFF, (nuint)0x0000000000000002)); + Assert.Equal(((nuint)0x4000000000000000, (nuint)0x0000000000000000), BinaryIntegerHelper.DivRem((nuint)0x8000000000000000, (nuint)0x0000000000000002)); + Assert.Equal(((nuint)0x4000000000000000, (nuint)0x0000000000000001), BinaryIntegerHelper.DivRem((nuint)0x8000000000000001, (nuint)0x0000000000000002)); + Assert.Equal(((nuint)0x7FFFFFFFFFFFFFFF, (nuint)0x0000000000000000), BinaryIntegerHelper.DivRem((nuint)0xFFFFFFFFFFFFFFFE, (nuint)0x0000000000000002)); + Assert.Equal(((nuint)0x7FFFFFFFFFFFFFFF, (nuint)0x0000000000000001), BinaryIntegerHelper.DivRem((nuint)0xFFFFFFFFFFFFFFFF, (nuint)0x0000000000000002)); + + Assert.Equal(((nuint)0x0000000000000000, (nuint)0x0000000000000000), BinaryIntegerHelper.DivRem((nuint)0x0000000000000000, (nuint)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((nuint)0x0000000000000000, (nuint)0x0000000000000001), BinaryIntegerHelper.DivRem((nuint)0x0000000000000001, (nuint)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((nuint)0x0000000000000000, (nuint)0x0000000000000002), BinaryIntegerHelper.DivRem((nuint)0x0000000000000002, (nuint)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((nuint)0x0000000000000000, (nuint)0x7FFFFFFFFFFFFFFE), BinaryIntegerHelper.DivRem((nuint)0x7FFFFFFFFFFFFFFE, (nuint)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((nuint)0x0000000000000000, (nuint)0x7FFFFFFFFFFFFFFF), BinaryIntegerHelper.DivRem((nuint)0x7FFFFFFFFFFFFFFF, (nuint)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((nuint)0x0000000000000000, (nuint)0x8000000000000000), BinaryIntegerHelper.DivRem((nuint)0x8000000000000000, (nuint)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((nuint)0x0000000000000000, (nuint)0x8000000000000001), BinaryIntegerHelper.DivRem((nuint)0x8000000000000001, (nuint)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((nuint)0x0000000000000001, (nuint)0x0000000000000000), BinaryIntegerHelper.DivRem((nuint)0xFFFFFFFFFFFFFFFE, (nuint)0xFFFFFFFFFFFFFFFE)); + Assert.Equal(((nuint)0x0000000000000001, (nuint)0x0000000000000001), BinaryIntegerHelper.DivRem((nuint)0xFFFFFFFFFFFFFFFF, (nuint)0xFFFFFFFFFFFFFFFE)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem((nuint)0x0000000000000000, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((nuint)0x0000000000000001, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((nuint)0xFFFFFFFFFFFFFFFF, 0)); + } + } + else + { + Assert.Equal(((nuint)0x00000000, (nuint)0x00000000), BinaryIntegerHelper.DivRem((nuint)0x00000000, (nuint)0x00000002)); + Assert.Equal(((nuint)0x00000000, (nuint)0x00000001), BinaryIntegerHelper.DivRem((nuint)0x00000001, (nuint)0x00000002)); + Assert.Equal(((nuint)0x00000001, (nuint)0x00000000), BinaryIntegerHelper.DivRem((nuint)0x00000002, (nuint)0x00000002)); + Assert.Equal(((nuint)0x3FFFFFFF, (nuint)0x00000000), BinaryIntegerHelper.DivRem((nuint)0x7FFFFFFE, (nuint)0x00000002)); + Assert.Equal(((nuint)0x3FFFFFFF, (nuint)0x00000001), BinaryIntegerHelper.DivRem((nuint)0x7FFFFFFF, (nuint)0x00000002)); + Assert.Equal(((nuint)0x40000000, (nuint)0x00000000), BinaryIntegerHelper.DivRem((nuint)0x80000000, (nuint)0x00000002)); + Assert.Equal(((nuint)0x40000000, (nuint)0x00000001), BinaryIntegerHelper.DivRem((nuint)0x80000001, (nuint)0x00000002)); + Assert.Equal(((nuint)0x7FFFFFFF, (nuint)0x00000000), BinaryIntegerHelper.DivRem((nuint)0xFFFFFFFE, (nuint)0x00000002)); + Assert.Equal(((nuint)0x7FFFFFFF, (nuint)0x00000001), BinaryIntegerHelper.DivRem((nuint)0xFFFFFFFF, (nuint)0x00000002)); + + Assert.Equal(((nuint)0x00000000, (nuint)0x00000000), BinaryIntegerHelper.DivRem((nuint)0x00000000, (nuint)0xFFFFFFFE)); + Assert.Equal(((nuint)0x00000000, (nuint)0x00000001), BinaryIntegerHelper.DivRem((nuint)0x00000001, (nuint)0xFFFFFFFE)); + Assert.Equal(((nuint)0x00000000, (nuint)0x00000002), BinaryIntegerHelper.DivRem((nuint)0x00000002, (nuint)0xFFFFFFFE)); + Assert.Equal(((nuint)0x00000000, (nuint)0x7FFFFFFE), BinaryIntegerHelper.DivRem((nuint)0x7FFFFFFE, (nuint)0xFFFFFFFE)); + Assert.Equal(((nuint)0x00000000, (nuint)0x7FFFFFFF), BinaryIntegerHelper.DivRem((nuint)0x7FFFFFFF, (nuint)0xFFFFFFFE)); + Assert.Equal(((nuint)0x00000000, (nuint)0x80000000), BinaryIntegerHelper.DivRem((nuint)0x80000000, (nuint)0xFFFFFFFE)); + Assert.Equal(((nuint)0x00000000, (nuint)0x80000001), BinaryIntegerHelper.DivRem((nuint)0x80000001, (nuint)0xFFFFFFFE)); + Assert.Equal(((nuint)0x00000001, (nuint)0x00000000), BinaryIntegerHelper.DivRem((nuint)0xFFFFFFFE, (nuint)0xFFFFFFFE)); + Assert.Equal(((nuint)0x00000001, (nuint)0x00000001), BinaryIntegerHelper.DivRem((nuint)0xFFFFFFFF, (nuint)0xFFFFFFFE)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem((nuint)0x00000000, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((nuint)0x00000001, 0)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((nuint)0xFFFFFFFF, 0)); + } + } + + [Fact] + public static void DivRemModeTest() + { + if (Environment.Is64BitProcess) + { + unchecked + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x0000000000000000, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((nuint)0x0000000000000000, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x0000000000000001, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((nuint)0x0000000000000001, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x0000000000000002, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((nuint)0x0000000000000002, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x7FFFFFFFFFFFFFFE, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((nuint)0x7FFFFFFFFFFFFFFE, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x7FFFFFFFFFFFFFFF, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((nuint)0x7FFFFFFFFFFFFFFF, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x8000000000000000, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((nuint)0x8000000000000000, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x8000000000000001, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((nuint)0x8000000000000001, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0xFFFFFFFFFFFFFFFE, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((nuint)0xFFFFFFFFFFFFFFFE, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0xFFFFFFFFFFFFFFFF, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.DivRem((nuint)0xFFFFFFFFFFFFFFFF, (nuint)0x0000000000000002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x0000000000000000, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nuint)0x0000000000000000, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x0000000000000001, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nuint)0x0000000000000001, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x0000000000000002, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nuint)0x0000000000000002, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x7FFFFFFFFFFFFFFE, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nuint)0x7FFFFFFFFFFFFFFE, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x7FFFFFFFFFFFFFFF, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nuint)0x7FFFFFFFFFFFFFFF, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x8000000000000000, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nuint)0x8000000000000000, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x8000000000000001, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nuint)0x8000000000000001, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0xFFFFFFFFFFFFFFFE, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nuint)0xFFFFFFFFFFFFFFFE, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0xFFFFFFFFFFFFFFFF, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nuint)0xFFFFFFFFFFFFFFFF, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem((nuint)0x0000000000000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((nuint)0x0000000000000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((nuint)0xFFFFFFFFFFFFFFFF, 0, mode)); + } + } + } + else + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x00000000, (nuint)0x00000002, mode), BinaryIntegerHelper.DivRem((nuint)0x00000000, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x00000001, (nuint)0x00000002, mode), BinaryIntegerHelper.DivRem((nuint)0x00000001, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x00000002, (nuint)0x00000002, mode), BinaryIntegerHelper.DivRem((nuint)0x00000002, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x7FFFFFFE, (nuint)0x00000002, mode), BinaryIntegerHelper.DivRem((nuint)0x7FFFFFFE, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x7FFFFFFF, (nuint)0x00000002, mode), BinaryIntegerHelper.DivRem((nuint)0x7FFFFFFF, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x80000000, (nuint)0x00000002, mode), BinaryIntegerHelper.DivRem((nuint)0x80000000, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x80000001, (nuint)0x00000002, mode), BinaryIntegerHelper.DivRem((nuint)0x80000001, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0xFFFFFFFE, (nuint)0x00000002, mode), BinaryIntegerHelper.DivRem((nuint)0xFFFFFFFE, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0xFFFFFFFF, (nuint)0x00000002, mode), BinaryIntegerHelper.DivRem((nuint)0xFFFFFFFF, (nuint)0x00000002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x00000000, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nuint)0x00000000, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x00000001, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nuint)0x00000001, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x00000002, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nuint)0x00000002, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x7FFFFFFE, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nuint)0x7FFFFFFE, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x7FFFFFFF, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nuint)0x7FFFFFFF, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x80000000, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nuint)0x80000000, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0x80000001, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nuint)0x80000001, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0xFFFFFFFE, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nuint)0xFFFFFFFE, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivRemExpected((nuint)0xFFFFFFFF, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.DivRem((nuint)0xFFFFFFFF, (nuint)0xFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.DivRem((nuint)0x00000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((nuint)0x00000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.DivRem((nuint)0xFFFFFFFF, 0, mode)); + } + } + } + + [Fact] + public static void DivideModeTest() + { + if (Environment.Is64BitProcess) + { + unchecked + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x0000000000000000, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.Divide((nuint)0x0000000000000000, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x0000000000000001, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.Divide((nuint)0x0000000000000001, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x0000000000000002, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.Divide((nuint)0x0000000000000002, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x7FFFFFFFFFFFFFFE, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.Divide((nuint)0x7FFFFFFFFFFFFFFE, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x7FFFFFFFFFFFFFFF, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.Divide((nuint)0x7FFFFFFFFFFFFFFF, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x8000000000000000, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.Divide((nuint)0x8000000000000000, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x8000000000000001, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.Divide((nuint)0x8000000000000001, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0xFFFFFFFFFFFFFFFE, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.Divide((nuint)0xFFFFFFFFFFFFFFFE, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0xFFFFFFFFFFFFFFFF, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.Divide((nuint)0xFFFFFFFFFFFFFFFF, (nuint)0x0000000000000002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x0000000000000000, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((nuint)0x0000000000000000, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x0000000000000001, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((nuint)0x0000000000000001, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x0000000000000002, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((nuint)0x0000000000000002, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x7FFFFFFFFFFFFFFE, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((nuint)0x7FFFFFFFFFFFFFFE, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x7FFFFFFFFFFFFFFF, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((nuint)0x7FFFFFFFFFFFFFFF, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x8000000000000000, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((nuint)0x8000000000000000, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x8000000000000001, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((nuint)0x8000000000000001, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0xFFFFFFFFFFFFFFFE, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((nuint)0xFFFFFFFFFFFFFFFE, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0xFFFFFFFFFFFFFFFF, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Divide((nuint)0xFFFFFFFFFFFFFFFF, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Divide((nuint)0x0000000000000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide((nuint)0x0000000000000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide((nuint)0xFFFFFFFFFFFFFFFF, 0, mode)); + } + } + } + else + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x00000000, (nuint)0x00000002, mode), BinaryIntegerHelper.Divide((nuint)0x00000000, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x00000001, (nuint)0x00000002, mode), BinaryIntegerHelper.Divide((nuint)0x00000001, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x00000002, (nuint)0x00000002, mode), BinaryIntegerHelper.Divide((nuint)0x00000002, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x7FFFFFFE, (nuint)0x00000002, mode), BinaryIntegerHelper.Divide((nuint)0x7FFFFFFE, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x7FFFFFFF, (nuint)0x00000002, mode), BinaryIntegerHelper.Divide((nuint)0x7FFFFFFF, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x80000000, (nuint)0x00000002, mode), BinaryIntegerHelper.Divide((nuint)0x80000000, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x80000001, (nuint)0x00000002, mode), BinaryIntegerHelper.Divide((nuint)0x80000001, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0xFFFFFFFE, (nuint)0x00000002, mode), BinaryIntegerHelper.Divide((nuint)0xFFFFFFFE, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0xFFFFFFFF, (nuint)0x00000002, mode), BinaryIntegerHelper.Divide((nuint)0xFFFFFFFF, (nuint)0x00000002, mode)); + + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x00000000, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((nuint)0x00000000, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x00000001, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((nuint)0x00000001, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x00000002, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((nuint)0x00000002, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x7FFFFFFE, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((nuint)0x7FFFFFFE, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x7FFFFFFF, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((nuint)0x7FFFFFFF, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x80000000, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((nuint)0x80000000, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0x80000001, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((nuint)0x80000001, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0xFFFFFFFE, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((nuint)0xFFFFFFFE, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.DivideExpected((nuint)0xFFFFFFFF, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.Divide((nuint)0xFFFFFFFF, (nuint)0xFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Divide((nuint)0x00000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide((nuint)0x00000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Divide((nuint)0xFFFFFFFF, 0, mode)); + } + } + } + + [Fact] + public static void RemainderModeTest() + { + if (Environment.Is64BitProcess) + { + unchecked + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x0000000000000000, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((nuint)0x0000000000000000, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x0000000000000001, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((nuint)0x0000000000000001, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x0000000000000002, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((nuint)0x0000000000000002, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x7FFFFFFFFFFFFFFE, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((nuint)0x7FFFFFFFFFFFFFFE, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x7FFFFFFFFFFFFFFF, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((nuint)0x7FFFFFFFFFFFFFFF, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x8000000000000000, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((nuint)0x8000000000000000, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x8000000000000001, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((nuint)0x8000000000000001, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0xFFFFFFFFFFFFFFFE, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((nuint)0xFFFFFFFFFFFFFFFE, (nuint)0x0000000000000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0xFFFFFFFFFFFFFFFF, (nuint)0x0000000000000002, mode), BinaryIntegerHelper.Remainder((nuint)0xFFFFFFFFFFFFFFFF, (nuint)0x0000000000000002, mode)); + + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x0000000000000000, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nuint)0x0000000000000000, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x0000000000000001, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nuint)0x0000000000000001, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x0000000000000002, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nuint)0x0000000000000002, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x7FFFFFFFFFFFFFFE, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nuint)0x7FFFFFFFFFFFFFFE, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x7FFFFFFFFFFFFFFF, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nuint)0x7FFFFFFFFFFFFFFF, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x8000000000000000, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nuint)0x8000000000000000, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x8000000000000001, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nuint)0x8000000000000001, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0xFFFFFFFFFFFFFFFE, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nuint)0xFFFFFFFFFFFFFFFE, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0xFFFFFFFFFFFFFFFF, (nuint)0xFFFFFFFFFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nuint)0xFFFFFFFFFFFFFFFF, (nuint)0xFFFFFFFFFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Remainder((nuint)0x0000000000000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder((nuint)0x0000000000000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder((nuint)0xFFFFFFFFFFFFFFFF, 0, mode)); + } + } + } + else + { + foreach (var mode in (DivisionRounding[])Enum.GetValues(typeof(DivisionRounding))) + { + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x00000000, (nuint)0x00000002, mode), BinaryIntegerHelper.Remainder((nuint)0x00000000, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x00000001, (nuint)0x00000002, mode), BinaryIntegerHelper.Remainder((nuint)0x00000001, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x00000002, (nuint)0x00000002, mode), BinaryIntegerHelper.Remainder((nuint)0x00000002, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x7FFFFFFE, (nuint)0x00000002, mode), BinaryIntegerHelper.Remainder((nuint)0x7FFFFFFE, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x7FFFFFFF, (nuint)0x00000002, mode), BinaryIntegerHelper.Remainder((nuint)0x7FFFFFFF, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x80000000, (nuint)0x00000002, mode), BinaryIntegerHelper.Remainder((nuint)0x80000000, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x80000001, (nuint)0x00000002, mode), BinaryIntegerHelper.Remainder((nuint)0x80000001, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0xFFFFFFFE, (nuint)0x00000002, mode), BinaryIntegerHelper.Remainder((nuint)0xFFFFFFFE, (nuint)0x00000002, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0xFFFFFFFF, (nuint)0x00000002, mode), BinaryIntegerHelper.Remainder((nuint)0xFFFFFFFF, (nuint)0x00000002, mode)); + + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x00000000, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nuint)0x00000000, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x00000001, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nuint)0x00000001, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x00000002, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nuint)0x00000002, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x7FFFFFFE, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nuint)0x7FFFFFFE, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x7FFFFFFF, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nuint)0x7FFFFFFF, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x80000000, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nuint)0x80000000, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0x80000001, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nuint)0x80000001, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0xFFFFFFFE, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nuint)0xFFFFFFFE, (nuint)0xFFFFFFFE, mode)); + Assert.Equal(BinaryIntegerHelper.RemainderExpected((nuint)0xFFFFFFFF, (nuint)0xFFFFFFFE, mode), BinaryIntegerHelper.Remainder((nuint)0xFFFFFFFF, (nuint)0xFFFFFFFE, mode)); + + Assert.Throws(() => BinaryIntegerHelper.Remainder((nuint)0x00000000, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder((nuint)0x00000001, 0, mode)); + Assert.Throws(() => BinaryIntegerHelper.Remainder((nuint)0xFFFFFFFF, 0, mode)); + } } }