diff --git a/README.md b/README.md index 761b80a..d827029 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ A directional move starts from the location named by the entity's location id (l Entities are matched by `Entity::getId()`, not by address. `Location::isEntityPresent`, `Location::removeEntity`, and `Environment::getEntity` all compare ids, so two distinct `Entity` objects that share an id are treated as the same entity. Removing through such a twin takes the stored entity out of its location but resets only the twin's ids, so the stored entity keeps ids describing a placement it no longer has. Entity ids should therefore be unique within an environment. -A newly constructed entity has an environment id and grid id of `-1` and a location id of `"N/S"`. Placing it through an `Environment` sets all three; `Environment::removeEntity` resets them to those same values. +A newly constructed entity has an environment id and grid id of `-1` and a location id of `"N/S"`. Placing it through an `Environment` sets all three; `Environment::removeEntity` resets them to those same values. The lower layers set only the ids they know: `Grid::addEntity` and `Grid::addEntityToLocation` set the grid id and location id but not the environment id, and `Grid::removeEntity` resets the grid id and location id but leaves the environment id unchanged. `Location::addEntity` and `Location::removeEntity` set and reset only the location id. `Environment::removeEntity` and `Grid::removeEntity` do nothing when the entity is not present. Calling either on an entity that is not in that environment or grid — including one placed in a different environment — leaves its environment id, grid id, location id, and actual placement unchanged. `Location::removeEntity` likewise leaves the location id of an entity that is not in that location unchanged. diff --git a/src/tests.cpp b/src/tests.cpp index 6dd2fba..7eebf97 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -1436,6 +1436,59 @@ void testMovingEntityToItsCurrentLocationLeavesItThere() { std::cout << " --- " << "Success" << std::endl; } +void testPlacingThroughGridOrLocationSetsOnlyThatLayersIds() { + std::cout << "Test 84 - Placing and removing through a grid or a location sets only the ids that layer knows"; + Grid grid(3, 2); + Entity placedInGrid(0, "Daniel"); + grid.addEntity(placedInGrid); + assert(placedInGrid.getGridId() == 3); + assert(placedInGrid.getEnvironmentId() == -1); + assert(grid.getLocation(placedInGrid.getLocationId()).isEntityPresent(&placedInGrid) == true); + assert(grid.getNumEntities() == 1); + + // the grid has no knowledge of an environment, so removal leaves the environment id alone + placedInGrid.setEnvironmentId(9); + grid.removeEntity(placedInGrid); + assert(placedInGrid.getGridId() == -1); + assert(placedInGrid.getLocationId() == "N/S"); + assert(placedInGrid.getEnvironmentId() == 9); + + Entity placedAtLocation(2, "Phil"); + assert(grid.addEntityToLocation(placedAtLocation, grid.getLocationByCoordinates(1, 1)) == true); + assert(placedAtLocation.getGridId() == 3); + assert(placedAtLocation.getLocationId() == "3-1-1"); + assert(placedAtLocation.getEnvironmentId() == -1); + + Location location("standalone", 0, 0); + Entity placedInLocation(1, "Bob"); + placedInLocation.setGridId(4); + placedInLocation.setEnvironmentId(5); + location.addEntity(&placedInLocation); + assert(placedInLocation.getLocationId() == "standalone"); + assert(placedInLocation.getGridId() == 4); + assert(placedInLocation.getEnvironmentId() == 5); + location.removeEntity(&placedInLocation); + assert(placedInLocation.getLocationId() == "N/S"); + assert(placedInLocation.getGridId() == 4); + assert(placedInLocation.getEnvironmentId() == 5); + std::cout << " --- " << "Success" << std::endl; +} + +void testRetrievingRandomLocationReturnsAGridElement() { + std::cout << "Test 85 - Retrieving a random location returns a reference into the grid's own locations"; + Grid grid(0, 3); + std::vector& locations = grid.getLocations(); + for (size_t i = 0; i < 100; i++) { + Location* random = &grid.getRandomLocation(); + assert(random >= &locations.front() && random <= &locations.back()); + } + Entity entity(0, "Daniel"); + grid.getRandomLocation().addEntity(&entity); + assert(grid.isEntityPresent(entity) == true); + assert(grid.getNumEntities() == 1); + std::cout << " --- " << "Success" << std::endl; +} + void seedRandomNumberGenerator() { srand (time (NULL)); } @@ -1528,5 +1581,7 @@ int main() { testMovingEntityWhoseLocationIdNamesNoGridLocationThrows(); testEnvironmentOfSizeZeroHasNoLocationsAndLookupsThrow(); testMovingEntityToItsCurrentLocationLeavesItThere(); + testPlacingThroughGridOrLocationSetsOnlyThatLayersIds(); + testRetrievingRandomLocationReturnsAGridElement(); return 0; } \ No newline at end of file