Esempio n. 1
0
 private static byte[] GetRandomByteArray(Random random)
 {
     return GetRandomByteArray(random, random.Next(0, 10));
 }
Esempio n. 2
0
        private static Byte[] GetRandomNegByteArray(Random random, int size)
        {
            byte[] value = new byte[size];

            for (int i = 0; i < value.Length; ++i)
            {
                value[i] = (byte)random.Next(0, 256);
            }
            value[value.Length - 1] |= 0x80;

            return value;
        }
Esempio n. 3
0
 private static byte[] GetRandomPosByteArray(Random random)
 {
     return GetRandomPosByteArray(random, random.Next(1, 100));
 }
Esempio n. 4
0
 private static byte[] GetRandomByteArray(Random random)
 {
     return MyBigIntImp.GetNonZeroRandomByteArray(random, random.Next(1, 18));
 }
Esempio n. 5
0
        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 Arround the boundry 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);

            // 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);
        }
Esempio n. 6
0
        private static int GetRandomInputForComparison(Random random, out BigInteger bigInteger1, out BigInteger bigInteger2)
        {
            byte[] byteArray1, byteArray2;
            bool sameSize = 0 == random.Next(0, 2);

            if (sameSize)
            {
                int size = random.Next(0, 1024);
                byteArray1 = GetRandomByteArray(random, size);
                byteArray2 = GetRandomByteArray(random, size);
            }
            else
            {
                byteArray1 = GetRandomByteArray(random);
                byteArray2 = GetRandomByteArray(random);
            }

            bigInteger1 = new BigInteger(byteArray1);
            bigInteger2 = new BigInteger(byteArray2);

            if (bigInteger1 > 0 && bigInteger2 > 0)
            {
                if (byteArray1.Length < byteArray2.Length)
                {
                    for (int i = byteArray2.Length - 1; byteArray1.Length <= i; --i)
                    {
                        if (0 != byteArray2[i])
                        {
                            return -1;
                        }
                    }
                }
                else if (byteArray1.Length > byteArray2.Length)
                {
                    for (int i = byteArray1.Length - 1; byteArray2.Length <= i; --i)
                    {
                        if (0 != byteArray1[i])
                        {
                            return 1;
                        }
                    }
                }
            }
            else if ((bigInteger1 < 0 && bigInteger2 > 0) || (bigInteger1 == 0 && bigInteger2 > 0) || (bigInteger1 < 0 && bigInteger2 == 0))
            {
                return -1;
            }
            else if ((bigInteger1 > 0 && bigInteger2 < 0) || (bigInteger1 == 0 && bigInteger2 < 0) || (bigInteger1 > 0 && bigInteger2 == 0))
            {
                return 1;
            }
            else if (bigInteger1 != 0 && bigInteger2 != 0)
            {
                if (byteArray1.Length < byteArray2.Length)
                {
                    for (int i = byteArray2.Length - 1; byteArray1.Length <= i; --i)
                    {
                        if (0xFF != byteArray2[i])
                        {
                            return 1;
                        }
                    }
                }
                else if (byteArray1.Length > byteArray2.Length)
                {
                    for (int i = byteArray1.Length - 1; byteArray2.Length <= i; --i)
                    {
                        if (0xFF != byteArray1[i])
                        {
                            return -1;
                        }
                    }
                }
            }

            for (int i = Math.Min(byteArray1.Length, byteArray2.Length) - 1; 0 <= i; --i)
            {
                if (byteArray1[i] > byteArray2[i])
                {
                    return 1;
                }
                else if (byteArray1[i] < byteArray2[i])
                {
                    return -1;
                }
            }

            return 0;
        }
Esempio n. 7
0
        private static Byte[] GetRandomPosByteArray(Random random, int size)
        {
            byte[] value = new byte[size];

            for (int i = 0; i < value.Length; i++)
            {
                value[i] = (byte)random.Next(0, 256);
            }
            value[value.Length - 1] &= 0x7F;

            return value;
        }
Esempio n. 8
0
 private static byte[] GetRandomByteArray(Random random)
 {
     return MyBigIntImp.GetRandomByteArray(random, random.Next(0, 1024));
 }
Esempio n. 9
0
        private static Byte[] GetRandomByteArray(Random random, int size)
        {
            byte[] value = new byte[size];
            bool zero = true;

            while (zero)
            {
                for (int i = 0; i < value.Length; ++i)
                {
                    value[i] = (byte)random.Next(0, 256);
                    if (value[i] != 0) zero = false;
                }
            }

            return value;
        }
Esempio n. 10
0
 private static Byte[] GetRandomByteArray(Random random)
 {
     return GetRandomByteArray(random, random.Next(1, 18));
 }
Esempio n. 11
0
 public static Color GetRandomColor()
 {
     Color.FromArgb(_random.Next(250), _random.Next(250), _random.Next(250));
 }