/** * Check that the <code>Plies</code> the <code>Piece</code> would return match the given set of * correct <code>Plies</code>. */ private void checkPlies(Set<Ply> correctPlies, Piece piece) { Set<Ply> obtainedPlies = new HashSet<Ply>(piece.getPlies()); assertEquals( (piece.isWhite() ? "White" : "Black") + " Piece fails to" + " produce correct set of Plies", correctPlies, obtainedPlies); }
/** * Test that the <code>Piece</code> sets itself up exactly as many times as it needs, when some of * its initial positions correspond to unusable cells. */ public void testSetUpLoopUnusable() { List<int[]> unusable = new ArrayList<int[]>(2); unusable.add(new int[] {6, 0}); unusable.add(new int[] {1, 7}); Board newBoard = new RectangularBoard( RectangularBoard.defaultLength, RectangularBoard.defaultHeight, unusable); Piece newKnight = new Knight(true, newBoard); try { newKnight.setUp(); assertEquals( "Piece did not set up correctly", newKnight, newBoard.getPiece(new int[] {1, 0})); newKnight = new Knight(true, newBoard); newKnight.setUp(); fail("Piece did not fail while setting up, as it should have"); } catch (InvalidInitialPositionException e1) { try { newKnight = new Knight(false, newBoard); newKnight.setUp(); assertEquals( "Piece did not set up correctly", newKnight, newBoard.getPiece(new int[] {6, 7})); newKnight = new Knight(false, newBoard); newKnight.setUp(); fail("Piece did not fail while setting up, as it should have"); } catch (InvalidInitialPositionException e2) { assertEquals("Number of Pieces in Board is incorrect", 1, newBoard.getPieces(true).size()); assertEquals("Number of Pieces in Board is incorrect", 1, newBoard.getPieces(false).size()); return; } fail("Black Piece set itself up successfully to an unusable cell."); } fail("White Piece set itself up successfully to an unusable cell."); }
/** * Test that the <code>Piece</code> sets itself up in a <code>Board</code> successfully and * correctly. It is set up as many times as it should handle. */ public void testSetUpNormal() { // For a white Piece Piece newKnight = new Knight(true, board); try { whiteKnight.setUp(); newKnight.setUp(); } catch (Exception e) { fail("White Piece failed to set up appropriately"); } assertTrue( "White Piece was not added to the Board after being set up", board.contains(whiteKnight)); assertTrue( "White Piece was not added to the Board after being set up", board.contains(newKnight)); assertEquals( "More than two Pieces (or less) were added when " + "setting up", 2, board.getPieces(true).size()); assertTrue( "Another Piece was added to the Board", board.getPiece(new int[] {1, 0}).getType().equals(new String("knight")) && board.getPiece(new int[] {6, 0}).getType().equals(new String("knight"))); assertEquals( "White Piece did not set up itself at the correct position", whiteKnight, board.getPiece(new int[] {1, 0})); // Notice order assertEquals( "White Piece did not set up itself at the correct position", newKnight, board.getPiece(new int[] {6, 0})); // For a black Piece newKnight = new Knight(false, board); try { blackKnight.setUp(); newKnight.setUp(); } catch (Exception e) { fail("Black Piece failed to set up appropriately"); } assertTrue( "Black Piece was not added to the Board after being set up", board.contains(blackKnight)); assertTrue( "Black Piece was not added to the Board after being set up", board.contains(newKnight)); assertEquals( "More than two Pieces (or less) were added when " + "setting up", 2, board.getPieces(false).size()); assertTrue( "Another Piece was added to the Board", board.getPiece(new int[] {1, 7}).getType().equals(new String("knight")) && board.getPiece(new int[] {6, 7}).getType().equals(new String("knight"))); assertEquals( "Black Piece did not set up itself at the correct position", blackKnight, board.getPiece(new int[] {1, 7})); // Notice order assertEquals( "Black Piece did not set up itself at the correct position", newKnight, board.getPiece(new int[] {6, 7})); }
/** Test the getType() method. */ public void testGetType() { assertEquals( "The toString() method is not returning what it should", new String("knight"), whiteKnight.getType()); assertEquals( "The toString() method is not returning what it should", new String("knight"), blackKnight.getType()); }
/** * Test valid <code>Plies</code> when the <code>Piece</code> is not in a <code>Board</code>. There * should be no valid <code>Plies</code>. */ public void testPliesNone() { // For white Piece assertEquals( "White Piece fails to produce correct set of Plies", new ArrayList<Ply>(), whiteKnight.getPlies()); // For black Piece assertEquals( "Black Piece fails to produce correct set of Plies", new ArrayList<Ply>(), blackKnight.getPlies()); }
/** Test that the <code>Piece</code> fails to be set up more than once. */ public void testSetUpTwice() { // For a white Piece while (true) { try { whiteKnight.setUp(); } catch (IllegalStateException e) { assertTrue( "White Piece was not added to the Board after being set up", board.contains(whiteKnight)); assertFalse( "A black Piece was added to the Board while testing a " + "white Piece", board.contains(blackKnight)); assertEquals( "More than one white Piece (or no Pieces) was (were) " + "added when setting up", 1, board.getPieces(true).size()); assertEquals( "White Piece did not set up itself at the correct position", whiteKnight, board.getPiece(new int[] {1, 0})); break; } catch (InvalidInitialPositionException e) { fail( "White Piece threw an InvalidInitialPositionException while " + "setting up more than once. This should not happen."); } } // For a black Piece while (true) { try { blackKnight.setUp(); } catch (IllegalStateException e) { assertTrue( "Black Piece was not added to the Board after being set up", board.contains(blackKnight)); assertEquals( "More than one black Piece (or no Pieces) was (were) " + "added when setting up", 1, board.getPieces(false).size()); assertEquals( "Black Piece did not set up itself at the correct position", blackKnight, board.getPiece(new int[] {1, 7})); break; } catch (InvalidInitialPositionException e) { fail( "Black Piece threw an InvalidInitialPositionException while " + "setting up more than once. This should not happen."); } } }
/** * Test that the <code>Piece</code> sets itself up exactly as many times as it needs, assuming its * initial positions correspond to usable cells. */ public void testSetUpLoopNormal() { // For a white Piece Piece newKnight = null; while (true) { try { newKnight = new Knight(true, board); newKnight.setUp(); } catch (IllegalStateException e) { // not really needed fail( "White Piece threw an IllegalStateException while set" + "ting up to an occupied cell. This should not happen."); } catch (InvalidInitialPositionException e) { assertFalse( "White Piece was added to the Board after failing to set up", board.contains(newKnight)); assertFalse( "A black Piece was added to the Board while testing a " + "white Piece", board.contains(newKnight)); assertEquals( "More than two white Pieces (or less) were " + "added when setting up", 2, board.getPieces(true).size()); break; } } // For a black Piece while (true) { try { newKnight = new Knight(false, board); newKnight.setUp(); } catch (IllegalStateException e) { // not really needed fail( "Black Piece threw an IllegalStateException while set" + "ting up to an occupied cell. This should not happen."); } catch (InvalidInitialPositionException e) { assertFalse( "Black Piece was added to the Board after failing to set up", board.contains(newKnight)); assertEquals( "More than two black Pieces (or less) were " + "added when setting up", 2, board.getPieces(false).size()); break; } } }