No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Vector4EqualityComparer.cs 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System.Collections.Generic;
  2. namespace UnityEngine.TestTools.Utils
  3. {
  4. /// <summary>
  5. /// Use this class to compare two Vector4 objects for equality with NUnit constraints. Call Vector4EqualityComparer.Instance to perform comparisons using default calculation error value 0.0001f. To set a custom test value, instantiate a new comparer using the one argument constructor.
  6. /// </summary>
  7. public class Vector4EqualityComparer : IEqualityComparer<Vector4>
  8. {
  9. private const float k_DefaultError = 0.0001f;
  10. private readonly float AllowedError;
  11. private static readonly Vector4EqualityComparer m_Instance = new Vector4EqualityComparer();
  12. /// <summary>
  13. /// A comparer instance with the default calculation error value set to 0.0001f.
  14. /// </summary>
  15. public static Vector4EqualityComparer Instance { get { return m_Instance; } }
  16. private Vector4EqualityComparer() : this(k_DefaultError) {}
  17. /// <summary>
  18. /// Initializes an instance of Vector4Equality comparer with custom allowed calculation error.
  19. /// </summary>
  20. /// <param name="allowedError">This value identifies the calculation error allowed.</param>
  21. public Vector4EqualityComparer(float allowedError)
  22. {
  23. this.AllowedError = allowedError;
  24. }
  25. /// <summary>
  26. /// Compares the actual and expected Vector4 objects for equality using <see cref="Utils.AreFloatsEqual"/> to compare the x, y, z, and w attributes of Vector4.
  27. /// </summary>
  28. /// <param name="expected">The expected Vector4 used for comparison</param>
  29. /// <param name="actual">The actual Vector4 to test</param>
  30. /// <returns>True if the vectors are equals, false otherwise.</returns>
  31. /// <example>
  32. /// <code>
  33. ///[TestFixture]
  34. /// public class Vector4Test
  35. /// {
  36. /// [Test]
  37. /// public void VerifyThat_TwoVector4ObjectsAreEqual()
  38. /// {
  39. /// // Custom error 10e-6f
  40. /// var actual = new Vector4(0, 0, 1e-6f, 1e-6f);
  41. /// var expected = new Vector4(1e-6f, 0f, 0f, 0f);
  42. /// var comparer = new Vector4EqualityComparer(10e-6f);
  43. ///
  44. /// Assert.That(actual, Is.EqualTo(expected).Using(comparer));
  45. ///
  46. /// // Default error 0.0001f
  47. /// actual = new Vector4(0.01f, 0.01f, 0f, 0f);
  48. /// expected = new Vector4(0.01f, 0.01f, 0f, 0f);
  49. ///
  50. /// Assert.That(actual, Is.EqualTo(expected).Using(Vector4EqualityComparer.Instance));
  51. /// }
  52. /// }
  53. /// </code>
  54. /// </example>
  55. public bool Equals(Vector4 expected, Vector4 actual)
  56. {
  57. return Utils.AreFloatsEqual(expected.x, actual.x, AllowedError) &&
  58. Utils.AreFloatsEqual(expected.y, actual.y, AllowedError) &&
  59. Utils.AreFloatsEqual(expected.z, actual.z, AllowedError) &&
  60. Utils.AreFloatsEqual(expected.w, actual.w, AllowedError);
  61. }
  62. /// <summary>
  63. /// Serves as the default hash function.
  64. /// </summary>
  65. /// <param name="vec4"> A not null Vector4 object</param>
  66. /// <returns>Returns 0</returns>
  67. public int GetHashCode(Vector4 vec4)
  68. {
  69. return 0;
  70. }
  71. }
  72. }