My tests to calculate the determinant of a 2x2 matrix do not work

38 Views Asked by At

I was testing a code to calculate the determinant of a 2x2 matrix. This is the code:

package maths;

public class Matrix {

    public static int determinant(int[][] matrix){
        int result = (matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]);
        return result;
    }

}

I am 99.9% sure that this code is fine, in fact I tried displaying the matrices and results on the screen and they were fine. Then I created this test in another class.

package maths;

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class SolutionTest {
    
    @Test
    public void detTest() {
        int[][] matrix1 = { { 1, 2 }, { 3, 4 } };
        int[][] matrix2 = { { 5, 6 }, { 7, 8 } };
        int[][] matrix3 = { { -2, 3 }, { 1, -3 } };
        assertEquals(Matrix.det2(matrix1), -2);
        assertEquals(Matrix.det2(matrix2), -2);
        assertEquals(Matrix.det2(matrix3), 9);
    }
}

I have tried other matrices calculating by hand and in determinant calculators in case I was entering wrong expected solutions but I am not. In fact sometimes I get expected<3> but was <9> when I have not put 3 in the tests as expected result but 9.

0

There are 0 best solutions below