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.

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