Ejemplo n.º 1
0
        public void TestDivideAndRemainder()
        {
            // TODO More basic tests

            IBigInteger n = new BigInteger(48, _random);

            IBigInteger[] qr = n.DivideAndRemainder(one);
            Assert.AreEqual(n, qr[0]);
            Assert.AreEqual(zero, qr[1]);

            for (int rep = 0; rep < 10; ++rep)
            {
                IBigInteger   a  = new BigInteger(100 - rep, 0, _random);
                IBigInteger   b  = new BigInteger(100 + rep, 0, _random);
                IBigInteger   c  = new BigInteger(10 + rep, 0, _random);
                IBigInteger   d  = a.Multiply(b).Add(c);
                IBigInteger[] es = d.DivideAndRemainder(a);

                Assert.AreEqual(b, es[0]);
                Assert.AreEqual(c, es[1]);
            }

            // Special tests for power of two since uses different code path internally
            for (int i = 0; i < 100; ++i)
            {
                int         shift  = _random.Next(64);
                IBigInteger a      = one.ShiftLeft(shift);
                IBigInteger b      = new BigInteger(64 + _random.Next(64), _random);
                IBigInteger bShift = b.ShiftRight(shift);
                IBigInteger bMod   = b.And(a.Subtract(one));

                string data = "shift=" + shift + ", b=" + b.ToString(16);

                qr = b.DivideAndRemainder(a);
                Assert.AreEqual(bShift, qr[0], data);
                Assert.AreEqual(bMod, qr[1], data);

                qr = b.DivideAndRemainder(a.Negate());
                Assert.AreEqual(bShift.Negate(), qr[0], data);
                Assert.AreEqual(bMod, qr[1], data);

                qr = b.Negate().DivideAndRemainder(a);
                Assert.AreEqual(bShift.Negate(), qr[0], data);
                Assert.AreEqual(bMod.Negate(), qr[1], data);

                qr = b.Negate().DivideAndRemainder(a.Negate());
                Assert.AreEqual(bShift, qr[0], data);
                Assert.AreEqual(bMod.Negate(), qr[1], data);
            }
        }
Ejemplo n.º 2
0
        public void TestBitCount()
        {
            Assert.AreEqual(0, zero.BitCount);
            Assert.AreEqual(1, one.BitCount);
            Assert.AreEqual(0, minusOne.BitCount);
            Assert.AreEqual(1, two.BitCount);
            Assert.AreEqual(1, minusTwo.BitCount);

            for (int i = 0; i < 100; ++i)
            {
                IBigInteger pow2 = one.ShiftLeft(i);

                Assert.AreEqual(1, pow2.BitCount);
                Assert.AreEqual(i, pow2.Negate().BitCount);
            }

            for (int i = 0; i < 10; ++i)
            {
                IBigInteger test     = new BigInteger(128, 0, _random);
                int         bitCount = 0;

                for (int bit = 0; bit < test.BitLength; ++bit)
                {
                    if (test.TestBit(bit))
                    {
                        ++bitCount;
                    }
                }

                Assert.AreEqual(bitCount, test.BitCount);
            }
        }
