Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Vakhtang Tabatadze
- * PieceTest.java
- *
- * Generated Tests for Piece
- * Coverage 96.8%
- */
- package tetris;
- import static org.junit.Assert.*;
- import java.util.*;
- import org.junit.*;
- /*
- Unit test for Piece class -- starter shell.
- */
- public class PieceTest {
- // You can create data to be used in the your
- // test cases like this. For each run of a test method,
- // a new PieceTest object is created and setUp() is called
- // automatically by JUnit.
- // For example, the code below sets up some
- // pyramid and s pieces in instance variables
- // that can be used in tests.
- private Piece pyr1, pyr2, pyr3, pyr4;
- private Piece p[];
- private Piece s, sRotated;
- @Before
- public void setUp() throws Exception {
- pyr1 = new Piece(Piece.PYRAMID_STR);
- pyr2 = pyr1.computeNextRotation();
- pyr3 = pyr2.computeNextRotation();
- pyr4 = pyr3.computeNextRotation();
- s = new Piece(Piece.S1_STR);
- sRotated = s.computeNextRotation();
- }
- // Here are some sample tests to get you started
- //Checks expected width values for all rotations
- private boolean checkWidth(int[] expected, Piece[] arr) {
- for (int i=0; i<arr.length; i++) {
- if (arr[i].getWidth() != expected[i])
- return false;
- }
- return true;
- }
- //Checks expected height values for all rotations
- private boolean checkHeight(int[] expected, Piece[] arr) {
- for (int i=0; i<arr.length; i++) {
- if (arr[i].getHeight() != expected[i])
- return false;
- }
- return true;
- }
- //Checks expected skirt values for specified rotation
- private boolean checkSkirt(int[] expected, int[] arr) {
- for (int i=0; i<arr.length; i++) {
- if (arr[i] != expected[i])
- return false;
- }
- return true;
- }
- //Makes all rotations for root using fastRotation()
- private Piece[] makeAllRotations(Piece root) {
- Piece newPiece = root;
- int ans = 1;
- while (!newPiece.fastRotation().equals(root)) {
- ans++;
- newPiece = newPiece.fastRotation();
- }
- Piece[] arr = new Piece[ans];
- arr[0] = root;
- newPiece = root;
- for (int i=1; i<ans; i++) {
- arr[i] = arr[i-1].fastRotation();
- }
- return arr;
- }
- /**
- * Generates all pieces and their rotations using Piece.getPieces();
- * @throws Exception
- */
- @Before
- public void SetUp2() throws Exception {
- p = Piece.getPieces();
- }
- /**
- * Checks width and height for all rotations for L1
- * Also checks skirt for one of them
- */
- @Test
- public void testL1Str() {
- Piece[] arr = makeAllRotations(p[Piece.L1]);
- assertTrue(checkWidth(new int[] {2, 3, 2, 3}, arr));
- assertTrue(checkHeight(new int[] {3, 2, 3, 2}, arr));
- assertTrue(checkSkirt(new int[] {0, 0, 0}, arr[1].getSkirt()));
- }
- /**
- * Checks width and height for all rotations for L2
- * Also checks skirt for one of them
- */
- @Test
- public void testL2Str() {
- Piece[] arr = makeAllRotations(p[Piece.L2]);
- assertTrue(checkWidth(new int[] {2, 3, 2, 3}, arr));
- assertTrue(checkHeight(new int[] {3, 2, 3, 2}, arr));
- assertTrue(checkSkirt(new int[] {1, 1, 0}, arr[1].getSkirt()));
- }
- /**
- * Checks width and height for all rotations for S1
- * Also checks skirt for one of them
- */
- @Test
- public void testS1Str() {
- Piece[] arr = makeAllRotations(p[Piece.S1]);
- assertTrue(checkWidth(new int[] {3, 2, 3, 2}, arr));
- assertTrue(checkHeight(new int[] {2, 3, 2, 3}, arr));
- assertTrue(checkSkirt(new int[] {0, 0, 1}, arr[0].getSkirt()));
- }
- /**
- * Checks width and height for all rotations for S2
- * Also checks skirt for one of them
- */
- @Test
- public void testS2Str() {
- Piece[] arr = makeAllRotations(p[Piece.S2]);
- assertTrue(checkWidth(new int[] {3, 2, 3, 2}, arr));
- assertTrue(checkHeight(new int[] {2, 3, 2, 3}, arr));
- assertTrue(checkSkirt(new int[] {0, 1}, arr[1].getSkirt()));
- }
- /**
- * Checks width and height for all rotations for Stick
- * Also checks skirt for one of them
- */
- @Test
- public void testStick() {
- Piece[] arr = makeAllRotations(p[Piece.STICK]);
- assertTrue(checkWidth(new int[] {1, 4}, arr));
- assertTrue(checkHeight(new int[] {4, 1}, arr));
- assertTrue(checkSkirt(new int[] {0, 0, 0, 0}, arr[1].getSkirt()));
- }
- /**
- * Checks width and height for all rotations for Box
- * Also checks skirt for one of them
- */
- @Test
- public void testBox() {
- Piece[] arr = makeAllRotations(p[Piece.SQUARE]);
- assertTrue(checkWidth(new int[] {2}, arr));
- assertTrue(checkHeight(new int[] {2}, arr));
- assertTrue(checkSkirt(new int[] {0, 0}, arr[0].getSkirt()));
- }
- /**
- * Checks width and height for all rotations for Pyramid
- * Also checks skirt for one of them
- */
- @Test
- public void testPyramid() {
- Piece[] arr = makeAllRotations(p[Piece.PYRAMID]);
- assertTrue(checkWidth(new int[] {3, 2, 3, 2}, arr));
- assertTrue(checkHeight(new int[] {2, 3, 2, 3}, arr));
- assertTrue(checkSkirt(new int[] {1, 0, 1, 0}, arr[2].getSkirt()));
- }
- @Test
- public void testSampleSize() {
- // Check size of pyr piece
- assertEquals(3, pyr1.getWidth());
- assertEquals(2, pyr1.getHeight());
- // Now try after rotation
- // Effectively we're testing size and rotation code here
- assertEquals(2, pyr2.getWidth());
- assertEquals(3, pyr2.getHeight());
- // Now try with some other piece, made a different way
- Piece l = new Piece(Piece.STICK_STR);
- assertEquals(1, l.getWidth());
- assertEquals(4, l.getHeight());
- }
- // Test the skirt returned by a few pieces
- @Test
- public void testSampleSkirt() {
- // Note must use assertTrue(Arrays.equals(... as plain .equals does not work
- // right for arrays.
- assertTrue(Arrays.equals(new int[] {0, 0, 0}, pyr1.getSkirt()));
- assertTrue(Arrays.equals(new int[] {1, 0, 1}, pyr3.getSkirt()));
- assertTrue(Arrays.equals(new int[] {0, 0, 1}, s.getSkirt()));
- assertTrue(Arrays.equals(new int[] {1, 0}, sRotated.getSkirt()));
- }
- @Test
- public void rotations() {
- int ans = 1;
- Piece[] p = Piece.getPieces();
- Piece current, root = p[1];
- current = root.fastRotation().fastRotation();
- while (!current.equals(root.fastRotation())) {
- ans++;
- current = current.fastRotation();
- }
- assertEquals(4, ans);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement