From e57a53d6d05b5004b07923deb84de9892f3e5241 Mon Sep 17 00:00:00 2001 From: Lukas Reichwein Date: Thu, 4 Jul 2019 09:12:12 +0200 Subject: [PATCH] Added and chnged tests in order to test Exceptions instead of if the funktion is returning null --- .../MatrixAdditionAndSubstractionTest.java | 71 +++++++++++++++++-- .../matrixcalc/MatrixMultiplicationTest.java | 28 ++++---- .../ugsbo/matrixcalc/MatrixTransposeTest.java | 8 ++- 3 files changed, 84 insertions(+), 23 deletions(-) diff --git a/src/test/java/com/ugsbo/matrixcalc/MatrixAdditionAndSubstractionTest.java b/src/test/java/com/ugsbo/matrixcalc/MatrixAdditionAndSubstractionTest.java index 44ebfa8..e80d737 100644 --- a/src/test/java/com/ugsbo/matrixcalc/MatrixAdditionAndSubstractionTest.java +++ b/src/test/java/com/ugsbo/matrixcalc/MatrixAdditionAndSubstractionTest.java @@ -43,7 +43,7 @@ public class MatrixAdditionAndSubstractionTest { double[][] result = math.matrixAddition(matrixA, matrixB); assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); - assertArrayEquals("The seound row is not correct", matrixC[1], result[1], 0.1); + assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1); } @Test @@ -56,7 +56,37 @@ public class MatrixAdditionAndSubstractionTest { double[][] result = math.matrixAddition(matrixA, matrixB); assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); - assertArrayEquals("The seound row is not correct", matrixC[1], result[1], 0.1); + assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1); + } + + @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); + } + + @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); + } + + @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); } @Test @@ -69,7 +99,7 @@ public class MatrixAdditionAndSubstractionTest { double[][] result = math.matrixSubstraction(matrixA, matrixB); assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); - assertArrayEquals("The seound row is not correct", matrixC[1], result[1], 0.1); + assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1); } @Test @@ -82,7 +112,7 @@ public class MatrixAdditionAndSubstractionTest { double[][] result = math.matrixSubstraction(matrixA, matrixB); assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); - assertArrayEquals("The seound row is not correct", matrixC[1], result[1], 0.1); + assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1); } @Test @@ -95,6 +125,37 @@ public class MatrixAdditionAndSubstractionTest { double[][] result = math.matrixSubstraction(matrixA, matrixB); assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); - assertArrayEquals("The seound row is not correct", matrixC[1], result[1], 0.1); + assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1); + } + + @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); + } + + @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); + } + + @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}}; + + math.matrixSubstraction(matrixA, matrixB); + } } \ 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 c09e80d..eb02ee2 100644 --- a/src/test/java/com/ugsbo/matrixcalc/MatrixMultiplicationTest.java +++ b/src/test/java/com/ugsbo/matrixcalc/MatrixMultiplicationTest.java @@ -54,7 +54,7 @@ public class MatrixMultiplicationTest { double[][] result = math.matrixMultiplication(matrixA, matrixB); assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); - assertArrayEquals("The seound row is not correct", matrixC[1], result[1], 0.1); + assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1); } @Test @@ -68,7 +68,7 @@ public class MatrixMultiplicationTest { double[][] result = math.matrixMultiplication(matrixA, matrixB); assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); - assertArrayEquals("The seound row is not correct", matrixC[1], result[1], 0.1); + assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1); } @Test @@ -82,30 +82,28 @@ public class MatrixMultiplicationTest { double[][] result = math.matrixMultiplication(matrixA, matrixB); assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); - assertArrayEquals("The seound 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(expected = IllegalArgumentException.class) + public void tryToMultiplyTowEmptyMatricies() { MatrixCalcMath math = new MatrixCalcMath(); - // A(0,0) B(0,0) => null + // A(0,0) B(0,0) => IllegalArgumentException double[][] matrixA = new double[0][0]; double[][] matrixB = new double[0][0]; - - double[][] result = math.matrixMultiplication(matrixA, matrixB); - - assertArrayEquals(null, result); + + math.matrixMultiplication(matrixA, matrixB); } - @Test + @Test(expected = IllegalArgumentException.class) + public void tryToMultiplyTowNullObjects() { MatrixCalcMath math = new MatrixCalcMath(); - // null null => null + // null null => IllegalArgumentException double[][] matrixA = null; double[][] matrixB = null; - - double[][] result = math.matrixMultiplication(matrixA, matrixB); - - assertArrayEquals(null, result); + + math.matrixMultiplication(matrixA, matrixB); } } diff --git a/src/test/java/com/ugsbo/matrixcalc/MatrixTransposeTest.java b/src/test/java/com/ugsbo/matrixcalc/MatrixTransposeTest.java index c6a9918..7bfa6de 100644 --- a/src/test/java/com/ugsbo/matrixcalc/MatrixTransposeTest.java +++ b/src/test/java/com/ugsbo/matrixcalc/MatrixTransposeTest.java @@ -19,7 +19,7 @@ public class MatrixTransposeTest { double[][] result = math.matrixTransponation(matrixA); assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); - assertArrayEquals("The seound row is not correct", matrixC[1], result[1], 0.1); + assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1); } @Test @@ -32,7 +32,8 @@ public class MatrixTransposeTest { double[][] result = math.matrixTransponation(matrixA); assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); - assertArrayEquals("The seound row is not correct", matrixC[1], result[1], 0.1); + assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1); + assertArrayEquals("The third row is not correct", matrixC[2], result[2], 0.1); } @Test @@ -45,6 +46,7 @@ public class MatrixTransposeTest { double[][] result = math.matrixTransponation(matrixA); assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); - assertArrayEquals("The seound row is not correct", matrixC[1], result[1], 0.1); + assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1); + assertArrayEquals("The third row is not correct", matrixC[2], result[2], 0.1); } }