Ejemplo n.º 3
0
        public void TestMultiply()
        {
            IBigInteger one = BigInteger.One;

            Assert.AreEqual(one, one.Negate().Multiply(one.Negate()));

            for (int i = 0; i < 100; ++i)
            {
                int aLen = 64 + _random.Next(64);
                int bLen = 64 + _random.Next(64);

                IBigInteger a = new BigInteger(aLen, _random).SetBit(aLen);
                IBigInteger b = new BigInteger(bLen, _random).SetBit(bLen);
                IBigInteger c = new BigInteger(32, _random);

                IBigInteger ab = a.Multiply(b);
                IBigInteger bc = b.Multiply(c);

                Assert.AreEqual(ab.Add(bc), a.Add(c).Multiply(b));
                Assert.AreEqual(ab.Subtract(bc), a.Subtract(c).Multiply(b));
            }

            // Special tests for power of two since uses different code path internally
            for (int i = 0; i < 100; ++i)
            {
                int         shift  = _random.Next(64);
                IBigInteger a      = one.ShiftLeft(shift);
                IBigInteger b      = new BigInteger(64 + _random.Next(64), _random);
                IBigInteger bShift = b.ShiftLeft(shift);

                Assert.AreEqual(bShift, a.Multiply(b));
                Assert.AreEqual(bShift.Negate(), a.Multiply(b.Negate()));
                Assert.AreEqual(bShift.Negate(), a.Negate().Multiply(b));
                Assert.AreEqual(bShift, a.Negate().Multiply(b.Negate()));

                Assert.AreEqual(bShift, b.Multiply(a));
                Assert.AreEqual(bShift.Negate(), b.Multiply(a.Negate()));
                Assert.AreEqual(bShift.Negate(), b.Negate().Multiply(a));
                Assert.AreEqual(bShift, b.Negate().Multiply(a.Negate()));
            }
        }
Ejemplo n.º 4
0
        public void TestSetBit()
        {
            Assert.AreEqual(one, zero.SetBit(0));
            Assert.AreEqual(one, one.SetBit(0));
            Assert.AreEqual(three, two.SetBit(0));

            Assert.AreEqual(two, zero.SetBit(1));
            Assert.AreEqual(three, one.SetBit(1));
            Assert.AreEqual(two, two.SetBit(1));

            // TODO Tests for setting bits in negative numbers

            // TODO Tests for setting extended bits

            for (int i = 0; i < 10; ++i)
            {
                IBigInteger n = new BigInteger(128, _random);

                for (int j = 0; j < 10; ++j)
                {
                    int         pos  = _random.Next(128);
                    IBigInteger m    = n.SetBit(pos);
                    bool        test = m.ShiftRight(pos).Remainder(two).Equals(one);

                    Assert.IsTrue(test);
                }
            }

            for (int i = 0; i < 100; ++i)
            {
                IBigInteger pow2      = one.ShiftLeft(i);
                IBigInteger minusPow2 = pow2.Negate();

                Assert.AreEqual(pow2, pow2.SetBit(i));
                Assert.AreEqual(minusPow2, minusPow2.SetBit(i));

                IBigInteger bigI = BigInteger.ValueOf(i);
                IBigInteger negI = bigI.Negate();

                for (int j = 0; j < 10; ++j)
                {
                    string data = "i=" + i + ", j=" + j;
                    Assert.AreEqual(bigI.Or(one.ShiftLeft(j)), bigI.SetBit(j), data);
                    Assert.AreEqual(negI.Or(one.ShiftLeft(j)), negI.SetBit(j), data);
                }
            }
        }
Ejemplo n.º 5
0
        public void TestModPow()
        {
            try
            {
                two.ModPow(one, zero);
                Assert.Fail("expected ArithmeticException");
            }
            catch (ArithmeticException) {}

            Assert.AreEqual(zero, zero.ModPow(zero, one));
            Assert.AreEqual(one, zero.ModPow(zero, two));
            Assert.AreEqual(zero, two.ModPow(one, one));
            Assert.AreEqual(one, two.ModPow(zero, two));

            for (int i = 0; i < 10; ++i)
            {
                IBigInteger m = BigInteger.ProbablePrime(10 + i * 3, _random);
                IBigInteger x = new BigInteger(m.BitLength - 1, _random);

                Assert.AreEqual(x, x.ModPow(m, m));
                if (x.SignValue != 0)
                {
                    Assert.AreEqual(zero, zero.ModPow(x, m));
                    Assert.AreEqual(one, x.ModPow(m.Subtract(one), m));
                }

                IBigInteger y  = new BigInteger(m.BitLength - 1, _random);
                IBigInteger n  = new BigInteger(m.BitLength - 1, _random);
                IBigInteger n3 = n.ModPow(three, m);

                IBigInteger resX = n.ModPow(x, m);
                IBigInteger resY = n.ModPow(y, m);
                IBigInteger res  = resX.Multiply(resY).Mod(m);
                IBigInteger res3 = res.ModPow(three, m);

                Assert.AreEqual(res3, n3.ModPow(x.Add(y), m));

                IBigInteger a = x.Add(one);                 // Make sure it's not zero
                IBigInteger b = y.Add(one);                 // Make sure it's not zero

                Assert.AreEqual(a.ModPow(b, m).ModInverse(m), a.ModPow(b.Negate(), m));
            }
        }
