You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
5.6 KiB
157 lines
5.6 KiB
|
|
package de.tims.fleetstorm.matchfield;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
import java.util.stream.Stream;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.params.ParameterizedTest;
|
|
import org.junit.jupiter.params.provider.Arguments;
|
|
import org.junit.jupiter.params.provider.MethodSource;
|
|
|
|
class MatchfieldShipTest {
|
|
|
|
private Matchfield matchfield;
|
|
private int matchfieldSize = 10;
|
|
|
|
@BeforeEach
|
|
void setup() {
|
|
matchfield = new Matchfield(matchfieldSize);
|
|
matchfield.createMatchfield();
|
|
}
|
|
|
|
@ParameterizedTest(name = "ship was set on the correct positions")
|
|
@MethodSource("testShipPositioning")
|
|
void testMatchfieldShipSetHasCorrectPositions(String testName, Coordinate coordinate, int direction, int length,
|
|
Coordinate[] coordinatesWithShip) {
|
|
|
|
matchfield.setShip(coordinate, length, direction);
|
|
|
|
int shipSetCounter = 0;
|
|
for (Coordinate toCheckCoordinate : coordinatesWithShip) {
|
|
if (matchfield.getField(toCheckCoordinate).getState() == Coordinate.SHIP) {
|
|
shipSetCounter++;
|
|
}
|
|
}
|
|
|
|
assertThat(shipSetCounter).describedAs(testName).isEqualTo(length);
|
|
}
|
|
|
|
static Stream<Arguments> testShipPositioning() {
|
|
Coordinate[] coordinatesWithShip00_2_0 = new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 0) };
|
|
Coordinate[] coordinatesWithShip01_3_0 = new Coordinate[] { new Coordinate(0, 1), new Coordinate(1, 1),
|
|
new Coordinate(2, 1) };
|
|
Coordinate[] coordinatesWithShip02_4_1 = new Coordinate[] { new Coordinate(0, 2), new Coordinate(0, 3),
|
|
new Coordinate(0, 4), new Coordinate(0, 5) };
|
|
|
|
return Stream.of(
|
|
Arguments.of("set ship from 0:0, length 2, direction 0", new Coordinate(0, 0), 0, 2,
|
|
coordinatesWithShip00_2_0),
|
|
Arguments.of("set ship from 0:1, length 3, direction 0", new Coordinate(0, 1), 0, 3,
|
|
coordinatesWithShip01_3_0),
|
|
Arguments.of("set ship from 0:2, length 4, direction 1", new Coordinate(0, 2), 1, 4,
|
|
coordinatesWithShip02_4_1));
|
|
}
|
|
|
|
@ParameterizedTest(name = "ship positioning on invalid or valid coordinates")
|
|
@MethodSource("testShipPositioningFailed")
|
|
void testMatchfieldShipSetWithInvalidOrValidCoordinates(String testName, Coordinate coordinate, int direction,
|
|
int length, boolean expectedResult) {
|
|
|
|
boolean calculatedResult = matchfield.setShip(coordinate, length, direction);
|
|
assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult);
|
|
}
|
|
|
|
static Stream<Arguments> testShipPositioningFailed() {
|
|
return Stream.of(
|
|
Arguments.of("invalid ship position from 9:0, length 2, direction 0 (x-axis)", new Coordinate(9, 0), 0,
|
|
2, false),
|
|
Arguments.of("valid ship position from 8:0, length 5, direction 1 (y-axis)", new Coordinate(9, 0), 1, 5,
|
|
true));
|
|
}
|
|
|
|
@Test
|
|
void testShipPositionAlreadySetWithShip() {
|
|
|
|
matchfield.setShip(new Coordinate(0, 0), 5, 1);
|
|
assertThat(matchfield.setShip(new Coordinate(0, 1), 5, 1)).describedAs("set ship on coordinate with ship")
|
|
.isEqualTo(false);
|
|
|
|
assertThat(matchfield.setShip(new Coordinate(1, 1), 4, 0)).describedAs("set ship on coordinate without ship")
|
|
.isEqualTo(true);
|
|
assertThat(matchfield.setShip(new Coordinate(4, 1), 2, 1))
|
|
.describedAs("set second ship on coordinate without ship").isEqualTo(false);
|
|
}
|
|
|
|
@Test
|
|
void testIfAllShipsHitReturnsFalseWhenNoShipIsHit() {
|
|
matchfield.setShip(new Coordinate(0, 0), 5, 1);
|
|
boolean calulatedResult = matchfield.areAllShipsHit();
|
|
assertFalse(calulatedResult);
|
|
}
|
|
|
|
@Test
|
|
void testIfAllShipsHitReturnsTrueWhenAllShipsAreHit() {
|
|
matchfield.setShip(new Coordinate(0, 0), 5, 1);
|
|
|
|
setFieldRangeHit(matchfield);
|
|
|
|
boolean calulatedResult = matchfield.areAllShipsHit();
|
|
assertTrue(calulatedResult);
|
|
}
|
|
|
|
@Test
|
|
void testIfAllShipsHitReturnsFalseWhenTwoShipsAreNotFullyHit() {
|
|
matchfield.setShip(new Coordinate(0, 0), 5, 1);
|
|
|
|
setFieldRangeHit(matchfield);
|
|
|
|
matchfield.setShip(new Coordinate(3, 4), 2, 0);
|
|
matchfield.getField(4, 4).setState(Coordinate.HIT);
|
|
|
|
boolean calulatedResult = matchfield.areAllShipsHit();
|
|
assertFalse(calulatedResult);
|
|
}
|
|
|
|
void setFieldRangeHit(Matchfield matchfield) {
|
|
for (int i = 0; i <= 4; i++) {
|
|
matchfield.getField(0, i).setState(Coordinate.HIT);
|
|
}
|
|
}
|
|
|
|
@ParameterizedTest(name = "matchfield returns correct state if a position is free or not")
|
|
@MethodSource("testIsFreeMethod")
|
|
void testMatchfieldReturnsCorrectValueAboutFreeCoordinateSlots(String testName, Coordinate originCoordinate,
|
|
int direction, int length, Coordinate[] coordinatesWithShip, boolean expectedResult) {
|
|
|
|
Matchfield matchfield = new Matchfield(matchfieldSize);
|
|
matchfield.createMatchfield();
|
|
|
|
for (Coordinate coordinate : coordinatesWithShip) {
|
|
matchfield.getField(coordinate).setState(Coordinate.SHIP);
|
|
}
|
|
|
|
boolean calculatedResult = matchfield.isFreePosition(originCoordinate, length, direction);
|
|
assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult);
|
|
|
|
}
|
|
|
|
static Stream<Arguments> testIsFreeMethod() {
|
|
return Stream.of(
|
|
Arguments.of("one ship is within the coordinates", new Coordinate(0, 0), 1, 2,
|
|
new Coordinate[] { new Coordinate(0, 0) }, false),
|
|
Arguments.of("no ship is within the coordinates", new Coordinate(1, 1), 0, 5, new Coordinate[] {},
|
|
true),
|
|
Arguments.of("one of the coordinates is hit, others on free fields", new Coordinate(5, 5), 1, 3,
|
|
new Coordinate[] { new Coordinate(5, 7), new Coordinate(5, 8), new Coordinate(5, 9) }, false));
|
|
}
|
|
|
|
@Test
|
|
void testIfRandomShipPositioningIsWorking() {
|
|
for (int shipLength = 2; shipLength <= 5; shipLength++) {
|
|
boolean calculatedResult = matchfield.setShipOnRandomPosition(shipLength);
|
|
assertTrue(calculatedResult);
|
|
}
|
|
}
|
|
}
|