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

MatrixTest.java 6.7KB

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