A game about forced loneliness, made by TACStudios
1using System;
2
3namespace UnityEngine.TestTools.Utils
4{
5 /// <summary>
6 /// This contains test utility functions for float value comparison and creating primitives.
7 /// </summary>
8 public static class Utils
9 {
10 /// <summary>
11 /// Relative epsilon comparison of two float values for equality.
12 /// The relative error is the absolute error divided by the magnitude of the exact value.
13 /// </summary>
14 /// <param name="expected">The expected float value used to compare.</param>
15 /// <param name="actual">The actual float value to test.</param>
16 /// <param name="epsilon"> Epsilon is the relative error to be used in relative epsilon comparison.</param>
17 /// <returns>Returns true if the actual value is equivalent to the expected value.</returns>
18 /// <example>
19 /// <code>
20 /// [TestFixture]
21 /// class UtilsTests
22 /// {
23 /// [Test]
24 /// public void CheckThat_FloatsAreEqual()
25 /// {
26 /// float expected = 10e-8f;
27 /// float actual = 0f;
28 /// float allowedRelativeError = 10e-6f;
29 ///
30 /// Assert.That(Utils.AreFloatsEqual(expected, actual, allowedRelativeError), Is.True);
31 /// }
32 /// }
33 /// </code>
34 /// </example>
35 public static bool AreFloatsEqual(float expected, float actual, float epsilon)
36 {
37 // special case for infinity
38 if (expected == Mathf.Infinity || actual == Mathf.Infinity || expected == Mathf.NegativeInfinity || actual == Mathf.NegativeInfinity)
39 return expected == actual;
40
41 // we cover both relative and absolute tolerance with this check
42 // which is better than just relative in case of small (in abs value) args
43 // please note that "usually" approximation is used [i.e. abs(x)+abs(y)+1]
44 // but we speak about test code so we dont care that much about performance
45 // but we do care about checks being more precise
46 return Math.Abs(actual - expected) <= epsilon * Mathf.Max(Mathf.Max(Mathf.Abs(actual), Mathf.Abs(expected)), 1.0f);
47 }
48
49 /// <summary>
50 /// Compares two floating point numbers for equality under the given absolute tolerance.
51 /// </summary>
52 /// <param name="expected">The expected float value used to compare.</param>
53 /// <param name="actual">The actual float value to test.</param>
54 /// <param name="allowedAbsoluteError">AllowedAbsoluteError is the permitted error tolerance.</param>
55 /// <returns> Returns true if the actual value is equivalent to the expected value under the given tolerance.
56 /// </returns>
57 /// <example>
58 /// <code>
59 /// [TestFixture]
60 /// class UtilsTests
61 /// {
62 /// [Test]
63 /// public void CheckThat_FloatsAreAbsoluteEqual()
64 /// {
65 /// float expected = 0f;
66 /// float actual = 10e-6f;
67 /// float error = 10e-5f;
68 ///
69 /// Assert.That(Utils.AreFloatsEqualAbsoluteError(expected, actual, error), Is.True);
70 /// }
71 /// }
72 /// </code>
73 /// </example>
74 public static bool AreFloatsEqualAbsoluteError(float expected, float actual, float allowedAbsoluteError)
75 {
76 return Math.Abs(actual - expected) <= allowedAbsoluteError;
77 }
78
79 /// <summary>
80 /// Analogous to GameObject.CreatePrimitive, but creates a primitive mesh renderer with fast shader instead of a default builtin shader.
81 /// Optimized for testing performance.
82 /// </summary>
83 /// <returns>A GameObject with primitive mesh renderer and collider.</returns>
84 /// <param name="type">The type of primitive object to create.</param>
85 public static GameObject CreatePrimitive(PrimitiveType type)
86 {
87 var prim = GameObject.CreatePrimitive(type);
88 var renderer = prim.GetComponent<Renderer>();
89 if (renderer)
90 renderer.sharedMaterial = new Material(Shader.Find("VertexLit"));
91 return prim;
92 }
93 }
94}