Пример #1
0
        public void DivULongTest()
        {
            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(64);

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

                BigUInt <Pow2.N32> v1 = new BigUInt <Pow2.N32>(value1, enable_clone: false);
                UInt64             v2 = UIntUtil.Pack(value2[1], value2[0]);
                BigInteger         bi1 = v1, bi2 = v2;

                if (v2 == 0)
                {
                    continue;
                }

                (BigUInt <Pow2.N32> vdiv, UInt64 vrem)
                    = BigUInt <Pow2.N32> .Div(v1, v2);

                BigInteger bidiv = bi1 / bi2, birem = bi1 - bi2 * bidiv;

                Assert.IsTrue(vrem < v2);

                Assert.AreEqual(birem, vrem, "rem");
                Assert.AreEqual(bidiv, vdiv, "div");

                Assert.AreEqual(v1, BigUInt <Pow2.N32> .Add(BigUInt <Pow2.N32> .Mul(vdiv, v2), vrem));
            }
        }
Пример #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;
                    }
                }
            }
        }
Пример #3
0
        public void DivULongFullTest()
        {
            for (int sft1 = 0; sft1 < BigUInt <Pow2.N8> .Bits; sft1++)
            {
                for (int sft2 = 0; sft2 < 64; sft2++)
                {
                    BigUInt <Pow2.N8> v1 = BigUInt <Pow2.N8> .Full >> sft1;
                    UInt64            v2 = 0xFFFFFFFFFFFFFFFFul >> sft2;
                    BigInteger        bi1 = v1, bi2 = v2;

                    (BigUInt <Pow2.N8> vdiv, UInt64 vrem)
                        = BigUInt <Pow2.N8> .Div(v1, v2);

                    BigInteger bidiv = bi1 / bi2, birem = bi1 - bi2 * bidiv;

                    Assert.IsTrue(vrem < v2);

                    Assert.AreEqual(birem, vrem, "rem");
                    Assert.AreEqual(bidiv, vdiv, "div");

                    Assert.AreEqual(v1, BigUInt <Pow2.N8> .Add(BigUInt <Pow2.N8> .Mul(vdiv, v2), vrem));
                }
            }
        }