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

Vector3EqualityComparer.cs 3.3KB

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