Ejemplo n.º 6
0
        public void TestToByteArrayUnsigned()
        {
            byte[] z = BigInteger.Zero.ToByteArrayUnsigned();
            Assert.IsTrue(Arrays.AreEqual(new byte[0], z));

            for (int i = 16; i <= 48; ++i)
            {
                IBigInteger x = BigInteger.ProbablePrime(i, _random);
                byte[]      b = x.ToByteArrayUnsigned();
                Assert.AreEqual((i + 7) / 8, b.Length);
                IBigInteger y = new BigInteger(1, b);
                Assert.AreEqual(x, y);

                x = x.Negate();
                b = x.ToByteArrayUnsigned();
                Assert.AreEqual(i / 8 + 1, b.Length);
                y = new BigInteger(b);
                Assert.AreEqual(x, y);
            }
        }
Ejemplo n.º 7
0
        public void TestBitLength()
        {
            Assert.AreEqual(0, zero.BitLength);
            Assert.AreEqual(1, one.BitLength);
            Assert.AreEqual(0, minusOne.BitLength);
            Assert.AreEqual(2, two.BitLength);
            Assert.AreEqual(1, minusTwo.BitLength);

            for (int i = 0; i < 100; ++i)
            {
                int         bit  = i + _random.Next(64);
                IBigInteger odd  = new BigInteger(bit, _random).SetBit(bit + 1).SetBit(0);
                IBigInteger pow2 = one.ShiftLeft(bit);

                Assert.AreEqual(bit + 2, odd.BitLength);
                Assert.AreEqual(bit + 2, odd.Negate().BitLength);
                Assert.AreEqual(bit + 1, pow2.BitLength);
                Assert.AreEqual(bit, pow2.Negate().BitLength);
            }
        }
Ejemplo n.º 8
0
        public void TestFlipBit()
        {
            for (int i = 0; i < 10; ++i)
            {
                IBigInteger a = new BigInteger(128, 0, _random);
                IBigInteger b = a;

                for (int x = 0; x < 100; ++x)
                {
                    // Note: Intentionally greater than initial size
                    int pos = _random.Next(256);

                    a = a.FlipBit(pos);
                    b = b.TestBit(pos) ? b.ClearBit(pos) : b.SetBit(pos);
                }

                Assert.AreEqual(a, b);
            }

            for (int i = 0; i < 100; ++i)
            {
                IBigInteger pow2      = one.ShiftLeft(i);
                IBigInteger minusPow2 = pow2.Negate();

                Assert.AreEqual(zero, pow2.FlipBit(i));
                Assert.AreEqual(minusPow2.ShiftLeft(1), minusPow2.FlipBit(i));

                IBigInteger bigI = BigInteger.ValueOf(i);
                IBigInteger negI = bigI.Negate();

                for (int j = 0; j < 10; ++j)
                {
                    string data = "i=" + i + ", j=" + j;
                    Assert.AreEqual(bigI.Xor(one.ShiftLeft(j)), bigI.FlipBit(j), data);
                    Assert.AreEqual(negI.Xor(one.ShiftLeft(j)), negI.FlipBit(j), data);
                }
            }
        }
        public IBigInteger Add(
            IBigInteger value)
        {
            if (this.SignValue == 0)
                return value;

            if (this.SignValue != value.SignValue)
            {
                if (value.SignValue == 0)
                    return this;

                if (value.SignValue < 0)
                    return Subtract(value.Negate());

                return value.Subtract(Negate());
            }

            return AddToMagnitude(value.Magnitude);
        }
        public IBigInteger Subtract(
            IBigInteger n)
        {
            if (n.SignValue == 0)
                return this;

            if (this.SignValue == 0)
                return n.Negate();

            if (this.SignValue != n.SignValue)
                return Add(n.Negate());

            int compare = CompareNoLeadingZeroes(0, Magnitude, 0, n.Magnitude);
            if (compare == 0)
                return Zero;

            IBigInteger bigun, lilun;
            if (compare < 0)
            {
                bigun = n;
                lilun = this;
            }
            else
            {
                bigun = this;
                lilun = n;
            }

            return new BigInteger(this.SignValue*compare, doSubBigLil(bigun.Magnitude, lilun.Magnitude), true);
        }
 public SimpleBigDecimal Negate()
 {
     return(new SimpleBigDecimal(bigInt.Negate(), scale));
 }
