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

GoCountingTest.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import org.junit.Ignore;
  2. import org.junit.Test;
  3. import java.awt.Point;
  4. import java.util.HashMap;
  5. import java.util.HashSet;
  6. import java.util.Set;
  7. import static org.junit.Assert.assertEquals;
  8. import org.junit.Rule;
  9. import org.junit.rules.ExpectedException;
  10. public class GoCountingTest {
  11. @Rule
  12. public ExpectedException expectedException = ExpectedException.none();
  13. String board5x5 = " B \n" +
  14. " B B \n" +
  15. "B W B\n" +
  16. " W W \n" +
  17. " W ";
  18. @Test
  19. public void blackCorner5x5BoardTest() {
  20. GoCounting gocounting = new GoCounting(board5x5);
  21. Set<Point> territory = new HashSet<>();
  22. territory.add(new Point(0, 0));
  23. territory.add(new Point(0, 1));
  24. territory.add(new Point(1, 0));
  25. assertEquals(Player.BLACK, gocounting.getTerritoryOwner(0, 1));
  26. assertEquals(territory, gocounting.getTerritory(0, 1));
  27. }
  28. @Ignore("Remove to run test")
  29. @Test
  30. public void whiteCenter5x5BoardTest() {
  31. GoCounting gocounting = new GoCounting(board5x5);
  32. Set<Point> territory = new HashSet<>();
  33. territory.add(new Point(2, 3));
  34. assertEquals(Player.WHITE, gocounting.getTerritoryOwner(2, 3));
  35. assertEquals(territory, gocounting.getTerritory(2, 3));
  36. }
  37. @Ignore("Remove to run test")
  38. @Test
  39. public void openCorner5x5BoardTest() {
  40. GoCounting gocounting = new GoCounting(board5x5);
  41. Set<Point> territory = new HashSet<>();
  42. territory.add(new Point(0, 3));
  43. territory.add(new Point(0, 4));
  44. territory.add(new Point(1, 4));
  45. assertEquals(Player.NONE, gocounting.getTerritoryOwner(1, 4));
  46. assertEquals(territory, gocounting.getTerritory(1, 4));
  47. }
  48. @Ignore("Remove to run test")
  49. @Test
  50. public void stoneNotTerritory5x5Board() {
  51. GoCounting gocounting = new GoCounting(board5x5);
  52. Set<Point> territory = new HashSet<>();
  53. assertEquals(Player.NONE, gocounting.getTerritoryOwner(1, 1));
  54. assertEquals(territory, gocounting.getTerritory(1, 1));
  55. }
  56. @Ignore("Remove to run test")
  57. @Test
  58. public void invalidXTooLow5x5Board() {
  59. GoCounting gocounting = new GoCounting(board5x5);
  60. expectedException.expect(IllegalArgumentException.class);
  61. expectedException.expectMessage("Invalid coordinate");
  62. gocounting.getTerritory(-1, 1);
  63. }
  64. @Ignore("Remove to run test")
  65. @Test
  66. public void invalidXTooHigh5x5Board() {
  67. GoCounting gocounting = new GoCounting(board5x5);
  68. expectedException.expect(IllegalArgumentException.class);
  69. expectedException.expectMessage("Invalid coordinate");
  70. gocounting.getTerritory(5, 1);
  71. }
  72. @Ignore("Remove to run test")
  73. @Test
  74. public void invalidYTooLow5x5Board() {
  75. GoCounting gocounting = new GoCounting(board5x5);
  76. expectedException.expect(IllegalArgumentException.class);
  77. expectedException.expectMessage("Invalid coordinate");
  78. gocounting.getTerritory(1, -1);
  79. }
  80. @Ignore("Remove to run test")
  81. @Test
  82. public void invalidYTooHigh5x5Board() {
  83. GoCounting gocounting = new GoCounting(board5x5);
  84. expectedException.expect(IllegalArgumentException.class);
  85. expectedException.expectMessage("Invalid coordinate");
  86. gocounting.getTerritory(1, 5);
  87. }
  88. @Ignore("Remove to run test")
  89. @Test
  90. public void oneTerritoryIsWholeBoardTest() {
  91. GoCounting gocounting = new GoCounting(" ");
  92. HashMap<Player, Set<Point>> territories = new HashMap<>();
  93. Set<Point> blackTerritory = new HashSet<>();
  94. Set<Point> whiteTerritory = new HashSet<>();
  95. Set<Point> noneTerritory = new HashSet<>();
  96. noneTerritory.add(new Point(0, 0));
  97. territories.put(Player.BLACK, blackTerritory);
  98. territories.put(Player.WHITE, whiteTerritory);
  99. territories.put(Player.NONE, noneTerritory);
  100. assertEquals(territories, gocounting.getTerritories());
  101. }
  102. @Ignore("Remove to run test")
  103. @Test
  104. public void twoTerritoryRectangularBoardTest() {
  105. GoCounting gocounting = new GoCounting(" BW \n BW ");
  106. Set<Point> blackTerritory = new HashSet<>();
  107. blackTerritory.add(new Point(0, 0));
  108. blackTerritory.add(new Point(0, 1));
  109. Set<Point> whiteTerritory = new HashSet<>();
  110. whiteTerritory.add(new Point(3, 0));
  111. whiteTerritory.add(new Point(3, 1));
  112. Set<Point> noneTerritory = new HashSet<>();
  113. HashMap<Player, Set<Point>> territories = new HashMap<>();
  114. territories.put(Player.BLACK, blackTerritory);
  115. territories.put(Player.WHITE, whiteTerritory);
  116. territories.put(Player.NONE, noneTerritory);
  117. assertEquals(territories, gocounting.getTerritories());
  118. }
  119. @Ignore("Remove to run test")
  120. @Test
  121. public void twoRegionRectangularBoardTest() {
  122. GoCounting gocounting = new GoCounting(" B ");
  123. HashMap<Player, Set<Point>> territories = new HashMap<>();
  124. Set<Point> blackTerritory = new HashSet<>();
  125. blackTerritory.add(new Point(0, 0));
  126. blackTerritory.add(new Point(2, 0));
  127. Set<Point> whiteTerritory = new HashSet<>();
  128. Set<Point> noneTerritory = new HashSet<>();
  129. territories.put(Player.BLACK, blackTerritory);
  130. territories.put(Player.WHITE, whiteTerritory);
  131. territories.put(Player.NONE, noneTerritory);
  132. assertEquals(territories, gocounting.getTerritories());
  133. }
  134. }