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.

169 lines
5.5 KiB

  1. package com.ugsbo.matrixcalc;
  2. import static org.junit.Assert.assertArrayEquals;
  3. import static org.junit.Assert.assertEquals;
  4. import static org.junit.Assert.assertFalse;
  5. import static org.junit.Assert.assertTrue;
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. /**
  9. * Tests how well worng input can be determent.
  10. */
  11. public class MatrixIOUtilsTest {
  12. private MatrixCalcIOUtils util;
  13. @Before
  14. public void setup() {
  15. util = new MatrixCalcIOUtils();
  16. }
  17. @Test(expected = IllegalArgumentException.class)
  18. public void inputEmptySouldThrowAIllegalArgumentException() {
  19. String input = "";
  20. util.checkInput(input);
  21. }
  22. @Test
  23. public void checkIfA1by3MatrixIsValidInput() {
  24. String input = "1 2 3";
  25. try {
  26. // act
  27. util.checkInput(input);
  28. // assert
  29. assertTrue("The 1 by 3 Matrix should be Matched as valid input.", true);
  30. } catch (IllegalArgumentException e) {
  31. assertFalse("The 1 by 3 Matrix should be Matched as valid input.", true);
  32. }
  33. }
  34. @Test
  35. public void checkIfA2by3MatrixIsValidInput() {
  36. // arrange
  37. String input = "1 2 3\n1 2 3";
  38. try {
  39. // act
  40. util.checkInput(input);
  41. // assert
  42. assertTrue("The 2 by 3 Matrix should be Matched as valid input.", true);
  43. } catch (IllegalArgumentException e) {
  44. assertFalse("The 2 by 3 Matrix should be Matched as valid input.", true);
  45. }
  46. }
  47. @Test
  48. public void checkIfA3by3MatrixIsValidInput() {
  49. String input = "1 2 3\n1 2 3\n1 2 3";
  50. try {
  51. util.checkInput(input);
  52. assertTrue("The 3 by 3 Matrix should be Matched as valid input.", true);
  53. } catch (IllegalArgumentException e) {
  54. assertFalse("The 3 by 3 Matrix should be Matched as valid input.", true);
  55. }
  56. }
  57. @Test
  58. public void StringWithSingleDigitNumbersToMatrix_ReturnsEquivalentMatrix() {
  59. String inputString = "1";
  60. double[][] expected = { { 1.0 } };
  61. double[][] result = util.stringToMatrix(inputString);
  62. assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
  63. }
  64. @Test
  65. public void StringWithfourDigitNumbersToMatrix_ReturnsEquivalentMatrix() {
  66. String inputString = "1 2\n3 4";
  67. double[][] expected = { { 1.0, 2.0 }, { 3.0, 4.0 } };
  68. double[][] result = util.stringToMatrix(inputString);
  69. assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
  70. assertArrayEquals("The second row is not correct", expected[1], result[1], 0.1);
  71. }
  72. @Test
  73. public void StringWithSixDigitNumbersToMatrix_Returns1by6Matrix() {
  74. String inputString = "1 2 3 4 5 6";
  75. double[][] expected = { { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 } };
  76. double[][] result = util.stringToMatrix(inputString);
  77. assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
  78. }
  79. @Test
  80. public void StringWithSixDigitNumbersToMatrix_Returns2by3Matrix() {
  81. String inputString = "1 2 3\n4 5 6";
  82. double[][] expected = { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 } };
  83. double[][] result = util.stringToMatrix(inputString);
  84. assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
  85. assertArrayEquals("The second row is not correct", expected[1], result[1], 0.1);
  86. }
  87. @Test
  88. public void StringWithSixDigitNumbersToMatrix_Returns3by2Matrix() {
  89. String inputString = "1 2\n3 4\n5 6";
  90. double[][] expected = { { 1.0, 2.0 }, { 3.0, 4.0 }, { 5.0, 6.0 } };
  91. double[][] result = util.stringToMatrix(inputString);
  92. assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
  93. assertArrayEquals("The second row is not correct", expected[1], result[1], 0.1);
  94. assertArrayEquals("The therd row is not correct", expected[2], result[2], 0.1);
  95. }
  96. @Test
  97. public void StringWithNineDigitNumbersToMatrix_Returns3by3Matrix() {
  98. String inputString = "1 2 3\n4 5 6\n7 8 9";
  99. double[][] expected = { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 }, { 7.0, 8.0, 9.0 } };
  100. double[][] result = util.stringToMatrix(inputString);
  101. assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
  102. assertArrayEquals("The second row is not correct", expected[1], result[1], 0.1);
  103. assertArrayEquals("The thierd row is not correct", expected[2], result[2], 0.1);
  104. }
  105. @Test
  106. public void convertsArrayToString_SingleNumber() {
  107. double[][] matrix = { { 1.0 } };
  108. String expected = "1.0 \n\n";
  109. String result = util.convertsArrayToStringInOrderToDisplayIt(matrix);
  110. assertEquals("The Strings do not Match", expected, result);
  111. }
  112. @Test
  113. public void convertsArrayToString_FourNumbersInARow() {
  114. double[][] matrix = { { 1.0, 2.0, 3.0, 4.0 } };
  115. String expected = "1.0 2.0 3.0 4.0 \n\n";
  116. String result = util.convertsArrayToStringInOrderToDisplayIt(matrix);
  117. assertEquals("The Strings do not Match", expected, result);
  118. }
  119. @Test
  120. public void convertsArrayToString_FourNumbersInTwoRows() {
  121. double[][] matrix = { { 1.0, 2.0 }, { 3.0, 4.0 } };
  122. String expected = "1.0 2.0 \n\n3.0 4.0 \n\n";
  123. String result = util.convertsArrayToStringInOrderToDisplayIt(matrix);
  124. assertEquals("The Strings do not Match", expected, result);
  125. }
  126. }