暫無描述
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.

Vector3EqualityComparer.cs 3.3KB

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