TEST_EQUAL

Test if two double arrays are equal up to a desired number of decimal places.

Back to Simple Unit Testing Toolbox Documentation.

Contents

Syntax

TEST_EQUAL(X1,X2)
TEST_EQUAL(X1,X2,n)
TEST_EQUAL(__,name,print,color)
outputs = TEST_EQUAL(__)

Description

TEST_EQUAL(X1,X2) tests if X1 and X2 are equal to 16 decimal places. The results of the test are printed to the Command Window.

TEST_EQUAL(X1,X2,n) tests if X1 and X2 are equal to n decimal places. The results of the test are printed to the Command Window.

TEST_EQUAL(...,name,print,color) does the same as the syntaxes above, but also allows the user to specify a test name, whether or not to print the test results to the Command Window, and whether or not to print those test results in color. This syntax is primarily used when defining unit test classes.

outputs = TEST_EQUAL(...) also returns the test outputs. Again, this syntax is primarily used when defining unit test classes.

Inputs

Variable Description Format
X1 double array #1 double array
X2 double array #2 double array
n (OPTIONAL) decimal places of precision (defaults to 16) 1×1
double
name (OPTIONAL) test name (defaults to empty string) char array
print (OPTIONAL) true if test result should be printed to Command Window, false otherwise (defaults to true) 1×1
logical
color (OPTIONAL) true if test result should be printed in color, false otherwise (defaults to true) 1×1
logical

Outputs

Variable Description Format
output test outputs

Field Description Format
passed true if test passed, false otherwise 1×1
logical
result string storing result of test char array
message string storing additional diagnostic message char array
1×1
struct

Example #1: Passed test.

Let's define two identical arrays.

X1 = [1  1;
      1  1];
X2 = [1  1;
      1  1];

Testing for equality,

TEST_EQUAL(X1,X2);
Passed.

Example #2: Failed test.

Let's define two nearly identical arrays.

X1 = [1  1;
      1  1];
X2 = [1  1;
      1  1.00001];

Since these arrays are not equal, testing for equality should result in a failed test.

TEST_EQUAL(X1,X2);
FAILED.
    >>>> Arrays are not equal to 16 decimal places.
    >>>> Arrays ARE equal to 4 decimal places.

Example #3: Testing to a specified precision.

Let's consider the same two nearly identical arrays from the previous example.

X1 = [1  1;
      1  1];
X2 = [1  1;
      1  1.00001];

These arrays are equal to 4 decimal places, but not equal to 5 decimal places. Therefore, testing for equality up to 6 decimal places should result in a failed test.

TEST_EQUAL(X1,X2,6);
FAILED.
    >>>> Arrays are not equal to 6 decimal places.
    >>>> Arrays ARE equal to 4 decimal places.

However, testing for equality up to 3 decimal places should result in a passed test.

TEST_EQUAL(X1,X2,3);
Passed.

Example #4: Testing with arrays of different sizes.

Let's define two arrays with elements that all have the same value but have different sizes.

X1 = [1  1];
X2 = [1  1;
      1  1];

If we test for equality, we should get a failed test because arrays that have different dimensions cannot be equal.

TEST_EQUAL(X1,X2);
FAILED.
    >>>> The two arrays are not the same size.

See also

TEST_NOT_EQUAL