-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use BFS class for GameMap.getDistance().
- Loading branch information
Showing
6 changed files
with
136 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
game-app/game-core/src/test/java/games/strategy/engine/data/GameMapTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package games.strategy.engine.data; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import games.strategy.triplea.xml.TestMapGameData; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class GameMapTest { | ||
private final GameData gameData = TestMapGameData.REVISED.getGameData(); | ||
private final Territory caucasus = gameData.getMap().getTerritory("Caucasus"); | ||
private final Territory germany = gameData.getMap().getTerritory("Germany"); | ||
private final Territory russia = gameData.getMap().getTerritory("Russia"); | ||
private final Territory uk = gameData.getMap().getTerritory("United Kingdom"); | ||
|
||
private int getLandDistance(Territory from, Territory to) { | ||
return gameData.getMap().getLandDistance(from, to); | ||
} | ||
|
||
@Test | ||
void testLandDistance() { | ||
assertThat(getLandDistance(caucasus, russia), is(1)); | ||
assertThat(getLandDistance(caucasus, germany), is(3)); | ||
} | ||
|
||
@Test | ||
void testLandDistanceNotFound() { | ||
assertThat(getLandDistance(caucasus, uk), is(-1)); | ||
} | ||
|
||
@Test | ||
void testLandDistanceSameTerritory() { | ||
assertThat(getLandDistance(caucasus, caucasus), is(0)); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
game-app/game-core/src/test/java/games/strategy/engine/data/util/BreadthFirstSearchTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package games.strategy.engine.data.util; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import games.strategy.engine.data.GameData; | ||
import games.strategy.engine.data.Territory; | ||
import games.strategy.triplea.delegate.Matches; | ||
import games.strategy.triplea.xml.TestMapGameData; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class BreadthFirstSearchTest { | ||
private final GameData gameData = TestMapGameData.REVISED.getGameData(); | ||
private final Territory caucasus = gameData.getMap().getTerritory("Caucasus"); | ||
private final Territory germany = gameData.getMap().getTerritory("Germany"); | ||
private final Territory russia = gameData.getMap().getTerritory("Russia"); | ||
private final Territory uk = gameData.getMap().getTerritory("United Kingdom"); | ||
|
||
private int getLandDistance(Territory from, Territory to) { | ||
var territoryFinder = new BreadthFirstSearch.TerritoryFinder(to); | ||
new BreadthFirstSearch(from, Matches.territoryIsLand()).traverse(territoryFinder); | ||
return territoryFinder.getDistanceFound(); | ||
} | ||
|
||
@Test | ||
void testLandDistance() { | ||
assertThat(getLandDistance(caucasus, russia), is(1)); | ||
assertThat(getLandDistance(caucasus, germany), is(3)); | ||
} | ||
|
||
@Test | ||
void testLandDistanceNotFound() { | ||
assertThat(getLandDistance(caucasus, uk), is(-1)); | ||
} | ||
|
||
@Test | ||
void testLandDistanceSameTerritory() { | ||
// Note: This is testing the limitation described in the API doc. | ||
// This is a test for the low-level helper class, but the high level API, which is tested by | ||
// GameMapTest.testLandDistanceSameTerritory() returns the expected result of 0. | ||
assertThat(getLandDistance(caucasus, caucasus), is(-1)); | ||
} | ||
} |