public void FloatPriorTest() { // edges Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatPrior(double.NaN)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatPrior(double.PositiveInfinity)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatPrior(double.NegativeInfinity)); Assert.AreEqual(double.NegativeInfinity, Math2.FloatPrior(double.MinValue)); Assert.AreEqual(-double.Epsilon, Math2.FloatPrior(-0.0), 1); // -0.0, double.Epsilon foreach (var item in _FloatData) { // swap result and x if (item.Distance != 1) { continue; } double x = item.Result; double expected = item.X; double actual = Math2.FloatPrior(x); Assert2.AreNear(expected, actual, 0, () => { return(string.Format("FloatPrior({0:E16}) = {1:E16}; got {2:E16}; ulps = {3}", x, expected, actual, 0)); }); } }
public void TestForceAdd() { ThreadPoolGlobalQueue col = new ThreadPoolGlobalQueue(100); for (int i = 0; i < 100; i++) { col.Add(new TestThreadPoolItem(i)); } Assert.AreEqual(100, col.OccupiedNodesCount); Assert.IsFalse(col.TryAdd(new TestThreadPoolItem(int.MaxValue), 0)); col.ForceAdd(new TestThreadPoolItem(100)); Assert.AreEqual(101, col.OccupiedNodesCount); Assert.AreEqual(100, col.BoundedCapacity); Assert.IsFalse(col.TryAdd(new TestThreadPoolItem(int.MaxValue), 0)); Assert2.AreEqual(0, col.Take()); Assert.AreEqual(100, col.OccupiedNodesCount); Assert.AreEqual(100, col.BoundedCapacity); Assert.IsFalse(col.TryAdd(new TestThreadPoolItem(int.MaxValue), 0)); for (int i = 1; i < 101; i++) { Assert2.AreEqual(i, col.Take()); } }
public void RisingFactorialTest() { Assert2.AreEqual <Exceptions.PoleException>(double.NaN, () => Math2.FactorialRising(1, -1)); // Our BigInteger routines don't return denorms TestCase2[] intdata = GetRisingFactorialIntData(); TestCaseSet <double, int>[] testCasesND = { new TestCaseSet <double, int>(intdata, "Integer Data", 280), }; NumericFunctionTest.RunSet(RisingFactorialNoDenorm, "FactorialRising", testCasesND); TestCaseSet <double, int>[] testCases = { new TestCaseSet <double, int>(_RisingFactorialData, "Spots", 60), }; NumericFunctionTest.RunSet(Math2.FactorialRising, "FactorialRising", testCases); }
public void TestMoveFromLocalQueueToGlobal() { ThreadPoolQueueController q = new ThreadPoolQueueController(100, 50); ThreadPoolLocalQueue local = new ThreadPoolLocalQueue(); for (int i = 0; i < 100; i++) { q.Add(new TestThreadPoolItem(i), null); } Assert.AreEqual(100, q.GlobalQueue.OccupiedNodesCount); Assert.AreEqual(0, q.GlobalQueue.FreeNodesCount); for (int i = 100; i < 110; i++) { Assert.IsTrue(local.TryAddLocal(new TestThreadPoolItem(i))); } q.MoveItemsFromLocalQueueToGlobal(local); Assert.AreEqual(0, q.GlobalQueue.FreeNodesCount); Assert.AreEqual(110, q.GlobalQueue.OccupiedNodesCount); Assert.AreEqual(110, q.GlobalQueue.ExtendedCapacity); for (int i = 0; i < 110; i++) { Assert2.AreEqual(i, q.Take(null)); } Assert.AreEqual(100, q.GlobalQueue.FreeNodesCount); Assert.AreEqual(0, q.GlobalQueue.OccupiedNodesCount); Assert.AreEqual(100, q.GlobalQueue.ExtendedCapacity); }
public void TestSilentTakeCancellation() { ThreadPoolQueueController q = new ThreadPoolQueueController(100, 1000); CancellationTokenSource cSrc = new CancellationTokenSource(); bool takeCompleted = false; bool takeResult = false; ThreadPoolWorkItem takenItem = null; int startedFlag = 0; Task.Run(() => { Interlocked.Exchange(ref startedFlag, 1); var res = q.TryTake(null, out takenItem, -1, cSrc.Token, false); Volatile.Write(ref takeResult, res); Volatile.Write(ref takeCompleted, true); }); TimingAssert.IsTrue(5000, () => Volatile.Read(ref startedFlag) == 1); Thread.Sleep(100); Assert.IsFalse(takeCompleted); cSrc.Cancel(); TimingAssert.IsTrue(5000, () => Volatile.Read(ref takeCompleted)); Assert.IsFalse(takeResult); Assert.IsTrue(q.TryAdd(new TestThreadPoolItem(1), null, false, 0, CancellationToken.None)); Assert2.AreEqual(1, q.Take(null)); Assert.AreEqual(0, q.GlobalQueue.OccupiedNodesCount); Assert.AreEqual(100, q.GlobalQueue.FreeNodesCount); }
public void TestSimpleAddTake() { ThreadPoolGlobalQueue q = new ThreadPoolGlobalQueue(100); Assert.IsTrue(q.TryAdd(new TestThreadPoolItem(10), 0, CancellationToken.None)); ThreadPoolWorkItem res = null; Assert.IsTrue(q.TryTake(out res, 0, CancellationToken.None, true)); Assert2.AreEqual(10, res); }
public void TestSimpleAddTake() { ThreadPoolQueueController q = new ThreadPoolQueueController(100, 1000); Assert.IsTrue(q.TryAdd(new TestThreadPoolItem(10), null, false, 0, CancellationToken.None)); ThreadPoolWorkItem res = null; Assert.IsTrue(q.TryTake(null, out res, 0, CancellationToken.None, true)); Assert2.AreEqual(10, res); }
public void TestSingleAddTake() { ThreadPoolConcurrentQueue q = new ThreadPoolConcurrentQueue(); q.Add(new TestThreadPoolItem(10)); ThreadPoolWorkItem res = null; Assert.IsTrue(q.TryTake(out res)); Assert.IsNotNull(res); Assert2.AreEqual(10, res); }
public void TestSingleAddSteal() { ThreadPoolLocalQueue q = new ThreadPoolLocalQueue(); Assert.IsTrue(q.TryAddLocal(new TestThreadPoolItem(1))); ThreadPoolWorkItem item = null; Assert.IsTrue(q.TrySteal(out item)); Assert.IsNotNull(item); Assert2.AreEqual(1, item); }
public void TestExtensionWork() { ThreadPoolQueueController q = new ThreadPoolQueueController(100, 1000); Assert.AreEqual(100, q.GlobalQueue.ExtendedCapacity); for (int i = 0; i < 100; i++) { q.Add(new TestThreadPoolItem(i), null); } bool addCompleted = false; int startedFlag = 0; Task.Run(() => { Interlocked.Exchange(ref startedFlag, 1); q.Add(new TestThreadPoolItem(100), null); Volatile.Write(ref addCompleted, true); }); TimingAssert.IsTrue(5000, () => Volatile.Read(ref startedFlag) == 1); Thread.Sleep(100); Assert.IsFalse(addCompleted); q.ExtendGlobalQueueCapacity(100); TimingAssert.IsTrue(5000, () => Volatile.Read(ref addCompleted)); Assert.AreEqual(200, q.GlobalQueue.ExtendedCapacity); for (int i = 101; i < 200; i++) { Assert.IsTrue(q.TryAdd(new TestThreadPoolItem(i), null, false, 0, CancellationToken.None)); } Assert.IsFalse(q.TryAdd(new TestThreadPoolItem(int.MaxValue), null, false, 0, CancellationToken.None)); for (int i = 0; i < 200; i++) { Assert2.AreEqual(i, q.Take(null)); } Assert.AreEqual(0, q.GlobalQueue.OccupiedNodesCount); Assert.AreEqual(100, q.GlobalQueue.ExtendedCapacity); Assert.AreEqual(100, q.GlobalQueue.FreeNodesCount); }
public void Expm1Test() { // // C99 Appendix F special cases: Assert.AreEqual(0, Math2.Expm1(0)); Assert.AreEqual(-1.0, Math2.Expm1(double.NegativeInfinity)); Assert.AreEqual(double.PositiveInfinity, Math2.Expm1(double.PositiveInfinity)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.Expm1(double.NaN)); TestCaseSet <double>[] testCases = { new TestCaseSet <double>(GetExpData(_Exp), "Expm1: Random Data", 3, 1), }; NumericFunctionTest.RunSet(Math2.Expm1, "Expm1", testCases); }
public void TestManyAddTake() { ThreadPoolConcurrentQueue q = new ThreadPoolConcurrentQueue(); for (int i = 0; i < 100000; i++) { q.Add(new TestThreadPoolItem(i)); } for (int i = 0; i < 100000; i++) { ThreadPoolWorkItem res = null; Assert.IsTrue(q.TryTake(out res)); Assert.IsNotNull(res); Assert2.AreEqual(i, res); } }
public void Log1pTest() { // // C99 Appendix F special cases: Assert.AreEqual(0, Math2.Log1p(0)); Assert.AreEqual(0, Math2.Log1p(-0)); Assert.AreEqual(double.NegativeInfinity, Math2.Log1p(-1)); Assert.AreEqual(double.PositiveInfinity, Math2.Log1p(double.PositiveInfinity)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.Log1p(double.NaN)); //BOOST_CHECK_THROW(Math2.Log1p(m_one)); TestCaseSet <double>[] testCases = { new TestCaseSet <double>(GetLogData(_Exp), "Log1p: Random Data", 2, 1), }; NumericFunctionTest.RunSet(Math2.Log1p, "Log1p", testCases); }
public void FloatAdvanceTest() { // test non-symettrical behaviors - NaN, -0.0, Infinity for (int i = 1; i < 255; i++) { Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatAdvance(double.NaN, i)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatAdvance(double.NaN, -i)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatAdvance(double.PositiveInfinity, i)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatAdvance(double.PositiveInfinity, -i)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatAdvance(double.NegativeInfinity, i)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatAdvance(double.NegativeInfinity, -i)); Assert.AreEqual(double.PositiveInfinity, Math2.FloatAdvance(double.MaxValue, i)); Assert.AreEqual(double.NegativeInfinity, Math2.FloatAdvance(double.MinValue, -i)); Assert.AreEqual(double.Epsilon * (double)i, Math2.FloatAdvance(-0.0, i)); // -0.0, double.Epsilon Assert.AreEqual(-double.Epsilon * (double)i, Math2.FloatAdvance(-0.0, -i)); // -0.0, -double.Epsilon } foreach (var item in _FloatData) { double x = item.X; int distance = item.Distance; double expected = item.Result; double actual = Math2.FloatAdvance(x, distance); Assert2.AreNear(expected, actual, 0, () => { return(string.Format("FloatAdvance({0:E16},{1}) = {2:E16}; got = {3:E16}; ulps = {4}", x, distance, expected, actual, 0)); }); // check to see that the reverse is true x = item.Result; distance = -item.Distance; expected = item.X; actual = Math2.FloatAdvance(x, distance); Assert2.AreNear(expected, actual, 0, () => { return(string.Format("FloatAdvance({0:E16},{1}) = {2:E16}; got = {3:E16}; ulps = {4}", x, distance, expected, actual, 0)); }); } }
public void TestAddCancellation() { ThreadPoolQueueController q = new ThreadPoolQueueController(100, 1000); CancellationTokenSource cSrc = new CancellationTokenSource(); for (int i = 0; i < 100; i++) { q.Add(new TestThreadPoolItem(i), null); } bool addCompleted = false; int startedFlag = 0; Task.Run(() => { try { Interlocked.Exchange(ref startedFlag, 1); q.TryAdd(new TestThreadPoolItem(int.MaxValue), null, false, -1, cSrc.Token); } catch (OperationCanceledException) { } Volatile.Write(ref addCompleted, true); }); TimingAssert.IsTrue(5000, () => Volatile.Read(ref startedFlag) == 1); Thread.Sleep(100); Assert.IsFalse(addCompleted); cSrc.Cancel(); TimingAssert.IsTrue(5000, () => Volatile.Read(ref addCompleted)); for (int i = 0; i < 100; i++) { Assert2.AreEqual(i, q.Take(null)); } Assert.AreEqual(0, q.GlobalQueue.OccupiedNodesCount); Assert.AreEqual(100, q.GlobalQueue.FreeNodesCount); }
public void LogbTest() { const int MaxNormalExponent = 1023; const int MinNormalExponent = -1022; const int MinDenormExponent = -1074; // NaN, 0, +/- Infinity Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.Logb(double.NaN)); Assert2.AreEqual <Exceptions.PoleException>(double.NegativeInfinity, () => Math2.Logb(0)); Assert.AreEqual(double.PositiveInfinity, Math2.Logb(double.PositiveInfinity)); Assert.AreEqual(double.PositiveInfinity, Math2.Logb(double.NegativeInfinity)); Assert.AreEqual(MinDenormExponent, Math2.Logb(double.Epsilon)); Assert.AreEqual(MaxNormalExponent, Math2.Logb(double.MaxValue)); Assert.AreEqual(MaxNormalExponent, Math2.Logb(double.MaxValue)); // check normal numbers -- powers of 2 for (int i = MinNormalExponent; i < MaxNormalExponent; i++) { double x = Math.Pow(2, i); Assert.AreEqual(i, Math2.Logb(x)); Assert.AreEqual(i, Math2.Logb(-x)); } // denorm for (int i = 0; i < 52; i++) { double x = double.Epsilon * (1UL << i); Assert.IsTrue(x > 0); Assert.AreEqual(MinDenormExponent + i, Math2.Logb(x)); Assert.AreEqual(MinDenormExponent + i, Math2.Logb(-x)); } foreach (var item in _FrexpData) { Assert.AreEqual(item.Exponent - 1, Math2.Logb(item.X)); } }
public void TestLocalThreadQueueUsage() { ThreadPoolLocalQueue local = new ThreadPoolLocalQueue(); ThreadPoolQueueController q = new ThreadPoolQueueController(100, 1000); q.AddLocalQueue(local); q.Add(new TestThreadPoolItem(1), local); ThreadPoolWorkItem item = null; Assert.IsTrue(local.TryTakeLocal(out item)); Assert2.AreEqual(1, item); local.TryAddLocal(new TestThreadPoolItem(1)); item = null; Assert.IsTrue(q.TryTake(local, out item, 0, CancellationToken.None, true)); Assert2.AreEqual(1, item); }
public void TestManyAddTake() { ThreadPoolQueueController q = new ThreadPoolQueueController(100, 1000); for (int i = 0; i < 100; i++) { Assert.IsTrue(q.TryAdd(new TestThreadPoolItem(i), null, false, 0, CancellationToken.None)); } Assert.IsFalse(q.TryAdd(new TestThreadPoolItem(int.MaxValue), null, false, 0, CancellationToken.None)); for (int i = 0; i < 100; i++) { ThreadPoolWorkItem res = null; Assert.IsTrue(q.TryTake(null, out res, 0, CancellationToken.None, true)); Assert2.AreEqual(i, res, "(TestThreadPoolItem)res == i"); } ThreadPoolWorkItem tmp = null; Assert.IsFalse(q.TryTake(null, out tmp, 0, CancellationToken.None, true)); }
public void AreEqual() { Console.WriteLine(Assert.ThrowsException <AssertFailedException>(() => Assert2.AreEqual(123, 456)).Message); Console.WriteLine(Assert.ThrowsException <AssertFailedException>(() => Assert2.AreEqual(new[] { 0, 1 }, new[] { 1, 2 })).Message); Console.WriteLine(Assert.ThrowsException <AssertFailedException>(() => Assert2.AreEqual("", null)).Message); Console.WriteLine(Assert.ThrowsException <AssertFailedException>(() => Assert2.AreEqual(null, Enumerable.Range(1, 10))).Message); Console.WriteLine(Assert.ThrowsException <AssertFailedException>(() => Assert2.AreEqual <object>(123, 123D)).Message); Console.WriteLine(Assert.ThrowsException <AssertFailedException>(() => Assert2.AreEqual <object>(new[] { 123 }, new[] { 123D })).Message); Console.WriteLine(Assert.ThrowsException <AssertFailedException>(() => Assert2.AreEqual <object>(0, Enumerable.Range(1, 10))).Message); Assert2.AreEqual <Uri>(null, null); Assert2.AreEqual <int[]>(null, null); Assert2.AreEqual(123, 123); Assert2.AreEqual <object>(123, 123); Assert2.AreEqual <object>(new int[0], new double[0]); Assert2.AreEqual(new[] { 1, 2, 3 }, Enumerable.Range(1, 3)); Assert2.AreEqual <object>(new[] { 1, 2, 3 }, Enumerable.Range(1, 3)); Assert2.AreEqual((2, new DateTime(2000, 1, 24)), (2, new DateTime(2000, 1, 23).AddDays(1))); Assert2.AreEqual(new Assert2Struct { X = 123, Y = 45.6 }, new Assert2Struct { X = 123, Y = 45.6 }); }
public void TestAddLock() { ThreadPoolGlobalQueue q = new ThreadPoolGlobalQueue(100); for (int i = 0; i < 100; i++) { q.Add(new TestThreadPoolItem(i)); } bool addCompleted = false; int startedFlag = 0; Task.Run(() => { Interlocked.Exchange(ref startedFlag, 1); q.Add(new TestThreadPoolItem(int.MaxValue)); Volatile.Write(ref addCompleted, true); }); TimingAssert.IsTrue(10000, () => Volatile.Read(ref startedFlag) == 1); Thread.Sleep(100); Assert.IsFalse(addCompleted); Assert2.AreEqual(0, q.Take()); TimingAssert.IsTrue(5000, () => Volatile.Read(ref addCompleted)); Assert.IsFalse(q.TryAdd(new TestThreadPoolItem(int.MinValue), 0, CancellationToken.None)); for (int i = 1; i < 100; i++) { Assert2.AreEqual(i, (TestThreadPoolItem)q.Take(), "(TestThreadPoolItem)q.Take(null, null) == i"); } Assert2.AreEqual(int.MaxValue, q.Take(), "(TestThreadPoolItem)q.Take(null, null) == int.MaxValue"); Assert.AreEqual(0, q.OccupiedNodesCount); }
public void TestTakeCancellation() { ThreadPoolGlobalQueue q = new ThreadPoolGlobalQueue(100); CancellationTokenSource cSrc = new CancellationTokenSource(); bool takeCompleted = false; ThreadPoolWorkItem takenItem = null; int startedFlag = 0; Task.Run(() => { try { Interlocked.Exchange(ref startedFlag, 1); q.TryTake(out takenItem, -1, cSrc.Token, true); } catch (OperationCanceledException) { } Volatile.Write(ref takeCompleted, true); }); TimingAssert.IsTrue(5000, () => Volatile.Read(ref startedFlag) == 1); Thread.Sleep(100); Assert.IsFalse(takeCompleted); cSrc.Cancel(); TimingAssert.IsTrue(5000, () => Volatile.Read(ref takeCompleted)); Assert.IsTrue(q.TryAdd(new TestThreadPoolItem(1), 0, CancellationToken.None)); Assert2.AreEqual(1, q.Take()); Assert.AreEqual(0, q.OccupiedNodesCount); Assert.AreEqual(100, q.FreeNodesCount); }
public void LdexpTest() { Assert.AreEqual(0.75 * Math.Pow(2, -1073), Math2.Ldexp(0.75, -1073)); const int MaxNormalExponent = 1023; const int MinNormalExponent = -1022; const int MinDenormExponent = -1074; // NaN int[] nValues = { -1, 0, 1, 2 }; foreach (int n in nValues) { Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.Ldexp(double.NaN, n)); } // edge values double[] xValues = { double.PositiveInfinity, double.NegativeInfinity, 0 }; nValues = new int[] { -1, 0, 1, 2 }; foreach (double x in xValues) { foreach (int n in nValues) { Assert.AreEqual(Math2.Ldexp(x, n), x); } } // overflow and underflow Assert.AreEqual(0.0, Math2.Ldexp(1, MinDenormExponent - 1)); Assert.AreEqual(-0.0, Math2.Ldexp(-1, MinDenormExponent - 1)); Assert.AreEqual(double.PositiveInfinity, Math2.Ldexp(1, MaxNormalExponent + 1)); Assert.AreEqual(double.NegativeInfinity, Math2.Ldexp(-1, MaxNormalExponent + 1)); // normal powers of 2 for (int i = MinNormalExponent; i < MaxNormalExponent; i++) { double expected = Math.Pow(2, i); double calculated = Math2.Ldexp(1, i); Assert2.AreNear(expected, calculated, 2, () => string.Format("Ldexp(1, {0}) = {1:E16}; got {2:E16}", i, expected, calculated)); expected = -expected; calculated = Math2.Ldexp(-1, i); Assert2.AreNear(expected, calculated, 2, () => string.Format("Ldexp(-1, {0}) = {1:E16}; got {2:E16}", i, expected, calculated)); } // denorm for (int i = 0; i < 52; i++) { double expected = double.Epsilon * (1UL << i); Assert.IsTrue(expected > 0); int n = MinDenormExponent + i; double calculated = Math2.Ldexp(1, n); Assert2.AreNear(expected, calculated, 0, () => string.Format("Ldexp(1, {0}) = {1:E16}; got {2:E16}", n, expected, calculated)); expected = -expected; calculated = Math2.Ldexp(-1, n); Assert2.AreNear(expected, calculated, 0, () => string.Format("Ldexp(-1, {0}) = {1:E16}; got {2:E16}", n, expected, calculated)); } // denorm for (int i = 0; i < 52; i++) { double expected = double.Epsilon * (1UL << i); Assert.IsTrue(expected > 0); double calculated = Math2.Ldexp(double.Epsilon, i); Assert2.AreNear(expected, calculated, 0, () => string.Format("Ldexp(double.Epsilon, {0}) = {1:E16}; got {2:E16}", i, expected, calculated)); expected = -expected; calculated = Math2.Ldexp(-double.Epsilon, i); Assert2.AreNear(expected, calculated, 0, () => string.Format("Ldexp(-double.Epsilon, {0}) = {1:E16}; got {2:E16}", i, expected, calculated)); } foreach (var item in _LdexpData) { double x = item.X; int n = item.N; double expected = item.Result; double computed = Math2.Ldexp(x, n); Assert2.AreNear(expected, computed, 1); } }
public void FloatDistanceTest() { // test non-symettrical behaviors - NaN, -0.0, Infinity Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatDistance(double.NaN, double.NaN)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatDistance(double.PositiveInfinity, double.NaN)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatDistance(double.NegativeInfinity, double.NaN)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatDistance(double.MaxValue, double.NaN)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatDistance(double.MinValue, double.NaN)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatDistance(0.0, double.NaN)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatDistance(-0.0, double.NaN)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatDistance(double.NaN, double.NaN)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatDistance(double.NaN, double.PositiveInfinity)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatDistance(double.NaN, double.NegativeInfinity)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatDistance(double.NaN, double.MaxValue)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatDistance(double.NaN, double.MinValue)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatDistance(double.NaN, 0.0)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatDistance(double.NaN, -0.0)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatDistance(double.PositiveInfinity, double.MaxValue)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatDistance(double.MaxValue, double.PositiveInfinity)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatDistance(double.NegativeInfinity, double.MinValue)); Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.FloatDistance(double.MinValue, double.NegativeInfinity)); Assert.AreEqual(1.0, Math2.FloatDistance(double.Epsilon, -0.0)); Assert.AreEqual(-1.0, Math2.FloatDistance(-double.Epsilon, -0.0)); Assert.AreEqual(-1.0, Math2.FloatDistance(-0.0, double.Epsilon)); Assert.AreEqual(1.0, Math2.FloatDistance(-0.0, -double.Epsilon)); // large distance double ZeroToMax = (double)BitConverter.DoubleToInt64Bits(double.MaxValue); double ZeroToMin = ZeroToMax + 1.0; double MaxToMin = ZeroToMax + ZeroToMin; Assert.AreEqual(ZeroToMax, Math2.FloatDistance(double.MaxValue, 0)); Assert.AreEqual(-ZeroToMax, Math2.FloatDistance(0, double.MaxValue)); Assert.AreEqual(-ZeroToMin, Math2.FloatDistance(double.MinValue, 0)); Assert.AreEqual(ZeroToMin, Math2.FloatDistance(0, double.MinValue)); Assert.AreEqual(MaxToMin, Math2.FloatDistance(double.MaxValue, double.MinValue)); Assert.AreEqual(-MaxToMin, Math2.FloatDistance(double.MinValue, double.MaxValue)); // now test symmetrical behavior foreach (var item in _FloatData) { double x = item.X; double y = item.Result; double expected = item.Distance; double actual = Math2.FloatDistance(y, x); Assert2.AreNear(expected, actual, 0, () => { return(string.Format("FloatDistance({0:E16},{1:E16}) = {2:E16}; got = {3:E16}; ulps = {4}", y, x, expected, actual, 0)); }); // Reverse parameters should yield negative answer expected = -item.Distance; actual = Math2.FloatDistance(x, y); Assert2.AreNear(expected, actual, 0, () => { return(string.Format("FloatDistance({0:E16},{1:E16}) = {2:E16}; got = {3:E16}; ulps = {4}", x, y, expected, actual, 0)); }); } }
public void FrexpTest() { const int MaxNormalExponent = 1023; const int MinNormalExponent = -1022; const int MinDenormExponent = -1074; int exponent; double mantissa; // Zero mantissa = Math2.Frexp(0.0, out exponent); Assert.AreEqual(0, mantissa); Assert.AreEqual(0, exponent); // NaN Assert2.AreEqual <Exceptions.DomainException>(double.NaN, () => Math2.Frexp(double.NaN, out exponent)); mantissa = Math2.Frexp(double.PositiveInfinity, out exponent); Assert.AreEqual(double.PositiveInfinity, mantissa); mantissa = Math2.Frexp(double.NegativeInfinity, out exponent); Assert.AreEqual(double.NegativeInfinity, mantissa); mantissa = Math2.Frexp(double.Epsilon, out exponent); Assert.AreEqual(0.5, mantissa); Assert.AreEqual(MinDenormExponent + 1, exponent); mantissa = Math2.Frexp(double.MaxValue, out exponent); // Assert.AreEqual(0.5, mantissa); Assert.AreEqual(1024, exponent); // check normal numbers -- powers of 2 for (int i = MinNormalExponent; i < MaxNormalExponent; i++) { double x = Math.Pow(2, i); mantissa = Math2.Frexp(x, out exponent); Assert2.AreNear(0.5, mantissa, 1); Assert.AreEqual(i + 1, exponent); x = -x; mantissa = Math2.Frexp(x, out exponent); Assert2.AreNear(-0.5, mantissa, 1); Assert.AreEqual(i + 1, exponent); } // denorm for (int i = 0; i < 52; i++) { double x = double.Epsilon * (double)(1UL << i); Assert.IsTrue(x > 0); mantissa = Math2.Frexp(x, out exponent); Assert2.AreNear(0.5, mantissa, 0); Assert.AreEqual(MinDenormExponent + 1 + i, exponent); x = -x; mantissa = Math2.Frexp(x, out exponent); Assert2.AreNear(-0.5, mantissa, 0); Assert.AreEqual(MinDenormExponent + 1 + i, exponent); } foreach (var item in _FrexpData) { double x = item.X; double fraction = item.Fraction; int exp = item.Exponent; int computed_exponent; double computed_fraction = Math2.Frexp(x, out computed_exponent); Assert2.AreNear(fraction, computed_fraction, 1); Assert.AreEqual(exp, computed_exponent); } }