mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-01-31 23:29:36 +01:00
Legger til en Direction enum og tester
This commit is contained in:
parent
827c8e0aed
commit
b31c52edd1
@ -0,0 +1,47 @@
|
||||
package inf112.fiasko.roborally.abstractions;
|
||||
|
||||
/**
|
||||
* This enum represents all possible directions for an element on the board
|
||||
*/
|
||||
public enum Direction {
|
||||
NORTH (1),
|
||||
NORTH_EAST (2),
|
||||
EAST (3),
|
||||
SOUTH_EAST (4),
|
||||
SOUTH (5),
|
||||
SOUTH_WEST (6),
|
||||
WEST (7),
|
||||
NORTH_WEST (8);
|
||||
|
||||
private final int directionID;
|
||||
|
||||
/**
|
||||
* Constructor to let a direction to be represented by a numerical identifier
|
||||
* @param directionID <p>The numerical identifier assigned to the direction</p>
|
||||
*/
|
||||
Direction(int directionID) {
|
||||
this.directionID = directionID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the numerical identifier used for alternate identification of a direction
|
||||
* @return <p>The numerical id of the direction</p>
|
||||
*/
|
||||
public int getDirectionID() {
|
||||
return this.directionID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a direction from its numerical id
|
||||
* @param directionID <p>The numerical representation of a direction</p>
|
||||
* @return <p>The enum value representing the direction, or null if the id is invalid</p>
|
||||
*/
|
||||
public static Direction getDirectionFromID(int directionID) {
|
||||
for (Direction direction : Direction.values()) {
|
||||
if (direction.directionID == directionID) {
|
||||
return direction;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
60
src/test/java/inf112/fiasko/roborally/DirectionTest.java
Normal file
60
src/test/java/inf112/fiasko/roborally/DirectionTest.java
Normal file
@ -0,0 +1,60 @@
|
||||
package inf112.fiasko.roborally;
|
||||
|
||||
import inf112.fiasko.roborally.abstractions.Direction;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
public class DirectionTest {
|
||||
|
||||
@Test
|
||||
public void getDirectionIdForNorth() {
|
||||
assertEquals(1, Direction.NORTH.getDirectionID());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDirectionFromNorthID() {
|
||||
assertEquals(Direction.NORTH, Direction.getDirectionFromID(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDirectionIdForSouthWest() {
|
||||
assertEquals(6, Direction.SOUTH_WEST.getDirectionID());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDirectionFromSouthWestID() {
|
||||
assertEquals(Direction.SOUTH_WEST, Direction.getDirectionFromID(6));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allDirectionsIDConversionToIDAndBack() {
|
||||
for (Direction direction : Direction.values()) {
|
||||
assertEquals(direction, Direction.getDirectionFromID(direction.getDirectionID()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invalidDirectionIDReturnsNull() {
|
||||
assertNull(Direction.getDirectionFromID(-1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allDirectionsHaveUniqueId() {
|
||||
/* This test is also done implicitly by the allDirectionsIDConversionToIDAndBack test, but that test may fail
|
||||
even if this test passes, so this test is needed for clarity. */
|
||||
Set<Integer> set = new HashSet<>();
|
||||
List<Integer> list = new ArrayList<>();
|
||||
for (Direction direction : Direction.values()) {
|
||||
set.add(direction.getDirectionID());
|
||||
list.add(direction.getDirectionID());
|
||||
}
|
||||
assertEquals(list.size(), set.size());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user