Removed AssertHelper because this functionality is now integrated in NUnit
git-svn-id: file:///srv/devel/repo-conversion/nusu@199 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
d8d7af174a
commit
1ac86eebb0
|
@ -50,10 +50,6 @@
|
|||
<Compile Include="Source\AffineThreadPool.Test.cs">
|
||||
<DependentUpon>AffineThreadPool.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Source\AssertHelper.cs" />
|
||||
<Compile Include="Source\AssertHelper.Test.cs">
|
||||
<DependentUpon>AssertHelper.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Source\Collections\Deque.cs" />
|
||||
<Compile Include="Source\Collections\Deque.Insertion.cs">
|
||||
<DependentUpon>Deque.cs</DependentUpon>
|
||||
|
|
|
@ -53,10 +53,6 @@
|
|||
<Compile Include="Source\AffineThreadPool.Test.cs">
|
||||
<DependentUpon>AffineThreadPool.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Source\AssertHelper.cs" />
|
||||
<Compile Include="Source\AssertHelper.Test.cs">
|
||||
<DependentUpon>AssertHelper.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Source\Collections\Deque.cs" />
|
||||
<Compile Include="Source\Collections\Deque.Insertion.cs">
|
||||
<DependentUpon>Deque.cs</DependentUpon>
|
||||
|
|
|
@ -64,10 +64,6 @@
|
|||
<Compile Include="Source\AffineThreadPool.Test.cs">
|
||||
<DependentUpon>AffineThreadPool.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Source\AssertHelper.cs" />
|
||||
<Compile Include="Source\AssertHelper.Test.cs">
|
||||
<DependentUpon>AssertHelper.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Source\Collections\Deque.cs" />
|
||||
<Compile Include="Source\Collections\Deque.Insertion.cs">
|
||||
<DependentUpon>Deque.cs</DependentUpon>
|
||||
|
|
|
@ -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 {
|
||||
|
||||
/// <summary>Unit Test for the special floating point assertions</summary>
|
||||
[TestFixture]
|
||||
public class AssertHelperTest {
|
||||
|
||||
/// <summary>
|
||||
/// Tests whether the almost equal check works with floating point values
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestAlmostEqualWithFloats() {
|
||||
AssertHelper.AreAlmostEqual(exactFloat, minusOneFloat, 1);
|
||||
AssertHelper.AreAlmostEqual(exactFloat, plusOneFloat, 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests whether the almost equal check detects a floating point value that is
|
||||
/// just barely too low
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestAlmostEqualWithTooLowFloat() {
|
||||
Assert.Throws<AssertionException>(
|
||||
delegate() { AssertHelper.AreAlmostEqual(exactFloat, minusTwoFloat, 1); }
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests whether the almost equal check detects a floating point value that is
|
||||
/// just barely too high
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestAlmostEqualWithTooHighFloat() {
|
||||
Assert.Throws<AssertionException>(
|
||||
delegate() { AssertHelper.AreAlmostEqual(exactFloat, plusTwoFloat, 1); }
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests whether the almost equal check works with double precision floating points
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestAlmostEqualWithDoubles() {
|
||||
AssertHelper.AreAlmostEqual(exactDouble, minusOneDouble, 1);
|
||||
AssertHelper.AreAlmostEqual(exactDouble, plusOneDouble, 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests whether the almost equal check detects a double precision floating point
|
||||
/// value that is just barely too low
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestAlmostEqualWithTooLowDouble() {
|
||||
Assert.Throws<AssertionException>(
|
||||
delegate() { AssertHelper.AreAlmostEqual(exactDouble, minusTwoDouble, 1); }
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests whether the almost equal check detects a double precision floating point
|
||||
/// value that is just barely too high
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestAlmostEqualWithTooHighDouble() {
|
||||
Assert.Throws<AssertionException>(
|
||||
delegate() { AssertHelper.AreAlmostEqual(exactDouble, plusTwoDouble, 1); }
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the AreAlmostEqual() helper works correctly when comparing
|
||||
/// two floating point arrays that are both null
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestAlmostEqualWithNullFloatArrays() {
|
||||
float[] nullArray = null;
|
||||
|
||||
AssertHelper.AreAlmostEqual(nullArray, nullArray, 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the AreAlmostEqual() helper works correctly when comparing
|
||||
/// two floating point arrays that are within the allowed deviation
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestAlmostEqualWithFloatArrays() {
|
||||
float[] referenceArray = new float[] { exactFloat, exactFloat, exactFloat };
|
||||
float[] testArray = new float[] { exactFloat, plusOneFloat, minusOneFloat };
|
||||
|
||||
AssertHelper.AreAlmostEqual(referenceArray, testArray, 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the AreAlmostEqual() helper throws an exception if two arrays
|
||||
/// of different length are compared to each other
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestAlmostEqualWithFloatArraysOfDifferentLength() {
|
||||
float[] referenceArray = new float[] { exactFloat, exactFloat, exactFloat };
|
||||
float[] testArray = new float[] { exactFloat, exactFloat };
|
||||
|
||||
Assert.Throws<AssertionException>(
|
||||
delegate() { AssertHelper.AreAlmostEqual(referenceArray, testArray, 1); }
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the AreAlmostEqual() helper throws an exception if the two
|
||||
/// arrays contain elements that deviate by more than the allowed amount
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestThrowOnAlmostEqualWithBarelyDifferingFloatArrays() {
|
||||
float[] referenceArray = new float[] { exactFloat, exactFloat, exactFloat };
|
||||
float[] testArray = new float[] { plusOneFloat, minusOneFloat, plusTwoFloat };
|
||||
|
||||
Assert.Throws<AssertionException>(
|
||||
delegate() { AssertHelper.AreAlmostEqual(referenceArray, testArray, 1); }
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adjusts a floating point value by the specified amount of neighbouring
|
||||
/// representable values
|
||||
/// </summary>
|
||||
/// <param name="value">Floating point value to be adjusted</param>
|
||||
/// <param name="ulps">Numbers of neighbouring representable values to step</param>
|
||||
/// <returns>The adjusted floating point value</returns>
|
||||
private static float adjust(float value, int ulps) {
|
||||
return FloatHelper.ReinterpretAsFloat(FloatHelper.ReinterpretAsInt(value) + ulps);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adjusts a double precision floating point value by the specified amount of
|
||||
/// neighbouring representable values
|
||||
/// </summary>
|
||||
/// <param name="value">Double precision floating point value to be adjusted</param>
|
||||
/// <param name="ulps">Numbers of neighbouring representable values to step</param>
|
||||
/// <returns>The adjusted double precision floating point value</returns>
|
||||
private static double adjust(double value, long ulps) {
|
||||
return FloatHelper.ReinterpretAsDouble(FloatHelper.ReinterpretAsLong(value) + ulps);
|
||||
}
|
||||
|
||||
/// <summary>The exact test value as a float</summary>
|
||||
private static readonly float exactFloat = 1234.5678f;
|
||||
/// <summary>The second next possible smaller float from the test value</summary>
|
||||
private static readonly float minusTwoFloat = adjust(exactFloat, -2);
|
||||
/// <summary>The next possible smaller float from the test value</summary>
|
||||
private static readonly float minusOneFloat = adjust(exactFloat, -1);
|
||||
/// <summary>The next possible greater float from the test value</summary>
|
||||
private static readonly float plusOneFloat = adjust(exactFloat, +1);
|
||||
/// <summary>The second next possible greater float from the test value</summary>
|
||||
private static readonly float plusTwoFloat = adjust(exactFloat, +2);
|
||||
|
||||
/// <summary>The exact test value as a float</summary>
|
||||
private static readonly double exactDouble = 1234.5678f;
|
||||
/// <summary>The second next possible smaller float from the test value</summary>
|
||||
private static readonly double minusTwoDouble = adjust(exactDouble, -2);
|
||||
/// <summary>The next possible smaller float from the test value</summary>
|
||||
private static readonly double minusOneDouble = adjust(exactDouble, -1);
|
||||
/// <summary>The next possible greater float from the test value</summary>
|
||||
private static readonly double plusOneDouble = adjust(exactDouble, +1);
|
||||
/// <summary>The second next possible greater float from the test value</summary>
|
||||
private static readonly double plusTwoDouble = adjust(exactDouble, +2);
|
||||
|
||||
}
|
||||
|
||||
} // namespace Nuclex.Support
|
||||
|
||||
#endif // UNITTEST
|
|
@ -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 {
|
||||
|
||||
/// <summary>Contains special assertions for floating numbers</summary>
|
||||
public static class AssertHelper {
|
||||
|
||||
/// <summary>Ensures that two double precision floating point values are equal</summary>
|
||||
/// <param name="expected">Expected double precision floating point value</param>
|
||||
/// <param name="actual">Actual double precision floating point value</param>
|
||||
/// <param name="deltaUlps">
|
||||
/// Allowed deviation in representable double precision floating point values
|
||||
/// </param>
|
||||
public static void AreAlmostEqual(double expected, double actual, int deltaUlps) {
|
||||
AreAlmostEqual(expected, actual, deltaUlps, null);
|
||||
}
|
||||
|
||||
/// <summary>Ensures that two double precision floating point values are equal</summary>
|
||||
/// <param name="expected">Expected double precision floating point value</param>
|
||||
/// <param name="actual">Actual double precision floating point value</param>
|
||||
/// <param name="deltaUlps">
|
||||
/// Allowed deviation in representable double precision floating point values
|
||||
/// </param>
|
||||
/// <param name="message">
|
||||
/// Message to display when the double precision floating point values are not equal
|
||||
/// </param>
|
||||
public static void AreAlmostEqual(
|
||||
double expected, double actual, int deltaUlps, string message
|
||||
) {
|
||||
if(!FloatHelper.AreAlmostEqual(expected, actual, deltaUlps)) {
|
||||
Assert.AreEqual(expected, actual, message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Ensures that two floating point values are equal</summary>
|
||||
/// <param name="expected">Expected floating point value</param>
|
||||
/// <param name="actual">Actual floating point value</param>
|
||||
/// <param name="deltaUlps">Allowed deviation in representable floating point values</param>
|
||||
public static void AreAlmostEqual(float expected, float actual, int deltaUlps) {
|
||||
AreAlmostEqual(expected, actual, deltaUlps, null);
|
||||
}
|
||||
|
||||
/// <summary>Ensures that two floating point values are equal</summary>
|
||||
/// <param name="expected">Expected floating point value</param>
|
||||
/// <param name="actual">Actual floating point value</param>
|
||||
/// <param name="deltaUlps">Allowed deviation in representable floating point values</param>
|
||||
/// <param name="message">
|
||||
/// Message to display when the floating point values are not equal
|
||||
/// </param>
|
||||
public static void AreAlmostEqual(
|
||||
float expected, float actual, int deltaUlps, string message
|
||||
) {
|
||||
if(!FloatHelper.AreAlmostEqual(expected, actual, deltaUlps)) {
|
||||
Assert.AreEqual(expected, actual, message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Ensures that two float arrays are equal</summary>
|
||||
/// <param name="expected">Expected float array</param>
|
||||
/// <param name="actual">Actual float array</param>
|
||||
/// <param name="deltaUlps">
|
||||
/// Allowed deviation for each value in representable floating point values
|
||||
/// </param>
|
||||
public static void AreAlmostEqual(float[] expected, float[] actual, int deltaUlps) {
|
||||
AreAlmostEqual(expected, actual, deltaUlps, null);
|
||||
}
|
||||
|
||||
/// <summary>Ensures that two float arrays are equal</summary>
|
||||
/// <param name="expected">Expected float array</param>
|
||||
/// <param name="actual">Actual float array</param>
|
||||
/// <param name="deltaUlps">
|
||||
/// Allowed deviation for each value in representable floating point values
|
||||
/// </param>
|
||||
/// <param name="message">Message to display when the arrays are not equal</param>
|
||||
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
|
Loading…
Reference in New Issue
Block a user