diff --git a/Nuclex.Support (net-2.0).csproj b/Nuclex.Support (net-2.0).csproj index 5cb288b..7c280cc 100644 --- a/Nuclex.Support (net-2.0).csproj +++ b/Nuclex.Support (net-2.0).csproj @@ -50,10 +50,6 @@ AffineThreadPool.cs - - - AssertHelper.cs - Deque.cs diff --git a/Nuclex.Support (net-4.0).csproj b/Nuclex.Support (net-4.0).csproj index d37a608..151bcbe 100644 --- a/Nuclex.Support (net-4.0).csproj +++ b/Nuclex.Support (net-4.0).csproj @@ -53,10 +53,6 @@ AffineThreadPool.cs - - - AssertHelper.cs - Deque.cs diff --git a/Nuclex.Support (xna-3.1-xbox360).csproj b/Nuclex.Support (xna-3.1-xbox360).csproj index f7e8b3f..d343abe 100644 --- a/Nuclex.Support (xna-3.1-xbox360).csproj +++ b/Nuclex.Support (xna-3.1-xbox360).csproj @@ -64,10 +64,6 @@ AffineThreadPool.cs - - - AssertHelper.cs - Deque.cs diff --git a/Source/AssertHelper.Test.cs b/Source/AssertHelper.Test.cs deleted file mode 100644 index f38f712..0000000 --- a/Source/AssertHelper.Test.cs +++ /dev/null @@ -1,195 +0,0 @@ -#region CPL License -/* -Nuclex Framework -Copyright (C) 2002-2009 Nuclex Development Labs - -This library is free software; you can redistribute it and/or -modify it under the terms of the IBM Common Public License as -published by the IBM Corporation; either version 1.0 of the -License, or (at your option) any later version. - -This 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 -IBM Common Public License for more details. - -You should have received a copy of the IBM Common Public -License along with this library -*/ -#endregion - -using System; -using System.Collections.Generic; - -#if UNITTEST - -using NUnit.Framework; - -namespace Nuclex.Support { - - /// Unit Test for the special floating point assertions - [TestFixture] - public class AssertHelperTest { - - /// - /// Tests whether the almost equal check works with floating point values - /// - [Test] - public void TestAlmostEqualWithFloats() { - AssertHelper.AreAlmostEqual(exactFloat, minusOneFloat, 1); - AssertHelper.AreAlmostEqual(exactFloat, plusOneFloat, 1); - } - - /// - /// Tests whether the almost equal check detects a floating point value that is - /// just barely too low - /// - [Test] - public void TestAlmostEqualWithTooLowFloat() { - Assert.Throws( - delegate() { AssertHelper.AreAlmostEqual(exactFloat, minusTwoFloat, 1); } - ); - } - - /// - /// Tests whether the almost equal check detects a floating point value that is - /// just barely too high - /// - [Test] - public void TestAlmostEqualWithTooHighFloat() { - Assert.Throws( - delegate() { AssertHelper.AreAlmostEqual(exactFloat, plusTwoFloat, 1); } - ); - } - - /// - /// Tests whether the almost equal check works with double precision floating points - /// - [Test] - public void TestAlmostEqualWithDoubles() { - AssertHelper.AreAlmostEqual(exactDouble, minusOneDouble, 1); - AssertHelper.AreAlmostEqual(exactDouble, plusOneDouble, 1); - } - - /// - /// Tests whether the almost equal check detects a double precision floating point - /// value that is just barely too low - /// - [Test] - public void TestAlmostEqualWithTooLowDouble() { - Assert.Throws( - delegate() { AssertHelper.AreAlmostEqual(exactDouble, minusTwoDouble, 1); } - ); - } - - /// - /// Tests whether the almost equal check detects a double precision floating point - /// value that is just barely too high - /// - [Test] - public void TestAlmostEqualWithTooHighDouble() { - Assert.Throws( - delegate() { AssertHelper.AreAlmostEqual(exactDouble, plusTwoDouble, 1); } - ); - } - - /// - /// Verifies that the AreAlmostEqual() helper works correctly when comparing - /// two floating point arrays that are both null - /// - [Test] - public void TestAlmostEqualWithNullFloatArrays() { - float[] nullArray = null; - - AssertHelper.AreAlmostEqual(nullArray, nullArray, 1); - } - - /// - /// Verifies that the AreAlmostEqual() helper works correctly when comparing - /// two floating point arrays that are within the allowed deviation - /// - [Test] - public void TestAlmostEqualWithFloatArrays() { - float[] referenceArray = new float[] { exactFloat, exactFloat, exactFloat }; - float[] testArray = new float[] { exactFloat, plusOneFloat, minusOneFloat }; - - AssertHelper.AreAlmostEqual(referenceArray, testArray, 1); - } - - /// - /// Verifies that the AreAlmostEqual() helper throws an exception if two arrays - /// of different length are compared to each other - /// - [Test] - public void TestAlmostEqualWithFloatArraysOfDifferentLength() { - float[] referenceArray = new float[] { exactFloat, exactFloat, exactFloat }; - float[] testArray = new float[] { exactFloat, exactFloat }; - - Assert.Throws( - delegate() { AssertHelper.AreAlmostEqual(referenceArray, testArray, 1); } - ); - } - - /// - /// Verifies that the AreAlmostEqual() helper throws an exception if the two - /// arrays contain elements that deviate by more than the allowed amount - /// - [Test] - public void TestThrowOnAlmostEqualWithBarelyDifferingFloatArrays() { - float[] referenceArray = new float[] { exactFloat, exactFloat, exactFloat }; - float[] testArray = new float[] { plusOneFloat, minusOneFloat, plusTwoFloat }; - - Assert.Throws( - delegate() { AssertHelper.AreAlmostEqual(referenceArray, testArray, 1); } - ); - } - - /// - /// Adjusts a floating point value by the specified amount of neighbouring - /// representable values - /// - /// Floating point value to be adjusted - /// Numbers of neighbouring representable values to step - /// The adjusted floating point value - private static float adjust(float value, int ulps) { - return FloatHelper.ReinterpretAsFloat(FloatHelper.ReinterpretAsInt(value) + ulps); - } - - /// - /// Adjusts a double precision floating point value by the specified amount of - /// neighbouring representable values - /// - /// Double precision floating point value to be adjusted - /// Numbers of neighbouring representable values to step - /// The adjusted double precision floating point value - private static double adjust(double value, long ulps) { - return FloatHelper.ReinterpretAsDouble(FloatHelper.ReinterpretAsLong(value) + ulps); - } - - /// The exact test value as a float - private static readonly float exactFloat = 1234.5678f; - /// The second next possible smaller float from the test value - private static readonly float minusTwoFloat = adjust(exactFloat, -2); - /// The next possible smaller float from the test value - private static readonly float minusOneFloat = adjust(exactFloat, -1); - /// The next possible greater float from the test value - private static readonly float plusOneFloat = adjust(exactFloat, +1); - /// The second next possible greater float from the test value - private static readonly float plusTwoFloat = adjust(exactFloat, +2); - - /// The exact test value as a float - private static readonly double exactDouble = 1234.5678f; - /// The second next possible smaller float from the test value - private static readonly double minusTwoDouble = adjust(exactDouble, -2); - /// The next possible smaller float from the test value - private static readonly double minusOneDouble = adjust(exactDouble, -1); - /// The next possible greater float from the test value - private static readonly double plusOneDouble = adjust(exactDouble, +1); - /// The second next possible greater float from the test value - private static readonly double plusTwoDouble = adjust(exactDouble, +2); - - } - -} // namespace Nuclex.Support - -#endif // UNITTEST diff --git a/Source/AssertHelper.cs b/Source/AssertHelper.cs deleted file mode 100644 index d3be35e..0000000 --- a/Source/AssertHelper.cs +++ /dev/null @@ -1,135 +0,0 @@ -#region CPL License -/* -Nuclex Framework -Copyright (C) 2002-2009 Nuclex Development Labs - -This library is free software; you can redistribute it and/or -modify it under the terms of the IBM Common Public License as -published by the IBM Corporation; either version 1.0 of the -License, or (at your option) any later version. - -This 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 -IBM Common Public License for more details. - -You should have received a copy of the IBM Common Public -License along with this library -*/ -#endregion - -#if UNITTEST - -using System; - -using NUnit.Framework; - -// Decide: -// - Move (part of) this to Nuclex.Support? [done] -// - Create new Assemblies Nuclex.NUnit.dll and Nuclex.NUnit.Xna.dll? - -namespace Nuclex.Support { - - /// Contains special assertions for floating numbers - public static class AssertHelper { - - /// Ensures that two double precision floating point values are equal - /// Expected double precision floating point value - /// Actual double precision floating point value - /// - /// Allowed deviation in representable double precision floating point values - /// - public static void AreAlmostEqual(double expected, double actual, int deltaUlps) { - AreAlmostEqual(expected, actual, deltaUlps, null); - } - - /// Ensures that two double precision floating point values are equal - /// Expected double precision floating point value - /// Actual double precision floating point value - /// - /// Allowed deviation in representable double precision floating point values - /// - /// - /// Message to display when the double precision floating point values are not equal - /// - public static void AreAlmostEqual( - double expected, double actual, int deltaUlps, string message - ) { - if(!FloatHelper.AreAlmostEqual(expected, actual, deltaUlps)) { - Assert.AreEqual(expected, actual, message); - } - } - - /// Ensures that two floating point values are equal - /// Expected floating point value - /// Actual floating point value - /// Allowed deviation in representable floating point values - public static void AreAlmostEqual(float expected, float actual, int deltaUlps) { - AreAlmostEqual(expected, actual, deltaUlps, null); - } - - /// Ensures that two floating point values are equal - /// Expected floating point value - /// Actual floating point value - /// Allowed deviation in representable floating point values - /// - /// Message to display when the floating point values are not equal - /// - public static void AreAlmostEqual( - float expected, float actual, int deltaUlps, string message - ) { - if(!FloatHelper.AreAlmostEqual(expected, actual, deltaUlps)) { - Assert.AreEqual(expected, actual, message); - } - } - - /// Ensures that two float arrays are equal - /// Expected float array - /// Actual float array - /// - /// Allowed deviation for each value in representable floating point values - /// - public static void AreAlmostEqual(float[] expected, float[] actual, int deltaUlps) { - AreAlmostEqual(expected, actual, deltaUlps, null); - } - - /// Ensures that two float arrays are equal - /// Expected float array - /// Actual float array - /// - /// Allowed deviation for each value in representable floating point values - /// - /// Message to display when the arrays are not equal - public static void AreAlmostEqual( - float[] expected, float[] actual, int deltaUlps, string message - ) { - - // If one is null, the other also has to be null - if((expected == null) || (actual == null)) { - Assert.AreEqual(expected, actual, message); - return; // This will be reached if both are null, which means they're equal - } - - // If the lengths do not match, let NUnit print its own message saying that - if(expected.Length != actual.Length) { - Assert.AreEqual(expected, actual, message); // will always fail - } - - // Compare the two arrays element by element - for(int i = 0; i < expected.Length; ++i) { - if(!FloatHelper.AreAlmostEqual(expected[i], actual[i], deltaUlps)) { - string safeMessage = (message == null) ? string.Empty : (message + " "); - Assert.AreEqual( - actual[i], expected[i], - safeMessage + "(arrays differ at index " + i.ToString() + ")" - ); - } - } - - } - - } - -} // namespace Nuclex.Geometry - -#endif // UNITTEST