Browse Source

Refactoring and renameing of some functions

featureMatrixCalculator
Lukas Reichwein 5 years ago
parent
commit
f7bd4010f7
  1. 55
      src/test/java/com/ugsbo/matrixcalc/MatrixAdditionAndSubstractionTest.java
  2. 37
      src/test/java/com/ugsbo/matrixcalc/MatrixCalcDeterminatTest.java
  3. 32
      src/test/java/com/ugsbo/matrixcalc/MatrixInputcheckTest.java
  4. 32
      src/test/java/com/ugsbo/matrixcalc/MatrixMultiplicationTest.java
  5. 23
      src/test/java/com/ugsbo/matrixcalc/MatrixTransposeTest.java

55
src/test/java/com/ugsbo/matrixcalc/MatrixAdditionAndSubstractionTest.java

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

37
src/test/java/com/ugsbo/matrixcalc/MatrixCalcDeterminatTest.java

@ -2,6 +2,7 @@ package com.ugsbo.matrixcalc;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
/** /**
@ -9,46 +10,52 @@ import org.junit.Test;
*/ */
public class MatrixCalcDeterminatTest { public class MatrixCalcDeterminatTest {
private MatrixCalcMath matrixMath;
@Before
public void setup() {
matrixMath = new MatrixCalcMath();
}
@Test @Test
public void CalculatesTheDeterminanteOfA2by2Matrix() { public void CalculatesTheDeterminanteOfA2by2Matrix() {
MatrixCalcMath math = new MatrixCalcMath();
// A(2,2) // A(2,2)
double[][] matrixA = { { 1.0, 2.0 }, { 3.0, 4.0 } }; double[][] matrixA = { { 1.0, 2.0 }, { 3.0, 4.0 } };
double determinat = -2.0; double determinat = -2.0;
double delta = 0.01;
double result = math.calcDeterminat(matrixA);
double result = matrixMath.calcDeterminat(matrixA);
assertEquals("The Determinant is not as it should be", determinat, result, 0.01);
assertEquals("The Determinant is not as it should be", determinat, result, delta);
} }
@Test @Test
public void CalculatesTheDeterminanteOfA3by3Matrix() { public void CalculatesTheDeterminanteOfA3by3Matrix() {
MatrixCalcMath math = new MatrixCalcMath();
// A(3,3) // A(3,3)
double[][] matrixA = { { 1.0, 2.0, 1.0 }, { 3.0, 4.0, 0.0 }, {5.0, 6.0, 0.0} };
double[][] matrixA = { { 1.0, 2.0, 1.0 }, { 3.0, 4.0, 0.0 }, { 5.0, 6.0, 0.0 } };
double determinat = -2.0; double determinat = -2.0;
double result = math.calcDeterminat(matrixA);
double delta = 0.01;
assertEquals("The Determinant is not as it should be", determinat, result, 0.01);
double result = matrixMath.calcDeterminat(matrixA);
assertEquals("The Determinant is not as it should be", determinat, result, delta);
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void tryToCalculateA4by4MatrixSouldResulInIllegalArgumentException() {
MatrixCalcMath math = new MatrixCalcMath();
public void tryToCalculateA4by4Matrix_ShouldResulInIllegalArgumentException() {
// A(4,4) // A(4,4)
double[][] matrixA = { { 1.0, 2.0, 1.0, 0.0 }, { 3.0, 4.0, 0.0, 0.0 }, {5.0, 6.0, 0.0, 0.0}, {5.0, 6.0, 0.0, 0.0} };
double[][] matrixA = { { 1.0, 2.0, 1.0, 0.0 }, { 3.0, 4.0, 0.0, 0.0 }, { 5.0, 6.0, 0.0, 0.0 },
{ 5.0, 6.0, 0.0, 0.0 } };
math.calcDeterminat(matrixA);
matrixMath.calcDeterminat(matrixA);
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void tryToCalculateANonQuadraticMatrixSouldResulInIllegalArgumentException() {
MatrixCalcMath math = new MatrixCalcMath();
public void tryToCalculateANonQuadraticMatrix_SouldResulInIllegalArgumentException() {
// A(2,4) // A(2,4)
double[][] matrixA = {{5.0, 6.0, 0.0, 0.0}, {5.0, 6.0, 0.0, 0.0} };
double[][] matrixA = { { 5.0, 6.0, 0.0, 0.0 }, { 5.0, 6.0, 0.0, 0.0 } };
math.calcDeterminat(matrixA);
matrixMath.calcDeterminat(matrixA);
} }
} }

32
src/test/java/com/ugsbo/matrixcalc/MatrixInputcheckTest.java

@ -2,6 +2,7 @@ package com.ugsbo.matrixcalc;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
/** /**
@ -9,41 +10,44 @@ import org.junit.Test;
*/ */
public class MatrixInputcheckTest { public class MatrixInputcheckTest {
private MatrixCalcController controller;
@Before
public void setup() {
controller = new MatrixCalcController();
}
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void inputEmptySouldThrowAIllegalArgumentException() { public void inputEmptySouldThrowAIllegalArgumentException() {
MatrixCalcController contr = new MatrixCalcController();
String input = ""; String input = "";
contr.checkInput(input);
controller.checkInput(input);
} }
@Test @Test
public void checkIfA1by3MatrixIsMatched(){
MatrixCalcController contr = new MatrixCalcController();
public void checkIfA1by3MatrixIsValidInput() {
String input = "1 2 3"; String input = "1 2 3";
boolean result = contr.checkInput(input);
boolean result = controller.checkInput(input);
assertTrue("The 1 by 3 Matrix was not Matched but it should be.", result);
assertTrue("The 1 by 3 Matrix should be Matched as valid input.", result);
} }
@Test @Test
public void checkIfA2by3MatrixIsMatched(){
MatrixCalcController contr = new MatrixCalcController();
public void checkIfA2by3MatrixIsValidInput() {
String input = "1 2 3\n1 2 3"; String input = "1 2 3\n1 2 3";
boolean result = contr.checkInput(input);
boolean result = controller.checkInput(input);
assertTrue("The 2 by 3 Matrix was not Matched but it should be.", result);
assertTrue("The 2 by 3 Matrix should be Matched as valid input.", result);
} }
@Test @Test
public void checkIfA3by3MatrixIsMatched(){
MatrixCalcController contr = new MatrixCalcController();
public void checkIfA3by3MatrixIsValidInput() {
String input = "1 2 3\n1 2 3\n1 2 3"; String input = "1 2 3\n1 2 3\n1 2 3";
boolean result = contr.checkInput(input);
boolean result = controller.checkInput(input);
assertTrue("The 3 by 3 Matrix was not Matched but it should be.", result);
assertTrue("The 3 by 3 Matrix should be Matched as valid input.", result);
} }
} }

32
src/test/java/com/ugsbo/matrixcalc/MatrixMultiplicationTest.java

@ -4,6 +4,7 @@ import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
/** /**
@ -11,47 +12,50 @@ import org.junit.Test;
*/ */
public class MatrixMultiplicationTest { public class MatrixMultiplicationTest {
private MatrixCalcMath matrixMath;
@Before
public void setup() {
matrixMath = new MatrixCalcMath();
}
@Test @Test
public void matrixAIsLinkedToMatrixBSouldBeTrue() { public void matrixAIsLinkedToMatrixBSouldBeTrue() {
MatrixCalcMath math = new MatrixCalcMath();
double[][] matrixA = new double[1][2]; double[][] matrixA = new double[1][2];
double[][] matrixB = new double[2][1]; double[][] matrixB = new double[2][1];
boolean result = math.checkIfMatriciesAreLinked(matrixA, matrixB);
boolean result = matrixMath.checkIfMatriciesAreLinked(matrixA, matrixB);
assertTrue("Matrix A is Linked to B but it is not detected that way", result); assertTrue("Matrix A is Linked to B but it is not detected that way", result);
} }
@Test @Test
public void matrixAAndMatrixBAreNullSouldReturnFalse() { public void matrixAAndMatrixBAreNullSouldReturnFalse() {
MatrixCalcMath math = new MatrixCalcMath();
double[][] matrixA = null; double[][] matrixA = null;
double[][] matrixB = null; double[][] matrixB = null;
boolean result = math.checkIfMatriciesAreLinked(matrixA, matrixB);
boolean result = matrixMath.checkIfMatriciesAreLinked(matrixA, matrixB);
assertFalse("Matrix A and B are null but detected as Linked", result); assertFalse("Matrix A and B are null but detected as Linked", result);
} }
@Test @Test
public void matrixAAndMatrixBAreNotLinkedSouldReturnFalse() { public void matrixAAndMatrixBAreNotLinkedSouldReturnFalse() {
MatrixCalcMath math = new MatrixCalcMath();
double[][] matrixA = new double[1][1]; double[][] matrixA = new double[1][1];
double[][] matrixB = new double[2][1]; double[][] matrixB = new double[2][1];
boolean result = math.checkIfMatriciesAreLinked(matrixA, matrixB);
boolean result = matrixMath.checkIfMatriciesAreLinked(matrixA, matrixB);
assertFalse("Matrix A and B are not Linked but detected as Linked", result); assertFalse("Matrix A and B are not Linked but detected as Linked", result);
} }
@Test @Test
public void multiplyTwoMatriciesWithSameDimensionsAndSameContent() { public void multiplyTwoMatriciesWithSameDimensionsAndSameContent() {
MatrixCalcMath math = new MatrixCalcMath();
double[][] matrixA = { { 1.0, 1.0 }, { 1.0, 1.0 } }; double[][] matrixA = { { 1.0, 1.0 }, { 1.0, 1.0 } };
double[][] matrixB = { { 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[][] matrixC = { { 2.0, 2.0 }, { 2.0, 2.0 } };
double[][] result = math.matrixMultiplication(matrixA, matrixB);
double[][] result = matrixMath.matrixMultiplication(matrixA, matrixB);
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); 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); assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
@ -59,13 +63,12 @@ public class MatrixMultiplicationTest {
@Test @Test
public void multiplyTowMatriciesWithDiffrentDimensions() { public void multiplyTowMatriciesWithDiffrentDimensions() {
MatrixCalcMath math = new MatrixCalcMath();
// A(2,3) B(3,2) => C(2,2) // A(2,3) B(3,2) => C(2,2)
double[][] matrixA = { { 1.0, 1.0, 1.0 }, { 1.0, 1.0, 1.0 } }; double[][] matrixA = { { 1.0, 1.0, 1.0 }, { 1.0, 1.0, 1.0 } };
double[][] matrixB = { { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 } }; double[][] matrixB = { { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 } };
double[][] matrixC = { { 3.0, 3.0 }, { 3.0, 3.0 } }; double[][] matrixC = { { 3.0, 3.0 }, { 3.0, 3.0 } };
double[][] result = math.matrixMultiplication(matrixA, matrixB);
double[][] result = matrixMath.matrixMultiplication(matrixA, matrixB);
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); 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); assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
@ -73,13 +76,12 @@ public class MatrixMultiplicationTest {
@Test @Test
public void multiplyTowMatriciesWithDiffrentDimensionsAndDiffentContent() { public void multiplyTowMatriciesWithDiffrentDimensionsAndDiffentContent() {
MatrixCalcMath math = new MatrixCalcMath();
// A(2,3) B(3,2) => C(2,2) // A(2,3) B(3,2) => C(2,2)
double[][] matrixA = { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 } }; double[][] matrixA = { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 } };
double[][] matrixB = { { 7.0, 8.0 }, { 9.0, 10.0 }, { 11.0, 12.0 } }; double[][] matrixB = { { 7.0, 8.0 }, { 9.0, 10.0 }, { 11.0, 12.0 } };
double[][] matrixC = { { 58.0, 64.0 }, { 139.0, 154.0 } }; double[][] matrixC = { { 58.0, 64.0 }, { 139.0, 154.0 } };
double[][] result = math.matrixMultiplication(matrixA, matrixB);
double[][] result = matrixMath.matrixMultiplication(matrixA, matrixB);
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); 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); assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
@ -88,22 +90,20 @@ public class MatrixMultiplicationTest {
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void tryToMultiplyTowEmptyMatricies() { public void tryToMultiplyTowEmptyMatricies() {
MatrixCalcMath math = new MatrixCalcMath();
// A(0,0) B(0,0) => IllegalArgumentException // A(0,0) B(0,0) => IllegalArgumentException
double[][] matrixA = new double[0][0]; double[][] matrixA = new double[0][0];
double[][] matrixB = new double[0][0]; double[][] matrixB = new double[0][0];
math.matrixMultiplication(matrixA, matrixB);
matrixMath.matrixMultiplication(matrixA, matrixB);
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void tryToMultiplyTowNullObjects() { public void tryToMultiplyTowNullObjects() {
MatrixCalcMath math = new MatrixCalcMath();
// null null => IllegalArgumentException // null null => IllegalArgumentException
double[][] matrixA = null; double[][] matrixA = null;
double[][] matrixB = null; double[][] matrixB = null;
math.matrixMultiplication(matrixA, matrixB);
matrixMath.matrixMultiplication(matrixA, matrixB);
} }
} }

23
src/test/java/com/ugsbo/matrixcalc/MatrixTransposeTest.java

@ -2,6 +2,7 @@ package com.ugsbo.matrixcalc;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
/** /**
@ -9,27 +10,32 @@ import org.junit.Test;
*/ */
public class MatrixTransposeTest { public class MatrixTransposeTest {
private MatrixCalcMath matrixMath;
@Before
public void setup() {
matrixMath = new MatrixCalcMath();
}
@Test @Test
public void TransformQuadraticMatrixAResultsInQuadraticMatrixC() {
MatrixCalcMath math = new MatrixCalcMath();
public void TransformQuadraticMatrixA_ResultsInQuadraticMatrixC() {
// A(2,2) => C(2,2) // A(2,2) => C(2,2)
double[][] matrixA = { { 1.0, 2.0 }, { 3.0, 4.0 } }; double[][] matrixA = { { 1.0, 2.0 }, { 3.0, 4.0 } };
double[][] matrixC = { { 1.0, 3.0 }, { 2.0, 4.0 } }; double[][] matrixC = { { 1.0, 3.0 }, { 2.0, 4.0 } };
double[][] result = math.matrixTransponation(matrixA);
double[][] result = matrixMath.matrixTransponation(matrixA);
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); 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); assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
} }
@Test @Test
public void Transform2by3MatrixAResultsIn3by2MatrixC() {
MatrixCalcMath math = new MatrixCalcMath();
public void Transform2by3MatrixA_ResultsIn3by2MatrixC() {
// A(2,3) => C(3,2) // A(2,3) => C(3,2)
double[][] matrixA = { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 } }; double[][] matrixA = { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 } };
double[][] matrixC = { { 1.0, 4.0 }, { 2.0, 5.0 }, { 3.0, 6.0 } }; double[][] matrixC = { { 1.0, 4.0 }, { 2.0, 5.0 }, { 3.0, 6.0 } };
double[][] result = math.matrixTransponation(matrixA);
double[][] result = matrixMath.matrixTransponation(matrixA);
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); 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); assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
@ -37,13 +43,12 @@ public class MatrixTransposeTest {
} }
@Test @Test
public void Transform1by3MatrixAResultsIn3by1MatrixC() {
MatrixCalcMath math = new MatrixCalcMath();
public void Transform1by3MatrixA_ResultsIn3by1MatrixC() {
// A(1,3) => C(1,3) // A(1,3) => C(1,3)
double[][] matrixA = { { 1.0, 2.0, 3.0 } }; double[][] matrixA = { { 1.0, 2.0, 3.0 } };
double[][] matrixC = { { 1.0 }, { 2.0 }, { 3.0 } }; double[][] matrixC = { { 1.0 }, { 2.0 }, { 3.0 } };
double[][] result = math.matrixTransponation(matrixA);
double[][] result = matrixMath.matrixTransponation(matrixA);
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); 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); assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);

Loading…
Cancel
Save