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.

Vector2EqualityComparer.cs 3.2KB

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