Advertisement
mixeila

Untitled

Apr 9th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.31 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), false);
  91. assertEquals(b.getGrid(-1, 1), true);
  92. assertEquals(b.getGrid(3, 1), true);
  93. assertEquals(b.getGrid(0, -1), true);
  94. assertEquals(b.getGrid(0, 7), true);
  95. b.commit();
  96. assertEquals(b.place(pieces[Piece.L1].fastRotation(), 0, 3), Board.PLACE_ROW_FILLED);
  97. b.commit();
  98. assertEquals(b.place(pieces[Piece.STICK], 0, 0), Board.PLACE_BAD);
  99. b.commit();
  100. assertEquals(b.place(pieces[Piece.STICK].fastRotation(), 0, 5), Board.PLACE_OUT_BOUNDS);
  101. b.commit();
  102. assertEquals(b.place(pieces[Piece.SQUARE], 0, 6), Board.PLACE_OUT_BOUNDS);
  103. b = new Board(3, 6);
  104. assertEquals(b.place(pieces[Piece.SQUARE], -1, 0), Board.PLACE_OUT_BOUNDS);
  105. b.commit();
  106. assertEquals(b.place(pieces[Piece.L1], 2, -1), Board.PLACE_OUT_BOUNDS);
  107. b.commit();
  108. assertEquals(b.place(pieces[Piece.PYRAMID], 3, 2), Board.PLACE_OUT_BOUNDS);
  109. }
  110.  
  111. @Test
  112. public void testClearRow() {
  113. b = new Board(3, 6);
  114. b.place(pieces[Piece.PYRAMID], 0, 0);
  115. b.commit();
  116. b.place(pieces[Piece.L2].fastRotation(), 0, 1);
  117. b.commit();
  118. b.place(pieces[Piece.S1], 0, 3);
  119. b.clearRows();
  120. b.commit();
  121. assertEquals(3, b.getMaxHeight());
  122. assertEquals(2, b.getColumnHeight(0));
  123. assertEquals(3, b.getColumnHeight(1));
  124. assertEquals(3, b.getColumnHeight(2));
  125. assertEquals(2, b.getRowWidth(0));
  126. assertEquals(2, b.getRowWidth(1));
  127. assertEquals(2, b.getRowWidth(2));
  128. assertEquals(0, b.getRowWidth(4));
  129. assertEquals(false, b.getGrid(0, 0));
  130. assertEquals(false, b.getGrid(2, 1));
  131. }
  132.  
  133. @Test
  134. public void testClearRow2() {
  135. b = new Board(4, 8);
  136. assertEquals(b.place(pieces[Piece.STICK].fastRotation(), 0, 0), Board.PLACE_ROW_FILLED);
  137. b.commit();
  138. assertEquals(b.place(pieces[Piece.STICK].fastRotation(), 0, 5), Board.PLACE_ROW_FILLED);
  139. assertEquals(b.getMaxHeight(), 6);
  140. b.clearRows();
  141. b.commit();
  142. assertEquals(b.getMaxHeight(), 0);
  143. assertEquals(b.getColumnHeight(0), 0);
  144. assertEquals(b.getRowWidth(0), 0);
  145. b.place(pieces[Piece.PYRAMID], 0, 0);
  146. b.commit();
  147. b.place(pieces[Piece.PYRAMID].fastRotation(), 2, 0);
  148. b.commit();
  149. b.place(pieces[Piece.L1].fastRotation().fastRotation().fastRotation(), 0, 1);
  150. b.commit();
  151. b.place(pieces[Piece.S1], 0, 3);
  152. b.clearRows();
  153. b.commit();
  154. assertEquals(b.getMaxHeight(), 2);
  155. assertEquals(b.getRowWidth(0), 2);
  156. assertEquals(b.getRowWidth(1), 2);
  157. assertEquals(b.getRowWidth(2), 0);
  158. assertEquals(b.getColumnHeight(0), 1);
  159. assertEquals(b.getColumnHeight(1), 2);
  160. assertEquals(b.getColumnHeight(2), 2);
  161. assertEquals(b.getColumnHeight(3), 0);
  162. }
  163.  
  164.  
  165. @Test
  166. public void testDropHeight() {
  167. b = new Board(3, 6);
  168. assertEquals(b.dropHeight(pieces[Piece.L1], 0), 0);
  169. b.place(pieces[Piece.PYRAMID], 0, 0);
  170. b.commit();
  171. assertEquals(b.dropHeight(pieces[Piece.L1], 0), 2);
  172. b.place(pieces[Piece.S2], 0, 3);
  173. assertEquals(b.dropHeight(pieces[Piece.L1].fastRotation(), 0), 5);
  174. assertEquals(b.dropHeight(pieces[Piece.STICK], 2), 4);
  175. assertEquals(b.dropHeight(pieces[Piece.STICK], 1), 5);
  176. }
  177.  
  178. @Test
  179. public void testDropheight2() {
  180. b = new Board(4, 8);
  181. b.place(pieces[Piece.L1].fastRotation(), 0, 0);
  182. assertEquals(b.dropHeight(pieces[Piece.PYRAMID], 0), 2);
  183. assertEquals(b.dropHeight(pieces[Piece.PYRAMID].fastRotation(), 0), 1);
  184. assertEquals(b.dropHeight(pieces[Piece.PYRAMID].fastRotation(), 2), 1);
  185. b.commit();
  186. assertEquals(b.dropHeight(pieces[Piece.L1].fastRotation().fastRotation(), 2), 0);
  187. b.place(pieces[Piece.L1].fastRotation().fastRotation(), 2, 0);
  188. assertEquals(b.dropHeight(pieces[Piece.STICK].fastRotation(), 0), 3);
  189. assertEquals(b.dropHeight(pieces[Piece.SQUARE], 0), 1);
  190. b.commit();
  191. b.place(pieces[Piece.STICK].fastRotation(), 0, 4);
  192. assertEquals(b.dropHeight(pieces[Piece.SQUARE], 0), 5);
  193. }
  194.  
  195. @Test
  196. public void testUndo() {
  197. b = new Board(4, 8);
  198. b.place(pieces[Piece.S1], 0, 0);
  199. b.undo();
  200. assertEquals(b.getMaxHeight(), 0);
  201. assertEquals(b.getRowWidth(0), 0);
  202. assertEquals(b.getRowWidth(1), 0);
  203. assertEquals(b.getColumnHeight(2), 0);
  204. assertEquals(b.place(pieces[Piece.STICK].fastRotation(), 0, 2), Board.PLACE_ROW_FILLED);
  205. b.commit();
  206. assertEquals(b.clearRows(), 1);
  207. b.undo();
  208. assertEquals(b.getRowWidth(2), 4);
  209. }
  210.  
  211. @Test
  212. public void testUndo2() {
  213. b = new Board(4, 8);
  214. b.place(pieces[Piece.L1], 0, 0);
  215. b.undo();
  216. b.undo();
  217. b.undo();
  218. assertEquals(b.getMaxHeight(), 0);
  219. assertEquals(b.getRowWidth(0), 0);
  220. assertEquals(b.getRowWidth(1), 0);
  221. assertEquals(b.getColumnHeight(0), 0);
  222. assertEquals(b.place(pieces[Piece.L1], 0, 0), Board.PLACE_OK);
  223. b.commit();
  224. assertEquals(b.place(pieces[Piece.L2], 2, 0), Board.PLACE_ROW_FILLED);
  225. b.commit();
  226. assertEquals(b.place(pieces[Piece.STICK], 2, 1), Board.PLACE_OK);
  227. assertEquals(1, b.clearRows());
  228. b.undo();
  229. b.undo();
  230. assertEquals(b.getRowWidth(0), 4);
  231. assertEquals(b.getMaxHeight(), 3);
  232. }
  233.  
  234. @Test
  235. public void testGeneral() {
  236. b = new Board(4, 8);
  237. assertEquals(b.place(pieces[Piece.S2].fastRotation(), 0, 0), Board.PLACE_OK);
  238. b.commit();
  239. assertEquals(b.dropHeight(pieces[Piece.PYRAMID].fastRotation().fastRotation().fastRotation(), 0), 2);
  240. assertEquals(b.place(pieces[Piece.PYRAMID].fastRotation().fastRotation().fastRotation(), 0, 2), Board.PLACE_OK);
  241. b.commit();
  242. assertEquals(b.place(pieces[Piece.L1].fastRotation(), 1, 0), Board.PLACE_ROW_FILLED);
  243. assertEquals(b.clearRows(), 1);
  244. assertEquals(b.getColumnHeight(1), 3);
  245. assertEquals(b.getColumnHeight(0), 4);
  246. assertEquals(b.getColumnHeight(2), 0);
  247. assertEquals(b.getColumnHeight(3), 1);
  248. b.undo();
  249. b.undo();
  250. assertEquals(b.getColumnHeight(1), 4);
  251. assertEquals(b.getRowWidth(0), 1);
  252. assertEquals(b.getMaxHeight(), 5);
  253. }
  254.  
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement