2014-03-04 21:08:50 -05: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;
|
2014-04-03 12:47:40 -04:00
|
|
|
using System.Linq;
|
2014-03-04 21:08:50 -05:00
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
|
|
|
|
namespace MPIR.Tests.HugeIntTests
|
|
|
|
{
|
|
|
|
[TestClass]
|
2014-03-23 15:08:30 -04:00
|
|
|
public class Conversions
|
2014-03-04 21:08:50 -05:00
|
|
|
{
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntToStringDecimal()
|
2014-03-04 21:08:50 -05:00
|
|
|
{
|
|
|
|
var n = "-23429384756298357462983476598345623984756";
|
|
|
|
using (var a = new HugeInt(n))
|
|
|
|
{
|
|
|
|
Assert.AreEqual(n, a.ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntToStringHex()
|
2014-03-04 21:08:50 -05:00
|
|
|
{
|
|
|
|
var n = "-23429abcdef298357462983fedcba345623984756";
|
|
|
|
using (var a = new HugeInt(n, 16))
|
|
|
|
{
|
2014-04-02 13:31:46 -04:00
|
|
|
Assert.AreEqual(n, a.ToString(16, true));
|
|
|
|
Assert.AreEqual(n.ToUpper(), a.ToString(16));
|
2014-03-04 21:08:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-23 15:08:30 -04:00
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntToAndFromUlong()
|
2014-03-23 15:08:30 -04:00
|
|
|
{
|
|
|
|
using (var a = new HugeInt())
|
|
|
|
{
|
|
|
|
ulong b = 0xF84739ABCDEF4876;
|
2014-03-23 23:29:30 -04:00
|
|
|
a.SetTo(b);
|
2014-03-23 15:08:30 -04:00
|
|
|
Assert.AreEqual(b.ToString(), a.ToString());
|
|
|
|
|
|
|
|
a.Value = -a;
|
2014-03-23 23:29:30 -04:00
|
|
|
ulong c = a.ToUlong();
|
2014-03-23 15:08:30 -04:00
|
|
|
Assert.AreEqual(b.ToString(), c.ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-23 15:11:57 -04:00
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntToAndFromLong()
|
2014-03-23 15:11:57 -04:00
|
|
|
{
|
|
|
|
using (var a = new HugeInt())
|
|
|
|
{
|
|
|
|
long b = -0x784739ABCDEF4876;
|
2014-03-23 23:29:30 -04:00
|
|
|
a.SetTo(b);
|
2014-03-23 15:11:57 -04:00
|
|
|
Assert.AreEqual(b.ToString(), a.ToString());
|
|
|
|
|
2014-03-23 23:29:30 -04:00
|
|
|
long c = a.ToLong();
|
2014-03-23 15:11:57 -04:00
|
|
|
Assert.AreEqual(b.ToString(), c.ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-23 17:37:00 -04:00
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntToAndFromDouble()
|
2014-03-23 17:37:00 -04:00
|
|
|
{
|
|
|
|
using (var a = new HugeInt())
|
|
|
|
using (var lo = new HugeInt())
|
|
|
|
using (var hi = new HugeInt())
|
|
|
|
{
|
2014-03-23 23:29:30 -04:00
|
|
|
a.SetTo(-123.45e20);
|
2014-03-23 17:37:00 -04:00
|
|
|
lo.Value = (a/10000000000).Rounding(RoundingModes.Floor);
|
|
|
|
hi.Value = (a/10000000000).Rounding(RoundingModes.Ceiling);
|
|
|
|
|
|
|
|
Assert.IsTrue(lo.ToString() == "-1234500000000" || hi.ToString() == "-1234500000000");
|
|
|
|
|
2014-03-23 23:29:30 -04:00
|
|
|
double c = a.ToDouble();
|
2014-03-23 17:37:00 -04:00
|
|
|
Assert.AreEqual(-123.45e20, c);
|
2014-03-24 00:04:42 -04:00
|
|
|
|
|
|
|
long exp;
|
|
|
|
a.Value = a + a;
|
|
|
|
c = a.ToDouble(out exp);
|
2014-04-03 12:47:40 -04:00
|
|
|
|
2014-03-24 00:04:42 -04:00
|
|
|
Assert.AreEqual(75, exp);
|
2014-04-03 12:47:40 -04:00
|
|
|
c *= System.Math.Pow(2, exp);
|
|
|
|
|
|
|
|
Assert.IsTrue(a + 10000000000 >= c);
|
|
|
|
Assert.IsTrue(a - 10000000000 <= c);
|
2014-03-23 17:37:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-23 23:39:32 -04:00
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntFromString()
|
2014-03-23 23:39:32 -04:00
|
|
|
{
|
|
|
|
using (var a = new HugeInt())
|
|
|
|
{
|
|
|
|
var n = "98762934876529834765234123984761";
|
|
|
|
a.SetTo(n);
|
|
|
|
Assert.AreEqual(n, a.ToString());
|
|
|
|
|
|
|
|
n = "-98ABCDEF876529834765234123984761";
|
|
|
|
a.SetTo(n, 16);
|
2014-04-02 13:31:46 -04:00
|
|
|
Assert.AreEqual(n, a.ToString(16));
|
2014-03-23 23:39:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
[ExpectedException(typeof(ArgumentException))]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntFromInvalidString()
|
2014-03-23 23:39:32 -04:00
|
|
|
{
|
|
|
|
using (var a = new HugeInt())
|
|
|
|
{
|
|
|
|
a.SetTo("12345A");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-03 12:47:40 -04:00
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntToStringTruncated()
|
2014-04-03 12:47:40 -04:00
|
|
|
{
|
|
|
|
var n = string.Concat("123456789".Select(c => new string(c, 30)));
|
|
|
|
using (var a = new HugeInt(n))
|
|
|
|
{
|
|
|
|
Assert.AreEqual(n, a.ToString(10));
|
|
|
|
Assert.AreEqual("..." + n.Substring(n.Length - 256), a.ToString());
|
|
|
|
a.Value = -a;
|
|
|
|
Assert.AreEqual("-..." + n.Substring(n.Length - 256), a.ToString());
|
|
|
|
}
|
|
|
|
}
|
2014-04-06 13:28:13 -04:00
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntFitsUlong()
|
2014-04-06 13:28:13 -04:00
|
|
|
{
|
2014-05-20 13:05:15 -04:00
|
|
|
using (var a = new HugeInt(ulong.MaxValue))
|
2014-04-06 13:28:13 -04:00
|
|
|
{
|
|
|
|
Assert.IsTrue(a.FitsUlong());
|
|
|
|
a.Value = a + 1;
|
|
|
|
Assert.IsFalse(a.FitsUlong());
|
|
|
|
a.SetTo(0);
|
|
|
|
Assert.IsTrue(a.FitsUlong());
|
|
|
|
a.Value = a - 1;
|
|
|
|
Assert.IsFalse(a.FitsUlong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntFitsLong()
|
2014-04-06 13:28:13 -04:00
|
|
|
{
|
2014-05-20 13:05:15 -04:00
|
|
|
using (var a = new HugeInt(long.MaxValue))
|
2014-04-06 13:28:13 -04:00
|
|
|
{
|
|
|
|
Assert.IsTrue(a.FitsLong());
|
|
|
|
a.Value = a + 1;
|
|
|
|
Assert.IsFalse(a.FitsLong());
|
|
|
|
a.SetTo(long.MinValue);
|
|
|
|
Assert.IsTrue(a.FitsLong());
|
|
|
|
a.Value = a - 1;
|
|
|
|
Assert.IsFalse(a.FitsLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntFitsUint()
|
2014-04-06 13:28:13 -04:00
|
|
|
{
|
2014-05-20 13:05:15 -04:00
|
|
|
using (var a = new HugeInt(uint.MaxValue))
|
2014-04-06 13:28:13 -04:00
|
|
|
{
|
|
|
|
Assert.IsTrue(a.FitsUint());
|
|
|
|
a.Value = a + 1;
|
|
|
|
Assert.IsFalse(a.FitsUint());
|
|
|
|
a.SetTo(0);
|
|
|
|
Assert.IsTrue(a.FitsUint());
|
|
|
|
a.Value = a - 1;
|
|
|
|
Assert.IsFalse(a.FitsUint());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntFitsInt()
|
2014-04-06 13:28:13 -04:00
|
|
|
{
|
2014-05-20 13:05:15 -04:00
|
|
|
using (var a = new HugeInt(int.MaxValue))
|
2014-04-06 13:28:13 -04:00
|
|
|
{
|
|
|
|
Assert.IsTrue(a.FitsInt());
|
|
|
|
a.Value = a + 1;
|
|
|
|
Assert.IsFalse(a.FitsInt());
|
|
|
|
a.SetTo(int.MinValue);
|
|
|
|
Assert.IsTrue(a.FitsInt());
|
|
|
|
a.Value = a - 1;
|
|
|
|
Assert.IsFalse(a.FitsInt());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntFitsUshort()
|
2014-04-06 13:28:13 -04:00
|
|
|
{
|
2014-05-20 13:05:15 -04:00
|
|
|
using (var a = new HugeInt(ushort.MaxValue))
|
2014-04-06 13:28:13 -04:00
|
|
|
{
|
|
|
|
Assert.IsTrue(a.FitsUshort());
|
|
|
|
a.Value = a + 1;
|
|
|
|
Assert.IsFalse(a.FitsUshort());
|
|
|
|
a.SetTo(0);
|
|
|
|
Assert.IsTrue(a.FitsUshort());
|
|
|
|
a.Value = a - 1;
|
|
|
|
Assert.IsFalse(a.FitsUshort());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntFitsShort()
|
2014-04-06 13:28:13 -04:00
|
|
|
{
|
2014-05-20 13:05:15 -04:00
|
|
|
using (var a = new HugeInt(short.MaxValue))
|
2014-04-06 13:28:13 -04:00
|
|
|
{
|
|
|
|
Assert.IsTrue(a.FitsShort());
|
|
|
|
a.Value = a + 1;
|
|
|
|
Assert.IsFalse(a.FitsShort());
|
|
|
|
a.SetTo(short.MinValue);
|
|
|
|
Assert.IsTrue(a.FitsShort());
|
|
|
|
a.Value = a - 1;
|
|
|
|
Assert.IsFalse(a.FitsShort());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
2014-05-20 15:37:16 -04:00
|
|
|
public void IntApproximateSizeInBase()
|
2014-04-06 13:28:13 -04:00
|
|
|
{
|
|
|
|
using (var a = new HugeInt("2983475029834750293429834750298347502934298347502983475029342983475029834750293429834750298347502934"))
|
|
|
|
{
|
|
|
|
AssertEither(100, 101, a.ApproximateSizeInBase(10));
|
2014-05-21 11:47:51 -04:00
|
|
|
AssertEither(331, 331, a.ApproximateSizeInBase(2));
|
|
|
|
AssertEither(83, 83, a.ApproximateSizeInBase(16));
|
2014-04-06 13:28:13 -04:00
|
|
|
AssertEither(64, 65, a.ApproximateSizeInBase(36));
|
|
|
|
AssertEither(56, 57, a.ApproximateSizeInBase(62));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AssertEither(int expected1, int expected2, long actual)
|
|
|
|
{
|
|
|
|
Assert.IsTrue(actual == expected1 || actual == expected2, "Expected {0} or {1}, actual {2}", expected1, expected2, actual);
|
|
|
|
}
|
2014-03-04 21:08:50 -05:00
|
|
|
}
|
|
|
|
}
|