lots of exercises in java... from https://github.com/exercism/java

MatrixTest.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import org.junit.Ignore;
  2. import org.junit.Test;
  3. import org.junit.experimental.runners.Enclosed;
  4. import org.junit.runner.RunWith;
  5. import org.junit.runners.Parameterized;
  6. import java.util.Arrays;
  7. import java.util.Collection;
  8. import static org.junit.Assert.assertArrayEquals;
  9. import static org.junit.Assert.assertEquals;
  10. @RunWith(Enclosed.class)
  11. public class MatrixTest {
  12. @RunWith(Parameterized.class)
  13. public static class GetFirstRowTest {
  14. private Matrix matrix;
  15. private int[] firstRow;
  16. @Parameterized.Parameters(name = "{index}: checking first row of matrix constructed with string \"{0}\".")
  17. public static Collection<Object[]> data() {
  18. return Arrays.asList(new Object[][]{
  19. {"0", new int[] {0}},
  20. {"0 1", new int[] {0, 1}},
  21. {"0\\n1", new int[] {0}},
  22. {"0 1\\n2 3", new int[] {0, 1}},
  23. {"0 1 2\\n3 4 5\\n6 7 8", new int[] {0, 1, 2}}
  24. });
  25. }
  26. public GetFirstRowTest(String matrixAsString, int[] firstRow) {
  27. this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
  28. this.firstRow = firstRow;
  29. }
  30. @Test
  31. public void getFirstRowTest() {
  32. assertArrayEquals(matrix.getRow(0), firstRow);
  33. }
  34. }
  35. @RunWith(Parameterized.class)
  36. public static class GetLastRowTest {
  37. private Matrix matrix;
  38. private int[] lastRow;
  39. @Parameterized.Parameters(name = "{index}: checking last row of matrix constructed with string \"{0}\".")
  40. public static Collection<Object[]> data() {
  41. return Arrays.asList(new Object[][]{
  42. {"0", new int[] {0}},
  43. {"0 1", new int[] {0, 1}},
  44. {"0\\n1", new int[] {1}},
  45. {"0 1\\n2 3", new int[] {2, 3}},
  46. {"0 1 2\\n3 4 5\\n6 7 8", new int[] {6, 7, 8}}
  47. });
  48. }
  49. public GetLastRowTest(String matrixAsString, int[] lastRow) {
  50. this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
  51. this.lastRow = lastRow;
  52. }
  53. @Ignore
  54. @Test
  55. public void getLastRowTest() {
  56. assertArrayEquals(matrix.getRow(matrix.getRowsCount() - 1), lastRow);
  57. }
  58. }
  59. @RunWith(Parameterized.class)
  60. public static class GetFirstColumnTest {
  61. private Matrix matrix;
  62. private int[] firstColumn;
  63. @Parameterized.Parameters(name = "{index}: checking first column of matrix constructed with string \"{0}\".")
  64. public static Collection<Object[]> data() {
  65. return Arrays.asList(new Object[][]{
  66. {"0", new int[] {0}},
  67. {"0 1", new int[] {0}},
  68. {"0\\n1", new int[] {0, 1}},
  69. {"0 1\\n2 3", new int[] {0, 2}},
  70. {"0 1 2\\n3 4 5\\n6 7 8", new int[] {0, 3, 6}}
  71. });
  72. }
  73. public GetFirstColumnTest(String matrixAsString, int[] firstColumn) {
  74. this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
  75. this.firstColumn = firstColumn;
  76. }
  77. @Ignore
  78. @Test
  79. public void getFirstColumnTest() {
  80. assertArrayEquals(matrix.getColumn(0), firstColumn);
  81. }
  82. }
  83. @RunWith(Parameterized.class)
  84. public static class GetLastColumnTest {
  85. private Matrix matrix;
  86. private int[] lastColumn;
  87. @Parameterized.Parameters(name = "{index}: checking last column of matrix constructed with string \"{0}\".")
  88. public static Collection<Object[]> data() {
  89. return Arrays.asList(new Object[][]{
  90. {"0", new int[] {0}},
  91. {"0 1", new int[] {1}},
  92. {"0\\n1", new int[] {0, 1}},
  93. {"0 1\\n2 3", new int[] {1, 3}},
  94. {"0 1 2\\n3 4 5\\n6 7 8", new int[] {2, 5, 8}}
  95. });
  96. }
  97. public GetLastColumnTest(String matrixAsString, int[] lastColumn) {
  98. this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
  99. this.lastColumn = lastColumn;
  100. }
  101. @Ignore
  102. @Test
  103. public void getLastColumnTest() {
  104. assertArrayEquals(matrix.getColumn(matrix.getColumnsCount() - 1), lastColumn);
  105. }
  106. }
  107. @RunWith(Parameterized.class)
  108. public static class CountColumnsTest {
  109. private Matrix matrix;
  110. private int numberOfColumns;
  111. @Parameterized.Parameters(name = "{index}: expected matrix constructed with string \"{0}\" to have {1} column(s).")
  112. public static Collection<Object[]> data() {
  113. return Arrays.asList(new Object[][]{
  114. {"0", 1},
  115. {"0 1", 2},
  116. {"0\\n1", 1},
  117. {"0 1\\n2 3", 2},
  118. {"0 1 2\\n3 4 5\\n6 7 8", 3}
  119. });
  120. }
  121. public CountColumnsTest(String matrixAsString, int numberOfColumns) {
  122. this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
  123. this.numberOfColumns = numberOfColumns;
  124. }
  125. @Ignore
  126. @Test
  127. public void countColumnsTest() {
  128. assertEquals(matrix.getColumnsCount(), numberOfColumns);
  129. }
  130. }
  131. @RunWith(Parameterized.class)
  132. public static class CountRowsTest {
  133. private Matrix matrix;
  134. private int numberOfRows;
  135. @Parameterized.Parameters(name = "{index}: expected matrix constructed with string \"{0}\" to have {1} row(s).")
  136. public static Collection<Object[]> data() {
  137. return Arrays.asList(new Object[][]{
  138. {"0", 1},
  139. {"0 1", 1},
  140. {"0\\n1", 2},
  141. {"0 1\\n2 3", 2},
  142. {"0 1 2\\n3 4 5\\n6 7 8", 3}
  143. });
  144. }
  145. public CountRowsTest(String matrixAsString, int numberOfRows) {
  146. this.matrix = new Matrix(matrixAsString.replaceAll("\\\\n", "\n"));
  147. this.numberOfRows = numberOfRows;
  148. }
  149. @Ignore
  150. @Test
  151. public void countRowsTest() {
  152. assertEquals(matrix.getRowsCount(), numberOfRows);
  153. }
  154. }
  155. }