mirror of
https://github.com/Anuken/Mindustry.git
synced 2025-07-16 18:58:01 +07:00
Added two unit tests for bounds checking
This commit is contained in:
@ -39,7 +39,7 @@ public class PowerTests{
|
|||||||
* @param block The block on the tile.
|
* @param block The block on the tile.
|
||||||
* @return The created tile or null in case of exceptions.
|
* @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{
|
try{
|
||||||
Tile tile = new Tile(x, y);
|
Tile tile = new Tile(x, y);
|
||||||
Field field = Tile.class.getDeclaredField("wall");
|
Field field = Tile.class.getDeclaredField("wall");
|
||||||
@ -47,7 +47,7 @@ public class PowerTests{
|
|||||||
field.set(tile, block);
|
field.set(tile, block);
|
||||||
field = Tile.class.getDeclaredField("floor");
|
field = Tile.class.getDeclaredField("floor");
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
field.set(tile, floor);
|
field.set(tile, (Floor)Blocks.sand);
|
||||||
tile.entity = block.newEntity();
|
tile.entity = block.newEntity();
|
||||||
tile.entity.power = new PowerModule();
|
tile.entity.power = new PowerModule();
|
||||||
return tile;
|
return tile;
|
||||||
@ -55,6 +55,7 @@ public class PowerTests{
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private static final float epsilon = 0.00001f;
|
||||||
|
|
||||||
/** Makes sure calculations are accurate for the case where produced power = consumed power. */
|
/** Makes sure calculations are accurate for the case where produced power = consumed power. */
|
||||||
@Test
|
@Test
|
||||||
@ -62,13 +63,13 @@ public class PowerTests{
|
|||||||
PowerGraph powerGraph = new PowerGraph();
|
PowerGraph powerGraph = new PowerGraph();
|
||||||
|
|
||||||
// Create one water extractor (5.4 power/Second = 0.09/tick)
|
// 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);
|
powerGraph.add(waterExtractorTile);
|
||||||
|
|
||||||
// Create 20 small solar panels (20*0.27=5.4 power/second = 0.09/tick)
|
// Create 20 small solar panels (20*0.27=5.4 power/second = 0.09/tick)
|
||||||
List<Tile> solarPanelTiles = new LinkedList<>();
|
List<Tile> solarPanelTiles = new LinkedList<>();
|
||||||
for(int counter = 0; counter < 20; counter++){
|
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);
|
powerGraph.add(solarPanelTile);
|
||||||
solarPanelTiles.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
|
// If these lines fail, you probably changed power production/consumption and need to adapt this test
|
||||||
// OR their implementation is corrupt.
|
// OR their implementation is corrupt.
|
||||||
// TODO: Create fake blocks which are independent of such changes
|
// TODO: Create fake blocks which are independent of such changes
|
||||||
float epsilon = 0.00001f;
|
|
||||||
assertEquals(powerNeeded, 0.09f, epsilon);
|
assertEquals(powerNeeded, 0.09f, epsilon);
|
||||||
assertEquals(powerProduced, 0.09f, epsilon);
|
assertEquals(powerProduced, 0.09f, epsilon);
|
||||||
// Note: The assertions above induce that powerNeeded = powerProduced (with floating point inaccuracy)
|
// Note: The assertions above induce that powerNeeded = powerProduced (with floating point inaccuracy)
|
||||||
@ -88,4 +88,39 @@ public class PowerTests{
|
|||||||
powerGraph.distributePower(powerNeeded, powerProduced);
|
powerGraph.distributePower(powerNeeded, powerProduced);
|
||||||
assertEquals(waterExtractorTile.entity.power.satisfaction, 1.0f, epsilon);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user