Copied-pasted float tests from rational

This commit is contained in:
Alex Dyachenko 2014-06-05 17:05:06 -04:00
parent 55f82b2046
commit f1034ab5dd
7 changed files with 2051 additions and 0 deletions

View File

@ -0,0 +1,507 @@
/*
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 Arithmetic
{
#region Add
[TestMethod]
public void RationalAddHugeRational()
{
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var b = new HugeRational("222987435987982730594288574029879874539/590872612825179551336102196593"))
using (var c = new HugeRational(a + b))
{
Assert.AreEqual(a.Numerator * b.Denominator + b.Numerator * a.Denominator, c.Numerator);
Assert.AreEqual(a.Denominator * b.Denominator, c.Denominator);
}
}
[TestMethod]
public void RationalAddHugeInt()
{
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var b = new HugeInt("222987435987982730594288574029879874539"))
using (var c = new HugeRational(a + b))
{
Assert.AreEqual(a.Numerator + b * a.Denominator, c.Numerator);
Assert.AreEqual(a.Denominator, c.Denominator);
c.Value = (b + 1) + a;
Assert.AreEqual(a.Numerator + (b + 1) * a.Denominator, c.Numerator);
Assert.AreEqual(a.Denominator, c.Denominator);
}
}
[TestMethod]
public void RationalAddLimb()
{
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var c = new HugeRational())
{
var b = 4288574029879874539UL;
c.Value = a + b;
Assert.AreEqual(a.Numerator + b * a.Denominator, c.Numerator);
Assert.AreEqual(a.Denominator, c.Denominator);
}
}
[TestMethod]
public void RationalAddToLimb()
{
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var c = new HugeRational())
{
var b = 4288574029879874539UL;
c.Value = b + a;
Assert.AreEqual(a.Numerator + b * a.Denominator, c.Numerator);
Assert.AreEqual(a.Denominator, c.Denominator);
}
}
[TestMethod]
public void RationalAddSignedLimb()
{
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var c = new HugeRational())
{
var b = -4288574029879874539L;
c.Value = a + b;
Assert.AreEqual(a.Numerator + b * a.Denominator, c.Numerator);
Assert.AreEqual(a.Denominator, c.Denominator);
}
}
[TestMethod]
public void RationalAddToSignedLimb()
{
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var c = new HugeRational())
{
var b = -4288574029879874539L;
c.Value = b + a;
Assert.AreEqual(a.Numerator + b * a.Denominator, c.Numerator);
Assert.AreEqual(a.Denominator, c.Denominator);
}
}
[TestMethod]
public void RationalAddToMaxSignedLimb()
{
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var c = new HugeRational())
{
var b = long.MinValue;
c.Value = b + a;
Assert.AreEqual(a.Numerator + b * a.Denominator, c.Numerator);
Assert.AreEqual(a.Denominator, c.Denominator);
}
}
[TestMethod]
public void RationalAddExpressionHugeRational()
{
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var b = new HugeRational("222987435987982730594288574029879874539/590872612825179551336102196593"))
using (var c = new HugeRational())
{
c.Value = 1 + (a + b);
Assert.AreEqual(a.Numerator * b.Denominator + b.Numerator * a.Denominator + a.Denominator * b.Denominator, c.Numerator);
Assert.AreEqual(a.Denominator * b.Denominator, c.Denominator);
}
}
#endregion
#region Subtract
[TestMethod]
public void RationalSubtractHugeRational()
{
using (var a = new HugeRational("445497268491433028939318409770173720259/115756986668303657898962467957"))
using (var b = new HugeRational("222987435987982730594288574029879874539/590872612825179551336102196593"))
using (var c = new HugeRational(a - b))
{
Assert.AreEqual(a.Numerator * b.Denominator - b.Numerator * a.Denominator, c.Numerator);
Assert.AreEqual(a.Denominator * b.Denominator, c.Denominator);
}
}
[TestMethod]
public void RationalSubtractHugeInt()
{
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var b = new HugeInt("222987435987982730594288574029879874539"))
using (var c = new HugeRational(a - b))
{
Assert.AreEqual(a.Numerator - b * a.Denominator, c.Numerator);
Assert.AreEqual(a.Denominator, c.Denominator);
c.Value = (b + 1) - a;
Assert.AreEqual((b + 1) * a.Denominator - a.Numerator, c.Numerator);
Assert.AreEqual(a.Denominator, c.Denominator);
}
}
[TestMethod]
public void RationalSubtractLimb()
{
var b = 4288574029879874539UL;
using (var a = new HugeRational("222509832503450298349318409770173720259/115756986668303657898962467957"))
using (var c = new HugeRational(a - b))
{
Assert.AreEqual(a.Numerator - b * a.Denominator, c.Numerator);
Assert.AreEqual(a.Denominator, c.Denominator);
}
}
[TestMethod]
public void RationalSubtractFromLimb()
{
var b = 4288574029879874539UL;
using (var a = new HugeRational("222509832503450298349318409770173720259/115756986668303657898962467957"))
using (var c = new HugeRational(b - a))
{
Assert.AreEqual(b * a.Denominator - a.Numerator, c.Numerator);
Assert.AreEqual(a.Denominator, c.Denominator);
}
}
[TestMethod]
public void RationalSubtractPositiveSignedLimb()
{
var b = 4288574029879874539L;
using (var a = new HugeRational("222509832503450298349318409770173720259/115756986668303657898962467957"))
using (var c = new HugeRational(a - b))
{
Assert.AreEqual(a.Numerator - b * a.Denominator, c.Numerator);
Assert.AreEqual(a.Denominator, c.Denominator);
}
}
[TestMethod]
public void RationalSubtractFromPositiveSignedLimb()
{
var b = 4288574029879874539L;
using (var a = new HugeRational("222509832503450298349318409770173720259/115756986668303657898962467957"))
using (var c = new HugeRational(a - b))
{
Assert.AreEqual(a.Numerator - b * a.Denominator, c.Numerator);
Assert.AreEqual(a.Denominator, c.Denominator);
}
}
[TestMethod]
public void RationalSubtractSignedLimb()
{
var b = -4288574029879874539L;
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var c = new HugeRational(a - b))
{
Assert.AreEqual(a.Numerator - b * a.Denominator, c.Numerator);
Assert.AreEqual(a.Denominator, c.Denominator);
}
}
[TestMethod]
public void RationalSubtractFromSignedLimb()
{
var b = -4288574029879874539L;
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var c = new HugeRational(b - a))
{
Assert.AreEqual(b * a.Denominator - a.Numerator, c.Numerator);
Assert.AreEqual(a.Denominator, c.Denominator);
}
}
#endregion
#region Multiply
[TestMethod]
public void RationalMultiplyByHugeRational()
{
using (var a = new HugeRational("90234098723098475098479385345098345/115756986668303657898962467957"))
using (var b = new HugeRational("7859487359873459872354987610987897/590872612825179551336102196593"))
using (var c = new HugeRational(a * b))
{
Assert.AreEqual(a.Numerator * b.Numerator, c.Numerator);
Assert.AreEqual(a.Denominator * b.Denominator, c.Denominator);
}
}
[TestMethod]
public void RationalMultiplytHugeInt()
{
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var b = new HugeInt("222987435987982730594288574029879874539"))
using (var c = new HugeRational(a * b))
{
Assert.AreEqual(a.Numerator * b, c.Numerator);
Assert.AreEqual(a.Denominator, c.Denominator);
c.Value = (b + 1) * a;
Assert.AreEqual((b + 1) * a.Numerator, c.Numerator);
Assert.AreEqual(a.Denominator, c.Denominator);
}
}
[TestMethod]
public void RationalMultiplyByLimb()
{
using (var a = new HugeRational("90234098723098475098479385345098345/115756986668303657898962467957"))
{
ulong b = 17390538260286101342;
a.Value = a * b;
Assert.AreEqual("1569219546226477273686601978789044606491747469626478990/115756986668303657898962467957", a.ToString());
}
}
[TestMethod]
public void RationalMultiplyLimbBy()
{
using (var a = new HugeRational("90234098723098475098479385345098345/115756986668303657898962467957"))
{
ulong b = 17390538260286101342;
a.Value = b * a;
Assert.AreEqual("1569219546226477273686601978789044606491747469626478990/115756986668303657898962467957", a.ToString());
}
}
[TestMethod]
public void RationalMultiplyBySignedLimb()
{
using (var a = new HugeRational("90234098723098475098479385345098345/115756986668303657898962467957"))
{
long b = -7390538260286101342;
a.Value = a * b;
Assert.AreEqual("-666878558995492522701808125338061156491747469626478990/115756986668303657898962467957", a.ToString());
}
}
[TestMethod]
public void RationalMultiplySignedLimbBy()
{
using (var a = new HugeRational("90234098723098475098479385345098345/115756986668303657898962467957"))
{
long b = -7390538260286101342;
a.Value = b * a;
Assert.AreEqual("-666878558995492522701808125338061156491747469626478990/115756986668303657898962467957", a.ToString());
}
}
#endregion
#region Shift Left
[TestMethod]
public void RationalShiftLeft()
{
using (var a = new HugeRational("-12345700987ABCDEF2345CBDEFA245230948/17607EF654EB9A13FFA163C75", 16))
{
ulong b = 40;
a.Value = a << b;
Assert.AreEqual("-12345700987ABCDEF2345CBDEFA2452309480000000000/17607EF654EB9A13FFA163C75", a.ToString(16));
}
}
#endregion
#region Shift Right
[TestMethod]
public void RationalShiftRight()
{
using (var a = new HugeRational("ABCDEF052834524092854092874502983745029345723098457209305983434345/17607EF654EB9A13FFA163C75", 16))
{
ulong b = 96;
a.Value = a >> b;
Assert.AreEqual("ABCDEF052834524092854092874502983745029345723098457209305983434345/17607EF654EB9A13FFA163C75000000000000000000000000", a.ToString(16));
}
}
#endregion
#region Negate
[TestMethod]
public void RationalNegate()
{
using (var a = new HugeRational("24092854092874502983745029345723098457209/115756986668303657898962467957"))
{
a.Value = -a;
Assert.AreEqual("-24092854092874502983745029345723098457209/115756986668303657898962467957", a.ToString());
a.Value = -a;
Assert.AreEqual("24092854092874502983745029345723098457209/115756986668303657898962467957", a.ToString());
}
}
#endregion
#region Abs
[TestMethod]
public void RationalMakeAbsolute()
{
using (var a = new HugeRational("-24092854092874502983745029345723098457209/115756986668303657898962467957"))
{
a.Value = a.Abs();
Assert.AreEqual("24092854092874502983745029345723098457209/115756986668303657898962467957", a.ToString());
a.Value = a.Abs();
Assert.AreEqual("24092854092874502983745029345723098457209/115756986668303657898962467957", a.ToString());
}
}
#endregion
#region Invert
[TestMethod]
public void RationalInvert()
{
using(var a = new HugeRational("-24092854092874502983745029345723098457209/115756986668303657898962467957"))
{
a.Value = a.Invert();
Assert.AreEqual("-115756986668303657898962467957/24092854092874502983745029345723098457209", a.ToString());
a.Value = a.Invert();
Assert.AreEqual("-24092854092874502983745029345723098457209/115756986668303657898962467957", a.ToString());
}
}
#endregion
#region Power
[TestMethod]
public void RationalPower()
{
using(var n = new HugeInt("-24092854092874502983745029345723098457209"))
using(var d = new HugeInt("115756986668303657898962467957"))
using(var a = new HugeRational(n, d))
{
a.Value = a ^ 5;
Assert.AreEqual(n ^ 5, a.Numerator);
Assert.AreEqual(d ^ 5, a.Denominator);
}
}
#endregion
#region Divide
#region Int
[TestMethod]
public void RationalDivideHugeRational()
{
using (var a = new HugeRational("115756986668303657898962467957/394580293847502987609283945873594873409587"))
using (var b = new HugeRational("593169091750307653294"))
{
a.Value = a / b;
Assert.AreEqual("115756986668303657898962467957/234052834524092854092760134269509268758750275703033222451729578", a.ToString());
}
}
[TestMethod]
public void RationalDivideHugeRational2()
{
using (var a = new HugeRational("90234098723098475098479385345098345/115756986668303657898962467957"))
using (var b = new HugeRational("6847944682037444681162770672798288913849/590872612825179551336102196593"))
using (var c = new HugeRational(a / b))
{
Assert.AreEqual(a.Numerator * b.Denominator, c.Numerator);
Assert.AreEqual(a.Denominator * b.Numerator, c.Denominator);
}
}
[TestMethod]
public void RationalDivideHugeInt()
{
using (var a = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var b = new HugeInt("222987435987982730594288574029879874539"))
using (var c = new HugeRational(a / b))
using (var d = new HugeRational())
{
Assert.AreEqual(a.Numerator, c.Numerator);
Assert.AreEqual(a.Denominator * b, c.Denominator);
c.Value = (b + 1) / a;
d.Numerator.Value = (b + 1) * a.Denominator;
d.Denominator.Value = a.Numerator;
d.Canonicalize();
Assert.AreEqual(d, c);
}
}
#endregion
#region Limb
[TestMethod]
public void RationalDivideLimb()
{
using (var a = new HugeRational("115756986668303657898962467957/39458029384750298767200622330399462537522498"))
{
ulong b = 5931690917503076532;
a.Value = a / b;
Assert.AreEqual("115756986668303657898962467957/234052834524092854092874502983745029345723092857791404165816936", a.ToString());
}
}
[TestMethod]
public void RationalDivideSignedLimb()
{
using(var a = new HugeRational("115756986668303657898962467957/39458029384750298767200622330399462537522498"))
{
long b = -5931690917503076532;
a.Value = a / b;
Assert.AreEqual("-115756986668303657898962467957/234052834524092854092874502983745029345723092857791404165816936", a.ToString());
}
}
[TestMethod]
public void RationalDivideLimbBy()
{
using(var a = new HugeRational("115756986668303657898962467957/39458029384750298767200622330399462537522498"))
{
ulong b = 5931690917503076532;
a.Value = b / a;
Assert.AreEqual("234052834524092854092874502983745029345723092857791404165816936/115756986668303657898962467957", a.ToString());
}
}
[TestMethod]
public void RationalDivideSignedLimbBy()
{
using(var a = new HugeRational("115756986668303657898962467957/39458029384750298767200622330399462537522498"))
{
long b = -5931690917503076532;
a.Value = b / a;
Assert.AreEqual("-234052834524092854092874502983745029345723092857791404165816936/115756986668303657898962467957", a.ToString());
}
}
#endregion
#endregion
}
}

View File

@ -0,0 +1,91 @@
/*
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 Assignment
{
[TestMethod]
public void RationalAssignCopy()
{
using (var a = new HugeRational("-222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var b = new HugeRational())
{
b.Value = a;
Assert.AreEqual("-222509832503450298345029835740293845720/115756986668303657898962467957", b.ToString());
}
}
[TestMethod]
public void RationalSwap()
{
using (var a = new HugeRational("-222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var b = new HugeRational("2039847290878794872059384789347534534/590872612825179551336102196593"))
{
var aValue = a._value();
var bValue = b._value();
a.Swap(b);
Assert.AreEqual(bValue, a._value());
Assert.AreEqual(aValue, b._value());
}
}
[TestMethod]
public void RationalCompoundOperators()
{
using (var a = new HugeRational("938475092834705928347523452345/115756986668303657898962467957"))
{
a.Value += 1;
a.Value *= 10;
Assert.AreEqual("10542320795030095862464859203020/115756986668303657898962467957", a.ToString());
}
}
[TestMethod]
public void RationalAssignInt()
{
using (var a = new HugeInt("222509832503450298345029835740293845720"))
using (var b = new HugeRational("1/3"))
{
b.SetTo(a);
Assert.AreEqual("222509832503450298345029835740293845720/1", b.ToString());
}
}
[TestMethod]
public void RationalAssignInt2()
{
using (var a = new HugeInt("222509832503450298345029835740293845719"))
using (var d = new HugeInt("115756986668303657898962467957"))
using (var b = new HugeRational("1/3"))
{
b.SetTo(a, d);
Assert.AreEqual("222509832503450298345029835740293845719/115756986668303657898962467957", b.ToString());
b.SetTo(b.Numerator - b.Denominator, b.Denominator * 5);
Assert.AreEqual(a - d, b.Numerator);
Assert.AreEqual(d * 5, b.Denominator);
}
}
//more tests coming here
}
}

View File

@ -0,0 +1,678 @@
/*
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 Comparisons
{
#region CompareTo
[TestMethod]
public void RationalCompareToHugeRational()
{
using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957"))
using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var c = new HugeRational("1/115756986668303657898962467957"))
{
Assert.AreEqual(1, System.Math.Sign(b.CompareTo(a)));
Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b + 1)));
Assert.AreEqual(0, System.Math.Sign((a + c).CompareTo(-b)));
Assert.AreEqual(1, System.Math.Sign(a.CompareTo(null)));
}
}
[TestMethod]
public void RationalCompareToObject()
{
using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957"))
using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var c = new HugeRational("1/115756986668303657898962467957"))
{
Assert.AreEqual(1, System.Math.Sign(((IComparable)b).CompareTo((object)a)));
Assert.AreEqual(-1, System.Math.Sign(((IComparable)a).CompareTo((object)b)));
Assert.AreEqual(1, System.Math.Sign(((IComparable)a).CompareTo(null)));
Assert.AreEqual(0, System.Math.Sign(((IComparable)(a + c)).CompareTo((object)-b)));
}
}
[TestMethod]
public void RationalCompareToExpression()
{
using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957"))
using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var c = new HugeRational("1/115756986668303657898962467957"))
{
Assert.AreEqual(1, System.Math.Sign(((IComparable<RationalExpression>)b).CompareTo(a)));
Assert.AreEqual(-1, System.Math.Sign(((IComparable<RationalExpression>)a).CompareTo(b)));
Assert.AreEqual(1, System.Math.Sign(((IComparable<RationalExpression>)a).CompareTo(null)));
Assert.AreEqual(0, System.Math.Sign(((IComparable<RationalExpression>)(a + c)).CompareTo(-b)));
}
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void RationalCompareToNonExpression()
{
using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957"))
{
a.CompareTo("abc");
}
}
[TestMethod]
public void RationalCompareToLimb()
{
using (var a = new HugeRational("-222509821"))
{
ulong b = 222509820;
Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b)));
Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b + 1)));
Assert.AreEqual(0, System.Math.Sign((-(a + 1)).CompareTo(b)));
Assert.AreEqual(1, System.Math.Sign((-a).CompareTo(b)));
}
}
[TestMethod]
public void RationalCompareToLimb2()
{
using(var a = new HugeRational("-222509947/127"))
{
ulong b = 222509820;
ulong d = 127;
Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b, d)));
Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b + 1, d)));
Assert.AreEqual(0, System.Math.Sign((-(a + 1)).CompareTo(b, d)));
Assert.AreEqual(0, System.Math.Sign((-a).CompareTo(b + d, d)));
Assert.AreEqual(1, System.Math.Sign((-a).CompareTo(b, d)));
}
}
[TestMethod]
public void RationalCompareToSignedLimb()
{
using (var a = new HugeRational("-222509821"))
{
long b = -222509820;
Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b)));
Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b + 1)));
Assert.AreEqual(0, System.Math.Sign((a + 1).CompareTo(b)));
Assert.AreEqual(0, System.Math.Sign(a.CompareTo(b - 1)));
Assert.AreEqual(1, System.Math.Sign(a.CompareTo(b - 2)));
}
}
[TestMethod]
public void RationalCompareToSignedLimb2()
{
using(var a = new HugeRational("-222509947/127"))
{
long b = -222509820;
long d = 127;
Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b, (ulong)d)));
Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b + 1, (ulong)d)));
Assert.AreEqual(0, System.Math.Sign((a + 1).CompareTo(b, (ulong)d)));
Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b - d + 1, (ulong)d)));
Assert.AreEqual(0, System.Math.Sign(a.CompareTo(b - d, (ulong)d)));
Assert.AreEqual(1, System.Math.Sign(a.CompareTo(b - d - 1, (ulong)d)));
}
}
[TestMethod]
public void RationalCompareToSignedDouble()
{
using (var a = new HugeRational("-222509821"))
{
double b = -222509820;
Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b)));
Assert.AreEqual(-1, System.Math.Sign(a.CompareTo(b + 1)));
Assert.AreEqual(0, System.Math.Sign((a + 1).CompareTo(b)));
Assert.AreEqual(0, System.Math.Sign(a.CompareTo(b - 1)));
Assert.AreEqual(1, System.Math.Sign(a.CompareTo(b - 1.1)));
}
}
#endregion
#region comparison operators with expr
[TestMethod]
public void RationalOperatorLessThan()
{
using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957"))
using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var c = new HugeRational())
{
c.Value = a;
Assert.IsTrue(a < b);
Assert.IsFalse(b < a);
Assert.IsFalse(a < c);
Assert.IsFalse(a < null);
Assert.IsTrue(null < a);
}
}
[TestMethod]
public void RationalOperatorLessThanOrEqual()
{
using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957"))
using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var c = new HugeRational())
{
c.Value = a;
Assert.IsTrue(a <= b);
Assert.IsFalse(b <= a);
Assert.IsTrue(a <= c);
Assert.IsFalse(a <= null);
Assert.IsTrue(null <= a);
}
}
[TestMethod]
public void RationalOperatorGreaterThan()
{
using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957"))
using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var c = new HugeRational())
{
c.Value = a;
Assert.IsFalse(a > b);
Assert.IsTrue(b > a);
Assert.IsFalse(a > c);
Assert.IsTrue(a > null);
Assert.IsFalse(null > a);
}
}
[TestMethod]
public void RationalOperatorGreaterThanOrEqual()
{
using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957"))
using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var c = new HugeRational())
{
c.Value = a;
Assert.IsFalse(a >= b);
Assert.IsTrue(b >= a);
Assert.IsTrue(a >= c);
Assert.IsTrue(a >= null);
Assert.IsFalse(null >= a);
}
}
#endregion
#region comparison operators with limb
[TestMethod]
public void RationalOperatorLessThanLimb()
{
using (var a = new HugeRational("3845721"))
{
ulong c = 5432;
ulong b = 5432349587;
Assert.IsTrue(a < b);
Assert.IsFalse(b < a);
Assert.IsFalse(a < c);
Assert.IsTrue(c < a);
}
}
[TestMethod]
public void RationalOperatorLessThanOrEqualLimb()
{
using (var a = new HugeRational("3845721"))
{
ulong c = 5432;
ulong b = 5432349587;
ulong d = 3845721;
Assert.IsTrue(a <= b);
Assert.IsFalse(b <= a);
Assert.IsFalse(a <= c);
Assert.IsTrue(c <= a);
Assert.IsTrue(a <= d);
Assert.IsTrue(d <= a);
}
}
[TestMethod]
public void RationalOperatorGreaterThanLimb()
{
using (var a = new HugeRational("3845721"))
{
ulong c = 5432;
ulong b = 5432349587;
Assert.IsFalse(a > b);
Assert.IsTrue(b > a);
Assert.IsTrue(a > c);
Assert.IsFalse(c > a);
}
}
[TestMethod]
public void RationalOperatorGreaterThanOrEqualLimb()
{
using (var a = new HugeRational("3845721"))
{
ulong c = 5432;
ulong b = 5432349587;
ulong d = 3845721;
Assert.IsFalse(a >= b);
Assert.IsTrue(b >= a);
Assert.IsTrue(a >= c);
Assert.IsFalse(c >= a);
Assert.IsTrue(a >= d);
Assert.IsTrue(d >= a);
}
}
#endregion
#region comparison operators with signed limb
[TestMethod]
public void RationalOperatorLessThanSignedLimb()
{
using (var a = new HugeRational("-3845721"))
{
long c = -543254325432;
long b = -9587;
Assert.IsTrue(a < b);
Assert.IsFalse(b < a);
Assert.IsFalse(a < c);
Assert.IsTrue(c < a);
}
}
[TestMethod]
public void RationalOperatorLessThanOrEqualSignedLimb()
{
using (var a = new HugeRational("-3845721"))
{
long c = -543254325432;
long b = -9587;
long d = -3845721;
Assert.IsTrue(a <= b);
Assert.IsFalse(b <= a);
Assert.IsFalse(a <= c);
Assert.IsTrue(c <= a);
Assert.IsTrue(a <= d);
Assert.IsTrue(d <= a);
}
}
[TestMethod]
public void RationalOperatorGreaterThanSignedLimb()
{
using (var a = new HugeRational("-3845721"))
{
long c = -543254325432;
long b = -9587;
Assert.IsFalse(a > b);
Assert.IsTrue(b > a);
Assert.IsTrue(a > c);
Assert.IsFalse(c > a);
}
}
[TestMethod]
public void RationalOperatorGreaterThanOrEqualSignedLimb()
{
using (var a = new HugeRational("-3845721"))
{
long c = -543254325432;
long b = -9587;
long d = -3845721;
Assert.IsFalse(a >= b);
Assert.IsTrue(b >= a);
Assert.IsTrue(a >= c);
Assert.IsFalse(c >= a);
Assert.IsTrue(a >= d);
Assert.IsTrue(d >= a);
}
}
#endregion
#region comparison operators with double
[TestMethod]
public void RationalOperatorLessThanDouble()
{
using (var a = new HugeRational("-3845721"))
{
double c = -543254325432;
double b = -9587;
Assert.IsTrue(a < b);
Assert.IsFalse(b < a);
Assert.IsFalse(a < c);
Assert.IsTrue(c < a);
}
}
[TestMethod]
public void RationalOperatorLessThanOrEqualDouble()
{
using (var a = new HugeRational("-3845721"))
{
double c = -543254325432;
double b = -9587;
double d = -3845721;
Assert.IsTrue(a <= b);
Assert.IsFalse(b <= a);
Assert.IsFalse(a <= c);
Assert.IsTrue(c <= a);
Assert.IsTrue(a <= d);
Assert.IsTrue(d <= a);
}
}
[TestMethod]
public void RationalOperatorGreaterThanDouble()
{
using (var a = new HugeRational("-3845721"))
{
double c = -543254325432;
double b = -9587;
Assert.IsFalse(a > b);
Assert.IsTrue(b > a);
Assert.IsTrue(a > c);
Assert.IsFalse(c > a);
}
}
[TestMethod]
public void RationalOperatorGreaterThanOrEqualDouble()
{
using (var a = new HugeRational("-3845721"))
{
double c = -543254325432;
double b = -9587;
double d = -3845721;
Assert.IsFalse(a >= b);
Assert.IsTrue(b >= a);
Assert.IsTrue(a >= c);
Assert.IsFalse(c >= a);
Assert.IsTrue(a >= d);
Assert.IsTrue(d >= a);
Assert.IsFalse(d - 0.1 >= a);
}
}
#endregion
#region Equals
[TestMethod]
public void RationalEqualsHugeRational()
{
using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957"))
using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var c = new HugeRational("1/115756986668303657898962467957"))
{
Assert.IsFalse(b.Equals(a));
Assert.IsFalse(a.Equals(b + c));
Assert.IsTrue((a + c).Equals(-b));
Assert.IsFalse(a.Equals(null));
Assert.IsTrue(Equals(a + c, -b));
}
}
[TestMethod]
public void RationalEqualsExpression()
{
using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957"))
using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var c = new HugeRational("1/115756986668303657898962467957"))
{
Assert.IsFalse(((IEquatable<RationalExpression>)b).Equals(a));
Assert.IsFalse(((IEquatable<RationalExpression>)a).Equals(b));
Assert.IsFalse(((IEquatable<RationalExpression>)a).Equals(null));
Assert.IsTrue(((IEquatable<RationalExpression>)(a + c)).Equals(-b));
}
}
[TestMethod]
public void RationalEqualsNonExpression()
{
using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957"))
{
Assert.IsFalse(a.Equals("abc"));
}
}
[TestMethod]
public void RationalEqualsLimb()
{
using (var a = new HugeRational("222509832503"))
{
ulong b = 222509832504;
Assert.IsFalse(a.Equals(b + 1));
Assert.IsTrue(a.Equals(b - 1));
Assert.IsTrue((a + 1).Equals(b));
}
}
[TestMethod]
public void RationalEqualsSignedLimb()
{
using (var a = new HugeRational("-222509832505"))
{
long b = -222509832504;
Assert.IsFalse(a.Equals(b + 1));
Assert.IsTrue(a.Equals(b - 1));
Assert.IsTrue((a + 1).Equals(b));
}
}
[TestMethod]
public void RationalEqualsLimb2()
{
using(var a = new HugeRational("222509832377/127"))
{
ulong b = 222509832504;
ulong d = 127;
Assert.IsFalse(a.Equals(b + 1, d));
Assert.IsTrue(a.Equals(b - d, d));
Assert.IsTrue((a + 1).Equals(b, d));
}
}
[TestMethod]
public void RationalEqualsSignedLimb2()
{
using(var a = new HugeRational("-222509832377/127"))
{
long b = -222509832504;
ulong d = 127;
Assert.IsFalse(a.Equals(b + 1, d));
Assert.IsTrue(a.Equals(b + (long)d, d));
Assert.IsTrue((a - 1).Equals(b, d));
}
}
[TestMethod]
public void RationalEqualsDouble()
{
using (var a = new HugeRational("-222509832505"))
{
double b = -222509832504;
Assert.IsFalse(a.Equals(b + 1));
Assert.IsTrue(a.Equals(b - 1));
Assert.IsTrue((a + 1).Equals(b));
Assert.IsFalse((a + 1).Equals(b + 0.1));
}
}
#endregion
#region Equality operators with expr
[TestMethod]
public void RationalEqualsOperatorHugeRational()
{
using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957"))
using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var c = new HugeRational("1/115756986668303657898962467957"))
{
Assert.IsFalse(b == a);
Assert.IsFalse(a == b + c);
Assert.IsTrue(a + c == -b);
Assert.IsFalse(a == null);
}
}
[TestMethod]
public void RationalNotEqualOperatorHugeRational()
{
using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957"))
using (var b = new HugeRational("222509832503450298345029835740293845720/115756986668303657898962467957"))
using (var c = new HugeRational("1/115756986668303657898962467957"))
{
Assert.IsTrue(b != a);
Assert.IsTrue(a != b + c);
Assert.IsFalse(a + c != -b);
Assert.IsTrue(a != null);
}
}
#endregion
#region Equality operators with Limb
[TestMethod]
public void RationalEqualsOperatorLimb()
{
using (var a = new HugeRational("-835740293845721"))
{
ulong b = 835740293845720;
Assert.IsFalse(b == a);
Assert.IsFalse(a == b + 1);
Assert.IsTrue(-(a + 1) == b);
}
}
[TestMethod]
public void RationalNotEqualOperatorLimb()
{
using (var a = new HugeRational("-835740293845721"))
{
ulong b = 835740293845720;
Assert.IsTrue(b != a);
Assert.IsTrue(a != b + 1);
Assert.IsFalse(-(a + 1) != b);
}
}
#endregion
#region Equality operators with Signed Limb
[TestMethod]
public void RationalEqualsOperatorSignedLimb()
{
using (var a = new HugeRational("-835740293845721"))
{
long b = -835740293845720;
Assert.IsFalse(b == a);
Assert.IsFalse(a == b + 1);
Assert.IsTrue(a + 1 == b);
}
}
[TestMethod]
public void RationalNotEqualOperatorSignedLimb()
{
using (var a = new HugeRational("-835740293845721"))
{
long b = -835740293845720;
Assert.IsTrue(b != a);
Assert.IsTrue(a != b + 1);
Assert.IsFalse(a + 1 != b);
}
}
#endregion
#region Equality operators with Double
[TestMethod]
public void RationalEqualsOperatorDouble()
{
using (var a = new HugeRational("-835740293845721"))
{
double b = -835740293845720;
Assert.IsFalse(b == a);
Assert.IsFalse(a == b + 1);
Assert.IsTrue(a + 1 == b);
Assert.IsFalse(a + 1 == b + 0.1);
}
}
[TestMethod]
public void RationalNotEqualOperatorDouble()
{
using (var a = new HugeRational("-835740293845721"))
{
double b = -835740293845720;
Assert.IsTrue(b != a);
Assert.IsTrue(a != b + 1);
Assert.IsFalse(a + 1 != b);
Assert.IsTrue(a + 1 != b + 0.1);
}
}
#endregion
#region GetHashCode
[TestMethod]
public void RationalGetHashCodeTest()
{
using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957"))
{
Assert.AreNotEqual(0, a.GetHashCode());
Assert.AreEqual(a.GetHashCode(), (a + 0).GetHashCode());
Assert.AreNotEqual(a.GetHashCode(), (-a).GetHashCode());
}
}
#endregion
#region Sign
[TestMethod]
public void RationalSign()
{
using (var a = new HugeRational("-222509832503450298345029835740293845721/115756986668303657898962467957"))
{
Assert.AreEqual(-1, a.Sign());
Assert.AreEqual(1, (-a).Sign());
Assert.AreEqual(0, (a-a).Sign());
}
}
#endregion
//more tests coming here
}
}

View File

@ -0,0 +1,287 @@
/*
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]
public void RationalDefaultConstructor()
{
using (var a = new HugeRational())
{
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());
}
}
[TestMethod]
public void RationalNumerator()
{
using (var a = new HugeRational())
{
a.Numerator.Dispose();
Assert.AreNotEqual(0, a.Numerator.NumberOfLimbsAllocated());
Assert.AreEqual("0", a.Numerator.ToString());
}
}
[TestMethod]
public void RationalDenominator()
{
using (var a = new HugeRational())
{
a.Denominator.Dispose();
Assert.AreNotEqual(0, a.Denominator.NumberOfLimbsAllocated());
Assert.AreEqual("1", a.Denominator.ToString());
}
}
[TestMethod]
public void RationalDispose()
{
var a = new HugeRational();
a.Dispose();
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());
}
[TestMethod]
public void RationalConstructorFromLong()
{
var n = "123456789123456";
var d = "12764787846358441471";
using (var a = new HugeRational(long.Parse(n), ulong.Parse(d)))
{
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());
}
}
[TestMethod]
public void RationalConstructorFromLongNegative()
{
var n = "-123456789123456";
var d = "12764787846358441471";
using (var a = new HugeRational(long.Parse(n), ulong.Parse(d)))
{
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());
}
}
[TestMethod]
public void RationalConstructorFromULong()
{
var d = "12764787846358441471";
using (var a = new HugeRational(ulong.MaxValue, ulong.Parse(d)))
{
Assert.AreEqual(1, a.NumeratorNumberOfLimbsAllocated());
Assert.AreEqual(1, a.NumeratorNumberOfLimbsUsed());
Assert.AreEqual(1, a.DenominatorNumberOfLimbsAllocated());
Assert.AreEqual(1, a.DenominatorNumberOfLimbsUsed());
Assert.AreEqual(ulong.MaxValue.ToString(), a.Numerator.ToString());
Assert.AreEqual(ulong.MaxValue.ToString() + "/" + d, a.ToString());
}
}
[TestMethod]
public void RationalConstructorFromDouble()
{
using (var a = new HugeRational(123456789123456.75))
{
Assert.AreEqual("493827156493827/4", a.ToString());
}
}
[TestMethod]
public void RationalConstructorFromDoubleNegative()
{
using (var a = new HugeRational(-123456789123456.75))
{
Assert.AreEqual("-493827156493827/4", a.ToString());
}
}
[TestMethod]
public void RationalAllocate()
{
using (var a = HugeRational.Allocate(129, 193))
{
Assert.AreEqual(3, a.NumeratorNumberOfLimbsAllocated());
Assert.AreEqual(0, a.NumeratorNumberOfLimbsUsed());
Assert.AreEqual(4, a.DenominatorNumberOfLimbsAllocated());
Assert.AreEqual(1, a.DenominatorNumberOfLimbsUsed());
Assert.AreEqual("0/1", a.ToString());
}
}
[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());
}
}
[TestMethod]
public void RationalStringConstructor()
{
var n = "5432109876543212345789023245987/362736035870515331128527330659";
using (var a = new HugeRational(n))
{
Assert.AreEqual(2, a.NumeratorNumberOfLimbsUsed());
Assert.AreEqual(n, a.ToString());
}
}
[TestMethod]
public void RationalStringConstructorNumeratorOnly()
{
var n = "5432109876543212345789023245987";
using(var a = new HugeRational(n))
{
Assert.AreEqual(2, a.NumeratorNumberOfLimbsUsed());
Assert.AreEqual(n + "/1", a.ToString());
}
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void RationalStringConstructorInvalid()
{
var a = new HugeRational("12345A");
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void RationalStringConstructorInvalid2()
{
var a = new HugeRational("12345/54321A");
}
[TestMethod]
public void RationalStringConstructorHex()
{
using (var i = new HugeInt("362736035870515331128527330659"))
{
var d = i.ToString(16);
var n = "143210ABCDEF32123457ACDB324598799";
using (var a = new HugeRational(n + "/" + d, 16))
{
Assert.AreEqual(n, a.Numerator.ToString(16));
Assert.AreEqual(3, a.NumeratorNumberOfLimbsUsed());
Assert.AreEqual(i, a.Denominator);
}
}
}
[TestMethod]
public void RationalStringConstructorHexPrefix()
{
using (var i = new HugeInt("362736035870515331128527330659"))
{
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(i, a.Denominator);
}
}
}
[TestMethod]
public void RationalStringAssignmentHexPrefix()
{
using (var i = new HugeInt("362736035870515331128527330659"))
{
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));
}
}
}
[TestMethod]
public void RationalConstructorFromExpression()
{
using (var a = new HugeRational("2340958273409578234095823045723490587/362736035870515331128527330659"))
using (var b = new HugeRational(a + 1))
{
Assert.AreEqual(a + 1, b);
}
}
[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);
}
}
}
}

View File

@ -0,0 +1,186 @@
/*
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 System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MPIR.Tests.HugeRationalTests
{
[TestClass]
public class Conversions
{
[TestMethod]
public void RationalToStringDecimal()
{
var n = "-23429384756298357462983476598345623984756";
var d = "115756986668303657898962467957";
var s = n + "/" + d;
using (var a = new HugeRational(s))
{
Assert.AreEqual(s, a.ToString());
}
}
[TestMethod]
public void RationalToStringHex()
{
var n = "-23429abcdef298357462983fedcba345623984756";
var d = "17607ef654eb9a13ffa163c75";
var s = n + "/" + d;
using (var a = new HugeRational(s, 16))
{
Assert.AreEqual(s, a.ToString(16, true));
Assert.AreEqual(s.ToUpper(), a.ToString(16));
}
}
[TestMethod]
public void RationalFromUlong()
{
using (var a = new HugeRational())
{
ulong b = 0xF84739ABCDEF4876;
ulong d = 12764787846358441471;
a.SetTo(b, d);
Assert.AreEqual(b.ToString() + "/" + d.ToString(), a.ToString());
a.SetTo(b);
Assert.AreEqual(b.ToString() + "/1", a.ToString());
}
}
[TestMethod]
public void RationalFromLong()
{
using (var a = new HugeRational())
{
long b = -0x784739ABCDEF4876;
ulong d = 12764787846358441471;
a.SetTo(b, d);
Assert.AreEqual(b.ToString() + "/" + d.ToString(), a.ToString());
a.SetTo(b);
Assert.AreEqual(b.ToString() + "/1", a.ToString());
}
}
[TestMethod]
public void RationalToAndFromDouble()
{
using (var a = new HugeRational())
{
a.SetTo(-123.25);
double c = a.ToDouble();
Assert.IsTrue(c.Equals(-123.25));
Assert.IsTrue(a.Equals(-123.25));
}
}
[TestMethod]
public void RationalToAndFromFloat()
{
using (var a = new HugeRational())
{
a.SetTo(-123.125f);
double c = a.ToDouble();
Assert.IsTrue(-123.125 == c);
Assert.IsTrue(-123.125 == a);
Assert.IsTrue(-123.125f == a);
}
}
[TestMethod]
public void RationalFromString()
{
using (var a = new HugeRational())
{
var n = "98762934876529834765234123984761/115756986668303657898962467957";
a.SetTo(n);
Assert.AreEqual(n, a.ToString());
n = "-98ABCDEF876529834765234123984761/17607EF654EB9A13FFA163C75";
a.SetTo(n, 16);
Assert.AreEqual(n, a.ToString(16));
n = "-98ABCDEF876529834765234123984761";
a.SetTo(n, 16);
Assert.AreEqual(n + "/1", a.ToString(16));
}
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void RationalFromInvalidString()
{
using (var a = new HugeRational())
{
a.SetTo("12345A");
}
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void RationalFromInvalidString2()
{
using (var a = new HugeRational())
{
a.SetTo("12345/13345A");
}
}
[TestMethod]
public void RationalToStringTruncated()
{
using (var d = new HugeInt("115756986668303657898962467957"))
using (var e = new HugeInt(d ^ 10))
{
var s = e.ToString(10);
var n = string.Concat("123456789".Select(c => new string(c, 30)));
using (var a = new HugeRational(n + "/" + s))
{
Assert.AreEqual(n + "/" + s, a.ToString(10));
Assert.AreEqual("..." + n.Substring(n.Length - 256) + "/..." + s.Substring(s.Length - 256), a.ToString());
a.Value = -a;
Assert.AreEqual("-..." + n.Substring(n.Length - 256) + "/..." + s.Substring(s.Length - 256), a.ToString());
}
}
}
[TestMethod]
public void RationalApproximateSizeInBase()
{
using (var a = new HugeRational("2983475029834750293429834750298347502934298347502983475029342983475029834750293429834750298347502934/115756986668303657898962467957"))
{
AssertBetween(131, 133, a.ApproximateSizeInBase(10));
AssertBetween(429, 429, a.ApproximateSizeInBase(2));
AssertBetween(109, 109, a.ApproximateSizeInBase(16));
AssertBetween(84, 86, a.ApproximateSizeInBase(36));
AssertBetween(74, 76, a.ApproximateSizeInBase(62));
}
}
private void AssertBetween(int min, int max, long actual)
{
Assert.IsTrue(actual >= min && actual <= max, "Expected {0} to {1}, actual {2}", min, max, actual);
}
}
}

View File

@ -0,0 +1,97 @@
/*
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 System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MPIR.Tests.HugeRationalTests
{
[TestClass]
public class ExpressionTests
{
[TestMethod]
public void RationalTestAllExpressions()
{
var baseExpr = typeof(RationalExpression);
var allExpressions =
baseExpr.Assembly.GetTypes()
.Where(x => baseExpr.IsAssignableFrom(x) && !x.IsAbstract)
.ToList();
using (var a = new HugeRational(-9, 1))
using (var b = new HugeRational(4, 1))
using (var c = new HugeInt(3))
using (var r = MpirRandom.Default())
{
var expr = a + (-a * 2) * 3 * (a.Abs() * -2 + -64 + a * a) + 116UL + a;
VerifyPartialResult(r, expr, 44);
expr = expr + a * 5 + (a+b) * (b + 1) * (b + -3) * b + (b * -a) - (b * 25UL) - a + (b << 3) - ((a*b) << 1);
VerifyPartialResult(r, expr, -52);
expr = expr - 2 - 3UL + (b - (a << 1)) + (b * b - 15UL) * (b - a) * (a - 11) * (b - 3UL) - (-340 - a) + (20UL - b);
VerifyPartialResult(r, expr, 52);
expr = expr + (-7 - 2 * a) + (28UL - 4 * b) + -(a + b * 2) + (3 * a).Abs();
VerifyPartialResult(r, expr, 103);
expr = 36 * (expr / a + expr / (3 * b) - a / b) - b / (a + 10) + 6;
VerifyPartialResult(r, expr, -20);
expr = expr + (b >> 1) + ((b / -7L) + (a / 7UL)) * 7 + (7L / a) - (2UL / (b + 5));
VerifyPartialResult(r, expr, -32);
expr = expr + (((a / b).Invert() * 3) ^ 3) - (b + 13) / a / -3;
VerifyPartialResult(r, expr, -35);
expr = expr + c + (b - 2 * c) + (-4 * c - a) - (c - 1) * (b - 1) - (a / c) + (c * 2) / (b - 1);
VerifyPartialResult(r, expr, -38);
MarkExpressionsUsed(allExpressions, expr);
}
Assert.AreEqual(0, allExpressions.Count, "Expression types not exercised: " + string.Join("",
allExpressions.Select(x => Environment.NewLine + x.Name).OrderBy(x => x)));
}
private void VerifyPartialResult(MpirRandom rnd, RationalExpression expr, long expected)
{
rnd.Seed(123);
using (var r = new HugeRational())
{
r.Value = expr;
Assert.AreEqual(expected.ToString() + "/1", r.ToString());
}
}
private void MarkExpressionsUsed(List<Type> allExpressions, RationalExpression expr)
{
var type = expr.GetType();
allExpressions.Remove(type);
var children = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
.Where(x => typeof(RationalExpression).IsAssignableFrom(x.FieldType))
.Select(x => (RationalExpression)x.GetValue(expr))
.Where(x => x != null)
.ToList();
foreach (var childExpr in children)
MarkExpressionsUsed(allExpressions, childExpr);
}
}
}

View File

@ -0,0 +1,205 @@
/*
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 System.IO;
using System.Text;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MPIR.Tests.HugeRationalTests
{
[TestClass]
public class IO
{
[TestMethod]
public void RationalInputOutputRaw()
{
using (var a = new HugeRational("0x10123456789ABCDEF0123456789ABCDEF0123456789ABCDEF/361720912810755408215708460645842859722715865206816237944587"))
using (var b = new HugeRational())
using (var ms = new MemoryStream())
{
a.Value = a ^ 100;
a.Write(ms);
ms.Position = 0;
b.Read(ms);
Assert.AreEqual(a, b);
Assert.AreEqual(ms.Length, ms.Position);
}
}
[TestMethod]
public void RationalInputOutputStr()
{
using (var a = new HugeRational("0x10123456789ABCDEF0123456789ABCDEF0123456789ABCDEF/361720912810755408215708460645842859722715865206816237944587"))
using (var b = new HugeRational())
using (var ms = new MemoryStream())
{
a.Value = a ^ 100;
using (var writer = new StreamWriter(ms, Encoding.UTF8, 1024, true))
a.Write(writer);
ms.Position = 0;
using (var reader = new StreamReader(ms, Encoding.UTF8, false, 1024, true))
b.Read(reader);
Assert.AreEqual(a, b);
Assert.AreEqual(ms.Length, ms.Position);
}
}
[TestMethod]
public void RationalInputOutputStrHex()
{
using (var a = new HugeRational("0x10123456789ABCDEF0123456789ABCDEF0123456789ABCDEF/361720912810755408215708460645842859722715865206816237944587"))
using (var b = new HugeRational())
using (var ms = new MemoryStream())
{
a.Value = a ^ 100;
using (var writer = new StreamWriter(ms, Encoding.UTF8, 1024, true))
a.Write(writer, 16);
ms.Position = 0;
using (var reader = new StreamReader(ms, Encoding.UTF8, false, 1024, true))
b.Read(reader, 16);
Assert.AreEqual(a, b);
Assert.AreEqual(ms.Length, ms.Position);
Assert.AreEqual((char)0xFEFF + a.ToString(16), Encoding.UTF8.GetString(ms.ToArray()));
}
}
[TestMethod]
public void RationalInputOutputStrHexPrefix()
{
using (var a = new HugeRational("0x10123456789ABCDEF0123456789ABCDEF0123456789ABCDEF/361720912810755408215708460645842859722715865206816237944587"))
using (var b = new HugeRational())
using (var ms = new MemoryStream())
{
a.Value = a ^ 100;
using (var writer = new StreamWriter(ms, Encoding.UTF8, 1024, true))
{
writer.Write("0x");
a.Numerator.Write(writer, 16);
writer.Write("/0");
a.Denominator.Write(writer, 8);
}
ms.Position = 0;
using (var reader = new StreamReader(ms, Encoding.UTF8, false, 1024, true))
b.Read(reader);
Assert.AreEqual(a, b);
Assert.AreEqual(ms.Length, ms.Position);
}
}
[TestMethod]
public void RationalInputOutputStrHexLower()
{
using (var a = new HugeRational("0x10123456789ABCDEF0123456789ABCDEF0123456789ABCDEF/361720912810755408215708460645842859722715865206816237944587"))
using (var b = new HugeRational())
using (var ms = new MemoryStream())
{
a.Value = a ^ 100;
using (var writer = new StreamWriter(ms, Encoding.UTF8, 1024, true))
a.Write(writer, 16, true);
ms.Position = 0;
using (var reader = new StreamReader(ms, Encoding.UTF8, false, 1024, true))
b.Read(reader, 16);
Assert.AreEqual(a, b);
Assert.AreEqual(ms.Length, ms.Position);
}
}
[TestMethod]
public void RationalInputOutputStrOctal()
{
using (var a = new HugeRational("0x10123456789ABCDEF0123456789ABCDEF0123456789ABCDEF/361720912810755408215708460645842859722715865206816237944587"))
using (var b = new HugeRational())
using (var ms = new MemoryStream())
{
a.Value = a ^ 100;
using (var writer = new StreamWriter(ms, Encoding.UTF8, 1024, true))
a.Write(writer, 8);
ms.Position = 0;
using (var reader = new StreamReader(ms, Encoding.UTF8, false, 1024, true))
b.Read(reader, 8);
Assert.AreEqual(a, b);
Assert.AreEqual(ms.Length, ms.Position);
Assert.AreEqual((char)0xFEFF + a.ToString(8), Encoding.UTF8.GetString(ms.ToArray()));
}
}
[TestMethod]
public void RationalInputOutputStrBinary()
{
using (var a = new HugeRational("0x10123456789ABCDEF0123456789ABCDEF0123456789ABCDEF/361720912810755408215708460645842859722715865206816237944587"))
using (var b = new HugeRational())
using (var ms = new MemoryStream())
{
a.Value = a ^ 100;
using (var writer = new StreamWriter(ms, Encoding.UTF8, 1024, true))
a.Write(writer, 2);
ms.Position = 0;
using (var reader = new StreamReader(ms, Encoding.UTF8, false, 1024, true))
b.Read(reader, 2);
Assert.AreEqual(a, b);
Assert.AreEqual(ms.Length, ms.Position);
Assert.AreEqual((char)0xFEFF + a.ToString(2), Encoding.UTF8.GetString(ms.ToArray()));
}
}
[TestMethod]
public void RationalInputOutputStr62()
{
using (var a = new HugeRational("0x10123456789ABCDEF0123456789ABCDEF0123456789ABCDEF/361720912810755408215708460645842859722715865206816237944587"))
using (var b = new HugeRational())
using (var ms = new MemoryStream())
{
a.Value = a ^ 100;
using (var writer = new StreamWriter(ms, Encoding.UTF8, 1024, true))
a.Write(writer, 62);
ms.Position = 0;
using (var reader = new StreamReader(ms, Encoding.UTF8, false, 1024, true))
b.Read(reader, 62);
Assert.AreEqual(a, b);
Assert.AreEqual(ms.Length, ms.Position);
Assert.AreEqual((char)0xFEFF + a.ToString(62), Encoding.UTF8.GetString(ms.ToArray()));
}
}
//more tests coming here
}
}