Ultra Geile Studenten Benutzer Oberfläche (UGSBO)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.9 KiB

  1. package com.ugsbo.matrixcalc;
  2. import static org.junit.Assert.assertArrayEquals;
  3. import static org.junit.Assert.assertFalse;
  4. import static org.junit.Assert.assertTrue;
  5. import org.junit.Test;
  6. /**
  7. * Testet die Matrix Multiplication funkionalität
  8. */
  9. public class MatrixMultiplicationTest {
  10. @Test
  11. public void matrixAIsLinkedToMatrixBSouldBeTrue() {
  12. MatrixCalcMath math = new MatrixCalcMath();
  13. double[][] matrixA = new double[1][2];
  14. double[][] matrixB = new double[2][1];
  15. boolean result = math.checkIfMatriciesAreLinked(matrixA, matrixB);
  16. assertTrue("Matrix A is Linked to B but it is not detected that way", result);
  17. }
  18. @Test
  19. public void matrixAAndMatrixBAreNullSouldReturnFalse() {
  20. MatrixCalcMath math = new MatrixCalcMath();
  21. double[][] matrixA = null;
  22. double[][] matrixB = null;
  23. boolean result = math.checkIfMatriciesAreLinked(matrixA, matrixB);
  24. assertFalse("Matrix A and B are null but detected as Linked", result);
  25. }
  26. @Test
  27. public void matrixAAndMatrixBAreNotLinkedSouldReturnFalse() {
  28. MatrixCalcMath math = new MatrixCalcMath();
  29. double[][] matrixA = new double[1][1];
  30. double[][] matrixB = new double[2][1];
  31. boolean result = math.checkIfMatriciesAreLinked(matrixA, matrixB);
  32. assertFalse("Matrix A and B are not Linked but detected as Linked", result);
  33. }
  34. @Test
  35. public void multiplyTwoMatriciesWithSameDimensionsAndSameContent() {
  36. MatrixCalcMath math = new MatrixCalcMath();
  37. double[][] matrixA = { { 1.0, 1.0 }, { 1.0, 1.0 } };
  38. double[][] matrixB = { { 1.0, 1.0 }, { 1.0, 1.0 } };
  39. double[][] matrixC = { { 2.0, 2.0 }, { 2.0, 2.0 } };
  40. double[][] result = math.matrixMultiplication(matrixA, matrixB);
  41. assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1);
  42. assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
  43. }
  44. @Test
  45. public void multiplyTowMatriciesWithDiffrentDimensions() {
  46. MatrixCalcMath math = new MatrixCalcMath();
  47. // A(2,3) B(3,2) => C(2,2)
  48. double[][] matrixA = { { 1.0, 1.0, 1.0 }, { 1.0, 1.0, 1.0 } };
  49. double[][] matrixB = { { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 } };
  50. double[][] matrixC = { { 3.0, 3.0 }, { 3.0, 3.0 } };
  51. double[][] result = math.matrixMultiplication(matrixA, matrixB);
  52. assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1);
  53. assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
  54. }
  55. @Test
  56. public void multiplyTowMatriciesWithDiffrentDimensionsAndDiffentContent() {
  57. MatrixCalcMath math = new MatrixCalcMath();
  58. // A(2,3) B(3,2) => C(2,2)
  59. double[][] matrixA = { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 } };
  60. double[][] matrixB = { { 7.0, 8.0 }, { 9.0, 10.0 }, { 11.0, 12.0 } };
  61. double[][] matrixC = { { 58.0, 64.0 }, { 139.0, 154.0 } };
  62. double[][] result = math.matrixMultiplication(matrixA, matrixB);
  63. assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1);
  64. assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
  65. }
  66. @Test(expected = IllegalArgumentException.class)
  67. public void tryToMultiplyTowEmptyMatricies() {
  68. MatrixCalcMath math = new MatrixCalcMath();
  69. // A(0,0) B(0,0) => IllegalArgumentException
  70. double[][] matrixA = new double[0][0];
  71. double[][] matrixB = new double[0][0];
  72. math.matrixMultiplication(matrixA, matrixB);
  73. }
  74. @Test(expected = IllegalArgumentException.class)
  75. public void tryToMultiplyTowNullObjects() {
  76. MatrixCalcMath math = new MatrixCalcMath();
  77. // null null => IllegalArgumentException
  78. double[][] matrixA = null;
  79. double[][] matrixB = null;
  80. math.matrixMultiplication(matrixA, matrixB);
  81. }
  82. }