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: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ An `Environment` owns its `Grid`. The grid is allocated in the `Environment` con

`Environment`'s copy constructor and copy assignment operator are deleted, since a copy would share the original's `Grid*` and both would delete it on destruction. Copying or assigning an `Environment` is therefore a compile error; pass it by reference or by pointer instead.

A `Grid` stores its locations by value in a `std::vector<Location>`. `Grid::getLocation`, `getLocationByCoordinates`, `getFirstLocation`, and `getRandomLocation` return a `Location&` into that vector, so any such reference may be invalidated by a later call to `Grid::addLocation` or `Grid::removeLocation`. `addLocation` also stores a copy of its argument; the grid subsequently operates on the copy, not on the caller's original object.
A `Grid` stores its locations by value in a `std::vector<Location>`. `Grid::getLocation`, `getLocationByCoordinates`, `getFirstLocation`, and `getRandomLocation` return a `Location&` into that vector, so any such reference may be invalidated by a later call to `Grid::addLocation` or `Grid::removeLocation`. `addLocation` also stores a copy of its argument; the grid subsequently operates on the copy, not on the caller's original object. If the argument already holds entities, the copy holds the same `Entity*` pointers, so the grid counts those entities and reports them as present, but `addLocation` sets none of their ids: their grid id stays as it was. From then on the two vectors are independent, so removing an entity from the original leaves it in the grid's copy.

`addLocation` does not check for an existing location with the same id or coordinates. When several match, `Grid::getLocation`, `Grid::getLocationByCoordinates`, and `addEntityToLocation` all use the first match in `getLocations()` order, while `Grid::removeLocation` removes every location with the given id.

## Grid Layout and Movement

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

void testLocationLookupsUseFirstMatchButRemovalTakesEvery() {
std::cout << "Test 88 - Lookups and placement use the first matching location, removal takes every match";
Grid grid(0, 1);
Location sameId("0-0-0", 3, 3);
Location sameCoordinates("extra-location", 0, 0);
grid.addLocation(sameId);
grid.addLocation(sameCoordinates);
assert(grid.getLocations().size() == 3);

// the generated location comes first, so each lookup returns it rather than a later match
assert(grid.getLocation("0-0-0").getX() == 0);
assert(grid.getLocationByCoordinates(0, 0).getId() == "0-0-0");

// placement by id goes into the first match only
Entity entity(0, "Daniel");
Location elsewhere("0-0-0", 9, 9);
assert(grid.addEntityToLocation(entity, elsewhere) == true);
assert(grid.getLocations()[0].getNumEntities() == 1);
assert(grid.getLocations()[1].getNumEntities() == 0);

// removal takes out every location with the id, leaving only the one with other coordinates
grid.removeLocation(sameId);
assert(grid.getLocations().size() == 1);
assert(grid.getLocations()[0].getId() == "extra-location");
assert(entity.getLocationId() == "N/S");
assert(entity.getGridId() == -1);
std::cout << " --- " << "Success" << std::endl;
}

void testAddingOccupiedLocationToGridCopiesItsEntityPointers() {
std::cout << "Test 89 - Adding an occupied location to a grid copies its entity pointers without setting grid ids";
Grid grid(7, 1);
Location original("extra-location", 5, 5);
Entity entity(0, "Daniel");
original.addEntity(&entity);
grid.addLocation(original);

// the copy holds the same pointer, so the grid sees and counts the entity
assert(grid.getNumEntities() == 1);
assert(grid.isEntityPresent(entity) == true);
assert(grid.getLocation("extra-location").getEntities()[0] == &entity);
// but addLocation sets no ids: the grid id stays unset, the location id is the one Location::addEntity set
assert(entity.getGridId() == -1);
assert(entity.getLocationId() == "extra-location");

// the two vectors are independent afterwards: emptying the original leaves the copy's pointer
original.removeEntity(&entity);
assert(original.getNumEntities() == 0);
assert(grid.isEntityPresent(entity) == true);
assert(entity.getLocationId() == "N/S");
std::cout << " --- " << "Success" << std::endl;
}

void seedRandomNumberGenerator() {
srand (time (NULL));
}
Expand Down Expand Up @@ -1623,5 +1676,7 @@ int main() {
testRetrievingRandomLocationReturnsAGridElement();
testPrintingEnvironmentInfoPrintsNameAndPointerCount();
testGridPresenceMatchesSameIdTwin();
testLocationLookupsUseFirstMatchButRemovalTakesEvery();
testAddingOccupiedLocationToGridCopiesItsEntityPointers();
return 0;
}
Loading