Singularity/Library/PackageCache/com.unity.test-framework@1..../Documentation~/reference-test-utils.md
2024-05-06 11:45:45 -07:00

2.4 KiB

Test Utils

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

Static Methods

Syntax Description
bool AreFloatsEqual(float expected, float actual, float allowedRelativeError) Relative epsilon comparison of two float values for equality. allowedRelativeError is the relative error to be used in relative epsilon comparison. The relative error is the absolute error divided by the magnitude of the exact value. Returns true if the actual value is equivalent to the expected value.
bool AreFloatsEqualAbsoluteError(float expected, float actual, float allowedAbsoluteError) Compares two floating point numbers for equality under the given absolute tolerance. allowedAbsoluteError is the permitted error tolerance. Returns true if the actual value is equivalent to the expected value under the given tolerance.
GameObject CreatePrimitive( type) Creates a GameObject with a primitive MeshRenderer. This is an analogue to the GameObject.CreatePrimitive, but creates a primitive MeshRenderer with a fast Shader instead of the default built-in Shader, optimized for testing performance. type is the primitive type of the required GameObject. Returns a GameObject with primitive MeshRenderer and Collider.

Example

[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);
    }
	
    [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);
    }
}