예제 #1
0
        public void SubTest()
        {
            Random random = new Random(1234);

            for (int i = 0; i <= 50000; i++)
            {
                int bits1 = random.Next(BigUInt <Pow2.N32> .Bits / 2 + 1, BigUInt <Pow2.N32> .Bits + 1);
                int bits2 = random.Next(BigUInt <Pow2.N32> .Bits - bits1);

                UInt32[] value1 = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, bits1);
                UInt32[] value2 = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, bits2);

                BigUInt <Pow2.N32> v1 = new BigUInt <Pow2.N32>(value1, enable_clone: false);
                BigUInt <Pow2.N32> v2 = new BigUInt <Pow2.N32>(value2, enable_clone: false);
                BigInteger         bi1 = v1, bi2 = v2;

                BigUInt <Pow2.N32> v = BigUInt <Pow2.N32> .Sub(v1, v2);

                BigInteger bi = bi1 - bi2;

                Console.WriteLine(bi);
                Console.WriteLine(v);
                Assert.AreEqual(bi, v);
            }
        }
예제 #2
0
        public void MulDivTest()
        {
            Random random = new Random(1234);

            for (int i = 0; i <= 50000; i++)
            {
                int bits1 = random.Next(BigUInt <Pow2.N32> .Bits + 1);
                int bits2 = random.Next(BigUInt <Pow2.N32> .Bits - bits1);

                UInt32[] value1 = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, bits1);
                UInt32[] value2 = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, bits2);

                BigUInt <Pow2.N32> v1 = new BigUInt <Pow2.N32>(value1, enable_clone: false);
                BigUInt <Pow2.N32> v2 = new BigUInt <Pow2.N32>(value2, enable_clone: false);
                BigUInt <Pow2.N32> v3 = new BigUInt <Pow2.N32>((UInt32)random.Next(4));

                if (v2.IsZero || v2 <= v3)
                {
                    continue;
                }

                if (random.Next(3) < 2 || v1.IsZero || v2.IsZero || v3.IsZero)
                {
                    (BigUInt <Pow2.N32> vdiv, BigUInt <Pow2.N32> vrem) =
                        BigUInt <Pow2.N32> .Div(BigUInt <Pow2.N32> .Add(BigUInt <Pow2.N32> .Mul(v1, v2), v3), v2);

                    Assert.AreEqual(v1, vdiv);
                    Assert.AreEqual(v3, vrem);
                }
                else
                {
                    try {
                        (BigUInt <Pow2.N32> vdiv, BigUInt <Pow2.N32> vrem) =
                            BigUInt <Pow2.N32> .Div(BigUInt <Pow2.N32> .Sub(BigUInt <Pow2.N32> .Mul(v1, v2), v3), v2);

                        Assert.AreEqual(v1 - new BigUInt <Pow2.N32>(1), vdiv);
                        Assert.AreEqual(v2 - v3, vrem);
                    }
                    catch (OverflowException) {
                        Console.WriteLine(v1);
                        Console.WriteLine(v2);
                        Console.WriteLine(v3);
                        throw;
                    }
                }
            }
        }