Difference between revisions of "Unit Testing"

From Efficient Java Matrix Library
Jump to navigation Jump to search
(Created page with "[http://en.wikipedia.org/wiki/Unit_testing Unit testing] is an essential part of modern software development that helps ensure correctness. EJML itself makes extensive use of...")
 
Line 9: Line 9:
 
EjmlUnitTests provides various functions for testing equality and matrix shape. Below is an example taken from an internal EJML unit test that compares the output from two different matrix decompositions with different matrix types:
 
EjmlUnitTests provides various functions for testing equality and matrix shape. Below is an example taken from an internal EJML unit test that compares the output from two different matrix decompositions with different matrix types:
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
     DenseMatrix64F Q = decomp.getQ(null);
+
     DMatrixRMaj Q = decomp.getQ(null);
     BlockMatrix64F Qb = decompB.getQ(null,false);
+
     DMatrixRBlock Qb = decompB.getQ(null,false);
  
     EjmlUnitTests.assertEquals(Q,Qb,1e-8);
+
     EjmlUnitTests.assertEquals(Q,Qb,UtilEjml.TEST_F64);
 
</syntaxhighlight>
 
</syntaxhighlight>
 
In this example it checks to see if each element of the two matrices are within 1e-8 of each other.  The reference EjmlUnitTests to can be avoided by invoking a "static import".  If an error is found and the test fails the exact element it failed at is printed.
 
In this example it checks to see if each element of the two matrices are within 1e-8 of each other.  The reference EjmlUnitTests to can be avoided by invoking a "static import".  If an error is found and the test fails the exact element it failed at is printed.
Line 22: Line 22:
 
MatrixFeatures is not designed with unit testing in mind, but provides many useful functions for unit tests.  For example, to test for equality between two matrices:
 
MatrixFeatures is not designed with unit testing in mind, but provides many useful functions for unit tests.  For example, to test for equality between two matrices:
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
   assertTrue(MatrixFeatures.isEquals(Q,Qb,1e-8));
+
   assertTrue(MatrixFeatures.isEquals(Q,Qb,UtilEjml.TEST_F64));
 
</syntaxhighlight>
 
</syntaxhighlight>
 
Here the JUnitTest function assertTrue() has been used.  MatrixFeatures.isEquals() returns true of the two matrices are within tolerance of each other.  If the test fails it doesn't print any additional information, such as which element it failed at.
 
Here the JUnitTest function assertTrue() has been used.  MatrixFeatures.isEquals() returns true of the two matrices are within tolerance of each other.  If the test fails it doesn't print any additional information, such as which element it failed at.
  
 
One advantage of MatrixFeatures is it provides support for many more specialized tests. For example if you want to know if a matrix is orthogonal call MatrixFeatures.isOrthogonal() or to test for symmetry call MatrixFeatures.isSymmetric().
 
One advantage of MatrixFeatures is it provides support for many more specialized tests. For example if you want to know if a matrix is orthogonal call MatrixFeatures.isOrthogonal() or to test for symmetry call MatrixFeatures.isSymmetric().

Revision as of 10:28, 18 May 2017

Unit testing is an essential part of modern software development that helps ensure correctness. EJML itself makes extensive use of unit tests as well as system level tests. EJML provides several functions are specifically designed for creating unit tests.

EjmlUnitTests and MatrixFeatures are two classes which contain useful functions for unit testing. EjmlUnitTests provides a similar interface to how JUnitTest operates. MatrixFeatures is primarily intended for extracting high level information about a matrix, but also contains several functions for testing if two matrices are equal or have specific characteristics.

The following is a brief introduction to unit testing with EJML. See the JavaDoc for a more detailed list of functions available in EjmlUnitTests and MatrixFeatures.

Example with EjmlUnitTests

EjmlUnitTests provides various functions for testing equality and matrix shape. Below is an example taken from an internal EJML unit test that compares the output from two different matrix decompositions with different matrix types:

    DMatrixRMaj Q = decomp.getQ(null);
    DMatrixRBlock Qb = decompB.getQ(null,false);

    EjmlUnitTests.assertEquals(Q,Qb,UtilEjml.TEST_F64);

In this example it checks to see if each element of the two matrices are within 1e-8 of each other. The reference EjmlUnitTests to can be avoided by invoking a "static import". If an error is found and the test fails the exact element it failed at is printed.

To maintain compatibility with different unit test libraries a generic runtime exception is thrown if a test fails.

Example using MatrixFeatures

MatrixFeatures is not designed with unit testing in mind, but provides many useful functions for unit tests. For example, to test for equality between two matrices:

   assertTrue(MatrixFeatures.isEquals(Q,Qb,UtilEjml.TEST_F64));

Here the JUnitTest function assertTrue() has been used. MatrixFeatures.isEquals() returns true of the two matrices are within tolerance of each other. If the test fails it doesn't print any additional information, such as which element it failed at.

One advantage of MatrixFeatures is it provides support for many more specialized tests. For example if you want to know if a matrix is orthogonal call MatrixFeatures.isOrthogonal() or to test for symmetry call MatrixFeatures.isSymmetric().