Files
Mindustry/tests/src/test/java/ApplicationTests.java

450 lines
13 KiB
Java
Raw Normal View History

2020-01-26 11:40:09 -05:00
import arc.*;
import arc.backend.headless.*;
import arc.math.geom.*;
2019-12-25 01:39:38 -05:00
import arc.struct.*;
2020-01-26 11:40:09 -05:00
import arc.util.*;
import mindustry.*;
2019-12-25 01:39:38 -05:00
import mindustry.content.*;
import mindustry.core.*;
2020-01-26 11:40:09 -05:00
import mindustry.core.GameState.*;
import mindustry.ctype.*;
import mindustry.entities.traits.BuilderTrait.*;
import mindustry.entities.type.*;
2019-12-25 01:39:38 -05:00
import mindustry.entities.type.base.*;
2020-01-26 11:40:09 -05:00
import mindustry.game.*;
import mindustry.io.*;
import mindustry.maps.*;
import mindustry.net.Net;
import mindustry.type.*;
2019-12-25 01:39:38 -05:00
import mindustry.world.*;
2020-01-26 11:40:09 -05:00
import mindustry.world.blocks.*;
2019-04-07 14:09:34 -04:00
import org.junit.jupiter.api.*;
2018-10-03 20:58:35 -04:00
2019-12-25 01:39:38 -05:00
import static mindustry.Vars.*;
2018-10-03 20:58:35 -04:00
import static org.junit.jupiter.api.Assertions.*;
public class ApplicationTests{
static Map testMap;
2019-04-07 14:53:53 -04:00
static boolean initialized;
2018-10-03 20:58:35 -04:00
@BeforeAll
static void launchApplication(){
2019-04-07 14:53:53 -04:00
//only gets called once
if(initialized) return;
initialized = true;
2018-10-03 20:58:35 -04:00
try{
boolean[] begins = {false};
Throwable[] exceptionThrown = {null};
Log.setUseColors(false);
2018-12-27 15:47:17 -05:00
ApplicationCore core = new ApplicationCore(){
2018-10-03 20:58:35 -04:00
@Override
2018-12-27 15:47:17 -05:00
public void setup(){
2018-10-03 20:58:35 -04:00
headless = true;
2019-09-07 14:11:50 -04:00
net = new Net(null);
2019-10-02 21:29:35 -04:00
tree = new FileTree();
Vars.init();
2019-12-07 14:10:39 -05:00
content.createBaseContent();
2018-10-03 20:58:35 -04:00
2018-12-27 15:47:17 -05:00
add(logic = new Logic());
add(netServer = new NetServer());
content.init();
2018-10-03 20:58:35 -04:00
}
@Override
2018-12-27 15:47:17 -05:00
public void init(){
super.init();
2018-10-03 20:58:35 -04:00
begins[0] = true;
2019-08-26 22:53:11 -04:00
testMap = maps.loadInternalMap("groundZero");
Thread.currentThread().interrupt();
2018-10-03 20:58:35 -04:00
}
};
2019-05-13 11:32:57 -04:00
new HeadlessApplication(core, null, throwable -> exceptionThrown[0] = throwable);
2018-10-03 20:58:35 -04:00
while(!begins[0]){
if(exceptionThrown[0] != null){
fail(exceptionThrown[0]);
}
Thread.sleep(10);
}
}catch(Throwable r){
fail(r);
}
}
@BeforeEach
void resetWorld(){
2019-04-08 09:03:18 -04:00
Time.setDeltaProvider(() -> 1f);
2018-10-03 20:58:35 -04:00
logic.reset();
state.set(State.menu);
}
@Test
void initialization(){
2018-10-07 23:22:54 -04:00
assertNotNull(logic);
assertNotNull(world);
2018-10-03 20:58:35 -04:00
assertTrue(content.getContentMap().length > 0);
}
@Test
void playMap(){
world.loadMap(testMap);
2018-10-03 20:58:35 -04:00
}
@Test
void spawnWaves(){
world.loadMap(testMap);
2019-08-26 22:53:11 -04:00
assertTrue(spawner.countSpawns() > 0, "No spawns present.");
2018-10-03 20:58:35 -04:00
logic.runWave();
//force trigger delayed spawns
Time.setDeltaProvider(() -> 1000f);
Time.update();
Time.update();
Time.setDeltaProvider(() -> 1f);
2019-12-26 17:46:01 -05:00
unitGroup.update();
assertFalse(unitGroup.isEmpty(), "No enemies spawned.");
2018-10-03 20:58:35 -04:00
}
@Test
void createMap(){
Tile[][] tiles = world.createTiles(8, 8);
world.beginMapLoad();
for(int x = 0; x < tiles.length; x++){
for(int y = 0; y < tiles[0].length; y++){
2019-05-09 14:39:52 -04:00
tiles[x][y] = new Tile(x, y);
2018-10-03 20:58:35 -04:00
}
}
world.endMapLoad();
}
@Test
void multiblock(){
createMap();
int bx = 4;
int by = 4;
2019-12-26 17:46:01 -05:00
world.tile(bx, by).set(Blocks.coreShard, Team.sharded);
2019-08-02 10:10:37 -04:00
assertEquals(world.tile(bx, by).getTeam(), Team.sharded);
2019-04-08 09:03:18 -04:00
for(int x = bx - 1; x <= bx + 1; x++){
for(int y = by - 1; y <= by + 1; y++){
2018-10-03 20:58:35 -04:00
if(x == bx && by == y){
2019-01-31 15:33:07 -05:00
assertEquals(world.tile(x, y).block(), Blocks.coreShard);
2018-10-03 20:58:35 -04:00
}else{
2019-05-09 14:39:52 -04:00
assertTrue(world.tile(x, y).block() instanceof BlockPart && world.tile(x, y).link() == world.tile(bx, by));
2018-10-03 20:58:35 -04:00
}
}
}
}
@Test
void blockInventories(){
multiblock();
Tile tile = world.tile(4, 4);
tile.entity.items.add(Items.coal, 5);
tile.entity.items.add(Items.titanium, 50);
2018-10-07 23:22:54 -04:00
assertEquals(tile.entity.items.total(), 55);
tile.entity.items.remove(Items.phasefabric, 10);
2018-10-03 20:58:35 -04:00
tile.entity.items.remove(Items.titanium, 10);
2018-10-07 23:22:54 -04:00
assertEquals(tile.entity.items.total(), 45);
2018-10-03 20:58:35 -04:00
}
@Test
void timers(){
boolean[] ran = {false};
Time.run(1.9999f, () -> ran[0] = true);
2018-10-03 20:58:35 -04:00
Time.update();
2018-10-03 20:58:35 -04:00
assertFalse(ran[0]);
Time.update();
2018-10-03 20:58:35 -04:00
assertTrue(ran[0]);
}
@Test
void manyTimers(){
int runs = 100000;
int[] total = {0};
for(int i = 0; i < runs; i++){
Time.run(0.999f, () -> total[0]++);
}
assertEquals(0, total[0]);
Time.update();
assertEquals(runs, total[0]);
}
@Test
void longTimers(){
Time.setDeltaProvider(() -> Float.MAX_VALUE);
Time.update();
int steps = 100;
float delay = 100000f;
Time.setDeltaProvider(() -> delay / steps + 0.01f);
int runs = 100000;
int[] total = {0};
for(int i = 0; i < runs; i++){
Time.run(delay, () -> total[0]++);
}
assertEquals(0, total[0]);
for(int i = 0; i < steps; i++){
Time.update();
}
assertEquals(runs, total[0]);
}
2018-10-03 20:58:35 -04:00
@Test
void save(){
world.loadMap(testMap);
2019-12-26 17:46:01 -05:00
assertTrue(state.teams.playerCores().size > 0);
SaveIO.save(saveDirectory.child("0.msav"));
2018-10-03 20:58:35 -04:00
}
@Test
void load(){
world.loadMap(testMap);
2018-10-03 20:58:35 -04:00
Map map = world.getMap();
SaveIO.save(saveDirectory.child("0.msav"));
2018-10-03 20:58:35 -04:00
resetWorld();
SaveIO.load(saveDirectory.child("0.msav"));
2018-10-03 20:58:35 -04:00
2019-03-11 11:43:23 -04:00
assertEquals(world.width(), map.width);
assertEquals(world.height(), map.height);
2019-12-26 17:46:01 -05:00
assertTrue(state.teams.playerCores().size > 0);
2018-10-03 20:58:35 -04:00
}
2018-10-04 17:59:16 -04:00
2020-01-26 11:40:09 -05:00
@Test
void conveyorBench(){
int[] items = {0};
world.loadMap(testMap);
state.set(State.playing);
2020-01-26 16:12:11 -05:00
int length = 128;
2020-01-26 11:40:09 -05:00
world.tile(0, 0).setBlock(Blocks.itemSource);
world.tile(0, 0).configureAny(Items.copper.id);
Array<TileEntity> entities = Array.with(world.tile(0, 0).entity);
for(int i = 0; i < length; i++){
world.tile(i + 1, 0).setBlock(Blocks.conveyor);
world.tile(i + 1, 0).rotation(0);
entities.add(world.tile(i + 1, 0).entity);
}
world.tile(length + 1, 0).setBlock(new Block("___"){
@Override
public void handleItem(Item item, Tile tile, Tile source){
items[0] ++;
}
@Override
public boolean acceptItem(Item item, Tile tile, Tile source){
return true;
}
});
//warmup
for(int i = 0; i < 100000; i++){
entities.each(TileEntity::update);
}
Time.mark();
for(int i = 0; i < 200000; i++){
entities.each(TileEntity::update);
}
Log.info(Time.elapsed() + "ms to process " + items[0] + " items");
assertTrue(items[0] > 0);
}
2019-06-12 15:29:41 -04:00
@Test
void load77Save(){
2019-06-12 15:29:41 -04:00
resetWorld();
SaveIO.load(Core.files.internal("77.msav"));
2019-06-12 15:29:41 -04:00
2019-06-12 15:52:47 -04:00
//just tests if the map was loaded properly and didn't crash, no validity checks currently
assertEquals(276, world.width());
assertEquals(10, world.height());
2019-06-12 15:29:41 -04:00
}
@Test
void load85Save(){
resetWorld();
SaveIO.load(Core.files.internal("85.msav"));
assertEquals(250, world.width());
assertEquals(300, world.height());
}
2019-09-12 12:17:28 -04:00
@Test
void arrayIterators(){
Array<String> arr = Array.with("a", "b" , "c", "d", "e", "f");
Array<String> results = new Array<>();
for(String s : arr);
for(String s : results);
Array.iteratorsAllocated = 0;
//simulate non-enhanced for loops, which should be correct
for(int i = 0; i < arr.size; i++){
for(int j = 0; j < arr.size; j++){
results.add(arr.get(i) + arr.get(j));
}
}
int index = 0;
//test nested for loops
for(String s : arr){
for(String s2 : arr){
assertEquals(results.get(index++), s + s2);
}
}
assertEquals(results.size, index);
assertEquals(0, Array.iteratorsAllocated, "No new iterators must have been allocated.");
}
2018-10-04 17:59:16 -04:00
@Test
2018-11-23 12:40:36 -05:00
void inventoryDeposit(){
2019-01-12 16:55:24 -05:00
depositTest(Blocks.surgeSmelter, Items.copper);
2019-01-07 18:39:06 -05:00
depositTest(Blocks.vault, Items.copper);
depositTest(Blocks.thoriumReactor, Items.thorium);
2018-11-23 12:40:36 -05:00
}
@Test
void edges(){
2018-12-26 13:22:31 -05:00
Point2[] edges = Edges.getEdges(1);
assertEquals(edges[0], new Point2(1, 0));
assertEquals(edges[1], new Point2(0, 1));
assertEquals(edges[2], new Point2(-1, 0));
assertEquals(edges[3], new Point2(0, -1));
2018-10-04 17:59:16 -04:00
2018-12-26 13:22:31 -05:00
Point2[] edges2 = Edges.getEdges(2);
2018-10-07 23:22:54 -04:00
assertEquals(8, edges2.length);
2018-10-04 17:59:16 -04:00
}
2018-11-23 12:40:36 -05:00
@Test
void buildingOverlap(){
initBuilding();
2019-12-09 11:27:09 -05:00
BuilderDrone d1 = (BuilderDrone)UnitTypes.phantom.create(Team.sharded);
BuilderDrone d2 = (BuilderDrone)UnitTypes.phantom.create(Team.sharded);
d1.set(10f, 20f);
d2.set(10f, 20f);
d1.addBuildRequest(new BuildRequest(0, 0, 0, Blocks.copperWallLarge));
d2.addBuildRequest(new BuildRequest(1, 1, 0, Blocks.copperWallLarge));
Time.setDeltaProvider(() -> 9999999f);
2019-02-02 15:59:07 -05:00
d1.updateBuilding();
d2.updateBuilding();
2019-01-07 18:39:06 -05:00
assertEquals(Blocks.copperWallLarge, world.tile(0, 0).block());
assertEquals(Blocks.air, world.tile(2, 2).block());
2019-05-09 14:39:52 -04:00
assertTrue(world.tile(1, 1).block() instanceof BlockPart);
}
@Test
void buildingDestruction(){
initBuilding();
2019-12-09 11:27:09 -05:00
BuilderDrone d1 = (BuilderDrone)UnitTypes.phantom.create(Team.sharded);
BuilderDrone d2 = (BuilderDrone)UnitTypes.phantom.create(Team.sharded);
d1.set(10f, 20f);
d2.set(10f, 20f);
d1.addBuildRequest(new BuildRequest(0, 0, 0, Blocks.copperWallLarge));
d2.addBuildRequest(new BuildRequest(1, 1));
Time.setDeltaProvider(() -> 3f);
2019-02-02 15:59:07 -05:00
d1.updateBuilding();
Time.setDeltaProvider(() -> 1f);
2019-02-02 15:59:07 -05:00
d2.updateBuilding();
assertEquals(content.getByName(ContentType.block, "build2"), world.tile(0, 0).block());
Time.setDeltaProvider(() -> 9999f);
2019-02-02 15:59:07 -05:00
d1.updateBuilding();
d2.updateBuilding();
assertEquals(Blocks.air, world.tile(0, 0).block());
assertEquals(Blocks.air, world.tile(2, 2).block());
assertEquals(Blocks.air, world.tile(1, 1).block());
}
2019-04-01 11:57:43 -04:00
@Test
void allBlockTest(){
Tile[][] tiles = world.createTiles(256*2 + 20, 10);
2019-04-01 11:57:43 -04:00
world.beginMapLoad();
for(int x = 0; x < tiles.length; x++){
for(int y = 0; y < tiles[0].length; y++){
2019-05-09 14:39:52 -04:00
tiles[x][y] = new Tile(x, y, Blocks.stone.id, (byte)0, (byte)0);
2019-04-01 11:57:43 -04:00
}
}
int i = 0;
2019-04-08 09:03:18 -04:00
for(int x = 5; x < tiles.length && i < content.blocks().size; ){
2019-04-01 11:57:43 -04:00
Block block = content.block(i++);
2019-10-15 08:51:48 -04:00
if(block.isBuildable()){
x += block.size;
2019-04-01 11:57:43 -04:00
tiles[x][5].setBlock(block);
x += block.size;
}
}
world.endMapLoad();
for(int x = 0; x < tiles.length; x++){
for(int y = 0; y < tiles[0].length; y++){
Tile tile = world.tile(x, y);
if(tile.entity != null){
try{
tile.entity.update();
}catch(Throwable t){
fail("Failed to update block '" + tile.block() + "'.", t);
}
assertEquals(tile.block(), tile.entity.block);
assertEquals(tile.block().health, tile.entity.health);
}
}
}
}
void initBuilding(){
createMap();
Tile core = world.tile(5, 5);
2019-12-26 17:46:01 -05:00
core.set(Blocks.coreShard, Team.sharded);
for(Item item : content.items()){
core.entity.items.set(item, 3000);
}
2019-12-26 17:46:01 -05:00
assertEquals(core.entity, state.teams.get(Team.sharded).core());
}
2018-11-23 12:40:36 -05:00
void depositTest(Block block, Item item){
2019-08-02 10:10:37 -04:00
BaseUnit unit = UnitTypes.spirit.create(Team.derelict);
2019-05-09 14:39:52 -04:00
Tile tile = new Tile(0, 0, Blocks.air.id, (byte)0, block.id);
2018-11-23 12:40:36 -05:00
int capacity = tile.block().itemCapacity;
2019-05-09 14:39:52 -04:00
assertNotNull(tile.entity, "Tile should have an entity, but does not: " + tile);
2018-11-23 12:40:36 -05:00
int deposited = tile.block().acceptStack(item, capacity - 1, tile, unit);
assertEquals(capacity - 1, deposited);
tile.block().handleStack(item, capacity - 1, tile, unit);
assertEquals(tile.entity.items.get(item), capacity - 1);
int overflow = tile.block().acceptStack(item, 10, tile, unit);
assertEquals(1, overflow);
tile.block().handleStack(item, 1, tile, unit);
assertEquals(capacity, tile.entity.items.get(item));
}
2018-10-03 20:58:35 -04:00
}