暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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