2014-05-05 21:11:28 -04:00
|
|
|
|
/*
|
|
|
|
|
Copyright 2014 Alex Dyachenko
|
|
|
|
|
|
|
|
|
|
This file is part of the MPIR Library.
|
|
|
|
|
|
|
|
|
|
The MPIR Library is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
|
|
|
|
by the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
|
your option) any later version.
|
|
|
|
|
|
|
|
|
|
The MPIR Library is distributed in the hope that it will be useful, but
|
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
|
License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
|
along with the MPIR Library. If not, see http://www.gnu.org/licenses/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
|
|
|
|
|
|
namespace MPIR.Tests.HugeRationalTests
|
|
|
|
|
{
|
|
|
|
|
[TestClass]
|
|
|
|
|
public class ConstructionAndDisposal
|
|
|
|
|
{
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
|
public void RationalDefaultConstructor()
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
|
|
|
|
using (var a = new HugeRational())
|
|
|
|
|
{
|
2014-05-16 16:52:05 -04:00
|
|
|
|
Assert.AreNotEqual(0, a.NumeratorNumberOfLimbsAllocated());
|
|
|
|
|
Assert.AreEqual(0, a.NumeratorNumberOfLimbsUsed());
|
|
|
|
|
Assert.AreNotEqual(IntPtr.Zero, a.NumeratorLimbs());
|
|
|
|
|
Assert.AreEqual("0", a.Numerator.ToString());
|
|
|
|
|
|
|
|
|
|
Assert.AreNotEqual(0, a.DenominatorNumberOfLimbsAllocated());
|
|
|
|
|
Assert.AreEqual(1, a.DenominatorNumberOfLimbsUsed());
|
|
|
|
|
Assert.AreNotEqual(IntPtr.Zero, a.DenominatorLimbs());
|
|
|
|
|
Assert.AreEqual("1", a.Denominator.ToString());
|
2014-05-05 21:11:28 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-16 16:52:05 -04:00
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
|
public void RationalNumerator()
|
2014-05-16 16:52:05 -04:00
|
|
|
|
{
|
|
|
|
|
using (var a = new HugeRational())
|
|
|
|
|
{
|
|
|
|
|
a.Numerator.Dispose();
|
|
|
|
|
Assert.AreNotEqual(0, a.Numerator.NumberOfLimbsAllocated());
|
|
|
|
|
Assert.AreEqual("0", a.Numerator.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
|
public void RationalDenominator()
|
2014-05-16 16:52:05 -04:00
|
|
|
|
{
|
|
|
|
|
using (var a = new HugeRational())
|
|
|
|
|
{
|
|
|
|
|
a.Denominator.Dispose();
|
|
|
|
|
Assert.AreNotEqual(0, a.Denominator.NumberOfLimbsAllocated());
|
|
|
|
|
Assert.AreEqual("1", a.Denominator.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-05 21:11:28 -04:00
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
|
public void RationalDispose()
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
|
|
|
|
var a = new HugeRational();
|
|
|
|
|
a.Dispose();
|
2014-05-16 16:52:05 -04:00
|
|
|
|
|
|
|
|
|
Assert.AreEqual(0, a.NumeratorNumberOfLimbsAllocated());
|
|
|
|
|
Assert.AreEqual(0, a.NumeratorNumberOfLimbsUsed());
|
|
|
|
|
Assert.AreEqual(IntPtr.Zero, a.NumeratorLimbs());
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(0, a.DenominatorNumberOfLimbsAllocated());
|
|
|
|
|
Assert.AreEqual(0, a.DenominatorNumberOfLimbsUsed());
|
|
|
|
|
Assert.AreEqual(IntPtr.Zero, a.DenominatorLimbs());
|
2014-05-05 21:11:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
|
public void RationalConstructorFromLong()
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
|
var n = Platform.Si(123456789123456, 1234567891);
|
|
|
|
|
var d = Platform.Ui(12764787846358441471U, 2860486313U);
|
|
|
|
|
|
|
|
|
|
using (var a = new HugeRational(n, d))
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
2014-05-20 13:14:30 -04:00
|
|
|
|
Assert.AreEqual(1, a.NumeratorNumberOfLimbsAllocated());
|
|
|
|
|
Assert.AreEqual(1, a.NumeratorNumberOfLimbsUsed());
|
|
|
|
|
Assert.AreEqual(1, a.DenominatorNumberOfLimbsAllocated());
|
|
|
|
|
Assert.AreEqual(1, a.DenominatorNumberOfLimbsUsed());
|
|
|
|
|
Assert.AreEqual(n + "/" + d, a.ToString());
|
2014-05-05 21:11:28 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
|
public void RationalConstructorFromLongNegative()
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
|
var n = Platform.Si(-123456789123456, -1234567891);
|
|
|
|
|
var d = Platform.Ui(12764787846358441471U, 2860486313U);
|
|
|
|
|
|
|
|
|
|
using (var a = new HugeRational(n, d))
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
2014-05-20 13:14:30 -04:00
|
|
|
|
Assert.AreEqual(1, a.NumeratorNumberOfLimbsAllocated());
|
|
|
|
|
Assert.AreEqual(-1, a.NumeratorNumberOfLimbsUsed());
|
|
|
|
|
Assert.AreEqual(1, a.DenominatorNumberOfLimbsAllocated());
|
|
|
|
|
Assert.AreEqual(1, a.DenominatorNumberOfLimbsUsed());
|
|
|
|
|
Assert.AreEqual(n + "/" + d, a.ToString());
|
2014-05-05 21:11:28 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
|
public void RationalConstructorFromULong()
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
|
var n = Platform.Ui(ulong.MaxValue, uint.MaxValue);
|
|
|
|
|
var d = Platform.Ui(12764787846358441471U, 2860486313U);
|
|
|
|
|
|
|
|
|
|
using (var a = new HugeRational(n, d))
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
2014-05-20 13:14:30 -04:00
|
|
|
|
Assert.AreEqual(1, a.NumeratorNumberOfLimbsAllocated());
|
|
|
|
|
Assert.AreEqual(1, a.NumeratorNumberOfLimbsUsed());
|
|
|
|
|
Assert.AreEqual(1, a.DenominatorNumberOfLimbsAllocated());
|
|
|
|
|
Assert.AreEqual(1, a.DenominatorNumberOfLimbsUsed());
|
2016-01-14 14:07:20 -05:00
|
|
|
|
Assert.AreEqual(n.ToString(), a.Numerator.ToString());
|
|
|
|
|
Assert.AreEqual(n.ToString() + "/" + d, a.ToString());
|
2014-05-05 21:11:28 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
|
public void RationalConstructorFromDouble()
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
2014-05-21 11:47:51 -04:00
|
|
|
|
using (var a = new HugeRational(123456789123456.75))
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
2014-05-21 11:47:51 -04:00
|
|
|
|
Assert.AreEqual("493827156493827/4", a.ToString());
|
2014-05-05 21:11:28 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
|
public void RationalConstructorFromDoubleNegative()
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
2014-05-21 11:47:51 -04:00
|
|
|
|
using (var a = new HugeRational(-123456789123456.75))
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
2014-05-21 11:47:51 -04:00
|
|
|
|
Assert.AreEqual("-493827156493827/4", a.ToString());
|
2014-05-05 21:11:28 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
|
public void RationalAllocate()
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
2014-05-21 13:03:05 -04:00
|
|
|
|
using (var a = HugeRational.Allocate(129, 193))
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
|
Assert.AreEqual(1 + 128 / MpirSettings.BITS_PER_LIMB, a.NumeratorNumberOfLimbsAllocated());
|
2014-05-20 13:14:30 -04:00
|
|
|
|
Assert.AreEqual(0, a.NumeratorNumberOfLimbsUsed());
|
2016-01-14 14:07:20 -05:00
|
|
|
|
Assert.AreEqual(1 + 192 / MpirSettings.BITS_PER_LIMB, a.DenominatorNumberOfLimbsAllocated());
|
2014-05-20 13:14:30 -04:00
|
|
|
|
Assert.AreEqual(1, a.DenominatorNumberOfLimbsUsed());
|
|
|
|
|
Assert.AreEqual("0/1", a.ToString());
|
2014-05-05 21:11:28 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 17:19:17 -04:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void RationalCanonicalize()
|
|
|
|
|
{
|
|
|
|
|
using (var a = new HugeRational(198, 15))
|
|
|
|
|
{
|
|
|
|
|
a.Denominator.Value = -a.Denominator;
|
|
|
|
|
Assert.AreEqual("198/-15", a.ToString());
|
|
|
|
|
a.Canonicalize();
|
|
|
|
|
Assert.AreEqual("-66/5", a.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-05 21:11:28 -04:00
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
|
public void RationalStringConstructor()
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
2014-05-20 13:14:30 -04:00
|
|
|
|
var n = "5432109876543212345789023245987/362736035870515331128527330659";
|
2014-05-05 21:11:28 -04:00
|
|
|
|
using (var a = new HugeRational(n))
|
|
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
|
Assert.AreEqual(128 / MpirSettings.BITS_PER_LIMB, a.NumeratorNumberOfLimbsUsed());
|
2014-05-05 21:11:28 -04:00
|
|
|
|
Assert.AreEqual(n, a.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-03 10:44:28 -04:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void RationalStringConstructorNumeratorOnly()
|
|
|
|
|
{
|
|
|
|
|
var n = "5432109876543212345789023245987";
|
|
|
|
|
using(var a = new HugeRational(n))
|
|
|
|
|
{
|
2016-01-14 14:07:20 -05:00
|
|
|
|
Assert.AreEqual(128 / MpirSettings.BITS_PER_LIMB, a.NumeratorNumberOfLimbsUsed());
|
2014-06-03 10:44:28 -04:00
|
|
|
|
Assert.AreEqual(n + "/1", a.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-05 21:11:28 -04:00
|
|
|
|
[TestMethod]
|
|
|
|
|
[ExpectedException(typeof(ArgumentException))]
|
2014-05-20 15:37:16 -04:00
|
|
|
|
public void RationalStringConstructorInvalid()
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
|
|
|
|
var a = new HugeRational("12345A");
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-20 13:14:30 -04:00
|
|
|
|
[TestMethod]
|
|
|
|
|
[ExpectedException(typeof(ArgumentException))]
|
2014-05-20 15:37:16 -04:00
|
|
|
|
public void RationalStringConstructorInvalid2()
|
2014-05-20 13:14:30 -04:00
|
|
|
|
{
|
|
|
|
|
var a = new HugeRational("12345/54321A");
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-05 21:11:28 -04:00
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
|
public void RationalStringConstructorHex()
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
2014-05-20 13:14:30 -04:00
|
|
|
|
using (var i = new HugeInt("362736035870515331128527330659"))
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
2014-05-20 13:14:30 -04:00
|
|
|
|
var d = i.ToString(16);
|
2014-06-03 10:44:28 -04:00
|
|
|
|
var n = "143210ABCDEF32123457ACDB324598799";
|
|
|
|
|
using (var a = new HugeRational(n + "/" + d, 16))
|
2014-05-20 13:14:30 -04:00
|
|
|
|
{
|
2014-06-03 10:44:28 -04:00
|
|
|
|
Assert.AreEqual(n, a.Numerator.ToString(16));
|
2016-01-14 14:07:20 -05:00
|
|
|
|
Assert.AreEqual(1 + 128 / MpirSettings.BITS_PER_LIMB, a.NumeratorNumberOfLimbsUsed());
|
2014-05-20 13:14:30 -04:00
|
|
|
|
Assert.AreEqual(i, a.Denominator);
|
|
|
|
|
}
|
2014-05-05 21:11:28 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
|
public void RationalStringConstructorHexPrefix()
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
2014-05-20 13:14:30 -04:00
|
|
|
|
using (var i = new HugeInt("362736035870515331128527330659"))
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
2014-05-20 13:14:30 -04:00
|
|
|
|
var d = i.ToString(16);
|
|
|
|
|
var n = "143210ABCDEF32123457ACDB324598799";
|
|
|
|
|
using (var a = new HugeRational("0x" + n + "/0x" + d))
|
|
|
|
|
{
|
2014-05-21 11:47:51 -04:00
|
|
|
|
Assert.AreEqual(n + "/" + d, a.ToString(16));
|
2014-05-20 13:14:30 -04:00
|
|
|
|
Assert.AreEqual(i, a.Denominator);
|
|
|
|
|
}
|
2014-05-05 21:11:28 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
|
public void RationalStringAssignmentHexPrefix()
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
2014-05-20 13:14:30 -04:00
|
|
|
|
using (var i = new HugeInt("362736035870515331128527330659"))
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
2014-05-20 13:14:30 -04:00
|
|
|
|
var d = i.ToString(16);
|
|
|
|
|
var n = "143210ABCDEF32123457ACDB324598799";
|
|
|
|
|
using (var a = new HugeRational("0x" + n + "/0x" + d))
|
|
|
|
|
{
|
|
|
|
|
Assert.AreEqual(n + "/" + d, a.ToString(16));
|
|
|
|
|
Assert.AreEqual(n + "/" + d, a.ToString(16, false));
|
|
|
|
|
Assert.AreEqual((n + "/" + d).ToLower(), a.ToString(16, true));
|
|
|
|
|
a.SetTo("-0x" + n + "/0x17");
|
|
|
|
|
Assert.AreEqual("-" + n + "/17", a.ToString(16));
|
|
|
|
|
}
|
2014-05-05 21:11:28 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
|
public void RationalConstructorFromExpression()
|
2014-05-05 21:11:28 -04:00
|
|
|
|
{
|
2014-05-20 13:14:30 -04:00
|
|
|
|
using (var a = new HugeRational("2340958273409578234095823045723490587/362736035870515331128527330659"))
|
2014-05-05 21:11:28 -04:00
|
|
|
|
using (var b = new HugeRational(a + 1))
|
|
|
|
|
{
|
|
|
|
|
Assert.AreEqual(a + 1, b);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-26 15:54:50 -04:00
|
|
|
|
|
2014-11-23 22:34:20 -05:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void RationalConstructorFromFloatExpression()
|
|
|
|
|
{
|
|
|
|
|
using (var a = new HugeFloat("2340958273409578234.25"))
|
|
|
|
|
using (var b = new HugeRational(a + 1))
|
|
|
|
|
{
|
|
|
|
|
Assert.AreEqual("9363833093638312941/4", b.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-26 15:54:50 -04:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void RationalConstructorFromIntExpression()
|
|
|
|
|
{
|
|
|
|
|
using (var a = new HugeInt("2340958273409578234095823045723490587"))
|
|
|
|
|
using (var b = new HugeRational(a + 1))
|
|
|
|
|
{
|
|
|
|
|
Assert.AreEqual("2340958273409578234095823045723490588/1", b.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void RationalConstructorFromIntExpression2()
|
|
|
|
|
{
|
|
|
|
|
using (var a = new HugeInt("2340958273409578234095823045723490587"))
|
|
|
|
|
using (var d = new HugeInt("362736035870515331128527330659"))
|
|
|
|
|
using (var b = new HugeRational(a + 2, d * 2))
|
|
|
|
|
{
|
|
|
|
|
Assert.AreEqual(a + 2, b.Numerator);
|
|
|
|
|
Assert.AreEqual(d * 2, b.Denominator);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-05 21:11:28 -04:00
|
|
|
|
}
|
|
|
|
|
}
|