Added several constants to MpirSettings and unit tests for all MpirSettings members

This commit is contained in:
Alex Dyachenko 2016-01-12 09:26:23 -05:00
parent 6da13eeb87
commit 3a013867a1
7 changed files with 149 additions and 7 deletions

View File

@ -172,8 +172,11 @@
<Compile Include="..\..\..\mpir.net\mpir.net-tests\IntegrationTests\XmlCommentsTests.cs"> <Compile Include="..\..\..\mpir.net\mpir.net-tests\IntegrationTests\XmlCommentsTests.cs">
<Link>IntegrationTests\XmlCommentsTests.cs</Link> <Link>IntegrationTests\XmlCommentsTests.cs</Link>
</Compile> </Compile>
<Compile Include="..\..\..\mpir.net\mpir.net-tests\RandomTests\Random.cs"> <Compile Include="..\..\..\mpir.net\mpir.net-tests\OtherTests\MpirSettings.cs">
<Link>RandomTests\Random.cs</Link> <Link>OtherTests\MpirSettings.cs</Link>
</Compile>
<Compile Include="..\..\..\mpir.net\mpir.net-tests\OtherTests\Random.cs">
<Link>OtherTests\Random.cs</Link>
</Compile> </Compile>
<Compile Include="..\..\..\mpir.net\mpir.net-tests\Utilities\Accessors.cs"> <Compile Include="..\..\..\mpir.net\mpir.net-tests\Utilities\Accessors.cs">
<Link>Utilities\Accessors.cs</Link> <Link>Utilities\Accessors.cs</Link>

View File

@ -172,8 +172,11 @@
<Compile Include="..\..\..\mpir.net\mpir.net-tests\IntegrationTests\XmlCommentsTests.cs"> <Compile Include="..\..\..\mpir.net\mpir.net-tests\IntegrationTests\XmlCommentsTests.cs">
<Link>IntegrationTests\XmlCommentsTests.cs</Link> <Link>IntegrationTests\XmlCommentsTests.cs</Link>
</Compile> </Compile>
<Compile Include="..\..\..\mpir.net\mpir.net-tests\RandomTests\Random.cs"> <Compile Include="..\..\..\mpir.net\mpir.net-tests\OtherTests\MpirSettings.cs">
<Link>RandomTests\Random.cs</Link> <Link>OtherTests\MpirSettings.cs</Link>
</Compile>
<Compile Include="..\..\..\mpir.net\mpir.net-tests\OtherTests\Random.cs">
<Link>OtherTests\Random.cs</Link>
</Compile> </Compile>
<Compile Include="..\..\..\mpir.net\mpir.net-tests\Utilities\Accessors.cs"> <Compile Include="..\..\..\mpir.net\mpir.net-tests\Utilities\Accessors.cs">
<Link>Utilities\Accessors.cs</Link> <Link>Utilities\Accessors.cs</Link>

View File

@ -173,8 +173,11 @@
<Compile Include="..\..\..\mpir.net\mpir.net-tests\IntegrationTests\XmlCommentsTests.cs"> <Compile Include="..\..\..\mpir.net\mpir.net-tests\IntegrationTests\XmlCommentsTests.cs">
<Link>IntegrationTests\XmlCommentsTests.cs</Link> <Link>IntegrationTests\XmlCommentsTests.cs</Link>
</Compile> </Compile>
<Compile Include="..\..\..\mpir.net\mpir.net-tests\RandomTests\Random.cs"> <Compile Include="..\..\..\mpir.net\mpir.net-tests\OtherTests\MpirSettings.cs">
<Link>RandomTests\Random.cs</Link> <Link>OtherTests\MpirSettings.cs</Link>
</Compile>
<Compile Include="..\..\..\mpir.net\mpir.net-tests\OtherTests\Random.cs">
<Link>OtherTests\Random.cs</Link>
</Compile> </Compile>
<Compile Include="..\..\..\mpir.net\mpir.net-tests\Utilities\Accessors.cs"> <Compile Include="..\..\..\mpir.net\mpir.net-tests\Utilities\Accessors.cs">
<Link>Utilities\Accessors.cs</Link> <Link>Utilities\Accessors.cs</Link>

View File

