
Untitled
By: a guest on
Aug 12th, 2012 | syntax:
None | size: 1.12 KB | hits: 11 | expires: Never
assert with custom comparison function
Assert.That(somevector.EqualWithinTolerance(new Vec3(0f, 1f, 0f)), Is.True);
Assert.That(somevector, Is.EqualTo(new Vec3(0f, 1f, 0f)));
Expected: True
But was: False
Expected: 0 1 0
But was: 1 0 9,536743E-07
/// <summary>
/// Equality comparer with a tolerance equivalent to using the 'EqualWithTolerance' method
///
/// Note: since it's pretty much impossible to have working hash codes
/// for a "fuzzy" comparer the GetHashCode method throws an exception.
/// </summary>
public class EqualityComparerWithTolerance : IEqualityComparer<Vec3>
{
private float tolerance;
public EqualityComparerWithTolerance(float tolerance = MathFunctions.Epsilon)
{
this.tolerance = tolerance;
}
public bool Equals(Vec3 v1, Vec3 v2)
{
return v1.EqualWithinTolerance(v2, tolerance);
}
public int GetHashCode(Vec3 obj)
{
throw new NotImplementedException();
}
}
Assert.That(somevector, Is.EqualTo(new Vec3(0f, 1f, 0f)).Using(fuzzyVectorComparer));