From a723e09014246eb15a8e2828b9bf69745d4889bb Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Fri, 6 Nov 2026 04:08:30 -0700 Subject: [PATCH 1/3] Add tests for removing locations and location lookup errors Co-Authored-By: Claude Opus 5.5 --- src/tests.cpp | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/src/tests.cpp b/src/tests.cpp index be13f1c..8d3d053 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -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)); } @@ -1276,5 +1367,10 @@ int main() { testRemovingEntityFromLocationRemovesEveryPointerAndKeepsOrder(); testMovingEntityAddedTwiceToOneLocationLeavesASinglePointer(); testRemovingEntityAlsoRemovesSameIdTwinInItsLocation(); + testRemovingLocationKeepsIdsOfEntityStillPlacedElsewhere(); + testRemovingLocationNamedByEntityPointsItAtARemainingLocation(); + testRemovingLocationDetachesEntityDespiteSameIdTwinElsewhere(); + testRemovingLocationsSharingAnIdDetachesEntityInBoth(); + testLocationLookupErrorsNameWhatWasLookedUp(); return 0; } \ No newline at end of file From 6ec604fe4e01b6729c29a25cbda4501655bdf6a2 Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Fri, 6 Nov 2026 04:10:23 -0700 Subject: [PATCH 2/3] Keep ids of entities still placed elsewhere when a location is removed Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- src/grid.cpp | 28 ++++++++++++++++++++++------ src/header/grid.h | 1 + 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 8b9d8fe..4a59a4c 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ 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 diff --git a/src/grid.cpp b/src/grid.cpp index f0f30fb..22f104a 100644 --- a/src/grid.cpp +++ b/src/grid.cpp @@ -1,5 +1,6 @@ #include "header/grid.h" +#include #include #include #include @@ -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 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& 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(); diff --git a/src/header/grid.h b/src/header/grid.h index a9c7eb5..6cf729d 100644 --- a/src/header/grid.h +++ b/src/header/grid.h @@ -36,6 +36,7 @@ namespace envlibcpp { std::vector locations; void generateLocations(); + void updateIdsOfDetachedEntity(Entity* entity, std::string removedLocationId); }; } From e14728c322be4fba325a2e1806dd35cedd609f30 Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Fri, 6 Nov 2026 04:12:16 -0700 Subject: [PATCH 3/3] Name the lookup and the grid in location lookup errors Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- src/grid.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4a59a4c..c8cfb6f 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ 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. diff --git a/src/grid.cpp b/src/grid.cpp index 22f104a..b72f9e6 100644 --- a/src/grid.cpp +++ b/src/grid.cpp @@ -30,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() { @@ -138,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() {