@ -34,7 +34,7 @@ namespace MPIR.Tests.HugeIntTests
[TestClass] [TestClass]
public class XmlCommentsTests public class XmlCommentsTests
{ {
private static readonly string[] ValidMemberTypePrefixes = { "M:", "P:", "F:" }; private static readonly string[] ValidMemberTypePrefixes = { "M:", "P:", "F:", "C:" };
[TestMethod] [TestMethod]
public void TestComments() public void TestComments()

View File

@ -0,0 +1,104 @@
/*
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.RandomTests
{
[TestClass]
public class MpirSettingsTests
{
[TestMethod]
public void BitsPerLimb()
{
unsafe
{
Assert.AreEqual(8 * sizeof(IntPtr), MpirSettings.BITS_PER_LIMB);
Assert.AreEqual(8 * sizeof(IntPtr), MpirSettings.USABLE_BITS_PER_LIMB);
Assert.AreEqual(0, MpirSettings.NAIL_BITS_PER_LIMB);
}
}
[TestMethod]
public void Version()
{
Assert.AreEqual("5.1.3", MpirSettings.GMP_VERSION.ToString());
Assert.AreEqual("2.7.2", MpirSettings.MPIR_VERSION.ToString());
}
[TestMethod]
public void ToStringDigits()
{
const string strA = "9520398475029834502983475028934705982734095827304958723409758230498573034928750938475987498473958743";
using (var a = new HugeInt(strA))
{
Assert.AreEqual(256, MpirSettings.ToStringDigits);
Assert.AreEqual(strA, a.ToString());
MpirSettings.ToStringDigits = 32;
Assert.AreEqual("..." + strA.Substring(strA.Length - 32), a.ToString());
MpirSettings.ToStringDigits = 256;
Assert.AreEqual(strA, a.ToString());
}
}
[TestMethod]
public void RoundingMode()
{
const string strA = "9520398475029834502983475028934705982734095827304958723409758230498573034928750938475987498473958743";
var down = strA.Substring(0, strA.Length - 1);
var up = strA.Substring(0, strA.Length - 2) + (char) (strA[strA.Length - 2] + 1);
using (var a = new HugeInt(strA))
using (var b = new HugeInt())
{
Assert.AreEqual(RoundingModes.Truncate, MpirSettings.RoundingMode);
b.Value = a / 10;
Assert.AreEqual(down, b.ToString());
b.Value = -a / 10;
Assert.AreEqual("-" + down, b.ToString());
MpirSettings.RoundingMode = RoundingModes.Default;
b.Value = a / 10;
Assert.AreEqual(down, b.ToString());
b.Value = -a / 10;
Assert.AreEqual("-" + down, b.ToString());
MpirSettings.RoundingMode = RoundingModes.Floor;
b.Value = a / 10;
Assert.AreEqual(down, b.ToString());
b.Value = -a / 10;
Assert.AreEqual("-" + up, b.ToString());
MpirSettings.RoundingMode = RoundingModes.Ceiling;
b.Value = a / 10;
Assert.AreEqual(up, b.ToString());
b.Value = -a / 10;
Assert.AreEqual("-" + down, b.ToString());
MpirSettings.RoundingMode = RoundingModes.Truncate;
}
}
}
}

View File

@ -1019,6 +1019,35 @@ namespace MPIR
static MPTYPE^ _toStringModulo; static MPTYPE^ _toStringModulo;
public: public:
#define _GMP_VERSION __GNU_MP_VERSION + "." + __GNU_MP_VERSION_MINOR + "." + __GNU_MP_VERSION_PATCHLEVEL
#undef GMP_VERSION
/// <summary>
/// Represents the total number of bits in a single MPIR limb, including data bits and nail bits
/// </summary>
literal int BITS_PER_LIMB = BITS_PER_UI;
/// <summary>
/// Represents the number of nail bits in a single MPIR limb. Nail bits are used internally by MPIR
/// </summary>
literal int NAIL_BITS_PER_LIMB = GMP_NAIL_BITS;
/// <summary>
/// Represents the number of data bits in a single MPIR limb
/// </summary>
literal int USABLE_BITS_PER_LIMB = GMP_NUMB_BITS;
/// <summary>
/// Represents the version of GMP with which the underlying MPIR library is compatible
/// </summary>
static initonly const Version^ GMP_VERSION = gcnew Version(_GMP_VERSION);
/// <summary>
/// Represents the version of the underlying MPIR library
/// </summary>
static initonly const Version^ MPIR_VERSION = gcnew Version(_MSC_MPIR_VERSION);
/// <summary> /// <summary>
/// Gets or sets the default rounding mode used for MPIR division operations that don't explicitly specify a rounding mode. /// Gets or sets the default rounding mode used for MPIR division operations that don't explicitly specify a rounding mode.
/// <para>Defaults to Truncate. /// <para>Defaults to Truncate.