Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. /**
  2. * @author Vakhtang Tabatadze
  3. * PieceTest.java
  4. *
  5. * Generated Tests for Piece
  6. * Coverage 96.8%
  7. */
  8. package tetris;
  9.  
  10. import static org.junit.Assert.*;
  11.  
  12. import java.util.*;
  13.  
  14. import org.junit.*;
  15.  
  16. /*
  17. Unit test for Piece class -- starter shell.
  18. */
  19. public class PieceTest {
  20. // You can create data to be used in the your
  21. // test cases like this. For each run of a test method,
  22. // a new PieceTest object is created and setUp() is called
  23. // automatically by JUnit.
  24. // For example, the code below sets up some
  25. // pyramid and s pieces in instance variables
  26. // that can be used in tests.
  27. private Piece pyr1, pyr2, pyr3, pyr4;
  28. private Piece p[];
  29. private Piece s, sRotated;
  30.  
  31. @Before
  32. public void setUp() throws Exception {
  33.  
  34. pyr1 = new Piece(Piece.PYRAMID_STR);
  35. pyr2 = pyr1.computeNextRotation();
  36. pyr3 = pyr2.computeNextRotation();
  37. pyr4 = pyr3.computeNextRotation();
  38.  
  39. s = new Piece(Piece.S1_STR);
  40. sRotated = s.computeNextRotation();
  41. }
  42.  
  43. // Here are some sample tests to get you started
  44.  
  45. //Checks expected width values for all rotations
  46. private boolean checkWidth(int[] expected, Piece[] arr) {
  47. for (int i=0; i<arr.length; i++) {
  48. if (arr[i].getWidth() != expected[i])
  49. return false;
  50. }
  51. return true;
  52. }
  53.  
  54. //Checks expected height values for all rotations
  55. private boolean checkHeight(int[] expected, Piece[] arr) {
  56. for (int i=0; i<arr.length; i++) {
  57. if (arr[i].getHeight() != expected[i])
  58. return false;
  59. }
  60. return true;
  61. }
  62.  
  63. //Checks expected skirt values for specified rotation
  64. private boolean checkSkirt(int[] expected, int[] arr) {
  65. for (int i=0; i<arr.length; i++) {
  66. if (arr[i] != expected[i])
  67. return false;
  68. }
  69. return true;
  70. }
  71.  
  72. //Makes all rotations for root using fastRotation()
  73. private Piece[] makeAllRotations(Piece root) {
  74. Piece newPiece = root;
  75. int ans = 1;
  76. while (!newPiece.fastRotation().equals(root)) {
  77. ans++;
  78. newPiece = newPiece.fastRotation();
  79. }
  80. Piece[] arr = new Piece[ans];
  81. arr[0] = root;
  82. newPiece = root;
  83. for (int i=1; i<ans; i++) {
  84. arr[i] = arr[i-1].fastRotation();
  85. }
  86. return arr;
  87. }
  88.  
  89. /**
  90. * Generates all pieces and their rotations using Piece.getPieces();
  91. * @throws Exception
  92. */
  93. @Before
  94. public void SetUp2() throws Exception {
  95. p = Piece.getPieces();
  96. }
  97.  
  98. /**
  99. * Checks width and height for all rotations for L1
  100. * Also checks skirt for one of them
  101. */
  102. @Test
  103. public void testL1Str() {
  104. Piece[] arr = makeAllRotations(p[Piece.L1]);
  105. assertTrue(checkWidth(new int[] {2, 3, 2, 3}, arr));
  106. assertTrue(checkHeight(new int[] {3, 2, 3, 2}, arr));
  107. assertTrue(checkSkirt(new int[] {0, 0, 0}, arr[1].getSkirt()));
  108. }
  109.  
  110. /**
  111. * Checks width and height for all rotations for L2
  112. * Also checks skirt for one of them
  113. */
  114. @Test
  115. public void testL2Str() {
  116. Piece[] arr = makeAllRotations(p[Piece.L2]);
  117. assertTrue(checkWidth(new int[] {2, 3, 2, 3}, arr));
  118. assertTrue(checkHeight(new int[] {3, 2, 3, 2}, arr));
  119. assertTrue(checkSkirt(new int[] {1, 1, 0}, arr[1].getSkirt()));
  120. }
  121.  
  122. /**
  123. * Checks width and height for all rotations for S1
  124. * Also checks skirt for one of them
  125. */
  126. @Test
  127. public void testS1Str() {
  128. Piece[] arr = makeAllRotations(p[Piece.S1]);
  129. assertTrue(checkWidth(new int[] {3, 2, 3, 2}, arr));
  130. assertTrue(checkHeight(new int[] {2, 3, 2, 3}, arr));
  131. assertTrue(checkSkirt(new int[] {0, 0, 1}, arr[0].getSkirt()));
  132. }
  133.  
  134. /**
  135. * Checks width and height for all rotations for S2
  136. * Also checks skirt for one of them
  137. */
  138. @Test
  139. public void testS2Str() {
  140. Piece[] arr = makeAllRotations(p[Piece.S2]);
  141. assertTrue(checkWidth(new int[] {3, 2, 3, 2}, arr));
  142. assertTrue(checkHeight(new int[] {2, 3, 2, 3}, arr));
  143. assertTrue(checkSkirt(new int[] {0, 1}, arr[1].getSkirt()));
  144. }
  145.  
  146. /**
  147. * Checks width and height for all rotations for Stick
  148. * Also checks skirt for one of them
  149. */
  150. @Test
  151. public void testStick() {
  152. Piece[] arr = makeAllRotations(p[Piece.STICK]);
  153. assertTrue(checkWidth(new int[] {1, 4}, arr));
  154. assertTrue(checkHeight(new int[] {4, 1}, arr));
  155. assertTrue(checkSkirt(new int[] {0, 0, 0, 0}, arr[1].getSkirt()));
  156. }
  157.  
  158. /**
  159. * Checks width and height for all rotations for Box
  160. * Also checks skirt for one of them
  161. */
  162. @Test
  163. public void testBox() {
  164. Piece[] arr = makeAllRotations(p[Piece.SQUARE]);
  165. assertTrue(checkWidth(new int[] {2}, arr));
  166. assertTrue(checkHeight(new int[] {2}, arr));
  167. assertTrue(checkSkirt(new int[] {0, 0}, arr[0].getSkirt()));
  168. }
  169.  
  170. /**
  171. * Checks width and height for all rotations for Pyramid
  172. * Also checks skirt for one of them
  173. */
  174. @Test
  175. public void testPyramid() {
  176. Piece[] arr = makeAllRotations(p[Piece.PYRAMID]);
  177. assertTrue(checkWidth(new int[] {3, 2, 3, 2}, arr));
  178. assertTrue(checkHeight(new int[] {2, 3, 2, 3}, arr));
  179. assertTrue(checkSkirt(new int[] {1, 0, 1, 0}, arr[2].getSkirt()));
  180. }
  181.  
  182. @Test
  183. public void testSampleSize() {
  184. // Check size of pyr piece
  185. assertEquals(3, pyr1.getWidth());
  186. assertEquals(2, pyr1.getHeight());
  187.  
  188. // Now try after rotation
  189. // Effectively we're testing size and rotation code here
  190. assertEquals(2, pyr2.getWidth());
  191. assertEquals(3, pyr2.getHeight());
  192.  
  193. // Now try with some other piece, made a different way
  194. Piece l = new Piece(Piece.STICK_STR);
  195. assertEquals(1, l.getWidth());
  196. assertEquals(4, l.getHeight());
  197. }
  198.  
  199.  
  200. // Test the skirt returned by a few pieces
  201. @Test
  202. public void testSampleSkirt() {
  203. // Note must use assertTrue(Arrays.equals(... as plain .equals does not work
  204. // right for arrays.
  205. assertTrue(Arrays.equals(new int[] {0, 0, 0}, pyr1.getSkirt()));
  206. assertTrue(Arrays.equals(new int[] {1, 0, 1}, pyr3.getSkirt()));
  207.  
  208. assertTrue(Arrays.equals(new int[] {0, 0, 1}, s.getSkirt()));
  209. assertTrue(Arrays.equals(new int[] {1, 0}, sRotated.getSkirt()));
  210. }
  211.  
  212. @Test
  213. public void rotations() {
  214. int ans = 1;
  215. Piece[] p = Piece.getPieces();
  216. Piece current, root = p[1];
  217. current = root.fastRotation().fastRotation();
  218. while (!current.equals(root.fastRotation())) {
  219. ans++;
  220. current = current.fastRotation();
  221. }
  222. assertEquals(4, ans);
  223. }
  224.  
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement