暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Vector2EqualityComparer.cs 3.2KB

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