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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Lookups that find nothing throw `std::runtime_error`:

`addEntityToLocation` does not throw when no grid location has the given location's id. The entity is then placed nowhere, and its grid id, environment id, and location id are left unchanged. `Grid::addEntityToLocation` returns whether it placed the entity; `Environment::addEntityToLocation` returns nothing.

`Grid::getFirstLocation` and `Grid::getRandomLocation` do not check for an empty grid. Calling either on a grid with no locations — including indirectly through `Environment::addEntity` on an environment of size `0` — is undefined behaviour.
`Grid::getFirstLocation` and `Grid::getRandomLocation` do not check for an empty grid. Calling either on a grid with no locations — including indirectly through `Environment::addEntity` on an environment of size `0` — is undefined behaviour. The other lookups are safe on an empty grid: `Grid::getLocation`, `Grid::getLocationByCoordinates`, `Environment::getEntity`, and `Environment::getFirstEntity` throw as described above, and `addEntityToLocation` places nothing.

## Building

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

void testEnvironmentOfSizeZeroHasNoLocationsAndLookupsThrow() {
std::cout << "Test 82 - An environment of size zero has no locations, its lookups throw, and it places nothing";
Environment environment(0, "Void", 0);
assert(environment.getGrid()->getLocations().size() == 0);
assert(environment.getGrid()->getSize() == 0);
assert(environment.getNumEntities() == 0);

bool threwById = false;
try {
environment.getGrid()->getLocation("0-0-0");
} catch (const std::runtime_error& e) {
threwById = true;
}
bool threwByCoordinates = false;
try {
environment.getGrid()->getLocationByCoordinates(0, 0);
} catch (const std::runtime_error& e) {
threwByCoordinates = true;
}
bool threwFirstEntity = false;
try {
environment.getFirstEntity();
} catch (const std::runtime_error& e) {
threwFirstEntity = true;
}
bool threwEntityById = false;
try {
environment.getEntity(0);
} catch (const std::runtime_error& e) {
threwEntityById = true;
}
assert(threwById == true);
assert(threwByCoordinates == true);
assert(threwFirstEntity == true);
assert(threwEntityById == true);

// a separately constructed location with a plausible id still names nothing in the grid
Entity entity(0, "Daniel");
Location elsewhere("0-0-0", 0, 0);
environment.addEntityToLocation(entity, elsewhere);
assert(environment.getNumEntities() == 0);
assert(environment.isEntityPresent(entity) == false);
assert(entity.getEnvironmentId() == -1);
assert(entity.getGridId() == -1);
assert(elsewhere.getNumEntities() == 0);

std::ostringstream buffer;
std::streambuf* previousBuffer = std::cout.rdbuf(buffer.rdbuf());
environment.printConsoleRepresentation();
std::cout.rdbuf(previousBuffer);
assert(buffer.str() == "\n");
std::cout << " --- " << "Success" << std::endl;
}

void testMovingEntityToItsCurrentLocationLeavesItThere() {
std::cout << "Test 83 - Moving an entity to the location it is already in leaves a single pointer there";
Environment environment(0, "Earth", 3);
Entity entity(0, "Daniel");
environment.addEntityToLocation(entity, environment.getGrid()->getLocationByCoordinates(1, 1));
environment.moveEntityToNewLocation(0, "0-1-1");
Location& location = environment.getGrid()->getLocation("0-1-1");
assert(location.getNumEntities() == 1);
assert(location.getEntities()[0] == &entity);
assert(entity.getLocationId() == "0-1-1");
assert(entity.getEnvironmentId() == 0);
assert(entity.getGridId() == 0);
assert(environment.getNumEntities() == 1);
std::cout << " --- " << "Success" << std::endl;
}

void seedRandomNumberGenerator() {
srand (time (NULL));
}
Expand Down Expand Up @@ -1456,5 +1526,7 @@ int main() {
testAddingEntityToLocationInGridReportsWhetherItWasPlaced();
testMovingEntityInDirectionStartsFromItsLocationId();
testMovingEntityWhoseLocationIdNamesNoGridLocationThrows();
testEnvironmentOfSizeZeroHasNoLocationsAndLookupsThrow();
testMovingEntityToItsCurrentLocationLeavesItThere();
return 0;
}
Loading