Float size checks. Three broken tests due to MPIR behavior, bug logged

This commit is contained in:
Alex Dyachenko 2014-06-27 10:54:19 -04:00
parent 35a2222d95
commit cd25b5b35c
2 changed files with 194 additions and 0 deletions

View File

@ -26,6 +26,14 @@ namespace MPIR.Tests.HugeFloatTests
[TestClass]
public class Conversions
{
private static HugeFloat AlmostOne;
[ClassInitialize]
public static void Init(TestContext context)
{
AlmostOne = new HugeFloat(0.99999);
}
[TestMethod]
public void FloatToStringDecimal()
{
@ -200,5 +208,155 @@ namespace MPIR.Tests.HugeFloatTests
}
}
}
[TestMethod]
public void FloatFitsLong()
{
using (var a = new HugeFloat(long.MaxValue))
using (var b = new HugeFloat())
{
Assert.IsTrue(a.FitsLong());
b.Value = a + AlmostOne;
Assert.IsTrue(b.FitsLong());
b.Value = a + 1;
Assert.IsFalse(b.FitsLong());
a.SetTo(long.MinValue);
Assert.IsTrue(a.FitsLong());
b.Value = a - AlmostOne;
Assert.IsTrue(b.FitsLong());
b.Value = a - 1;
Assert.IsFalse(b.FitsLong());
}
}
[TestMethod]
public void FloatFitsUlong()
{
using(var a = new HugeFloat(ulong.MaxValue))
using(var b = new HugeFloat())
{
Assert.IsTrue(a.FitsUlong());
b.Value = a + AlmostOne;
Assert.IsTrue(b.FitsUlong());
b.Value = a + 1;
Assert.IsFalse(b.FitsUlong());
a.SetTo(ulong.MinValue);
Assert.IsTrue(a.FitsUlong());
b.Value = a - AlmostOne;
Assert.IsTrue(b.FitsUlong());
b.Value = a - 1;
Assert.IsFalse(b.FitsUlong());
}
}
[TestMethod]
public void FloatFitsInt()
{
using(var a = new HugeFloat(int.MaxValue))
using(var b = new HugeFloat())
{
Assert.IsTrue(a.FitsInt());
b.Value = a + AlmostOne;
Assert.IsTrue(b.FitsInt());
b.Value = a + 1;
Assert.IsFalse(b.FitsInt());
a.SetTo(int.MinValue);
Assert.IsTrue(a.FitsInt());
b.Value = a - AlmostOne;
Assert.IsTrue(b.FitsInt());
b.Value = a - 1;
Assert.IsFalse(b.FitsInt());
}
}
[TestMethod]
public void FloatFitsUint()
{
using(var a = new HugeFloat(uint.MaxValue))
using(var b = new HugeFloat())
{
Assert.IsTrue(a.FitsUint());
b.Value = a + AlmostOne;
Assert.IsTrue(b.FitsUint());
b.Value = a + 1;
Assert.IsFalse(b.FitsUint());
a.SetTo(uint.MinValue);
Assert.IsTrue(a.FitsUint());
b.Value = a - AlmostOne;
Assert.IsTrue(b.FitsUint());
b.Value = a - 1;
Assert.IsFalse(b.FitsUint());
}
}
[TestMethod]
public void FloatFitsShort()
{
using(var a = new HugeFloat(short.MaxValue))
using(var b = new HugeFloat())
{
Assert.IsTrue(a.FitsShort());
b.Value = a + AlmostOne;
Assert.IsTrue(b.FitsShort());
b.Value = a + 1;
Assert.IsFalse(b.FitsShort());
a.SetTo(short.MinValue);
Assert.IsTrue(a.FitsShort());
b.Value = a - AlmostOne;
Assert.IsTrue(b.FitsShort());
b.Value = a - 1;
Assert.IsFalse(b.FitsShort());
}
}
[TestMethod]
public void FloatFitsUshort()
{
using(var a = new HugeFloat(ushort.MaxValue))
using(var b = new HugeFloat())
{
Assert.IsTrue(a.FitsUshort());
b.Value = a + AlmostOne;
Assert.IsTrue(b.FitsUshort());
b.Value = a + 1;
Assert.IsFalse(b.FitsUshort());
a.SetTo(ushort.MinValue);
Assert.IsTrue(a.FitsUshort());
b.Value = a - AlmostOne;
Assert.IsTrue(b.FitsUshort());
b.Value = a - 1;
Assert.IsFalse(b.FitsUshort());
}
}
}
}

View File

@ -1187,6 +1187,42 @@ namespace MPIR
_allocatedPrecision = precision;
}
/// <summary>
/// Returns true if the value of the source number, if truncated to an integer, is in the long range.
/// </summary>
/// <returns>true if the value will fit in a long</returns>
bool FitsLong() { return MP(fits_si_p)(_value) != 0; }
/// <summary>
/// Returns true if the value of the source number, if truncated to an integer, is in the ulong range.
/// </summary>
/// <returns>true if the value will fit in a long</returns>
bool FitsUlong() { return MP(fits_ui_p)(_value) != 0; }
/// <summary>
/// Returns true if the value of the source number, if truncated to an integer, is in the int range.
/// </summary>
/// <returns>true if the value will fit in an int</returns>
bool FitsInt() { return MP(fits_sint_p)(_value) != 0; }
/// <summary>
/// Returns true if the value of the source number, if truncated to an integer, is in the uint range.
/// </summary>
/// <returns>true if the value will fit in an int</returns>
bool FitsUint() { return MP(fits_uint_p)(_value) != 0; }
/// <summary>
/// Returns true if the value of the source number, if truncated to an integer, is in the short range.
/// </summary>
/// <returns>true if the value will fit in a short</returns>
bool FitsShort() { return MP(fits_sshort_p)(_value) != 0; }
/// <summary>
/// Returns true if the value of the source number, if truncated to an integer, is in the ushort range.
/// </summary>
/// <returns>true if the value will fit in a short</returns>
bool FitsUshort() { return MP(fits_ushort_p)(_value) != 0; }
#pragma endregion
#pragma region IO