Utils.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. namespace UnityEngine.TestTools.Utils
  3. {
  4. public static class Utils
  5. {
  6. public static bool AreFloatsEqual(float expected, float actual, float epsilon)
  7. {
  8. // special case for infinity
  9. if (expected == Mathf.Infinity || actual == Mathf.Infinity || expected == Mathf.NegativeInfinity || actual == Mathf.NegativeInfinity)
  10. return expected == actual;
  11. // we cover both relative and absolute tolerance with this check
  12. // which is better than just relative in case of small (in abs value) args
  13. // please note that "usually" approximation is used [i.e. abs(x)+abs(y)+1]
  14. // but we speak about test code so we dont care that much about performance
  15. // but we do care about checks being more precise
  16. return Math.Abs(actual - expected) <= epsilon * Mathf.Max(Mathf.Max(Mathf.Abs(actual), Mathf.Abs(expected)), 1.0f);
  17. }
  18. public static bool AreFloatsEqualAbsoluteError(float expected, float actual, float allowedAbsoluteError)
  19. {
  20. return Math.Abs(actual - expected) <= allowedAbsoluteError;
  21. }
  22. /// <summary>
  23. /// Analogous to GameObject.CreatePrimitive, but creates a primitive mesh renderer with fast shader instead of a default builtin shader.
  24. /// Optimized for testing performance.
  25. /// </summary>
  26. /// <returns>A GameObject with primitive mesh renderer and collider.</returns>
  27. public static GameObject CreatePrimitive(PrimitiveType type)
  28. {
  29. var prim = GameObject.CreatePrimitive(type);
  30. var renderer = prim.GetComponent<Renderer>();
  31. if (renderer)
  32. renderer.sharedMaterial = new Material(Shader.Find("VertexLit"));
  33. return prim;
  34. }
  35. }
  36. }