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

BowlingTest.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. import org.junit.Before;
  2. import org.junit.Ignore;
  3. import org.junit.Rule;
  4. import org.junit.Test;
  5. import org.junit.rules.ExpectedException;
  6. import static org.junit.Assert.assertEquals;
  7. public class BowlingTest {
  8. private BowlingGame game;
  9. @Rule
  10. public ExpectedException expectedException = ExpectedException.none();
  11. @Before
  12. public void setup() {
  13. game = new BowlingGame();
  14. }
  15. private void playGame(int[] rolls) {
  16. for (int pins : rolls) {
  17. game.roll(pins);
  18. }
  19. }
  20. @Test
  21. public void shouldBeAbleToScoreAGameWithAllZeros() {
  22. int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  23. playGame(rolls);
  24. assertEquals(0, game.score());
  25. }
  26. @Ignore("Remove to run test")
  27. @Test
  28. public void shouldBeAbleToScoreAGameWithNoStrikesOrSpares() {
  29. int[] rolls = {3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6};
  30. playGame(rolls);
  31. assertEquals(90, game.score());
  32. }
  33. @Ignore("Remove to run test")
  34. @Test
  35. public void aSpareFollowedByZerosIsWorthTenPoints() {
  36. int[] rolls = {6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  37. playGame(rolls);
  38. assertEquals(10, game.score());
  39. }
  40. @Ignore("Remove to run test")
  41. @Test
  42. public void pointsScoredInTheRollAfterASpareAreCountedTwice() {
  43. int[] rolls = {6, 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  44. playGame(rolls);
  45. assertEquals(16, game.score());
  46. }
  47. @Ignore("Remove to run test")
  48. @Test
  49. public void consecutiveSparesEachGetAOneRollBonus() {
  50. int[] rolls = {5, 5, 3, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  51. playGame(rolls);
  52. assertEquals(31, game.score());
  53. }
  54. @Ignore("Remove to run test")
  55. @Test
  56. public void aSpareInTheLastFrameGetsAOneRollBonusThatIsCountedOnce() {
  57. int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 7};
  58. playGame(rolls);
  59. assertEquals(17, game.score());
  60. }
  61. @Ignore("Remove to run test")
  62. @Test
  63. public void aStrikeEarnsTenPointsInFrameWithASingleRoll() {
  64. int[] rolls = {10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  65. playGame(rolls);
  66. assertEquals(10, game.score());
  67. }
  68. @Ignore("Remove to run test")
  69. @Test
  70. public void pointsScoredInTheTwoRollsAfterAStrikeAreCountedTwiceAsABonus() {
  71. int[] rolls = {10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  72. playGame(rolls);
  73. assertEquals(26, game.score());
  74. }
  75. @Ignore("Remove to run test")
  76. @Test
  77. public void consecutiveStrikesEachGetTheTwoRollBonus() {
  78. int[] rolls = {10, 10, 10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  79. playGame(rolls);
  80. assertEquals(81, game.score());
  81. }
  82. @Ignore("Remove to run test")
  83. @Test
  84. public void aStrikeInTheLastFrameGetsATwoRollBonusThatIsCountedOnce() {
  85. int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 1};
  86. playGame(rolls);
  87. assertEquals(18, game.score());
  88. }
  89. @Ignore("Remove to run test")
  90. @Test
  91. public void rollingASpareWithTheTwoRollBonusDoesNotGetABonusRoll() {
  92. int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 3};
  93. playGame(rolls);
  94. assertEquals(20, game.score());
  95. }
  96. @Ignore("Remove to run test")
  97. @Test
  98. public void strikesWithTheTwoRollBonusDoNotGetBonusRolls() {
  99. int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10};
  100. playGame(rolls);
  101. assertEquals(30, game.score());
  102. }
  103. @Ignore("Remove to run test")
  104. @Test
  105. public void aStrikeWithTheOneRollBonusAfterASpareInTheLastFrameDoesNotGetABonus() {
  106. int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 10};
  107. playGame(rolls);
  108. assertEquals(20, game.score());
  109. }
  110. @Ignore("Remove to run test")
  111. @Test
  112. public void allStrikesIsAPerfectGame() {
  113. int[] rolls = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10};
  114. playGame(rolls);
  115. assertEquals(300, game.score());
  116. }
  117. @Ignore("Remove to run test")
  118. @Test
  119. public void rollsCanNotScoreNegativePoints() {
  120. int[] rolls = {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  121. playGame(rolls);
  122. expectedException.expect(IllegalStateException.class);
  123. expectedException.expectMessage("Negative roll is invalid");
  124. game.score();
  125. }
  126. @Ignore("Remove to run test")
  127. @Test
  128. public void aRollCanNotScoreMoreThan10Points() {
  129. int[] rolls = {11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  130. playGame(rolls);
  131. expectedException.expect(IllegalStateException.class);
  132. expectedException.expectMessage("Pin count exceeds pins on the lane");
  133. game.score();
  134. }
  135. @Ignore("Remove to run test")
  136. @Test
  137. public void twoRollsInAFrameCanNotScoreMoreThan10Points() {
  138. int[] rolls = {5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  139. playGame(rolls);
  140. expectedException.expect(IllegalStateException.class);
  141. expectedException.expectMessage("Pin count exceeds pins on the lane");
  142. game.score();
  143. }
  144. @Ignore("Remove to run test")
  145. @Test
  146. public void bonusRollAfterAStrikeInTheLastFrameCanNotScoreMoreThan10Points() {
  147. int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 0};
  148. playGame(rolls);
  149. expectedException.expect(IllegalStateException.class);
  150. expectedException.expectMessage("Pin count exceeds pins on the lane");
  151. game.score();
  152. }
  153. @Ignore("Remove to run test")
  154. @Test
  155. public void twoBonusRollsAfterAStrikeInTheLastFrameCanNotScoreMoreThan10Points() {
  156. int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 5, 6};
  157. playGame(rolls);
  158. expectedException.expect(IllegalStateException.class);
  159. expectedException.expectMessage("Pin count exceeds pins on the lane");
  160. game.score();
  161. }
  162. @Ignore("Remove to run test")
  163. @Test
  164. public void twoBonusRollsAfterAStrikeInTheLastFrameCanScoreMoreThan10PointsIfOneIsAStrike() {
  165. int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 6};
  166. playGame(rolls);
  167. assertEquals(26, game.score());
  168. }
  169. @Ignore("Remove to run test")
  170. @Test
  171. public void theSecondBonusRollsAfterAStrikeInTheLastFrameCanNotBeAStrikeIfTheFirstOneIsNotAStrike() {
  172. int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 6, 10};
  173. playGame(rolls);
  174. expectedException.expect(IllegalStateException.class);
  175. expectedException.expectMessage("Pin count exceeds pins on the lane");
  176. game.score();
  177. }
  178. @Ignore("Remove to run test")
  179. @Test
  180. public void secondBonusRollAfterAStrikeInTheLastFrameCanNotScoreMoreThan10Points() {
  181. int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 11};
  182. playGame(rolls);
  183. expectedException.expect(IllegalStateException.class);
  184. expectedException.expectMessage("Pin count exceeds pins on the lane");
  185. game.score();
  186. }
  187. @Ignore("Remove to run test")
  188. @Test
  189. public void anUnstartedGameCanNotBeScored() {
  190. int[] rolls = new int[0];
  191. playGame(rolls);
  192. expectedException.expect(IllegalStateException.class);
  193. expectedException.expectMessage("Score cannot be taken until the end of the game");
  194. game.score();
  195. }
  196. @Ignore("Remove to run test")
  197. @Test
  198. public void anIncompleteGameCanNotBeScored() {
  199. int[] rolls = {0, 0};
  200. playGame(rolls);
  201. expectedException.expect(IllegalStateException.class);
  202. expectedException.expectMessage("Score cannot be taken until the end of the game");
  203. game.score();
  204. }
  205. @Ignore("Remove to run test")
  206. @Test
  207. public void canNotRollIfGameAlreadyHasTenFrames() {
  208. int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  209. playGame(rolls);
  210. expectedException.expect(IllegalStateException.class);
  211. expectedException.expectMessage("Cannot roll after game is over");
  212. game.score();
  213. }
  214. @Ignore("Remove to run test")
  215. @Test
  216. public void bonusRollsForAStrikeInTheLastFrameMustBeRolledBeforeScoreCanBeCalculated() {
  217. int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10};
  218. playGame(rolls);
  219. expectedException.expect(IllegalStateException.class);
  220. expectedException.expectMessage("Score cannot be taken until the end of the game");
  221. game.score();
  222. }
  223. @Ignore("Remove to run test")
  224. @Test
  225. public void bothBonusRollsForAStrikeInTheLastFrameMustBeRolledBeforeScoreCanBeCalculated() {
  226. int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10};
  227. playGame(rolls);
  228. expectedException.expect(IllegalStateException.class);
  229. expectedException.expectMessage("Score cannot be taken until the end of the game");
  230. game.score();
  231. }
  232. @Ignore("Remove to run test")
  233. @Test
  234. public void bonusRollForASpareInTheLastFrameMustBeRolledBeforeScoreCanBeCalculated() {
  235. int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3};
  236. playGame(rolls);
  237. expectedException.expect(IllegalStateException.class);
  238. expectedException.expectMessage("Score cannot be taken until the end of the game");
  239. game.score();
  240. }
  241. @Ignore("Remove to run test")
  242. @Test
  243. public void canNotRollAfterBonusRollForSpare() {
  244. int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 2, 2};
  245. playGame(rolls);
  246. expectedException.expect(IllegalStateException.class);
  247. expectedException.expectMessage("Cannot roll after game is over");
  248. game.score();
  249. }
  250. @Ignore("Remove to run test")
  251. @Test
  252. public void canNotRollAfterBonusRollForStrike() {
  253. int[] rolls = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 3, 2, 2};
  254. playGame(rolls);
  255. expectedException.expect(IllegalStateException.class);
  256. expectedException.expectMessage("Cannot roll after game is over");
  257. game.score();
  258. }
  259. }