public static void RunUInt32ExplicitCastFromBigIntegerTests()
{
uint value;
BigInteger bigInteger;
// UInt32 Explicit Cast from BigInteger: Random value < UInt32.MinValue
bigInteger = GenerateRandomBigIntegerLessThan(UInt32.MinValue, s_random);
value = BitConverter.ToUInt32(ByteArrayMakeMinSize(bigInteger.ToByteArray(), 4), 0);
Assert.Throws<OverflowException>(() => VerifyUInt32ExplicitCastFromBigInteger(value, bigInteger));
// UInt32 Explicit Cast from BigInteger: UInt32.MinValue - 1
bigInteger = new BigInteger(UInt32.MinValue);
bigInteger -= BigInteger.One;
value = BitConverter.ToUInt32(new byte[] { 0xff, 0xff, 0xff, 0xff }, 0);
Assert.Throws<OverflowException>(() => VerifyUInt32ExplicitCastFromBigInteger(value, bigInteger));
// UInt32 Explicit Cast from BigInteger: UInt32.MinValue
VerifyUInt32ExplicitCastFromBigInteger(UInt32.MinValue);
// UInt32 Explicit Cast from BigInteger: 0
VerifyUInt32ExplicitCastFromBigInteger(0);
// UInt32 Explicit Cast from BigInteger: 1
VerifyUInt32ExplicitCastFromBigInteger(1);
// UInt32 Explicit Cast from BigInteger: Random Positive
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifyUInt32ExplicitCastFromBigInteger((UInt32)(UInt32.MaxValue * s_random.NextDouble()));
}
// UInt32 Explicit Cast from BigInteger: UInt32.MaxValue
VerifyUInt32ExplicitCastFromBigInteger(UInt32.MaxValue);
// UInt32 Explicit Cast from BigInteger: UInt32.MaxValue + 1
bigInteger = new BigInteger(UInt32.MaxValue);
bigInteger += BigInteger.One;
value = BitConverter.ToUInt32(bigInteger.ToByteArray(), 0);
Assert.Throws<OverflowException>(() => VerifyUInt32ExplicitCastFromBigInteger(value, bigInteger));
// UInt32 Explicit Cast from BigInteger: Random value > UInt32.MaxValue
bigInteger = GenerateRandomBigIntegerGreaterThan(UInt32.MaxValue, s_random);
value = BitConverter.ToUInt32(bigInteger.ToByteArray(), 0);
Assert.Throws<OverflowException>(() => VerifyUInt32ExplicitCastFromBigInteger(value, bigInteger));
}