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

                if (random.Next(2) < 1)
                {
                    value1[0] = 0;
                }

                if (random.Next(2) < 1)
                {
                    value2[0] = 0;
                }

                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> .Mul(v1, v2);

                BigInteger bi = bi1 * bi2;

                Console.WriteLine(bi);
                Console.WriteLine(v);
                Assert.AreEqual(bi, v);
            }
        }
Пример #2
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));
            }
        }
Пример #3
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;
                    }
                }
            }
        }
Пример #4
0
        public void MulULongFullTest()
        {
            {
                UInt32[] value1 = (new UInt32[BigUInt <Pow2.N32> .Length])
                                  .Select((_, idx) => idx < BigUInt <Pow2.N32> .Length - 2 ? 0xFFFFFFFFu : 0
                                          ).ToArray();

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

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

                BigInteger bi = bi1 * bi2;

                Console.WriteLine(bi);
                Console.WriteLine(v);
                Assert.AreEqual(bi, v);
            }
        }
Пример #5
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));
                }
            }
        }
Пример #6
0
        public void MulFullTest()
        {
            {
                UInt32[] value = (new UInt32[BigUInt <Pow2.N32> .Length])
                                 .Select((_, idx) => idx < BigUInt <Pow2.N32> .Length / 2 ? 0xFFFFFFFFu : 0
                                         ).ToArray();

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

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

                BigInteger bi = bi1 * bi2;

                Console.WriteLine(bi);
                Console.WriteLine(v);
                Assert.AreEqual(bi, v);
            }

            {
                UInt32[] value = (new UInt32[BigUInt <Pow2.N8> .Length])
                                 .Select((_, idx) => idx < BigUInt <Pow2.N8> .Length / 2 ? 0xFFFFFFFFu : 0
                                         ).ToArray();

                BigUInt <Pow2.N8> v1 = new BigUInt <Pow2.N8>(value, enable_clone: false);
                BigUInt <Pow2.N8> v2 = new BigUInt <Pow2.N8>(value, enable_clone: false);
                BigInteger        bi1 = v1, bi2 = v2;

                BigUInt <Pow2.N8> v = BigUInt <Pow2.N8> .Mul(v1, v2);

                BigInteger bi = bi1 * bi2;

                Console.WriteLine(bi);
                Console.WriteLine(v);
                Assert.AreEqual(bi, v);
            }

            {
                UInt32[] value = (new UInt32[BigUInt <N9> .Length])
                                 .Select((_, idx) => idx < BigUInt <N9> .Length / 2 ? 0xFFFFFFFFu : 0
                                         ).ToArray();

                BigUInt <N9> v1 = new BigUInt <N9>(value, enable_clone: false);
                BigUInt <N9> v2 = new BigUInt <N9>(value, enable_clone: false);
                BigInteger   bi1 = v1, bi2 = v2;

                BigUInt <N9> v = BigUInt <N9> .Mul(v1, v2);

                BigInteger bi = bi1 * bi2;

                Console.WriteLine(bi);
                Console.WriteLine(v);
                Assert.AreEqual(bi, v);
            }

            {
                UInt32[] value = (new UInt32[BigUInt <N10> .Length])
                                 .Select((_, idx) => idx < BigUInt <N10> .Length / 2 ? 0xFFFFFFFFu : 0
                                         ).ToArray();

                BigUInt <N10> v1 = new BigUInt <N10>(value, enable_clone: false);
                BigUInt <N10> v2 = new BigUInt <N10>(value, enable_clone: false);
                BigInteger    bi1 = v1, bi2 = v2;

                BigUInt <N10> v = BigUInt <N10> .Mul(v1, v2);

                BigInteger bi = bi1 * bi2;

                Console.WriteLine(bi);
                Console.WriteLine(v);
                Assert.AreEqual(bi, v);
            }

            {
                UInt32[] value = (new UInt32[BigUInt <N11> .Length])
                                 .Select((_, idx) => idx < BigUInt <N11> .Length / 2 ? 0xFFFFFFFFu : 0
                                         ).ToArray();

                BigUInt <N11> v1 = new BigUInt <N11>(value, enable_clone: false);
                BigUInt <N11> v2 = new BigUInt <N11>(value, enable_clone: false);
                BigInteger    bi1 = v1, bi2 = v2;

                BigUInt <N11> v = BigUInt <N11> .Mul(v1, v2);

                BigInteger bi = bi1 * bi2;

                Console.WriteLine(bi);
                Console.WriteLine(v);
                Assert.AreEqual(bi, v);
            }

            {
                UInt32[] value = (new UInt32[BigUInt <N12> .Length])
                                 .Select((_, idx) => idx < BigUInt <N12> .Length / 2 ? 0xFFFFFFFFu : 0
                                         ).ToArray();

                BigUInt <N12> v1 = new BigUInt <N12>(value, enable_clone: false);
                BigUInt <N12> v2 = new BigUInt <N12>(value, enable_clone: false);
                BigInteger    bi1 = v1, bi2 = v2;

                BigUInt <N12> v = BigUInt <N12> .Mul(v1, v2);

                BigInteger bi = bi1 * bi2;

                Console.WriteLine(bi);
                Console.WriteLine(v);
                Assert.AreEqual(bi, v);
            }
        }