Ingen beskrivning
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

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