Added tests for Piece and added a valid move check.

This commit is contained in:
Jonathan Turner 2024-02-02 20:10:22 -05:00
parent 6025d318ff
commit a1e6bad176
4 changed files with 214 additions and 0 deletions

11
.idea/libraries/junit.xml Normal file
View File

@ -0,0 +1,11 @@
<component name="libraryTable">
<library name="junit" type="repository">
<properties maven-id="junit:junit:4.13.2" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.2/junit-4.13.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@ -0,0 +1,68 @@
package Assignments.A1.models;
/**
* This class keeps track of the current state (whether in permutation or not) of the board.
*
* @author Jonathan Turner
* @version Spring 2024
*/
public class Board {
private final Piece[] pieces;
/**
* Default constructor that generates the solved board.
*
* @precondition none
* @postcondition a solved board is created.
*/
public Board() {
this.pieces = new Piece[9];
for (int index = 1; index <= 9; index++) {
this.pieces[index-1] = new Piece(index-1, index);
}
}
/**
* Constructor used to create a board with a pre-provided board.
*
* @precondition board.length == 9
* @postcondition a board is created with given locations
*
* @param board the provided state of the board.
*/
public Board(Piece[] board) {
if (board.length != 9) {
throw new IllegalArgumentException("The board must be a array size of 9.");
}
this.pieces = board;
}
public boolean isTaken(int loc) {
if (invalidLocation(loc)) {
throw new IllegalArgumentException("Invalid location specified. Valid Range: 0 <= Location <= 8");
}
return (this.pieces[loc].getValue() != 0);
}
/**
* Returns the array of pieces.
*
* @precondition none
* @postcondition none
* @return the array of pieces.
*/
public Piece[] getPieces() {
return this.pieces;
}
/* Private Methods Below. End of Java Docs. */
/* Checks if a given location is a valid location. */
private boolean invalidLocation(int loc) {
return (loc >= 0 && loc <= 8);
}
}

View File

@ -0,0 +1,75 @@
package Assignments.A1.tests.Piece;
import static org.junit.Assert.*;
import Assignments.A1.models.Piece;
import org.junit.Test;
/**
* Tests the constructor for Piece.java.
*
* @author Jonathan Turner
* @version Spring 2024
*/
public class TestConstructor {
@Test
public void testZeroValue() {
assertThrows(IllegalArgumentException.class, () -> {
new Piece(5, 0);
});
}
@Test
public void testOneValue() {
Piece test = new Piece(5, 1);
assertEquals(1, test.getValue());
}
@Test
public void testEightValue() {
Piece test = new Piece(5, 8);
assertEquals(8, test.getValue());
}
@Test
public void testNineValue() {
assertThrows(IllegalArgumentException.class, () -> {
new Piece(5, 9);
});
}
@Test
public void testNegativeLoc() {
assertThrows(IllegalArgumentException.class, () -> {
new Piece(-1, 5);
});
}
@Test
public void testZeroLoc() {
Piece test = new Piece(0, 5);
assertEquals(0, test.getLoc());
}
@Test
public void testEightLoc() {
Piece test = new Piece(8, 5);
assertEquals(8, test.getLoc());
}
@Test
public void testNineLoc() {
assertThrows(IllegalArgumentException.class, () -> {
new Piece(9, 5);
});
}
@Test
public void validParams() {
Piece test = new Piece(3, 6);
assertEquals(3, test.getLoc());
assertEquals(6, test.getValue());
}
}

View File

@ -0,0 +1,60 @@
package Assignments.A1.tests.Piece;
import static org.junit.Assert.*;
import Assignments.A1.models.Piece;
import org.junit.Test;
/**
* Tests the moveTo() for Piece.java.
*
* @author Jonathan Turner
* @version Spring 2024
*/
public class TestMoveTo {
@Test
public void testNegativeMove() {
Piece test = new Piece(3, 5);
assertThrows(IllegalArgumentException.class, () -> {
test.moveTo(-1);
});
}
@Test
public void testZeroMove() {
Piece test = new Piece(3, 5);
test.moveTo(0);
assertEquals(0, test.getLoc());
}
@Test
public void testEightMove() {
Piece test = new Piece(5, 5);
test.moveTo(8);
assertEquals(8, test.getLoc());
}
@Test
public void testNineMove() {
Piece test = new Piece(3, 5);
assertThrows(IllegalArgumentException.class, () -> {
test.moveTo(9);
});
}
@Test
public void MoveToSameLocation() {
Piece test = new Piece(3, 5);
assertThrows(IllegalArgumentException.class, () -> {
test.moveTo(3);
});
}
@Test
public void ValidMove() {
Piece test = new Piece(3, 5);
test.moveTo(4);
assertEquals(4, test.getLoc());
}
}