Skip to content

Commit 1cc1b0c

Browse files
committed
Some checks more
1 parent c8ab739 commit 1cc1b0c

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

src/dsf/mobility/Agent.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,18 @@ namespace dsf::mobility {
3232
void Agent::setSrcNodeId(Id srcNodeId) { m_srcNodeId = srcNodeId; }
3333
void Agent::setStreetId(std::optional<Id> streetId) {
3434
if (!streetId.has_value()) {
35-
assert(m_nextStreetId.has_value());
35+
if (!m_nextStreetId.has_value()) {
36+
throw std::logic_error(std::format(
37+
"Agent {} has no next street id to set the current street id to", m_id));
38+
}
3639
m_streetId = std::exchange(m_nextStreetId, std::nullopt);
3740
return;
3841
}
39-
assert(m_nextStreetId.has_value() ? streetId == m_nextStreetId.value() : true);
42+
if (m_nextStreetId.has_value()) {
43+
throw std::logic_error(std::format(
44+
"Agent {} has a next street id already set, cannot set street id directly",
45+
m_id));
46+
}
4047
m_streetId = streetId;
4148
m_nextStreetId = std::nullopt;
4249
}

src/dsf/mobility/Agent.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,6 @@ struct std::formatter<dsf::mobility::Agent> {
127127
constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
128128
template <typename FormatContext>
129129
auto format(const dsf::mobility::Agent& agent, FormatContext&& ctx) const {
130-
auto const strItinerary = agent.trip().empty() ? std::string("RANDOM")
131-
: std::to_string(agent.itineraryId());
132130
return std::format_to(
133131
ctx.out(),
134132
"Agent(id: {}, streetId: {}, srcNodeId: {}, nextStreetId: {}, "
@@ -139,7 +137,7 @@ struct std::formatter<dsf::mobility::Agent> {
139137
agent.srcNodeId().has_value() ? std::to_string(agent.srcNodeId().value()) : "N/A",
140138
agent.nextStreetId().has_value() ? std::to_string(agent.nextStreetId().value())
141139
: "N/A",
142-
strItinerary,
140+
agent.isRandom() ? std::string("RANDOM") : std::to_string(agent.itineraryId()),
143141
agent.speed(),
144142
agent.distance(),
145143
agent.spawnTime(),

test/mobility/Test_agent.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ TEST_CASE("Agent methods") {
6565
}
6666
SUBCASE("setStreetId with value") {
6767
agent.setNextStreetId(55);
68-
agent.setStreetId(55);
69-
CHECK(agent.streetId().has_value());
68+
CHECK_THROWS_AS(agent.setStreetId(55), std::logic_error);
69+
CHECK_FALSE(agent.streetId().has_value());
70+
agent.setStreetId();
7071
CHECK_EQ(agent.streetId().value(), 55);
7172
CHECK_FALSE(agent.nextStreetId().has_value());
7273
}
7374
SUBCASE("setStreetId with nullopt uses nextStreetId") {
7475
agent.setNextStreetId(77);
75-
agent.setStreetId(std::nullopt);
76+
agent.setStreetId();
7677
CHECK(agent.streetId().has_value());
7778
CHECK_EQ(agent.streetId().value(), 77);
7879
}
@@ -104,7 +105,6 @@ TEST_CASE("Agent methods") {
104105
agent.setSpeed(10.);
105106
agent.incrementDistance(5.);
106107
agent.setFreeTime(99);
107-
agent.setNextStreetId(88);
108108
agent.setStreetId(88);
109109
agent.updateItinerary();
110110
agent.reset(555);

0 commit comments

Comments
 (0)