Added some unit testing
This commit is contained in:
parent
11ae6c425c
commit
cfd397e10a
@ -135,4 +135,7 @@ public class ECS {
|
|||||||
void setSystemSignature(Type system, BitSet signature){
|
void setSystemSignature(Type system, BitSet signature){
|
||||||
systemManager.setSignature(system, signature);
|
systemManager.setSignature(system, signature);
|
||||||
}
|
}
|
||||||
|
Integer getMaxEntities(){
|
||||||
|
return entityManager.currentSize;
|
||||||
|
}
|
||||||
}
|
}
|
@ -81,7 +81,7 @@ class EntityManager{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean resize(int newSize, SystemManager systemManager, ComponentManager componentManager){
|
public boolean resize(int newSize, SystemManager systemManager, ComponentManager componentManager){
|
||||||
if (newSize >= currentSize - unusedEntities.size()){
|
if (newSize < currentSize - unusedEntities.size()){
|
||||||
System.err.println("Attempted to resize the maximum entity count to a number smaller than the current assigned entity count.");
|
System.err.println("Attempted to resize the maximum entity count to a number smaller than the current assigned entity count.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -113,7 +113,7 @@ class EntityManager{
|
|||||||
}
|
}
|
||||||
for (int i = newSize; i < currentSize; i++) {
|
for (int i = newSize; i < currentSize; i++) {
|
||||||
// Remove out-of-bounds data
|
// Remove out-of-bounds data
|
||||||
entityRegistrations.remove(i);
|
entityRegistrations.remove(newSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
@ -3,16 +3,155 @@ package nz.ac.massey.programming_project_159333_s1_2021;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
|
||||||
|
import java.util.BitSet;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit test for simple App.
|
* Unit test for simple App.
|
||||||
*/
|
*/
|
||||||
class AppTest {
|
class AppTest {
|
||||||
|
|
||||||
|
ECS gameEngine;
|
||||||
|
TestSystem system;
|
||||||
|
|
||||||
|
class TestObject{
|
||||||
|
int i = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestSystem extends ECSSystem{
|
||||||
|
public void init(){}
|
||||||
|
public void update() {
|
||||||
|
for (Integer entity : entities) {
|
||||||
|
TestObject entityComponent = (TestObject)gameEngine.getComponentData(entity, TestObject.class);
|
||||||
|
entityComponent.i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Rigorous Test.
|
* Test assigning an entity
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void testApp() {
|
void testAssignOne() {
|
||||||
assertEquals(1, 1);
|
int entity = gameEngine.createEntity();
|
||||||
|
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
||||||
|
assertEquals(1, ((TestObject)gameEngine.getComponentData(entity, TestObject.class)).i);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test assigning the maximum number of entities
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testAssignMax(){
|
||||||
|
for (int i = 0; i < gameEngine.getMaxEntities(); i++) {
|
||||||
|
int entity = gameEngine.createEntity();
|
||||||
|
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
||||||
|
assertEquals(1, ((TestObject)gameEngine.getComponentData(entity, TestObject.class)).i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Test assigning more than the maximum number of entities
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testAssignMoreThanMax(){
|
||||||
|
for (int i = 0; i < gameEngine.getMaxEntities() + 5; i++) {
|
||||||
|
int entity = gameEngine.createEntity();
|
||||||
|
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test removing data
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testRemoveOne(){
|
||||||
|
int entity = gameEngine.createEntity();
|
||||||
|
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
||||||
|
gameEngine.destroyEntity(entity);
|
||||||
|
assertNull(((TestObject)gameEngine.getComponentData(entity, TestObject.class)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test running the system several times
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testRunSystem(){
|
||||||
|
int entity = gameEngine.createEntity();
|
||||||
|
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
||||||
|
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
system.update();
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(6, ((TestObject)gameEngine.getComponentData(entity, TestObject.class)).i);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test resizing (minimal, with only 1 entity)
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testResizeMinimal(){
|
||||||
|
int entity = gameEngine.createEntity();
|
||||||
|
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
||||||
|
|
||||||
|
gameEngine.resizeMaximum(10);
|
||||||
|
|
||||||
|
assertEquals(10, gameEngine.getMaxEntities());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test large creations & deletions
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testLargeCreateDelete(){
|
||||||
|
for (int i = 0; i < gameEngine.getMaxEntities(); i++) {
|
||||||
|
int entity = gameEngine.createEntity();
|
||||||
|
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
||||||
|
assertEquals(1, ((TestObject)gameEngine.getComponentData(entity, TestObject.class)).i);
|
||||||
|
}
|
||||||
|
for (int i = 256; i < 512; i++) {
|
||||||
|
gameEngine.destroyEntity(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test large creations & deletions & smaller resize
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testLargeCreateDeleteResize(){
|
||||||
|
for (int i = 0; i < gameEngine.getMaxEntities(); i++) {
|
||||||
|
int entity = gameEngine.createEntity();
|
||||||
|
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
||||||
|
assertEquals(1, ((TestObject)gameEngine.getComponentData(entity, TestObject.class)).i);
|
||||||
|
}
|
||||||
|
for (int i = 256; i < 512; i++) {
|
||||||
|
gameEngine.destroyEntity(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
gameEngine.resizeMaximum(1024 - 255);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Establish a simple ECS, with a single system and component
|
||||||
|
*/
|
||||||
|
@BeforeEach
|
||||||
|
void testSetup(){
|
||||||
|
gameEngine = new ECS();
|
||||||
|
gameEngine.registerComponent(TestObject.class);
|
||||||
|
|
||||||
|
system = new TestSystem();
|
||||||
|
gameEngine.registerSystem(TestSystem.class, system);
|
||||||
|
{
|
||||||
|
BitSet signature = new BitSet();
|
||||||
|
signature.set(gameEngine.getComponentIndex(TestObject.class));
|
||||||
|
gameEngine.setSystemSignature(TestSystem.class, signature);
|
||||||
|
}
|
||||||
|
|
||||||
|
system.init();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user