Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ namespace envlibcpp {
location.removeEntity(entity);
}
}
entity.setGridId(-1);
}

bool Grid::isEntityPresent(Entity &entity) {
Expand Down
12 changes: 9 additions & 3 deletions src/location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ namespace envlibcpp {
}

void Location::removeEntity(Entity &entity) {
for (auto i = entities.begin(); i != entities.end(); i++) {
if (i->getId() == entity.getId()) {
entities.erase(i);
int index = -1;
for (int i = 0; i < getNumEntities(); i++) {
if (entities[i].getId() == entity.getId()) {
index = i;
break;
}
}
if (index != -1) {
entities.erase(entities.begin() + index);
}
entity.setLocationId(-1);
}

bool Location::isEntityPresent(Entity &entity) {
Expand Down
73 changes: 58 additions & 15 deletions src/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,37 @@ using namespace envlibcpp;

void testTemplate() {
std::cout << "---" << std::endl;
std::cout << "Test - Template" << std::endl;

std::cout << "Test 0 - Template" << std::endl;
assert(true);
std::cout << "Success" << std::endl;
}

void testPlacingEntityInLocation() {
std::cout << "---" << std::endl;
std::cout << "Test - Placing entity in location" << std::endl;
std::cout << "Test 1 - Placing entity in location" << std::endl;
Entity entity("Daniel");
Location location(0, 0);

location.addEntity(entity);

assert(location.isEntityPresent(entity) == true);
assert(entity.getLocationId() == location.getId());
std::cout << "Success" << std::endl;
}

void testRemovingEntityFromLocation() {
std::cout << "---" << std::endl;
std::cout << "Test 2 - Removing entity from location" << std::endl;
Entity entity("Daniel");
Location location(0, 0);
location.addEntity(entity);
location.removeEntity(entity);
assert(!location.isEntityPresent(entity));
assert(entity.getLocationId() == -1);
std::cout << "Success" << std::endl;
}

void testGeneratingGrid() {
std::cout << "---" << std::endl;
std::cout << "Test - Generating grid" << std::endl;
std::cout << "Test 3 - Generating grid" << std::endl;
int size = 4;
Environment environment("test environment", 4);
int numLocations = environment.getGrid().getLocations().size();
Expand All @@ -42,7 +52,7 @@ void testGeneratingGrid() {

void testRetrievingLocation() {
std::cout << "---" << std::endl;
std::cout << "Test - Retrieving location" << std::endl;
std::cout << "Test 4 - Retrieving location" << std::endl;
Grid grid(5);
Location firstLocation = grid.getFirstLocation();
assert(firstLocation.getX() == 0 && firstLocation.getY() == 0);
Expand All @@ -51,10 +61,11 @@ void testRetrievingLocation() {

void testRetrievingLocationAfterModification() {
std::cout << "---" << std::endl;
std::cout << "Test - Retrieving location after modification" << std::endl;
std::cout << "Test 5 - Retrieving location after modification" << std::endl;
Grid grid(5);
Location firstLocation = grid.getFirstLocation();
Entity entity = Entity("test");
firstLocation = grid.getFirstLocation();
firstLocation.addEntity(entity);
assert(firstLocation.getX() == 0 && firstLocation.getY() == 0);
assert(firstLocation.isEntityPresent(entity));
Expand All @@ -63,35 +74,67 @@ void testRetrievingLocationAfterModification() {

void testPlacingEntityInGrid() {
std::cout << "---" << std::endl;
std::cout << "Test - Placing entity in grid" << std::endl;
std::cout << "Test 6 - Placing entity in grid" << std::endl;
Entity entity("Daniel");
Grid grid(4);

grid.addEntity(entity);

assert(grid.isEntityPresent(entity));
assert(entity.getGridId() == grid.getId());
std::cout << "Success" << std::endl;
}

void testRemovingEntityFromGrid() {
std::cout << "---" << std::endl;
std::cout << "Test 7 - Removing entity from grid" << std::endl;
Entity entity("Daniel");
Grid grid(4);
grid.addEntity(entity);
grid.removeEntity(entity);
assert(!grid.isEntityPresent(entity));
assert(entity.getGridId() == -1);
std::cout << "Success" << std::endl;
}

void testPlacingEntityInEnvironment() {
std::cout << "---" << std::endl;
std::cout << "Test - Placing entity in environment" << std::endl;
std::cout << "Test 8 - Placing entity in environment" << std::endl;
Entity entity("Daniel");
Environment environment("Earth", 2);

environment.addEntity(entity);
assert(environment.isEntityPresent(entity));
assert(entity.getEnvironmentId() == environment.getId());
std::cout << "Success" << std::endl;
}

assert(environment.isEntityPresent(entity) == true);
void testRemovingEntityFromEnvironment() {
std::cout << "---" << std::endl;
std::cout << "Test 9 - Removing entity from environment" << std::endl;
Entity entity("Daniel");
Environment environment("Earth", 2);
environment.addEntity(entity);
environment.removeEntity(entity);
assert(!environment.isEntityPresent(entity));
assert(entity.getEnvironmentId() == -1);
std::cout << "Success" << std::endl;
}

int main() {
void seedRandomNumberGenerator() {
srand (time (NULL));
}

int main() {
// seed RNG
seedRandomNumberGenerator();

// tests
testPlacingEntityInLocation();
testRemovingEntityFromLocation();
testGeneratingGrid();
testRetrievingLocation();
testRetrievingLocationAfterModification();
testPlacingEntityInGrid();
testRemovingEntityFromGrid();
testPlacingEntityInEnvironment();
testRemovingEntityFromEnvironment();
return 0;
}
Binary file modified tests
Binary file not shown.