private static void VerifyCtorByteArray(byte[] value) { BigInteger bigInteger; byte[] roundTrippedByteArray; bool isZero = MyBigIntImp.IsZero(value); bigInteger = new BigInteger(value); roundTrippedByteArray = bigInteger.ToByteArray(); for (int i = Math.Min(value.Length, roundTrippedByteArray.Length) - 1; 0 <= i; --i) { Assert.True(value[i] == roundTrippedByteArray[i], String.Format("Round Tripped ByteArray at {0}", i)); } if (value.Length < roundTrippedByteArray.Length) { for (int i = value.Length; i < roundTrippedByteArray.Length; ++i) { Assert.True(0 == roundTrippedByteArray[i], String.Format("Round Tripped ByteArray is larger than the original array and byte is non zero at {0}", i)); } } else if (value.Length > roundTrippedByteArray.Length) { for (int i = roundTrippedByteArray.Length; i < value.Length; ++i) { Assert.False((((0 != value[i]) && ((roundTrippedByteArray[roundTrippedByteArray.Length - 1] & 0x80) == 0)) || ((0xFF != value[i]) && ((roundTrippedByteArray[roundTrippedByteArray.Length - 1] & 0x80) != 0))), String.Format("Round Tripped ByteArray is smaller than the original array and byte is non zero at {0}", i)); } } if (value.Length < 8) { byte[] newvalue = new byte[8]; for (int i = 0; i < 8; i++) { if (bigInteger < 0) { newvalue[i] = 0xFF; } else { newvalue[i] = 0; } } for (int i = 0; i < value.Length; i++) { newvalue[i] = value[i]; } value = newvalue; } else if (value.Length > 8) { int newlength = value.Length; for (; newlength > 8; newlength--) { if (bigInteger < 0) { if ((value[newlength - 1] != 0xFF) | ((value[newlength - 2] & 0x80) == 0)) { break; } } else { if ((value[newlength - 1] != 0) | ((value[newlength - 2] & 0x80) != 0)) { break; } } } byte[] newvalue = new byte[newlength]; for (int i = 0; i < newlength; i++) { newvalue[i] = value[i]; } value = newvalue; } if (IsOutOfRangeInt64(value)) { // Try subtracting a value from the BigInteger that will allow it to be represented as an Int64 byte[] tempByteArray; BigInteger tempBigInteger; bool isNeg = ((value[value.Length - 1] & 0x80) != 0); tempByteArray = new byte[value.Length]; Array.Copy(value, 8, tempByteArray, 8, value.Length - 8); tempBigInteger = bigInteger - (new BigInteger(tempByteArray)); tempByteArray = new byte[8]; Array.Copy(value, 0, tempByteArray, 0, 8); if (!(((tempByteArray[7] & 0x80) == 0) ^ isNeg)) { tempByteArray[7] ^= 0x80; tempBigInteger = tempBigInteger + (new BigInteger(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0x80 })); } if (isNeg & (tempBigInteger > 0)) { tempBigInteger = tempBigInteger + (new BigInteger(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0xFF })); } Assert.Equal(BitConverter.ToInt64(tempByteArray, 0), (Int64)tempBigInteger); } else { Assert.Equal(BitConverter.ToInt64(value, 0), (Int64)bigInteger); } if (IsOutOfRangeUInt64(value)) { // Try subtracting a value from the BigInteger that will allow it to be represented as an UInt64 byte[] tempByteArray; BigInteger tempBigInteger; bool isNeg = ((value[value.Length - 1] & 0x80) != 0); tempByteArray = new byte[value.Length]; Array.Copy(value, 8, tempByteArray, 8, value.Length - 8); tempBigInteger = bigInteger - (new BigInteger(tempByteArray)); tempByteArray = new byte[8]; Array.Copy(value, 0, tempByteArray, 0, 8); if ((tempByteArray[7] & 0x80) != 0) { tempByteArray[7] &= 0x7f; if (tempBigInteger < 0) { tempBigInteger = tempBigInteger - (new BigInteger(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0x80 })); } else { tempBigInteger = tempBigInteger + (new BigInteger(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0x80 })); } } Assert.Equal(BitConverter.ToUInt64(tempByteArray, 0), (UInt64)tempBigInteger); } else { Assert.Equal(BitConverter.ToUInt64(value, 0), (UInt64)bigInteger); } VerifyBigIntegerUsingIdentities(bigInteger, isZero); }
private static void RunPositiveTests(Random random) { BigInteger bigInteger1, bigInteger2; int expectedResult; byte[] byteArray; bool isNegative; //1 Inputs from BigInteger Properties // BigInteger.MinusOne, BigInteger.MinusOne VerifyComparison(BigInteger.MinusOne, BigInteger.MinusOne, 0); // BigInteger.MinusOne, BigInteger.Zero VerifyComparison(BigInteger.MinusOne, BigInteger.Zero, -1); // BigInteger.MinusOne, BigInteger.One VerifyComparison(BigInteger.MinusOne, BigInteger.One, -1); // BigInteger.MinusOne, Large Negative VerifyComparison(BigInteger.MinusOne, -1L * ((BigInteger)Int64.MaxValue), 1); // BigInteger.MinusOne, Small Negative VerifyComparison(BigInteger.MinusOne, -1L * ((BigInteger)Int16.MaxValue), 1); // BigInteger.MinusOne, Large Number VerifyComparison(BigInteger.MinusOne, (BigInteger)Int32.MaxValue + 1, -1); // BigInteger.MinusOne, Small Number VerifyComparison(BigInteger.MinusOne, (BigInteger)Int32.MaxValue - 1, -1); // BigInteger.MinusOne, One Less VerifyComparison(BigInteger.MinusOne, BigInteger.MinusOne - 1, 1); // BigInteger.Zero, BigInteger.Zero VerifyComparison(BigInteger.Zero, BigInteger.Zero, 0); // BigInteger.Zero, Large Negative VerifyComparison(BigInteger.Zero, -1L * ((BigInteger)Int32.MaxValue + 1), 1); // BigInteger.Zero, Small Negative VerifyComparison(BigInteger.Zero, -1L * ((BigInteger)Int32.MaxValue - 1), 1); // BigInteger.Zero, Large Number VerifyComparison(BigInteger.Zero, (BigInteger)Int32.MaxValue + 1, -1); // BigInteger.Zero, Small Number VerifyComparison(BigInteger.Zero, (BigInteger)Int32.MaxValue - 1, -1); // BigInteger.One, BigInteger.One VerifyComparison(BigInteger.One, BigInteger.One, 0); // BigInteger.One, BigInteger.MinusOne VerifyComparison(BigInteger.One, BigInteger.MinusOne, 1); // BigInteger.One, BigInteger.Zero VerifyComparison(BigInteger.One, BigInteger.Zero, 1); // BigInteger.One, Large Negative VerifyComparison(BigInteger.One, -1 * ((BigInteger)Int32.MaxValue + 1), 1); // BigInteger.One, Small Negative VerifyComparison(BigInteger.One, -1 * ((BigInteger)Int32.MaxValue - 1), 1); // BigInteger.One, Large Number VerifyComparison(BigInteger.One, (BigInteger)Int32.MaxValue + 1, -1); // BigInteger.One, Small Number VerifyComparison(BigInteger.One, (BigInteger)Int32.MaxValue - 1, -1); //Basic Checks // BigInteger.MinusOne, (Int32) -1 VerifyComparison(BigInteger.MinusOne, (Int32)(-1), 0); // BigInteger.Zero, (Int32) 0 VerifyComparison(BigInteger.Zero, (Int32)(0), 0); // BigInteger.One, 1 VerifyComparison(BigInteger.One, (Int32)(1), 0); //1 Inputs Around the boundary of UInt32 // -1 * UInt32.MaxValue, -1 * UInt32.MaxValue VerifyComparison(-1L * (BigInteger)UInt32.MaxValue - 1, -1L * (BigInteger)UInt32.MaxValue - 1, 0); // -1 * UInt32.MaxValue, -1 * UInt32.MaxValue -1 VerifyComparison(-1L * (BigInteger)UInt32.MaxValue, (-1L * (BigInteger)UInt32.MaxValue) - 1L, 1); // UInt32.MaxValue, -1 * UInt32.MaxValue VerifyComparison((BigInteger)UInt32.MaxValue, -1L * (BigInteger)UInt32.MaxValue, 1); // UInt32.MaxValue, UInt32.MaxValue VerifyComparison((BigInteger)UInt32.MaxValue, (BigInteger)UInt32.MaxValue, 0); // UInt32.MaxValue, UInt32.MaxValue + 1 VerifyComparison((BigInteger)UInt32.MaxValue, (BigInteger)UInt32.MaxValue + 1, -1); // UInt64.MaxValue, UInt64.MaxValue VerifyComparison((BigInteger)UInt64.MaxValue, (BigInteger)UInt64.MaxValue, 0); // UInt64.MaxValue + 1, UInt64.MaxValue VerifyComparison((BigInteger)UInt64.MaxValue + 1, UInt64.MaxValue, 1); //Other cases // -1 * Large Bigint, -1 * Large BigInt VerifyComparison(-1L * ((BigInteger)Int32.MaxValue + 1), -1L * ((BigInteger)Int32.MaxValue + 1), 0); // Large Bigint, Large Negative BigInt VerifyComparison((BigInteger)Int32.MaxValue + 1, -1L * ((BigInteger)Int32.MaxValue + 1), 1); // Large Bigint, UInt32.MaxValue VerifyComparison((BigInteger)Int64.MaxValue + 1, (BigInteger)UInt32.MaxValue, 1); // Large Bigint, One More VerifyComparison((BigInteger)Int32.MaxValue + 1, ((BigInteger)Int32.MaxValue) + 2, -1); // -1 * Small Bigint, -1 * Small BigInt VerifyComparison(-1L * ((BigInteger)Int32.MaxValue - 1), -1L * ((BigInteger)Int32.MaxValue - 1), 0); // Small Bigint, Small Negative BigInt VerifyComparison((BigInteger)Int32.MaxValue - 1, -1L * ((BigInteger)Int32.MaxValue - 1), 1); // Small Bigint, UInt32.MaxValue VerifyComparison((BigInteger)Int32.MaxValue - 1, (BigInteger)UInt32.MaxValue - 1, -1); // Small Bigint, One More VerifyComparison((BigInteger)Int32.MaxValue - 2, ((BigInteger)Int32.MaxValue) - 1, -1); //BigInteger vs. Int32 // One Larger (BigInteger), Int32.MaxValue VerifyComparison((BigInteger)Int32.MaxValue + 1, Int32.MaxValue, 1); // Larger BigInteger, Int32.MaxValue VerifyComparison((BigInteger)UInt64.MaxValue + 1, Int32.MaxValue, 1); // Smaller BigInteger, Int32.MaxValue VerifyComparison((BigInteger)Int16.MinValue - 1, Int32.MaxValue, -1); // One Smaller (BigInteger), Int32.MaxValue VerifyComparison((BigInteger)Int32.MaxValue - 1, Int32.MaxValue, -1); // (BigInteger) Int32.MaxValue, Int32.MaxValue VerifyComparison((BigInteger)Int32.MaxValue, Int32.MaxValue, 0); //BigInteger vs. UInt32 // One Larger (BigInteger), UInt32.MaxValue VerifyComparison((BigInteger)UInt32.MaxValue + 1, UInt32.MaxValue, 1); // Larger BigInteger, UInt32.MaxValue VerifyComparison((BigInteger)Int64.MaxValue + 1, UInt32.MaxValue, 1); // Smaller BigInteger, UInt32.MaxValue VerifyComparison((BigInteger)Int16.MinValue - 1, UInt32.MaxValue, -1); // One Smaller (BigInteger), UInt32.MaxValue VerifyComparison((BigInteger)UInt32.MaxValue - 1, UInt32.MaxValue, -1); // (BigInteger UInt32.MaxValue, UInt32.MaxValue VerifyComparison((BigInteger)UInt32.MaxValue, UInt32.MaxValue, 0); //BigInteger vs. UInt64 // One Larger (BigInteger), UInt64.MaxValue VerifyComparison((BigInteger)UInt64.MaxValue + 1, UInt64.MaxValue, 1); // Larger BigInteger, UInt64.MaxValue VerifyComparison((BigInteger)UInt64.MaxValue + 100, UInt64.MaxValue, 1); // Smaller BigInteger, UInt64.MaxValue VerifyComparison((BigInteger)Int16.MinValue - 1, UInt64.MaxValue, -1); VerifyComparison((BigInteger)Int16.MaxValue - 1, UInt64.MaxValue, -1); VerifyComparison((BigInteger)Int32.MaxValue + 1, UInt64.MaxValue, -1); // One Smaller (BigInteger), UInt64.MaxValue VerifyComparison((BigInteger)UInt64.MaxValue - 1, UInt64.MaxValue, -1); // (BigInteger UInt64.MaxValue, UInt64.MaxValue VerifyComparison((BigInteger)UInt64.MaxValue, UInt64.MaxValue, 0); //BigInteger vs. Int64 // One Smaller (BigInteger), Int64.MaxValue VerifyComparison((BigInteger)Int64.MaxValue - 1, Int64.MaxValue, -1); // Larger BigInteger, Int64.MaxValue VerifyComparison((BigInteger)UInt64.MaxValue + 100, Int64.MaxValue, 1); // Smaller BigInteger, Int32.MaxValue VerifyComparison((BigInteger)Int16.MinValue - 1, Int64.MaxValue, -1); // (BigInteger Int64.MaxValue, Int64.MaxValue VerifyComparison((BigInteger)Int64.MaxValue, Int64.MaxValue, 0); // One Larger (BigInteger), Int64.MaxValue VerifyComparison((BigInteger)Int64.MaxValue + 1, Int64.MaxValue, 1); //1 Random Inputs // Random BigInteger only differs by sign for (int i = 0; i < NumberOfRandomIterations; ++i) { do { byteArray = GetRandomByteArray(random); }while (MyBigIntImp.IsZero(byteArray)); BigInteger b2 = new BigInteger(byteArray); if (b2 > (BigInteger)0) { VerifyComparison(b2, -1L * b2, 1); } else { VerifyComparison(b2, -1L * b2, -1); } } // Random BigInteger, Random BigInteger for (int i = 0; i < NumberOfRandomIterations; ++i) { expectedResult = GetRandomInputForComparison(random, out bigInteger1, out bigInteger2); VerifyComparison(bigInteger1, bigInteger2, expectedResult); } // Random BigInteger for (int i = 0; i < NumberOfRandomIterations; ++i) { byteArray = GetRandomByteArray(random); isNegative = 0 == random.Next(0, 2); VerifyComparison(new BigInteger(byteArray), isNegative, new BigInteger(byteArray), isNegative, 0); } //1 Identical values constructed multiple ways // BigInteger.Zero, BigInteger constructed with a byte[] isNegative=true VerifyComparison(BigInteger.Zero, false, new BigInteger(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }), true, 0); // BigInteger.Zero, BigInteger constructed with a byte[] isNegative=false VerifyComparison(BigInteger.Zero, false, new BigInteger(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }), false, 0); // BigInteger.Zero, BigInteger constructed from an Int64 VerifyComparison(BigInteger.Zero, 0L, 0); // BigInteger.Zero, BigInteger constructed from a Double VerifyComparison(BigInteger.Zero, (BigInteger)0d, 0); // BigInteger.Zero, BigInteger constructed from a Decimal VerifyComparison(BigInteger.Zero, (BigInteger)0, 0); // BigInteger.Zero, BigInteger constructed with Addition byteArray = GetRandomByteArray(random); isNegative = 0 == random.Next(0, 2); VerifyComparison(BigInteger.Zero, false, new BigInteger(byteArray) + (-1 * new BigInteger(byteArray)), isNegative, 0); // BigInteger.Zero, BigInteger constructed with Subtraction byteArray = GetRandomByteArray(random); isNegative = 0 == random.Next(0, 2); VerifyComparison(BigInteger.Zero, false, new BigInteger(byteArray) - new BigInteger(byteArray), isNegative, 0); // BigInteger.Zero, BigInteger constructed with Multiplication byteArray = GetRandomByteArray(random); isNegative = 0 == random.Next(0, 2); VerifyComparison(BigInteger.Zero, false, 0 * new BigInteger(byteArray), isNegative, 0); // BigInteger.Zero, BigInteger constructed with Division do { byteArray = GetRandomByteArray(random); }while (MyBigIntImp.IsZero(byteArray)); isNegative = 0 == random.Next(0, 2); VerifyComparison(BigInteger.Zero, false, 0 / new BigInteger(byteArray), isNegative, 0); // BigInteger.One, BigInteger constructed with a byte[] VerifyComparison(BigInteger.One, false, new BigInteger(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0 }), false, 0); // BigInteger.One, BigInteger constructed from an Int64 VerifyComparison(BigInteger.One, 1L, 0); // BigInteger.One, BigInteger constructed from a Double VerifyComparison(BigInteger.One, (BigInteger)1d, 0); // BigInteger.One, BigInteger constructed from a Decimal VerifyComparison(BigInteger.One, (BigInteger)1, 0); // BigInteger.One, BigInteger constructed with Addition byteArray = GetRandomByteArray(random); isNegative = 0 == random.Next(0, 2); VerifyComparison(BigInteger.One, false, (((BigInteger)(-1)) * new BigInteger(byteArray)) + (new BigInteger(byteArray)) + 1, false, 0); // BigInteger.One, BigInteger constructed with Subtraction byteArray = GetRandomByteArray(random); isNegative = 0 == random.Next(0, 2); BigInteger b = new BigInteger(byteArray); if (b > (BigInteger)0) { VerifyComparison(BigInteger.One, false, (b + 1) - (b), false, 0); } else { b = -1L * b; VerifyComparison(BigInteger.One, false, (b + 1) - (b), false, 0); b = -1L * b; } // BigInteger.One, BigInteger constructed with Multiplication byteArray = GetRandomByteArray(random); isNegative = 0 == random.Next(0, 2); VerifyComparison(BigInteger.One, (BigInteger)1 * (BigInteger)1, 0); // BigInteger.One, BigInteger constructed with Division do { byteArray = GetRandomByteArray(random); }while (MyBigIntImp.IsZero(byteArray)); BigInteger b1 = new BigInteger(byteArray); VerifyComparison(BigInteger.One, false, b1 / b1, false, 0); }