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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ Adding is likewise unchecked. `addEntity` and `addEntityToLocation` do not test

Removal takes out every pointer with a matching id. An entity added to two different locations is removed from both, and one added twice to the same location is removed entirely, so a move leaves a single pointer at the destination. Since matching is by id, a distinct same-id twin stored in that location is taken out too, and that twin keeps its ids.

`Grid::removeLocation` also detaches any entities in the removed location: it resets their location id to `"N/S"` and their grid id to `-1`. It leaves their environment id unchanged, since the grid has no knowledge of its environment.
`Grid::removeLocation` also detaches any entities in the removed location: it resets their location id to `"N/S"` and their grid id to `-1`. It leaves their environment id unchanged, since the grid has no knowledge of its environment. An entity that is also stored in another location of the grid stays placed there: it keeps its grid id, and if its location id named the removed location, it now names a location that still holds it. This check compares addresses, so a same-id twin elsewhere does not count.

## Errors

Lookups that find nothing throw `std::runtime_error`:

- `Environment::getFirstEntity`, when the environment contains no entities.
- `Environment::getEntity`, when no entity has the given id. The message lists the ids that were found.
- `Grid::getLocation` and `Grid::getLocationByCoordinates`, when no location matches.
- `Grid::getLocation` and `Grid::getLocationByCoordinates`, when no location matches. The message names the id or coordinates requested and the grid's id.

`Environment::moveEntityToNewLocation` looks up both the entity and the destination before moving anything, so an unknown entity id or location id throws and leaves the entity in place. The directional move methods catch the off-grid case and return `false`, but still throw if `entityId` does not name an entity in the environment.

Expand Down
32 changes: 24 additions & 8 deletions src/grid.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "header/grid.h"

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <stdexcept>
Expand Down Expand Up @@ -29,7 +30,7 @@ namespace envlibcpp {
return location;
}
}
throw std::runtime_error("Location not found");
throw std::runtime_error("Location at (" + std::to_string(x) + ", " + std::to_string(y) + ") not found in grid '" + std::to_string(id) + "'");
}

int Grid::getSize() {
Expand Down Expand Up @@ -62,19 +63,34 @@ namespace envlibcpp {
std::string targetId = location.getId();
for (auto i = locations.begin(); i != locations.end();) {
if (i->getId() == targetId) {
// an entity in a removed location is no longer part of the grid, so its
// ids are cleared the same way removeEntity() clears them
for (Entity* entity : i->getEntities()) {
entity->resetLocationId();
entity->setGridId(-1);
}
std::vector<Entity*> detached = i->getEntities();
i = locations.erase(i);
for (Entity* entity : detached) {
updateIdsOfDetachedEntity(entity, targetId);
}
} else {
i++;
}
}
}

void Grid::updateIdsOfDetachedEntity(Entity* entity, std::string removedLocationId) {
// compared by address, since a same-id twin elsewhere does not keep this entity placed
for (Location& location : locations) {
std::vector<Entity*>& entities = location.getEntities();
if (std::find(entities.begin(), entities.end(), entity) != entities.end()) {
if (entity->getLocationId() == removedLocationId) {
entity->setLocationId(location.getId());
}
return;
}
}
// an entity left in no location is no longer part of the grid, so its ids are
// cleared the same way removeEntity() clears them
entity->resetLocationId();
entity->setGridId(-1);
}

void Grid::addEntity(Entity& entity) {
entity.setGridId(id);
Location& firstLocation = getRandomLocation();
Expand Down Expand Up @@ -122,7 +138,7 @@ namespace envlibcpp {
return location;
}
}
throw std::runtime_error("Location not found");
throw std::runtime_error("Location with id '" + locationId + "' not found in grid '" + std::to_string(id) + "'");
}

Location& Grid::getRandomLocation() {
Expand Down
1 change: 1 addition & 0 deletions src/header/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace envlibcpp {
std::vector<Location> locations;

void generateLocations();
void updateIdsOfDetachedEntity(Entity* entity, std::string removedLocationId);
};
}

Expand Down
96 changes: 96 additions & 0 deletions src/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,97 @@ void testRemovingEntityAlsoRemovesSameIdTwinInItsLocation() {
std::cout << " --- " << "Success" << std::endl;
}

