Browse Source

Enhance FloatRoundingComparer

af/merge-core
James Jackson-South 9 years ago
parent
commit
15d67c843d
  1. 23
      tests/ImageSharp.Tests/TestUtilities/FloatRoundingComparer.cs

23
tests/ImageSharp.Tests/TestUtilities/FloatRoundingComparer.cs

@ -1,12 +1,13 @@
namespace ImageSharp.Tests.TestUtilities
namespace ImageSharp.Tests
{
using System;
using System.Collections.Generic;
using System.Numerics;
/// <summary>
/// Allows the comparison of single-precision floating point values by precision.
/// </summary>
public struct FloatRoundingComparer : IEqualityComparer<float>
public struct FloatRoundingComparer : IEqualityComparer<float>, IEqualityComparer<Vector4>
{
/// <summary>
/// Initializes a new instance of the <see cref="FloatRoundingComparer"/> struct.
@ -14,6 +15,7 @@
/// <param name="precision">The number of decimal places (valid values: 0-7)</param>
public FloatRoundingComparer(int precision)
{
Guard.MustBeBetweenOrEqualTo(precision, 0, 7, nameof(precision));
this.Precision = precision;
}
@ -31,6 +33,12 @@
return Comparer<float>.Default.Compare(xp, yp) == 0;
}
/// <inheritdoc />
public bool Equals(Vector4 x, Vector4 y)
{
return Equals(x.X, y.X) && Equals(x.Y, y.Y) && Equals(x.Z, y.Z) && Equals(x.W, y.W);
}
/// <inheritdoc />
public int GetHashCode(float obj)
{
@ -41,5 +49,16 @@
return hashCode;
}
}
/// <inheritdoc />
public int GetHashCode(Vector4 obj)
{
unchecked
{
int hashCode = obj.GetHashCode();
hashCode = (hashCode * 397) ^ this.Precision.GetHashCode();
return hashCode;
}
}
}
}
Loading…
Cancel
Save