docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class Utils

    This contains test utility functions for float value comparison and creating primitives.

    Inheritance
    object
    Utils
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: UnityEngine.TestTools.Utils
    Assembly: UnityEngine.TestRunner.dll
    Syntax
    public static class Utils

    Methods

    AreFloatsEqual(float, float, float)

    Relative epsilon comparison of two float values for equality. The relative error is the absolute error divided by the magnitude of the exact value.

    Declaration
    public static bool AreFloatsEqual(float expected, float actual, float epsilon)
    Parameters
    Type Name Description
    float expected

    The expected float value used to compare.

    float actual

    The actual float value to test.

    float epsilon

    Epsilon is the relative error to be used in relative epsilon comparison.

    Returns
    Type Description
    bool

    Returns true if the actual value is equivalent to the expected value.

    Examples
    [TestFixture]
    class UtilsTests
    {
        [Test]
        public void CheckThat_FloatsAreEqual()
        {
            float expected = 10e-8f;
            float actual = 0f;
            float allowedRelativeError = 10e-6f;
    
            Assert.That(Utils.AreFloatsEqual(expected, actual, allowedRelativeError), Is.True);
        }
    }

    AreFloatsEqualAbsoluteError(float, float, float)

    Compares two floating point numbers for equality under the given absolute tolerance.

    Declaration
    public static bool AreFloatsEqualAbsoluteError(float expected, float actual, float allowedAbsoluteError)
    Parameters
    Type Name Description
    float expected

    The expected float value used to compare.

    float actual

    The actual float value to test.

    float allowedAbsoluteError

    AllowedAbsoluteError is the permitted error tolerance.

    Returns
    Type Description
    bool

    Returns true if the actual value is equivalent to the expected value under the given tolerance.

    Examples
    [TestFixture]
    class UtilsTests
    {
        [Test]
        public void CheckThat_FloatsAreAbsoluteEqual()
        {
            float expected = 0f;
            float actual = 10e-6f;
            float error = 10e-5f;
    
            Assert.That(Utils.AreFloatsEqualAbsoluteError(expected, actual, error), Is.True);
        }
    }

    CreatePrimitive(PrimitiveType)

    Analogous to GameObject.CreatePrimitive, but creates a primitive mesh renderer with fast shader instead of a default builtin shader. Optimized for testing performance.

    Declaration
    public static GameObject CreatePrimitive(PrimitiveType type)
    Parameters
    Type Name Description
    PrimitiveType type

    The type of primitive object to create.

    Returns
    Type Description
    GameObject

    A GameObject with primitive mesh renderer and collider.

    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)