This commit is contained in:
Anuken 2020-03-14 12:29:12 -04:00
commit 7e781d9ff8
21 changed files with 120 additions and 90 deletions

View file

@ -43,6 +43,13 @@ public class ApplicationTests{
net = new Net(null);
tree = new FileTree();
Vars.init();
world = new World(){
@Override
public float getDarkness(int x, int y){
//for world borders
return 0;
}
};
content.createBaseContent();
add(logic = new Logic());
@ -124,11 +131,8 @@ public class ApplicationTests{
assertEquals(world.tile(bx, by).team(), Team.sharded);
for(int x = bx - 1; x <= bx + 1; x++){
for(int y = by - 1; y <= by + 1; y++){
if(x == bx && by == y){
assertEquals(world.tile(x, y).block(), Blocks.coreShard);
}else{
assertTrue(world.tile(x, y).block() instanceof BlockPart && world.tile(x, y).link() == world.tile(bx, by));
}
assertEquals(world.tile(x, y).block(), Blocks.coreShard);
assertEquals(world.tile(x, y).entity, world.tile(bx, by).entity);
}
}
}
@ -215,18 +219,18 @@ public class ApplicationTests{
world.tile(0, 0).setBlock(Blocks.conveyor);
world.tile(0, 0).rotation(0);
Blocks.conveyor.acceptStack(world.tile(0, 0), Items.copper, 1000, null);
world.tile(0, 0).entity.acceptStack(Items.copper, 1000, null);
}
@Test
void conveyorBench(){
int[] items = {0};
int[] itemsa = {0};
world.loadMap(testMap);
state.set(State.playing);
int length = 128;
world.tile(0, 0).setBlock(Blocks.itemSource);
world.tile(0, 0).configureAny(Items.copper.id);
world.tile(0, 0).configureAny(Items.copper);
Array<Tilec> entities = Array.with(world.tile(0, 0).entity);
@ -236,17 +240,22 @@ public class ApplicationTests{
entities.add(world.tile(i + 1, 0).entity);
}
world.tile(length + 1, 0).setBlock(new Block("___"){
@Override
public void handleItem(Tile tile, Tile source, Item item){
items[0] ++;
}
world.tile(length + 1, 0).setBlock(new Block("___"){{
hasItems = true;
entityType = () -> new TileEntity(){
@Override
public void handleItem(Tilec source, Item item){
itemsa[0] ++;
}
@Override
public boolean acceptItem(Tile tile, Tile source, Item item){
return true;
}
});
@Override
public boolean acceptItem(Tilec source, Item item){
return true;
}
};
}});
entities.each(Tilec::updateProximity);
//warmup
for(int i = 0; i < 100000; i++){
@ -257,8 +266,8 @@ public class ApplicationTests{
for(int i = 0; i < 200000; i++){
entities.each(Tilec::update);
}
Log.info(Time.elapsed() + "ms to process " + items[0] + " items");
assertTrue(items[0] > 0);
Log.info(Time.elapsed() + "ms to process " + itemsa[0] + " items");
assertNotEquals(0, itemsa[0]);
}
@Test
@ -337,8 +346,11 @@ public class ApplicationTests{
Builderc d1 = (Builderc)UnitTypes.phantom.create(Team.sharded);
Builderc d2 = (Builderc)UnitTypes.phantom.create(Team.sharded);
d1.set(10f, 20f);
d2.set(10f, 20f);
//infinite build range
state.rules.editor = true;
d1.set(0f, 0f);
d2.set(20f, 20f);
d1.addBuild(new BuildRequest(0, 0, 0, Blocks.copperWallLarge));
d2.addBuild(new BuildRequest(1, 1, 0, Blocks.copperWallLarge));
@ -349,7 +361,8 @@ public class ApplicationTests{
assertEquals(Blocks.copperWallLarge, world.tile(0, 0).block());
assertEquals(Blocks.air, world.tile(2, 2).block());
assertTrue(world.tile(1, 1).block() instanceof BlockPart);
assertEquals(Blocks.copperWallLarge, world.tile(1, 1).block());
assertEquals(world.tile(1, 1).entity, world.tile(0, 0).entity);
}
@Test
@ -375,6 +388,12 @@ public class ApplicationTests{
Time.setDeltaProvider(() -> 9999f);
d1.update();
assertEquals(Blocks.copperWallLarge, world.tile(0, 0).block());
assertEquals(Blocks.copperWallLarge, world.tile(1, 1).block());
d2.clearBuilding();
d2.addBuild(new BuildRequest(1, 1));
d2.update();
assertEquals(Blocks.air, world.tile(0, 0).block());
@ -439,16 +458,16 @@ public class ApplicationTests{
assertNotNull(tile.entity, "Tile should have an entity, but does not: " + tile);
int deposited = tile.acceptStack(item, capacity - 1, unit);
int deposited = tile.entity.acceptStack(item, capacity - 1, unit);
assertEquals(capacity - 1, deposited);
tile.handleStack(item, capacity - 1, unit);
tile.entity.handleStack(item, capacity - 1, unit);
assertEquals(tile.entity.items().get(item), capacity - 1);
int overflow = tile.acceptStack(item, 10, unit);
int overflow = tile.entity.acceptStack(item, 10, unit);
assertEquals(1, overflow);
tile.handleStack(item, 1, unit);
tile.entity.handleStack(item, 1, unit);
assertEquals(capacity, tile.entity.items().get(item));
}
}

View file

@ -32,6 +32,7 @@ public class DirectConsumerTests extends PowerTestFixture{
void testUnitFactory(int siliconAmount, int leadAmount, float producedPower, float requestedPower, float expectedSatisfaction){
Tile consumerTile = createFakeTile(0, 0, new UnitFactory("fakefactory"){{
entityType = UnitFactoryEntity::new;
unitType = UnitTypes.spirit;
produceTime = 60;
consumes.power(requestedPower);
@ -44,8 +45,8 @@ public class DirectConsumerTests extends PowerTestFixture{
producerTile.<PowerGenerator.GeneratorEntity>ent().productionEfficiency = 1f;
PowerGraph graph = new PowerGraph();
graph.add(producerTile);
graph.add(consumerTile);
graph.add(producerTile.entity);
graph.add(consumerTile.entity);
consumerTile.entity.update();
graph.update();

View file

@ -38,6 +38,7 @@ public class ItemLiquidGeneratorTests extends PowerTestFixture{
powerProduction = 0.1f;
itemDuration = fakeItemDuration;
maxLiquidGenerate = maximumLiquidUsage;
entityType = ItemLiquidGeneratorEntity::new;
}
@Override
@ -85,13 +86,13 @@ public class ItemLiquidGeneratorTests extends PowerTestFixture{
final float expectedRemainingLiquidAmount = Math.max(0.0f, availableLiquidAmount - expectedConsumptionPerTick * Time.delta());
createGenerator(inputType);
assertTrue(generator.acceptLiquid(tile, null, liquid, availableLiquidAmount), inputType + " | " + parameterDescription + ": Liquids which will be declined by the generator don't need to be tested - The code won't be called for those cases.");
assertTrue(entity.acceptLiquid(null, liquid, availableLiquidAmount), inputType + " | " + parameterDescription + ": Liquids which will be declined by the generator don't need to be tested - The code won't be called for those cases.");
entity.liquids().add(liquid, availableLiquidAmount);
entity.cons().update();
// Perform an update on the generator once - This should use up any resource up to the maximum liquid usage
generator.update(tile);
entity.updateTile();
assertEquals(expectedRemainingLiquidAmount, entity.liquids().get(liquid), inputType + " | " + parameterDescription + ": Remaining liquid amount mismatch.");
assertEquals(expectedEfficiency, entity.productionEfficiency, inputType + " | " + parameterDescription + ": Efficiency mismatch.");
@ -127,7 +128,7 @@ public class ItemLiquidGeneratorTests extends PowerTestFixture{
final float expectedRemainingItemAmount = Math.max(0, amount - 1);
createGenerator(inputType);
assertTrue(generator.acceptItem(tile, null, item), inputType + " | " + parameterDescription + ": Items which will be declined by the generator don't need to be tested - The code won't be called for those cases.");
assertTrue(entity.acceptItem(null, item), inputType + " | " + parameterDescription + ": Items which will be declined by the generator don't need to be tested - The code won't be called for those cases.");
if(amount > 0){
entity.items().add(item, amount);
@ -136,7 +137,7 @@ public class ItemLiquidGeneratorTests extends PowerTestFixture{
// Perform an update on the generator once - This should use up one or zero items - dependent on if the item is accepted and available or not.
try{
generator.update(tile);
entity.updateTile();
assertEquals(expectedRemainingItemAmount, entity.items().get(item), inputType + " | " + parameterDescription + ": Remaining item amount mismatch.");
assertEquals(expectedEfficiency, entity.productionEfficiency, inputType + " | " + parameterDescription + ": Efficiency mismatch.");
@ -164,16 +165,16 @@ public class ItemLiquidGeneratorTests extends PowerTestFixture{
// Burn a single coal and test for the duration
entity.items().add(Items.coal, 1);
entity.cons().update();
generator.update(tile);
entity.updateTile();
float expectedEfficiency = entity.productionEfficiency;
float currentDuration = 0.0f;
while((currentDuration += Time.delta()) <= fakeItemDuration){
generator.update(tile);
entity.updateTile();
assertEquals(expectedEfficiency, entity.productionEfficiency, "Duration: " + currentDuration);
}
generator.update(tile);
entity.updateTile();
assertEquals(0.0f, entity.productionEfficiency, "Duration: " + currentDuration);
}

View file

@ -7,13 +7,13 @@ import mindustry.*;
import mindustry.content.*;
import mindustry.core.*;
import mindustry.ctype.*;
import mindustry.game.*;
import mindustry.gen.*;
import mindustry.world.*;
import mindustry.world.blocks.power.*;
import mindustry.world.modules.*;
import org.junit.jupiter.api.*;
import java.lang.reflect.*;
import static mindustry.Vars.*;
/**
@ -44,18 +44,21 @@ public class PowerTestFixture{
protected static PowerGenerator createFakeProducerBlock(float producedPower){
return new PowerGenerator("fakegen"){{
entityType = () -> new GeneratorEntity();
powerProduction = producedPower;
}};
}
protected static Battery createFakeBattery(float capacity){
return new Battery("fakebattery"){{
entityType = () -> new BatteryEntity();
consumes.powerBuffered(capacity);
}};
}
protected static Block createFakeDirectConsumer(float powerPerTick){
return new PowerBlock("fakedirectconsumer"){{
entityType = TileEntity::create;
consumes.power(powerPerTick);
}};
}
@ -80,16 +83,11 @@ public class PowerTestFixture{
// Since this part shall not be part of the test and would require more work anyway, we manually set the block and floor
// through reflections and then simulate part of what the changed() method does.
Field field = Tile.class.getDeclaredField("block");
field.setAccessible(true);
field.set(tile, block);
field = Tile.class.getDeclaredField("floor");
field.setAccessible(true);
field.set(tile, Blocks.sand);
Reflect.set(Tile.class, tile, "block", block);
Reflect.set(Tile.class, tile, "floor", Blocks.sand);
// Simulate the "changed" method. Calling it through reflections would require half the game to be initialized.
tile.entity = block.newEntity().init(tile, false);
tile.entity = block.newEntity().init(tile, Team.sharded, false);
tile.entity.cons(new ConsumeModule(tile.entity));
if(block.hasItems) tile.entity.items(new ItemModule());
if(block.hasLiquids) tile.entity.liquids(new LiquidModule());
@ -102,7 +100,7 @@ public class PowerTestFixture{
return 1f;
}
};
tile.entity.power().graph.add(tile);
tile.entity.power().graph.add(tile.entity);
}
// Assign incredibly high health so the block does not get destroyed on e.g. burning Blast Compound

View file

@ -58,8 +58,8 @@ public class PowerTests extends PowerTestFixture{
Tile directConsumerTile = createFakeTile(0, 1, createFakeDirectConsumer(requiredPower));
PowerGraph powerGraph = new PowerGraph();
powerGraph.add(producerTile);
powerGraph.add(directConsumerTile);
powerGraph.add(producerTile.entity);
powerGraph.add(directConsumerTile.entity);
assertEquals(producedPower * Time.delta(), powerGraph.getPowerProduced(), Mathf.FLOAT_ROUNDING_ERROR);
assertEquals(requiredPower * Time.delta(), powerGraph.getPowerNeeded(), Mathf.FLOAT_ROUNDING_ERROR);
@ -95,18 +95,18 @@ public class PowerTests extends PowerTestFixture{
if(producedPower > 0.0f){
Tile producerTile = createFakeTile(0, 0, createFakeProducerBlock(producedPower));
producerTile.<PowerGenerator.GeneratorEntity>ent().productionEfficiency = 1f;
powerGraph.add(producerTile);
powerGraph.add(producerTile.entity);
}
Tile directConsumerTile = null;
if(requestedPower > 0.0f){
directConsumerTile = createFakeTile(0, 1, createFakeDirectConsumer(requestedPower));
powerGraph.add(directConsumerTile);
powerGraph.add(directConsumerTile.entity);
}
float maxCapacity = 100f;
Tile batteryTile = createFakeTile(0, 2, createFakeBattery(maxCapacity));
batteryTile.entity.power().status = initialBatteryCapacity / maxCapacity;
powerGraph.add(batteryTile);
powerGraph.add(batteryTile.entity);
powerGraph.update();
assertEquals(expectedBatteryCapacity / maxCapacity, batteryTile.entity.power().status, Mathf.FLOAT_ROUNDING_ERROR, parameterDescription + ": Expected battery status did not match");
@ -123,14 +123,14 @@ public class PowerTests extends PowerTestFixture{
Tile consumerTile = createFakeTile(0, 1, createFakeDirectConsumer(5.0f));
PowerGraph powerGraph = new PowerGraph();
powerGraph.add(producerTile);
powerGraph.add(consumerTile);
powerGraph.add(producerTile.entity);
powerGraph.add(consumerTile.entity);
powerGraph.update();
assertEquals(1.0f, consumerTile.entity.power().status, Mathf.FLOAT_ROUNDING_ERROR);
powerGraph.remove(producerTile);
powerGraph.add(consumerTile);
powerGraph.remove(producerTile.entity);
powerGraph.add(consumerTile.entity);
powerGraph.update();
assertEquals(0.0f, consumerTile.entity.power().status, Mathf.FLOAT_ROUNDING_ERROR);