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