junit testdata for matrices

H

harryos

hi,
I am writing some code that manipulates some matrices and arrays of
double type.I wanted to write some junit testcases for the code.
suppose I have a method that does some calculation on a 2D array and
returns another 2D array.
public class MatrixManipulator{
....
public double[][] calculateNewMatrix(double[][] originalMatrix){
...
}
....
}
I wanted to put all the test data in a single class ,say MatrixData,
I wrote the testcase as below,

public class MatrixTests{
private double[][] origMatrix;
private double[][] finalMatrix;
private MatrixManipulator mat;
@Before
public void setUp(){
mat = new MatrixManipulator();
MatrixData mData = new MatrixData();
origMatrix = mData.getOrigMatrix();
finalMatrix = mData.getFinalMatrix();
}
@test
public void testCalculateNewMatrix(){
double[][] result = mat.calculateNewMatrix(origMatrix);
for(int i = 0; i< result.length; i++){
Assert.assertArrayEquals(finalMatrix, result,
0.0001);
}

}

}

I wanted to know if this is the proper way of accessing test data and
testing array equality.Any comments/opinions/suggestions most welcome.
thanks,
harry
 
A

Arne Vajhøj

I am writing some code that manipulates some matrices and arrays of
double type.I wanted to write some junit testcases for the code.
suppose I have a method that does some calculation on a 2D array and
returns another 2D array.
public class MatrixManipulator{
...
public double[][] calculateNewMatrix(double[][] originalMatrix){
...
}
...
}
I wanted to put all the test data in a single class ,say MatrixData,
I wrote the testcase as below,

public class MatrixTests{
private double[][] origMatrix;
private double[][] finalMatrix;
private MatrixManipulator mat;
@Before
public void setUp(){
mat = new MatrixManipulator();
MatrixData mData = new MatrixData();
origMatrix = mData.getOrigMatrix();
finalMatrix = mData.getFinalMatrix();
}
@Test
public void testCalculateNewMatrix(){
double[][] result = mat.calculateNewMatrix(origMatrix);
for(int i = 0; i< result.length; i++){
Assert.assertArrayEquals(finalMatrix, result,
0.0001);
}

}

}

I wanted to know if this is the proper way of accessing test data and
testing array equality.Any comments/opinions/suggestions most welcome.


The test looks fine to me.

I think I would have the input as local variable and initialize
them in the test instead of grabbing them this way.

It is easier to read with many test methods.

And it is more thread safe.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top