void testRemovingLocationKeepsIdsOfEntityStillPlacedElsewhere() {
std::cout << "Test 72 - Removing a location keeps the ids of an entity still placed in another location";
Environment environment(5, "Earth", 2);
Entity entity(0, "Daniel");
Grid* grid = environment.getGrid();
environment.addEntityToLocation(entity, grid->getLocationByCoordinates(0, 0));
environment.addEntityToLocation(entity, grid->getLocationByCoordinates(1, 1));
grid->removeLocation(grid->getLocationByCoordinates(0, 0));
assert(environment.isEntityPresent(entity) == true);
assert(environment.getNumEntities() == 1);
assert(entity.getEnvironmentId() == 5);
assert(entity.getGridId() == 5);
assert(entity.getLocationId() == "5-1-1");
assert(environment.moveEntityUp(0) == true);
assert(entity.getLocationId() == "5-1-0");
std::cout << " --- " << "Success" << std::endl;
}

void testRemovingLocationNamedByEntityPointsItAtARemainingLocation() {
std::cout << "Test 73 - Removing the location an entity names points it at a location still holding it";
Environment environment(5, "Earth", 2);
Entity entity(0, "Daniel");
Grid* grid = environment.getGrid();
environment.addEntityToLocation(entity, grid->getLocationByCoordinates(1, 1));
environment.addEntityToLocation(entity, grid->getLocationByCoordinates(0, 0));
assert(entity.getLocationId() == "5-0-0");
grid->removeLocation(grid->getLocationByCoordinates(0, 0));
assert(environment.getNumEntities() == 1);
assert(entity.getGridId() == 5);
assert(entity.getLocationId() == "5-1-1");
assert(grid->getLocation(entity.getLocationId()).isEntityPresent(&entity) == true);
std::cout << " --- " << "Success" << std::endl;
}

void testRemovingLocationDetachesEntityDespiteSameIdTwinElsewhere() {
std::cout << "Test 74 - Removing a location detaches an entity even if a same-id twin is placed elsewhere";
Environment environment(5, "Earth", 2);
Entity original(7, "Daniel");
Entity twin(7, "Bob");
Grid* grid = environment.getGrid();
environment.addEntityToLocation(original, grid->getLocationByCoordinates(0, 0));
environment.addEntityToLocation(twin, grid->getLocationByCoordinates(1, 1));
grid->removeLocation(grid->getLocationByCoordinates(0, 0));
assert(original.getGridId() == -1);
assert(original.getLocationId() == "N/S");
assert(twin.getGridId() == 5);
assert(twin.getLocationId() == "5-1-1");
assert(environment.getNumEntities() == 1);
std::cout << " --- " << "Success" << std::endl;
}

void testRemovingLocationsSharingAnIdDetachesEntityInBoth() {
std::cout << "Test 75 - Removing locations that share an id detaches an entity placed in both";
Grid grid(0, 1);
Location extra("extra-location", 5, 5);
grid.addLocation(extra);
grid.addLocation(extra);
Entity entity(0, "Daniel");
grid.getLocations()[1].addEntity(&entity);
grid.getLocations()[2].addEntity(&entity);
entity.setGridId(0);
grid.removeLocation(extra);
assert(grid.getLocations().size() == 1);
assert(grid.getNumEntities() == 0);
assert(entity.getGridId() == -1);
assert(entity.getLocationId() == "N/S");
std::cout << " --- " << "Success" << std::endl;
}

void testLocationLookupErrorsNameWhatWasLookedUp() {
std::cout << "Test 76 - Location lookup errors name what was looked up and the grid";
Grid grid(3, 2);
std::string messageById = "";
try {
grid.getLocation("no-such-location");
} catch (const std::runtime_error& e) {
messageById = e.what();
}
std::string messageByCoordinates = "";
try {
grid.getLocationByCoordinates(2, 0);
} catch (const std::runtime_error& e) {
messageByCoordinates = e.what();
}
assert(messageById.find("'no-such-location'") != std::string::npos);
assert(messageById.find("'3'") != std::string::npos);
assert(messageByCoordinates.find("(2, 0)") != std::string::npos);
assert(messageByCoordinates.find("'3'") != std::string::npos);
std::cout << " --- " << "Success" << std::endl;
}

void seedRandomNumberGenerator() {
srand (time (NULL));
}
Expand Down Expand Up @@ -1276,5 +1367,10 @@ int main() {
testRemovingEntityFromLocationRemovesEveryPointerAndKeepsOrder();
testMovingEntityAddedTwiceToOneLocationLeavesASinglePointer();
testRemovingEntityAlsoRemovesSameIdTwinInItsLocation();
testRemovingLocationKeepsIdsOfEntityStillPlacedElsewhere();
testRemovingLocationNamedByEntityPointsItAtARemainingLocation();
testRemovingLocationDetachesEntityDespiteSameIdTwinElsewhere();
testRemovingLocationsSharingAnIdDetachesEntityInBoth();
testLocationLookupErrorsNameWhatWasLookedUp();
return 0;
}
Loading