public static void RunInt16ExplicitCastFromBigIntegerTests()
{
short value;
BigInteger bigInteger;
// Int16 Explicit Cast from BigInteger: Random value < Int16.MinValue
bigInteger = GenerateRandomBigIntegerLessThan(Int16.MinValue, s_random);
value = BitConverter.ToInt16(bigInteger.ToByteArray(), 0);
Assert.Throws<OverflowException>(() => VerifyInt16ExplicitCastFromBigInteger(value, bigInteger));
// Int16 Explicit Cast from BigInteger: Int16.MinValue - 1
bigInteger = new BigInteger(Int16.MinValue);
bigInteger -= BigInteger.One;
value = BitConverter.ToInt16(bigInteger.ToByteArray(), 0);
Assert.Throws<OverflowException>(() => VerifyInt16ExplicitCastFromBigInteger(value, bigInteger));
// Int16 Explicit Cast from BigInteger: Int16.MinValue
VerifyInt16ExplicitCastFromBigInteger(Int16.MinValue);
// Int16 Explicit Cast from BigInteger: Random Negative
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifyInt16ExplicitCastFromBigInteger((Int16)s_random.Next(Int16.MinValue, 0));
}
// Int16 Explicit Cast from BigInteger: -1
VerifyInt16ExplicitCastFromBigInteger(-1);
// Int16 Explicit Cast from BigInteger: 0
VerifyInt16ExplicitCastFromBigInteger(0);
// Int16 Explicit Cast from BigInteger: 1
VerifyInt16ExplicitCastFromBigInteger(1);
// Int16 Explicit Cast from BigInteger: Random Positive
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifyInt16ExplicitCastFromBigInteger((Int16)s_random.Next(1, Int16.MaxValue));
}
// Int16 Explicit Cast from BigInteger: Int16.MaxValue
VerifyInt16ExplicitCastFromBigInteger(Int16.MaxValue);
// Int16 Explicit Cast from BigInteger: Int16.MaxValue + 1
bigInteger = new BigInteger(Int16.MaxValue);
bigInteger += BigInteger.One;
value = BitConverter.ToInt16(bigInteger.ToByteArray(), 0);
Assert.Throws<OverflowException>(() => VerifyInt16ExplicitCastFromBigInteger(value, bigInteger));
// Int16 Explicit Cast from BigInteger: Random value > Int16.MaxValue
bigInteger = GenerateRandomBigIntegerGreaterThan((UInt64)Int16.MaxValue, s_random);
value = BitConverter.ToInt16(bigInteger.ToByteArray(), 0);
Assert.Throws<OverflowException>(() => VerifyInt16ExplicitCastFromBigInteger(value, bigInteger));
}