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); } }