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.7 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.Before;
  6. import org.junit.Test;
  7. /**
  8. * Testet die Matrix Multiplication funkionalität
  9. */
  10. public class MatrixMultiplicationTest {
  11. private MatrixCalcMath matrixMath;
  12. @Before
  13. public void setup() {
  14. matrixMath = new MatrixCalcMath();
  15. }
  16. @Test
  17. public void matrixAIsLinkedToMatrixBSouldBeTrue() {
  18. double[][] matrixA = new double[1][2];
  19. double[][] matrixB = new double[2][1];
  20. boolean result = matrixMath.checkIfMatriciesAreLinked(matrixA, matrixB);
  21. assertTrue("Matrix A is Linked to B but it is not detected that way", result);
  22. }
  23. @Test
  24. public void matrixAAndMatrixBAreNullSouldReturnFalse() {
  25. double[][] matrixA = null;
  26. double[][] matrixB = null;
  27. boolean result = matrixMath.checkIfMatriciesAreLinked(matrixA, matrixB);
  28. assertFalse("Matrix A and B are null but detected as Linked", result);
  29. }
  30. @Test
  31. public void matrixAAndMatrixBAreNotLinkedSouldReturnFalse() {
  32. double[][] matrixA = new double[1][1];
  33. double[][] matrixB = new double[2][1];
  34. boolean result = matrixMath.checkIfMatriciesAreLinked(matrixA, matrixB);
  35. assertFalse("Matrix A and B are not Linked but detected as Linked", result);
  36. }
  37. @Test
  38. public void multiplyTwoMatriciesWithSameDimensionsAndSameContent() {
  39. double[][] matrixA = { { 1.0, 1.0 }, { 1.0, 1.0 } };
  40. double[][] matrixB = { { 1.0, 1.0 }, { 1.0, 1.0 } };
  41. double[][] matrixC = { { 2.0, 2.0 }, { 2.0, 2.0 } };
  42. double[][] result = matrixMath.matrixMultiplication(matrixA, matrixB);
  43. assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1);
  44. assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
  45. }
  46. @Test
  47. public void multiplyTowMatriciesWithDiffrentDimensions() {
  48. // A(2,3) B(3,2) => C(2,2)
  49. double[][] matrixA = { { 1.0, 1.0, 1.0 }, { 1.0, 1.0, 1.0 } };
  50. double[][] matrixB = { { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 } };
  51. double[][] matrixC = { { 3.0, 3.0 }, { 3.0, 3.0 } };
  52. double[][] result = matrixMath.matrixMultiplication(matrixA, matrixB);
  53. assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1);
  54. assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
  55. }
  56. @Test
  57. public void multiplyTowMatriciesWithDiffrentDimensionsAndDiffentContent() {
  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 = matrixMath.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. // A(0,0) B(0,0) => IllegalArgumentException
  69. double[][] matrixA = new double[0][0];
  70. double[][] matrixB = new double[0][0];
  71. matrixMath.matrixMultiplication(matrixA, matrixB);
  72. }
  73. @Test(expected = IllegalArgumentException.class)
  74. public void tryToMultiplyTowNullObjects() {
  75. // null null => IllegalArgumentException
  76. double[][] matrixA = null;
  77. double[][] matrixB = null;
  78. matrixMath.matrixMultiplication(matrixA, matrixB);
  79. }
  80. }