|
|
@ -4,6 +4,7 @@ import static org.junit.Assert.assertArrayEquals; |
|
|
|
import static org.junit.Assert.assertFalse; |
|
|
|
import static org.junit.Assert.assertTrue; |
|
|
|
|
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
/** |
|
|
@ -11,49 +12,52 @@ import org.junit.Test; |
|
|
|
*/ |
|
|
|
public class MatrixAdditionAndSubstractionTest { |
|
|
|
|
|
|
|
private MatrixCalcMath matrixMath; |
|
|
|
|
|
|
|
@Before |
|
|
|
public void setup() { |
|
|
|
matrixMath = new MatrixCalcMath(); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void twoMatriciesHaveTheSameDimensions() { |
|
|
|
MatrixCalcMath math = new MatrixCalcMath(); |
|
|
|
double[][] matrixA = new double[1][1]; |
|
|
|
double[][] matrixB = new double[1][1]; |
|
|
|
|
|
|
|
boolean result = math.checkIfMatriciesAreTheSameDimension(matrixA, matrixB); |
|
|
|
boolean result = matrixMath.checkIfMatriciesAreTheSameDimension(matrixA, matrixB); |
|
|
|
|
|
|
|
assertTrue("Two Matricies with the same Dimension were not detected as that", result); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void twoMatriciesDONOTHaveTheSameDimensions() { |
|
|
|
MatrixCalcMath math = new MatrixCalcMath(); |
|
|
|
double[][] matrixA = new double[2][1]; |
|
|
|
double[][] matrixB = new double[1][1]; |
|
|
|
|
|
|
|
boolean result = math.checkIfMatriciesAreTheSameDimension(matrixA, matrixB); |
|
|
|
boolean result = matrixMath.checkIfMatriciesAreTheSameDimension(matrixA, matrixB); |
|
|
|
|
|
|
|
assertFalse("Two Matricies without the same Dimension were detected as that", result); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void addTwoMatriciesWithSameContent() { |
|
|
|
MatrixCalcMath math = new MatrixCalcMath(); |
|
|
|
double[][] matrixA = { { 1.0, 1.0 }, { 1.0, 1.0 } }; |
|
|
|
double[][] matrixB = { { 1.0, 1.0 }, { 1.0, 1.0 } }; |
|
|
|
double[][] matrixC = { { 2.0, 2.0 }, { 2.0, 2.0 } }; |
|
|
|
|
|
|
|
double[][] result = math.matrixAddition(matrixA, matrixB); |
|
|
|
double[][] result = matrixMath.matrixAddition(matrixA, matrixB); |
|
|
|
|
|
|
|
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); |
|
|
|
assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void addTwoMatriciesWithDiffrentContent() { |
|
|
|
MatrixCalcMath math = new MatrixCalcMath(); |
|
|
|
double[][] matrixA = { { 7.0, 3.0 }, { 2.0, 9.0 } }; |
|
|
|
double[][] matrixB = { { 6.0, 3.0 }, { 7.0, 11.0 } }; |
|
|
|
double[][] matrixC = { { 13.0, 6.0 }, { 9.0, 20.0 } }; |
|
|
|
|
|
|
|
double[][] result = math.matrixAddition(matrixA, matrixB); |
|
|
|
double[][] result = matrixMath.matrixAddition(matrixA, matrixB); |
|
|
|
|
|
|
|
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); |
|
|
|
assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1); |
|
|
@ -61,42 +65,38 @@ public class MatrixAdditionAndSubstractionTest { |
|
|
|
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
public void tryToAddTowEmptyMatricies() { |
|
|
|
MatrixCalcMath math = new MatrixCalcMath(); |
|
|
|
// A(0,0) B(0,0) => IllegalArgumentException |
|
|
|
double[][] matrixA = new double[0][0]; |
|
|
|
double[][] matrixB = new double[0][0]; |
|
|
|
|
|
|
|
math.matrixAddition(matrixA, matrixB); |
|
|
|
|
|
|
|
matrixMath.matrixAddition(matrixA, matrixB); |
|
|
|
} |
|
|
|
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
public void tryToAddTowNullMatrices() { |
|
|
|
MatrixCalcMath math = new MatrixCalcMath(); |
|
|
|
// A(0,0) B(0,0) => IllegalArgumentException |
|
|
|
double[][] matrixA = null; |
|
|
|
double[][] matrixB = null; |
|
|
|
|
|
|
|
math.matrixAddition(matrixA, matrixB); |
|
|
|
|
|
|
|
matrixMath.matrixAddition(matrixA, matrixB); |
|
|
|
} |
|
|
|
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
public void tryToAddTowMatricesWithDifferentDimensions() { |
|
|
|
MatrixCalcMath math = new MatrixCalcMath(); |
|
|
|
// A(0,0) B(0,0) => IllegalArgumentException |
|
|
|
double[][] matrixA = {{1.0, 2.0}}; |
|
|
|
double[][] matrixB = {{1.0}, {2.0}}; |
|
|
|
|
|
|
|
math.matrixAddition(matrixA, matrixB); |
|
|
|
double[][] matrixA = { { 1.0, 2.0 } }; |
|
|
|
double[][] matrixB = { { 1.0 }, { 2.0 } }; |
|
|
|
|
|
|
|
matrixMath.matrixAddition(matrixA, matrixB); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void substractTwoMatriciesWithSameContent() { |
|
|
|
MatrixCalcMath math = new MatrixCalcMath(); |
|
|
|
double[][] matrixA = { { 1.0, 1.0 }, { 1.0, 1.0 } }; |
|
|
|
double[][] matrixB = { { 1.0, 1.0 }, { 1.0, 1.0 } }; |
|
|
|
double[][] matrixC = { { 0.0, 0.0 }, { 0.0, 0.0 } }; |
|
|
|
|
|
|
|
double[][] result = math.matrixSubstraction(matrixA, matrixB); |
|
|
|
double[][] result = matrixMath.matrixSubstraction(matrixA, matrixB); |
|
|
|
|
|
|
|
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); |
|
|
|
assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1); |
|
|
@ -104,12 +104,11 @@ public class MatrixAdditionAndSubstractionTest { |
|
|
|
|
|
|
|
@Test |
|
|
|
public void substractTwoMatriciesWithDiffrentContent() { |
|
|
|
MatrixCalcMath math = new MatrixCalcMath(); |
|
|
|
double[][] matrixA = { { 1.0, 2.0 }, { 3.0, 4.0 } }; |
|
|
|
double[][] matrixB = { { 5.0, 6.0 }, { 7.0, 8.0 } }; |
|
|
|
double[][] matrixC = { { -4.0, -4.0 }, { -4.0, -4.0 } }; |
|
|
|
|
|
|
|
double[][] result = math.matrixSubstraction(matrixA, matrixB); |
|
|
|
double[][] result = matrixMath.matrixSubstraction(matrixA, matrixB); |
|
|
|
|
|
|
|
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); |
|
|
|
assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1); |
|
|
@ -117,12 +116,11 @@ public class MatrixAdditionAndSubstractionTest { |
|
|
|
|
|
|
|
@Test |
|
|
|
public void substractTwoMatriciesWithNegativeContent() { |
|
|
|
MatrixCalcMath math = new MatrixCalcMath(); |
|
|
|
double[][] matrixA = { { -1.0, -2.0 }, { -3.0, -4.0 } }; |
|
|
|
double[][] matrixB = { { 5.0, 6.0 }, { 7.0, 8.0 } }; |
|
|
|
double[][] matrixC = { { -6.0, -8.0 }, { -10.0, -12.0 } }; |
|
|
|
|
|
|
|
double[][] result = math.matrixSubstraction(matrixA, matrixB); |
|
|
|
double[][] result = matrixMath.matrixSubstraction(matrixA, matrixB); |
|
|
|
|
|
|
|
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); |
|
|
|
assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1); |
|
|
@ -130,32 +128,29 @@ public class MatrixAdditionAndSubstractionTest { |
|
|
|
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
public void tryToSubstractTowEmptyMatricies() { |
|
|
|
MatrixCalcMath math = new MatrixCalcMath(); |
|
|
|
// A(0,0) B(0,0) => IllegalArgumentException |
|
|
|
double[][] matrixA = new double[0][0]; |
|
|
|
double[][] matrixB = new double[0][0]; |
|
|
|
|
|
|
|
math.matrixSubstraction(matrixA, matrixB); |
|
|
|
matrixMath.matrixSubstraction(matrixA, matrixB); |
|
|
|
} |
|
|
|
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
public void tryToSubstractTowNullMatrices() { |
|
|
|
MatrixCalcMath math = new MatrixCalcMath(); |
|
|
|
// A(0,0) B(0,0) => IllegalArgumentException |
|
|
|
double[][] matrixA = null; |
|
|
|
double[][] matrixB = null; |
|
|
|
|
|
|
|
math.matrixSubstraction(matrixA, matrixB); |
|
|
|
matrixMath.matrixSubstraction(matrixA, matrixB); |
|
|
|
} |
|
|
|
|
|
|
|
@Test(expected = IllegalArgumentException.class) |
|
|
|
public void tryToSubstractTowMatricesWithDifferentDimensions() { |
|
|
|
MatrixCalcMath math = new MatrixCalcMath(); |
|
|
|
// A(0,0) B(0,0) => IllegalArgumentException |
|
|
|
double[][] matrixA = {{1.0, 2.0}}; |
|
|
|
double[][] matrixB = {{1.0}, {2.0}}; |
|
|
|
double[][] matrixA = { { 1.0, 2.0 } }; |
|
|
|
double[][] matrixB = { { 1.0 }, { 2.0 } }; |
|
|
|
|
|
|
|
math.matrixSubstraction(matrixA, matrixB); |
|
|
|
matrixMath.matrixSubstraction(matrixA, matrixB); |
|
|
|
|
|
|
|
} |
|
|
|
} |