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