Added two unit tests for bounds checking

This commit is contained in:
Timmeey86 2018-11-21 23:57:24 +01:00
parent ebb763db03
commit 7683e86d8b

View File

@ -39,7 +39,7 @@ public class PowerTests{
* @param block The block on the tile.
* @return The created tile or null in case of exceptions.
*/
private static Tile createFakeTile(int x, int y, Floor floor, Block block){
private static Tile createFakeTile(int x, int y, Block block){
try{
Tile tile = new Tile(x, y);
Field field = Tile.class.getDeclaredField("wall");
@ -47,7 +47,7 @@ public class PowerTests{
field.set(tile, block);
field = Tile.class.getDeclaredField("floor");
field.setAccessible(true);
field.set(tile, floor);
field.set(tile, (Floor)Blocks.sand);
tile.entity = block.newEntity();
tile.entity.power = new PowerModule();
return tile;
@ -55,6 +55,7 @@ public class PowerTests{
return null;
}
}
private static final float epsilon = 0.00001f;
/** Makes sure calculations are accurate for the case where produced power = consumed power. */
@Test
@ -62,13 +63,13 @@ public class PowerTests{
PowerGraph powerGraph = new PowerGraph();
// Create one water extractor (5.4 power/Second = 0.09/tick)
Tile waterExtractorTile = createFakeTile(0, 0, (Floor)Blocks.sand, ProductionBlocks.waterExtractor);
Tile waterExtractorTile = createFakeTile(0, 0, ProductionBlocks.waterExtractor);
powerGraph.add(waterExtractorTile);
// Create 20 small solar panels (20*0.27=5.4 power/second = 0.09/tick)
List<Tile> solarPanelTiles = new LinkedList<>();
for(int counter = 0; counter < 20; counter++){
Tile solarPanelTile = createFakeTile( 2 + counter / 2, counter % 2, (Floor)Blocks.sand, PowerBlocks.solarPanel);
Tile solarPanelTile = createFakeTile( 2 + counter / 2, counter % 2, PowerBlocks.solarPanel);
powerGraph.add(solarPanelTile);
solarPanelTiles.add(solarPanelTile);
}
@ -79,7 +80,6 @@ public class PowerTests{
// If these lines fail, you probably changed power production/consumption and need to adapt this test
// OR their implementation is corrupt.
// TODO: Create fake blocks which are independent of such changes
float epsilon = 0.00001f;
assertEquals(powerNeeded, 0.09f, epsilon);
assertEquals(powerProduced, 0.09f, epsilon);
// Note: The assertions above induce that powerNeeded = powerProduced (with floating point inaccuracy)
@ -88,4 +88,39 @@ public class PowerTests{
powerGraph.distributePower(powerNeeded, powerProduced);
assertEquals(waterExtractorTile.entity.power.satisfaction, 1.0f, epsilon);
}
/** Makes sure there are no problems with zero production. */
@Test
void test_noProducers(){
PowerGraph powerGraph = new PowerGraph();
Tile waterExtractorTile = createFakeTile(0, 0, ProductionBlocks.waterExtractor);
powerGraph.add(waterExtractorTile);
float powerNeeded = powerGraph.getPowerNeeded();
float powerProduced = powerGraph.getPowerProduced();
assertEquals(powerGraph.getPowerNeeded(), 0.09f, epsilon);
assertEquals(powerGraph.getPowerProduced(), 0.0f, epsilon);
powerGraph.distributePower(powerNeeded, powerProduced);
assertEquals(waterExtractorTile.entity.power.satisfaction, 0.0f, epsilon);
}
/** Makes sure there are no problems with zero consumers. */
@Test
void test_noConsumers(){
PowerGraph powerGraph = new PowerGraph();
Tile solarPanelTile = createFakeTile( 0, 0, PowerBlocks.solarPanel);
powerGraph.add(solarPanelTile);
float powerNeeded = powerGraph.getPowerNeeded();
float powerProduced = powerGraph.getPowerProduced();
assertEquals(powerGraph.getPowerNeeded(), 0.0f, epsilon);
assertEquals(powerGraph.getPowerProduced(), 0.0045f, epsilon);
powerGraph.distributePower(powerNeeded, powerProduced);
}
}