summaryrefslogtreecommitdiff
path: root/src/tetris/tests/piece/TetrisPieceTest.java
blob: 450ed252fef42a664b3768bd438e52e3d44c7ada (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Jesse Morgan <jesterpm@u.washington.edu>
 * 
 * TCSS 305 - Autumn 2009 Tetris Project 17 November 2009
 */

package tetris.tests.piece;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import tetris.model.IntPoint;
import tetris.piece.TetrisPiece;

/**
 * Parent JUnit test class for Tetris Pieces.
 * 
 * @author Jesse Morgan <jesterpm@u.washington.edu>
 * @version 1.0 17 November 2009
 */
public abstract class TetrisPieceTest {
  // Private Constants
  /**
   * Random X position we use to test.
   */
  private static final int X_POSITION = 5;
  
  /**
   * Random Y position we use to test.
   */
  private static final int Y_POSITION = 3;
  
  /**
   * Number of rotations require to rotate a piece 360 degrees.
   */
  private static final int NUMBER_OF_ROTATIONS = 4;
  
  /**
   * Test that a piece is created properly.
   */
  @Test
  public void testPieceConstruction() {
    final TetrisPiece piece = getPiece(5, 3);
    
    // Test the X and Y coords.
    assertEquals("Inital X", X_POSITION, piece.getX());
    assertEquals("Inital Y", Y_POSITION, piece.getY());
    
    // Test Board Position
    assertTrue("Initial Board Positon Fails",
               testBoardPosition(piece, X_POSITION, Y_POSITION));
  }
  
  /**
   * Test that a piece is the same after four rotations.
   */
  @Test
  public void testPieceAfterRotations() {
    final TetrisPiece before = getPiece(5, 3);
    TetrisPiece after;
    
    // Left rotations
    after = getPiece(X_POSITION, Y_POSITION);
    for (int i = 0; i < NUMBER_OF_ROTATIONS; i++) {
      after = after.rotateLeft();
    }
    
    assertEquals("Pieces differ after 4 left rotations", before.toString(), after.toString());
    
    // Right rotations
    after = getPiece(X_POSITION, Y_POSITION);
    for (int i = 0; i < NUMBER_OF_ROTATIONS; i++) {
      after = after.rotateRight();
    }
    
    assertEquals("Pieces differ after 4 right rotations", before.toString(), after.toString());
  }
  
  /**
   * Test Translations.
   */
  @Test
  public void testTranslations() {
    TetrisPiece piece = getPiece(X_POSITION, Y_POSITION);
    
    // Negative Translation
    piece = piece.translate(-1, -1);
    assertTrue("Negative Translations Fail",
               testBoardPosition(piece, X_POSITION - 1 , Y_POSITION - 1));
    
    // Positive Translation
    piece = piece.translate(1, 1);
    assertTrue("Positive Translations Fail",
               testBoardPosition(piece, X_POSITION - 1 + 1, Y_POSITION - 1 + 1));
  }
  
  /**
   * Test each left rotation String.
   */
  @Test
  public void testLeftRotations() {
    TetrisPiece piece = getPiece(1, 0);

    final String[] rotations = getRotationStrings();
    
    for (int i = 0; i <= NUMBER_OF_ROTATIONS; i++) {
      assertEquals(i + "th Left Rotation Failed",
                   rotations[i % rotations.length], piece.toString());
      
      piece = piece.rotateLeft();
    }
  }
  
  /**
   * Test each right rotation String.
   */
  @Test
  public void testRightRotations() {
    TetrisPiece piece = getPiece(1, 0);

    final String[] rotations = getRotationStrings();
    
    for (int i = NUMBER_OF_ROTATIONS; i >= 0; i--) {
      assertEquals((i - NUMBER_OF_ROTATIONS) + "th Right Rotation Failed",
                   rotations[i % rotations.length], piece.toString());
      
      piece = piece.rotateRight();
    }
  }
  
  /**
   * Tests the board position.
   * @param the_piece The tetris piece.
   * @param the_x Board X.
   * @param the_y Board y.
   * @return true if all points are correct, false otherwise.
   */
  protected boolean testBoardPosition(final TetrisPiece the_piece,
                                   final int the_x, final int the_y) {
    boolean result = true;
    
    final int[] orig_positions = getOriginalPoints();
    final IntPoint[] cur_positions = the_piece.getBoardCoordinates();
    
    // Both sets should contain the same number of points.
    assertSame("Original and Current point counts differ.",
               orig_positions.length, 2 * cur_positions.length);
    
    for (int i = 0; i < orig_positions.length - 1; i = i + 2) {
      // Check that each X and Y was shifted properly.
      if (orig_positions[i] + the_x != cur_positions[i / 2].getX()) {
        result = false;
      }
      
      if (orig_positions[i + 1] + the_y != cur_positions[i / 2].getY()) {
        result = false;
      }
    }
    
    return result;
  }

  /**
   * @param the_x X-coord.
   * @param the_y X-coord.
   * @return Return a tetris piece of the type we're testing.
   */
  protected abstract TetrisPiece getPiece(final int the_x, final int the_y);
  
  /**
   * @return int[] containing the orignal points for the piece.
   */
  protected abstract int[] getOriginalPoints();
  
  /**
   * @return int[] containing the string representation of various rotation states
   */
  protected abstract String[] getRotationStrings();
}