public static void RunDecimalExplicitCastFromBigIntegerTests()
{
int[] bits = new int[3];
uint temp2;
bool carry;
byte[] temp;
Decimal value;
BigInteger bigInteger;
// Decimal Explicit Cast from BigInteger: Random value < Decimal.MinValue
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
bigInteger = GenerateRandomBigIntegerLessThan(Decimal.MinValue, s_random);
temp = bigInteger.ToByteArray();
carry = true;
for (int j = 0; j < 3; j++)
{
temp2 = BitConverter.ToUInt32(temp, 4 * j);
temp2 = ~temp2;
if (carry)
{
carry = false;
temp2 += 1;
if (temp2 == 0)
{
carry = true;
}
}
bits[j] = (int)temp2;
}
value = new Decimal(bits[0], bits[1], bits[2], true, 0);
Assert.Throws<OverflowException>(() => VerifyDecimalExplicitCastFromBigInteger(value, bigInteger));
}
// Decimal Explicit Cast from BigInteger: Decimal.MinValue - 1
bigInteger = new BigInteger(Decimal.MinValue);
bigInteger -= BigInteger.One;
temp = bigInteger.ToByteArray();
carry = true;
for (int j = 0; j < 3; j++)
{
temp2 = BitConverter.ToUInt32(temp, 4 * j);
temp2 = ~temp2;
if (carry)
{
carry = false;
temp2 += 1;
if (temp2 == 0)
{
carry = true;
}
}
bits[j] = (int)temp2;
}
value = new Decimal(bits[0], bits[1], bits[2], true, 0);
Assert.Throws<OverflowException>(() => VerifyDecimalExplicitCastFromBigInteger(value, bigInteger));
// Decimal Explicit Cast from BigInteger: Decimal.MinValue
VerifyDecimalExplicitCastFromBigInteger(Decimal.MinValue);
// Decimal Explicit Cast from BigInteger: Random Negative
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifyDecimalExplicitCastFromBigInteger(((Decimal)((Double)Decimal.MaxValue * s_random.NextDouble())) - Decimal.MaxValue);
}
// Decimal Explicit Cast from BigInteger: Random Negative Non-Integral > -100
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
value = (Decimal)(100 * s_random.NextDouble() - 100);
VerifyDecimalExplicitCastFromBigInteger(Decimal.Truncate(value), new BigInteger(value));
}
// Decimal Explicit Cast from BigInteger: -1
VerifyDecimalExplicitCastFromBigInteger(-1);
// Decimal Explicit Cast from BigInteger: 0
VerifyDecimalExplicitCastFromBigInteger(0);
// Decimal Explicit Cast from BigInteger: 1
VerifyDecimalExplicitCastFromBigInteger(1);
// Decimal Explicit Cast from BigInteger: Random Positive Non-Integral < 100
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
value = (Decimal)(100 * s_random.NextDouble());
VerifyDecimalExplicitCastFromBigInteger(Decimal.Truncate(value), new BigInteger(value));
}
// Decimal Explicit Cast from BigInteger: Random Positive
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
VerifyDecimalExplicitCastFromBigInteger((Decimal)((Double)Decimal.MaxValue * s_random.NextDouble()));
}
// Decimal Explicit Cast from BigInteger: Decimal.MaxValue
VerifyDecimalExplicitCastFromBigInteger(Decimal.MaxValue);
// Decimal Explicit Cast from BigInteger: Decimal.MaxValue + 1
bigInteger = new BigInteger(Decimal.MaxValue);
bigInteger += BigInteger.One;
temp = bigInteger.ToByteArray();
for (int j = 0; j < 3; j++)
{
bits[j] = BitConverter.ToInt32(temp, 4 * j);
}
value = new Decimal(bits[0], bits[1], bits[2], false, 0);
Assert.Throws<OverflowException>(() => VerifyDecimalExplicitCastFromBigInteger(value, bigInteger));
// Decimal Explicit Cast from BigInteger: Random value > Decimal.MaxValue
for (int i = 0; i < NumberOfRandomIterations; ++i)
{
bigInteger = GenerateRandomBigIntegerGreaterThan(Decimal.MaxValue, s_random);
temp = bigInteger.ToByteArray();
for (int j = 0; j < 3; j++)
{
bits[j] = BitConverter.ToInt32(temp, 4 * j);
}
value = new Decimal(bits[0], bits[1], bits[2], false, 0);
Assert.Throws<OverflowException>(() => VerifyDecimalExplicitCastFromBigInteger(value, bigInteger));
}
}