Advertisement
mixeila

Untitled

Apr 7th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. package tetris;
  2.  
  3. import static org.junit.Assert.*;
  4.  
  5. import org.junit.*;
  6.  
  7. public class BoardTest {
  8. Board b;
  9. Piece pyr1, pyr2, pyr3, pyr4, s, sRotated;
  10. Piece pieces[];
  11. // This shows how to build things in setUp() to re-use
  12. // across tests.
  13.  
  14. // In this case, setUp() makes shapes,
  15. // and also a 3X6 board, with pyr placed at the bottom,
  16. // ready to be used by tests.
  17. @Before
  18. public void setUp() throws Exception {
  19. b = new Board(3, 6);
  20.  
  21. pyr1 = new Piece(Piece.PYRAMID_STR);
  22. pyr2 = pyr1.computeNextRotation();
  23. pyr3 = pyr2.computeNextRotation();
  24. pyr4 = pyr3.computeNextRotation();
  25.  
  26. s = new Piece(Piece.S1_STR);
  27. sRotated = s.computeNextRotation();
  28. pieces = Piece.getPieces();
  29. b.place(pyr1, 0, 0);
  30. }
  31.  
  32. // Check the basic width/height/max after the one placement
  33. @Test
  34. public void testSample1() {
  35. assertEquals(1, b.getColumnHeight(0));
  36. assertEquals(2, b.getColumnHeight(1));
  37. assertEquals(2, b.getMaxHeight());
  38. assertEquals(3, b.getRowWidth(0));
  39. assertEquals(1, b.getRowWidth(1));
  40. assertEquals(0, b.getRowWidth(2));
  41. }
  42.  
  43. // Place sRotated into the board, then check some measures
  44. @Test
  45. public void testSample2() {
  46. b.commit();
  47. int result = b.place(sRotated, 1, 1);
  48. assertEquals(Board.PLACE_OK, result);
  49. assertEquals(1, b.getColumnHeight(0));
  50. assertEquals(4, b.getColumnHeight(1));
  51. assertEquals(3, b.getColumnHeight(2));
  52. assertEquals(4, b.getMaxHeight());
  53. }
  54.  
  55. // Make more tests, by putting together longer series of
  56. // place, clearRows, undo, place ... checking a few col/row/max
  57. // numbers that the board looks right after the operations.
  58. @Test
  59. public void testSample3() {
  60. b = new Board(3, 6);
  61. assertEquals(b.getHeight(), 6);
  62. assertEquals(b.getWidth(), 3);
  63. b.commit();
  64. assertEquals(b.place(pieces[Piece.S1], 0, 0), Board.PLACE_OK);
  65. b.commit();
  66. assertEquals(b.place(pieces[Piece.S2], 0, 2), Board.PLACE_OK);
  67. b.commit();
  68. assertEquals(b.place(pieces[Piece.S1], 0, 4), Board.PLACE_OK);
  69. b.clearRows();
  70. b.clearRows();
  71. assertEquals(b.getColumnHeight(0), 5);
  72. assertEquals(b.getColumnHeight(1), 6);
  73. assertEquals(b.getColumnHeight(2), 6);
  74. assertEquals(b.getRowWidth(0), 2);
  75. assertEquals(b.getRowWidth(1), 2);
  76. assertEquals(b.getRowWidth(2), 2);
  77. assertEquals(b.getRowWidth(4), 2);
  78. assertEquals(b.getRowWidth(5), 2);
  79. assertEquals(b.getMaxHeight(), 6);
  80. }
  81.  
  82. @Test
  83. public void testPlace() {
  84. b = new Board(3,7);
  85. b.commit();
  86. assertEquals(b.place(pieces[Piece.L1], 0, 0), Board.PLACE_OK);
  87. assertEquals(b.getGrid(0, 0), true);
  88. assertEquals(b.getGrid(1, 0), true);
  89. assertEquals(b.getGrid(0, 1), true);
  90. assertEquals(b.getGrid(1, 1), true);
  91. b.commit();
  92. assertEquals(b.place(pieces[Piece.L1].fastRotation(), 0, 3), Board.PLACE_ROW_FILLED);
  93. b.commit();
  94. assertEquals(b.place(pieces[Piece.STICK], 0, 0), Board.PLACE_BAD);
  95. b.commit();
  96. assertEquals(b.place(pieces[Piece.STICK].fastRotation(), 0, 5), Board.PLACE_OUT_BOUNDS);
  97. b.commit();
  98. assertEquals(b.place(pieces[Piece.SQUARE], 0, 6), Board.PLACE_OUT_BOUNDS);
  99. }
  100.  
  101. @Test
  102. public void testClearRow() {
  103. b = new Board(3, 6);
  104. b.place(pieces[Piece.PYRAMID], 0, 0);
  105. b.commit();
  106. b.place(pieces[Piece.L2].fastRotation(), 0, 1);
  107. b.commit();
  108. b.place(pieces[Piece.S1], 0, 3);
  109. b.clearRows();
  110. b.commit();
  111. assertEquals(3, b.getMaxHeight());
  112. assertEquals(2, b.getColumnHeight(0));
  113. assertEquals(3, b.getColumnHeight(1));
  114. assertEquals(3, b.getColumnHeight(2));
  115. assertEquals(2, b.getRowWidth(0));
  116. assertEquals(2, b.getRowWidth(1));
  117. assertEquals(2, b.getRowWidth(2));
  118. assertEquals(0, b.getRowWidth(4));
  119. assertEquals(false, b.getGrid(0, 0));
  120. assertEquals(false, b.getGrid(2, 1));
  121. }
  122.  
  123. @Test
  124. public void testClearRow2() {
  125. b = new Board(4, 8);
  126. assertEquals(b.place(pieces[Piece.STICK].fastRotation(), 0, 0), Board.PLACE_ROW_FILLED);
  127. b.commit();
  128. assertEquals(b.place(pieces[Piece.STICK].fastRotation(), 0, 5), Board.PLACE_ROW_FILLED);
  129. assertEquals(b.getMaxHeight(), 6);
  130. b.clearRows();
  131. b.commit();
  132. assertEquals(b.getMaxHeight(), 0);
  133. assertEquals(b.getColumnHeight(0), 0);
  134. assertEquals(b.getRowWidth(0), 0);
  135. b.place(pieces[Piece.PYRAMID], 0, 0);
  136. b.commit();
  137. b.place(pieces[Piece.PYRAMID].fastRotation(), 2, 0);
  138. b.commit();
  139. b.place(pieces[Piece.L1].fastRotation().fastRotation().fastRotation(), 0, 1);
  140. b.commit();
  141. b.place(pieces[Piece.S1], 0, 3);
  142. b.clearRows();
  143. b.commit();
  144. assertEquals(b.getMaxHeight(), 2);
  145. assertEquals(b.getRowWidth(0), 2);
  146. assertEquals(b.getRowWidth(1), 2);
  147. assertEquals(b.getRowWidth(2), 0);
  148. assertEquals(b.getColumnHeight(0), 1);
  149. assertEquals(b.getColumnHeight(1), 2);
  150. assertEquals(b.getColumnHeight(2), 2);
  151. assertEquals(b.getColumnHeight(3), 0);
  152. }
  153.  
  154.  
  155. @Test
  156. public void testDropHeight() {
  157. b = new Board(3, 6);
  158. assertEquals(b.dropHeight(pieces[Piece.L1], 0), 0);
  159. b.place(pieces[Piece.PYRAMID], 0, 0);
  160. b.commit();
  161. assertEquals(b.dropHeight(pieces[Piece.L1], 0), 2);
  162. b.place(pieces[Piece.S2], 0, 3);
  163. assertEquals(b.dropHeight(pieces[Piece.L1].fastRotation(), 0), 5);
  164. assertEquals(b.dropHeight(pieces[Piece.STICK], 2), 4);
  165. assertEquals(b.dropHeight(pieces[Piece.STICK], 1), 5);
  166. }
  167.  
  168. @Test
  169. public void testDropheight2() {
  170. b = new Board(4, 8);
  171. b.place(pieces[Piece.L1].fastRotation(), 0, 0);
  172. assertEquals(b.dropHeight(pieces[Piece.PYRAMID], 0), 2);
  173. assertEquals(b.dropHeight(pieces[Piece.PYRAMID].fastRotation(), 0), 1);
  174. assertEquals(b.dropHeight(pieces[Piece.PYRAMID].fastRotation(), 2), 1);
  175. b.commit();
  176. assertEquals(b.dropHeight(pieces[Piece.L1].fastRotation().fastRotation(), 2), 0);
  177. b.place(pieces[Piece.L1].fastRotation().fastRotation(), 2, 0);
  178. assertEquals(b.dropHeight(pieces[Piece.STICK].fastRotation(), 0), 3);
  179. assertEquals(b.dropHeight(pieces[Piece.SQUARE], 0), 1);
  180. b.commit();
  181. b.place(pieces[Piece.STICK].fastRotation(), 0, 4);
  182. assertEquals(b.dropHeight(pieces[Piece.SQUARE], 0), 5);
  183. }
  184.  
  185. // @Test
  186. // public void testUndo() {
  187. // b = new Board(4, 8);
  188. //
  189. // }
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement