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.

160 lines
5.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. * Tests the funktionality to add and substract two matricies
  8. */
  9. public class MatrixAdditionAndSubstractionTest {
  10. @Test
  11. public void twoMatriciesHaveTheSameDimensions() {
  12. MatrixCalcMath math = new MatrixCalcMath();
  13. double[][] matrixA = new double[1][1];
  14. double[][] matrixB = new double[1][1];
  15. boolean result = math.checkIfMatriciesAreTheSameDimension(matrixA, matrixB);
  16. assertTrue("Two Matricies with the same Dimension were not detected as that", result);
  17. }
  18. @Test
  19. public void twoMatriciesDONOTHaveTheSameDimensions() {
  20. MatrixCalcMath math = new MatrixCalcMath();
  21. double[][] matrixA = new double[2][1];
  22. double[][] matrixB = new double[1][1];
  23. boolean result = math.checkIfMatriciesAreTheSameDimension(matrixA, matrixB);
  24. assertFalse("Two Matricies without the same Dimension were detected as that", result);
  25. }
  26. @Test
  27. public void addTwoMatriciesWithSameContent() {
  28. MatrixCalcMath math = new MatrixCalcMath();
  29. double[][] matrixA = { { 1.0, 1.0 }, { 1.0, 1.0 } };
  30. double[][] matrixB = { { 1.0, 1.0 }, { 1.0, 1.0 } };
  31. double[][] matrixC = { { 2.0, 2.0 }, { 2.0, 2.0 } };
  32. double[][] result = math.matrixAddition(matrixA, matrixB);
  33. assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1);
  34. assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
  35. }
  36. @Test
  37. public void addTwoMatriciesWithDiffrentContent() {
  38. MatrixCalcMath math = new MatrixCalcMath();
  39. double[][] matrixA = { { 7.0, 3.0 }, { 2.0, 9.0 } };
  40. double[][] matrixB = { { 6.0, 3.0 }, { 7.0, 11.0 } };
  41. double[][] matrixC = { { 13.0, 6.0 }, { 9.0, 20.0 } };
  42. double[][] result = math.matrixAddition(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(expected = IllegalArgumentException.class)
  47. public void tryToAddTowEmptyMatricies() {
  48. MatrixCalcMath math = new MatrixCalcMath();
  49. // A(0,0) B(0,0) => IllegalArgumentException
  50. double[][] matrixA = new double[0][0];
  51. double[][] matrixB = new double[0][0];
  52. math.matrixAddition(matrixA, matrixB);
  53. }
  54. @Test(expected = IllegalArgumentException.class)
  55. public void tryToAddTowNullMatrices() {
  56. MatrixCalcMath math = new MatrixCalcMath();
  57. // A(0,0) B(0,0) => IllegalArgumentException
  58. double[][] matrixA = null;
  59. double[][] matrixB = null;
  60. math.matrixAddition(matrixA, matrixB);
  61. }
  62. @Test(expected = IllegalArgumentException.class)
  63. public void tryToAddTowMatricesWithDifferentDimensions() {
  64. MatrixCalcMath math = new MatrixCalcMath();
  65. // A(0,0) B(0,0) => IllegalArgumentException
  66. double[][] matrixA = {{1.0, 2.0}};
  67. double[][] matrixB = {{1.0}, {2.0}};
  68. math.matrixAddition(matrixA, matrixB);
  69. }
  70. @Test
  71. public void substractTwoMatriciesWithSameContent() {
  72. MatrixCalcMath math = new MatrixCalcMath();
  73. double[][] matrixA = { { 1.0, 1.0 }, { 1.0, 1.0 } };
  74. double[][] matrixB = { { 1.0, 1.0 }, { 1.0, 1.0 } };
  75. double[][] matrixC = { { 0.0, 0.0 }, { 0.0, 0.0 } };
  76. double[][] result = math.matrixSubstraction(matrixA, matrixB);
  77. assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1);
  78. assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
  79. }
  80. @Test
  81. public void substractTwoMatriciesWithDiffrentContent() {
  82. MatrixCalcMath math = new MatrixCalcMath();
  83. double[][] matrixA = { { 1.0, 2.0 }, { 3.0, 4.0 } };
  84. double[][] matrixB = { { 5.0, 6.0 }, { 7.0, 8.0 } };
  85. double[][] matrixC = { { -4.0, -4.0 }, { -4.0, -4.0 } };
  86. double[][] result = math.matrixSubstraction(matrixA, matrixB);
  87. assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1);
  88. assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
  89. }
  90. @Test
  91. public void substractTwoMatriciesWithNegativeContent() {
  92. MatrixCalcMath math = new MatrixCalcMath();
  93. double[][] matrixA = { { -1.0, -2.0 }, { -3.0, -4.0 } };
  94. double[][] matrixB = { { 5.0, 6.0 }, { 7.0, 8.0 } };
  95. double[][] matrixC = { { -6.0, -8.0 }, { -10.0, -12.0 } };
  96. double[][] result = math.matrixSubstraction(matrixA, matrixB);
  97. assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1);
  98. assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
  99. }
  100. @Test(expected = IllegalArgumentException.class)
  101. public void tryToSubstractTowEmptyMatricies() {
  102. MatrixCalcMath math = new MatrixCalcMath();
  103. // A(0,0) B(0,0) => IllegalArgumentException
  104. double[][] matrixA = new double[0][0];
  105. double[][] matrixB = new double[0][0];
  106. math.matrixSubstraction(matrixA, matrixB);
  107. }
  108. @Test(expected = IllegalArgumentException.class)
  109. public void tryToSubstractTowNullMatrices() {
  110. MatrixCalcMath math = new MatrixCalcMath();
  111. // A(0,0) B(0,0) => IllegalArgumentException
  112. double[][] matrixA = null;
  113. double[][] matrixB = null;
  114. math.matrixSubstraction(matrixA, matrixB);
  115. }
  116. @Test(expected = IllegalArgumentException.class)
  117. public void tryToSubstractTowMatricesWithDifferentDimensions() {
  118. MatrixCalcMath math = new MatrixCalcMath();
  119. // A(0,0) B(0,0) => IllegalArgumentException
  120. double[][] matrixA = {{1.0, 2.0}};
  121. double[][] matrixB = {{1.0}, {2.0}};
  122. math.matrixSubstraction(matrixA, matrixB);
  123. }
  124. }