public static void RunUInt16ExplicitCastFromBigIntegerTests()
{
ushort value;
BigInteger bigInteger;
// UInt16 Explicit Cast from BigInteger: Random value < UInt16.MinValue
bigInteger = GenerateRandomBigIntegerLessThan(UInt16.MinValue, s_random);
value = BitConverter.ToUInt16(ByteArrayMakeMinSize(bigInteger.ToByteArray(), 2), 0);
Assert.Throws<OverflowException>(() => VerifyUInt16ExplicitCastFromBigInteger(value, bigInteger));
// UInt16 Explicit Cast from BigInteger: UInt16.MinValue - 1
bigInteger = new BigInteger(UInt16.MinValue);
bigInteger -= BigInteger.One;
value = BitConverter.ToUInt16(new byte[] { 0xff, 0xff }, 0);
Assert.Throws<OverflowException>(() => VerifyUInt16ExplicitCastFromBigInteger(value, bigInteger));
// UInt16 Explicit Cast from BigInteger: UInt16.MinValue
VerifyUInt16ExplicitCastFromBigInteger(UInt16.MinValue);
// UInt16 Explicit Cast from BigInteger: 0
VerifyUInt16ExplicitCastFromBigInteger(0);
// UInt16 Explicit Cast from BigInteger: 1
VerifyUInt16ExplicitCastFromBigInteger(1);
// UInt16 Explicit Cast from BigInteger: Random Positive
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifyUInt16ExplicitCastFromBigInteger((UInt16)s_random.Next(1, UInt16.MaxValue));
}
// UInt16 Explicit Cast from BigInteger: UInt16.MaxValue
VerifyUInt16ExplicitCastFromBigInteger(UInt16.MaxValue);
// UInt16 Explicit Cast from BigInteger: UInt16.MaxValue + 1
bigInteger = new BigInteger(UInt16.MaxValue);
bigInteger += BigInteger.One;
value = BitConverter.ToUInt16(bigInteger.ToByteArray(), 0);
Assert.Throws<OverflowException>(() => VerifyUInt16ExplicitCastFromBigInteger(value, bigInteger));
// UInt16 Explicit Cast from BigInteger: Random value > UInt16.MaxValue
bigInteger = GenerateRandomBigIntegerGreaterThan(UInt16.MaxValue, s_random);
value = BitConverter.ToUInt16(bigInteger.ToByteArray(), 0);
Assert.Throws<OverflowException>(() => VerifyUInt16ExplicitCastFromBigInteger(value, bigInteger));
}