Ejemplo n.º 12
0
        public void TestDivide()
        {
            for (int i = -5; i <= 5; ++i)
            {
                try
                {
                    val(i).Divide(zero);
                    Assert.Fail("expected ArithmeticException");
                }
                catch (ArithmeticException) {}
            }

            int product     = 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9;
            int productPlus = product + 1;

            IBigInteger bigProduct     = val(product);
            IBigInteger bigProductPlus = val(productPlus);

            for (int divisor = 1; divisor < 10; ++divisor)
            {
                // Exact division
                IBigInteger expected = val(product / divisor);

                Assert.AreEqual(expected, bigProduct.Divide(val(divisor)));
                Assert.AreEqual(expected.Negate(), bigProduct.Negate().Divide(val(divisor)));
                Assert.AreEqual(expected.Negate(), bigProduct.Divide(val(divisor).Negate()));
                Assert.AreEqual(expected, bigProduct.Negate().Divide(val(divisor).Negate()));

                expected = val((product + 1) / divisor);

                Assert.AreEqual(expected, bigProductPlus.Divide(val(divisor)));
                Assert.AreEqual(expected.Negate(), bigProductPlus.Negate().Divide(val(divisor)));
                Assert.AreEqual(expected.Negate(), bigProductPlus.Divide(val(divisor).Negate()));
                Assert.AreEqual(expected, bigProductPlus.Negate().Divide(val(divisor).Negate()));
            }

            for (int rep = 0; rep < 10; ++rep)
            {
                IBigInteger a = new BigInteger(100 - rep, 0, _random);
                IBigInteger b = new BigInteger(100 + rep, 0, _random);
                IBigInteger c = new BigInteger(10 + rep, 0, _random);
                IBigInteger d = a.Multiply(b).Add(c);
                IBigInteger e = d.Divide(a);

                Assert.AreEqual(b, e);
            }

            // Special tests for power of two since uses different code path internally
            for (int i = 0; i < 100; ++i)
            {
                int         shift  = _random.Next(64);
                IBigInteger a      = one.ShiftLeft(shift);
                IBigInteger b      = new BigInteger(64 + _random.Next(64), _random);
                IBigInteger bShift = b.ShiftRight(shift);

                string data = "shift=" + shift + ", b=" + b.ToString(16);

                Assert.AreEqual(bShift, b.Divide(a), data);
                Assert.AreEqual(bShift.Negate(), b.Divide(a.Negate()), data);
                Assert.AreEqual(bShift.Negate(), b.Negate().Divide(a), data);
                Assert.AreEqual(bShift, b.Negate().Divide(a.Negate()), data);
            }

            // Regression
            {
                int         shift  = 63;
                IBigInteger a      = one.ShiftLeft(shift);
                IBigInteger b      = new BigInteger(1, Hex.Decode("2504b470dc188499"));
                IBigInteger bShift = b.ShiftRight(shift);

                string data = "shift=" + shift + ", b=" + b.ToString(16);

                Assert.AreEqual(bShift, b.Divide(a), data);
                Assert.AreEqual(bShift.Negate(), b.Divide(a.Negate()), data);
//				Assert.AreEqual(bShift.Negate(), b.Negate().Divide(a), data);
                Assert.AreEqual(bShift, b.Negate().Divide(a.Negate()), data);
            }
        }