From f7bd4010f7fca34e25b054ef925d99e01d72e885 Mon Sep 17 00:00:00 2001 From: Lukas Reichwein Date: Wed, 10 Jul 2019 14:14:02 +0200 Subject: [PATCH] Refactoring and renameing of some functions --- .../MatrixAdditionAndSubstractionTest.java | 63 +++++++++---------- .../matrixcalc/MatrixCalcDeterminatTest.java | 47 ++++++++------ .../matrixcalc/MatrixInputcheckTest.java | 40 ++++++------ .../matrixcalc/MatrixMultiplicationTest.java | 38 +++++------ .../ugsbo/matrixcalc/MatrixTransposeTest.java | 23 ++++--- 5 files changed, 111 insertions(+), 100 deletions(-) diff --git a/src/test/java/com/ugsbo/matrixcalc/MatrixAdditionAndSubstractionTest.java b/src/test/java/com/ugsbo/matrixcalc/MatrixAdditionAndSubstractionTest.java index e80d737..7a83419 100644 --- a/src/test/java/com/ugsbo/matrixcalc/MatrixAdditionAndSubstractionTest.java +++ b/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.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); } } \ No newline at end of file diff --git a/src/test/java/com/ugsbo/matrixcalc/MatrixCalcDeterminatTest.java b/src/test/java/com/ugsbo/matrixcalc/MatrixCalcDeterminatTest.java index a40ff64..9031ad7 100644 --- a/src/test/java/com/ugsbo/matrixcalc/MatrixCalcDeterminatTest.java +++ b/src/test/java/com/ugsbo/matrixcalc/MatrixCalcDeterminatTest.java @@ -2,6 +2,7 @@ package com.ugsbo.matrixcalc; import static org.junit.Assert.assertEquals; +import org.junit.Before; import org.junit.Test; /** @@ -9,46 +10,52 @@ import org.junit.Test; */ public class MatrixCalcDeterminatTest { + private MatrixCalcMath matrixMath; + + @Before + public void setup() { + matrixMath = new MatrixCalcMath(); + } + @Test public void CalculatesTheDeterminanteOfA2by2Matrix() { - MatrixCalcMath math = new MatrixCalcMath(); // A(2,2) double[][] matrixA = { { 1.0, 2.0 }, { 3.0, 4.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 public void CalculatesTheDeterminanteOfA3by3Matrix() { - MatrixCalcMath math = new MatrixCalcMath(); // 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 result = math.calcDeterminat(matrixA); - - assertEquals("The Determinant is not as it should be", determinat, result, 0.01); + double delta = 0.01; + + double result = matrixMath.calcDeterminat(matrixA); + + assertEquals("The Determinant is not as it should be", determinat, result, delta); - } @Test(expected = IllegalArgumentException.class) - public void tryToCalculateA4by4MatrixSouldResulInIllegalArgumentException() { - MatrixCalcMath math = new MatrixCalcMath(); + public void tryToCalculateA4by4Matrix_ShouldResulInIllegalArgumentException() { // 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} }; - - math.calcDeterminat(matrixA); + 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 } }; + + matrixMath.calcDeterminat(matrixA); } @Test(expected = IllegalArgumentException.class) - public void tryToCalculateANonQuadraticMatrixSouldResulInIllegalArgumentException() { - MatrixCalcMath math = new MatrixCalcMath(); + public void tryToCalculateANonQuadraticMatrix_SouldResulInIllegalArgumentException() { // A(2,4) - double[][] matrixA = {{5.0, 6.0, 0.0, 0.0}, {5.0, 6.0, 0.0, 0.0} }; - - math.calcDeterminat(matrixA); + double[][] matrixA = { { 5.0, 6.0, 0.0, 0.0 }, { 5.0, 6.0, 0.0, 0.0 } }; + + matrixMath.calcDeterminat(matrixA); } -} \ No newline at end of file +} diff --git a/src/test/java/com/ugsbo/matrixcalc/MatrixInputcheckTest.java b/src/test/java/com/ugsbo/matrixcalc/MatrixInputcheckTest.java index 483e000..04593c5 100644 --- a/src/test/java/com/ugsbo/matrixcalc/MatrixInputcheckTest.java +++ b/src/test/java/com/ugsbo/matrixcalc/MatrixInputcheckTest.java @@ -2,6 +2,7 @@ package com.ugsbo.matrixcalc; import static org.junit.Assert.assertTrue; +import org.junit.Before; import org.junit.Test; /** @@ -9,41 +10,44 @@ import org.junit.Test; */ public class MatrixInputcheckTest { + private MatrixCalcController controller; + + @Before + public void setup() { + controller = new MatrixCalcController(); + } + @Test(expected = IllegalArgumentException.class) public void inputEmptySouldThrowAIllegalArgumentException() { - MatrixCalcController contr = new MatrixCalcController(); - String input = ""; + String input = ""; - contr.checkInput(input); + controller.checkInput(input); } @Test - public void checkIfA1by3MatrixIsMatched(){ - MatrixCalcController contr = new MatrixCalcController(); - String input = "1 2 3"; + public void checkIfA1by3MatrixIsValidInput() { + 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 - public void checkIfA2by3MatrixIsMatched(){ - MatrixCalcController contr = new MatrixCalcController(); - String input = "1 2 3\n1 2 3"; + public void checkIfA2by3MatrixIsValidInput() { + 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 - public void checkIfA3by3MatrixIsMatched(){ - MatrixCalcController contr = new MatrixCalcController(); - String input = "1 2 3\n1 2 3\n1 2 3"; + public void checkIfA3by3MatrixIsValidInput() { + 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); } } \ No newline at end of file diff --git a/src/test/java/com/ugsbo/matrixcalc/MatrixMultiplicationTest.java b/src/test/java/com/ugsbo/matrixcalc/MatrixMultiplicationTest.java index eb02ee2..e322d1f 100644 --- a/src/test/java/com/ugsbo/matrixcalc/MatrixMultiplicationTest.java +++ b/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.assertTrue; +import org.junit.Before; import org.junit.Test; /** @@ -11,47 +12,50 @@ import org.junit.Test; */ public class MatrixMultiplicationTest { + private MatrixCalcMath matrixMath; + + @Before + public void setup() { + matrixMath = new MatrixCalcMath(); + } + @Test public void matrixAIsLinkedToMatrixBSouldBeTrue() { - MatrixCalcMath math = new MatrixCalcMath(); double[][] matrixA = new double[1][2]; 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); } @Test public void matrixAAndMatrixBAreNullSouldReturnFalse() { - MatrixCalcMath math = new MatrixCalcMath(); double[][] matrixA = 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); } @Test public void matrixAAndMatrixBAreNotLinkedSouldReturnFalse() { - MatrixCalcMath math = new MatrixCalcMath(); double[][] matrixA = new double[1][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); } @Test public void multiplyTwoMatriciesWithSameDimensionsAndSameContent() { - 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.matrixMultiplication(matrixA, matrixB); + double[][] result = matrixMath.matrixMultiplication(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); @@ -59,13 +63,12 @@ public class MatrixMultiplicationTest { @Test public void multiplyTowMatriciesWithDiffrentDimensions() { - MatrixCalcMath math = new MatrixCalcMath(); // A(2,3) B(3,2) => C(2,2) 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[][] 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 second row is not correct", matrixC[1], result[1], 0.1); @@ -73,13 +76,12 @@ public class MatrixMultiplicationTest { @Test public void multiplyTowMatriciesWithDiffrentDimensionsAndDiffentContent() { - MatrixCalcMath math = new MatrixCalcMath(); // A(2,3) B(3,2) => C(2,2) 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[][] 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 second row is not correct", matrixC[1], result[1], 0.1); @@ -88,22 +90,20 @@ public class MatrixMultiplicationTest { @Test(expected = IllegalArgumentException.class) public void tryToMultiplyTowEmptyMatricies() { - MatrixCalcMath math = new MatrixCalcMath(); // A(0,0) B(0,0) => IllegalArgumentException double[][] matrixA = new double[0][0]; double[][] matrixB = new double[0][0]; - - math.matrixMultiplication(matrixA, matrixB); + + matrixMath.matrixMultiplication(matrixA, matrixB); } @Test(expected = IllegalArgumentException.class) public void tryToMultiplyTowNullObjects() { - MatrixCalcMath math = new MatrixCalcMath(); // null null => IllegalArgumentException double[][] matrixA = null; double[][] matrixB = null; - - math.matrixMultiplication(matrixA, matrixB); + + matrixMath.matrixMultiplication(matrixA, matrixB); } } diff --git a/src/test/java/com/ugsbo/matrixcalc/MatrixTransposeTest.java b/src/test/java/com/ugsbo/matrixcalc/MatrixTransposeTest.java index 7bfa6de..51a5f55 100644 --- a/src/test/java/com/ugsbo/matrixcalc/MatrixTransposeTest.java +++ b/src/test/java/com/ugsbo/matrixcalc/MatrixTransposeTest.java @@ -2,6 +2,7 @@ package com.ugsbo.matrixcalc; import static org.junit.Assert.assertArrayEquals; +import org.junit.Before; import org.junit.Test; /** @@ -9,27 +10,32 @@ import org.junit.Test; */ public class MatrixTransposeTest { + private MatrixCalcMath matrixMath; + + @Before + public void setup() { + matrixMath = new MatrixCalcMath(); + } + @Test - public void TransformQuadraticMatrixAResultsInQuadraticMatrixC() { - MatrixCalcMath math = new MatrixCalcMath(); + public void TransformQuadraticMatrixA_ResultsInQuadraticMatrixC() { // A(2,2) => C(2,2) double[][] matrixA = { { 1.0, 2.0 }, { 3.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 second row is not correct", matrixC[1], result[1], 0.1); } @Test - public void Transform2by3MatrixAResultsIn3by2MatrixC() { - MatrixCalcMath math = new MatrixCalcMath(); + public void Transform2by3MatrixA_ResultsIn3by2MatrixC() { // A(2,3) => C(3,2) 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[][] result = math.matrixTransponation(matrixA); + double[][] result = matrixMath.matrixTransponation(matrixA); 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); @@ -37,13 +43,12 @@ public class MatrixTransposeTest { } @Test - public void Transform1by3MatrixAResultsIn3by1MatrixC() { - MatrixCalcMath math = new MatrixCalcMath(); + public void Transform1by3MatrixA_ResultsIn3by1MatrixC() { // A(1,3) => C(1,3) double[][] matrixA = { { 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 second row is not correct", matrixC[1], result[1], 0.1);