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 src/dsf/dsf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

static constexpr uint8_t DSF_VERSION_MAJOR = 4;
static constexpr uint8_t DSF_VERSION_MINOR = 6;
static constexpr uint8_t DSF_VERSION_PATCH = 0;
static constexpr uint8_t DSF_VERSION_PATCH = 1;

static auto const DSF_VERSION =
std::format("{}.{}.{}", DSF_VERSION_MAJOR, DSF_VERSION_MINOR, DSF_VERSION_PATCH);
Expand Down
11 changes: 9 additions & 2 deletions src/dsf/mobility/Street.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace dsf::mobility {
assert(index < m_exitQueues.size());
m_exitQueues[index] = std::move(queue);
}
void Street::enableCounter(std::string name) {
void Street::enableCounter(std::string name, CounterPosition position) {
if (m_counter.has_value()) {
throw std::runtime_error(
std::format("{} already has a counter named {}", *this, m_counter->name()));
Expand All @@ -89,6 +89,7 @@ namespace dsf::mobility {
}
m_counter.emplace();
m_counter->setName(name);
m_counterPosition = position;
}
void Street::resetCounter() {
if (!hasCoil()) {
Expand All @@ -102,21 +103,27 @@ namespace dsf::mobility {
assert(!isFull());
spdlog::debug("Adding {} on {}", *pAgent, *this);
m_movingAgents.push(std::move(pAgent));
if (m_counter.has_value() && m_counterPosition == CounterPosition::ENTRY) {
++(*m_counter);
}
}
void Street::enqueue(std::size_t const& queueId) {
assert(!m_movingAgents.empty());
m_movingAgents.top()->incrementDistance(m_length);
m_exitQueues[queueId].push(
std::move(const_cast<std::unique_ptr<Agent>&>(m_movingAgents.top())));
m_movingAgents.pop();
if (m_counter.has_value()) {
if (m_counter.has_value() && m_counterPosition == CounterPosition::MIDDLE) {
++(*m_counter);
}
}
std::unique_ptr<Agent> Street::dequeue(std::size_t const& index) {
assert(!m_exitQueues[index].empty());
auto pAgent{std::move(m_exitQueues[index].front())};
m_exitQueues[index].pop();
if (m_counter.has_value() && m_counterPosition == CounterPosition::EXIT) {
++(*m_counter);
}
return pAgent;
}

Expand Down
8 changes: 7 additions & 1 deletion src/dsf/mobility/Street.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ namespace dsf::mobility {

class Agent;

/// @brief The position of the counter on the street
enum class CounterPosition : uint8_t { ENTRY = 0, MIDDLE = 1, EXIT = 2 };

/// @brief The Street class represents a street in the network.
class Street : public Road {
private:
Expand All @@ -49,6 +52,7 @@ namespace dsf::mobility {
m_movingAgents;
std::vector<Direction> m_laneMapping;
std::optional<Counter> m_counter;
CounterPosition m_counterPosition{CounterPosition::EXIT};
double m_stationaryWeight{1.0};

public:
Expand Down Expand Up @@ -94,7 +98,9 @@ namespace dsf::mobility {
}
/// @brief Enable a coil (dsf::Counter sensor) on the street
/// @param name The name of the counter (default is "Coil_<street_id>")
void enableCounter(std::string name = std::string());
/// @param position The position of the counter on the street (default is EXIT)
void enableCounter(std::string name = std::string(),
CounterPosition position = CounterPosition::EXIT);
/// @brief Reset the counter of the street
/// @throw std::runtime_error If the street does not have a coil
void resetCounter();
Expand Down
33 changes: 31 additions & 2 deletions test/mobility/Test_street.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
Agent a3{0, 1, 0};
a3.setFreeTime(7);

Street street{1, std::make_pair(0, 1), 3.5};

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note test

MISRA 12.3 rule
street.addAgent(std::make_unique<Agent>(a1));
CHECK_EQ(street.movingAgents().top()->freeTime(), 5);
street.addAgent(std::make_unique<Agent>(a2));
Expand Down Expand Up @@ -182,10 +182,24 @@
}

TEST_CASE("Street with a coil") {
SUBCASE("Counts") {
SUBCASE("Entry Counter") {
GIVEN("A street with an entry counter") {
Street street{1, std::make_pair(0, 1), 3.5};

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule Note test

MISRA 12.3 rule
street.enableCounter("EntryCoil", dsf::mobility::CounterPosition::ENTRY);
CHECK_EQ(street.counterName(), "EntryCoil");
WHEN("An agent is added") {
street.addAgent(std::make_unique<Agent>(0, 1));
THEN("The input flow is one immediately") { CHECK_EQ(street.counts(), 1); }
street.enqueue(0);
THEN("The input flow is still one") { CHECK_EQ(street.counts(), 1); }
}
}
}

SUBCASE("Middle Counter") {
GIVEN("A street with a counter") {
Street street{1, std::make_pair(0, 1), 3.5};
street.enableCounter();
street.enableCounter("", dsf::mobility::CounterPosition::MIDDLE);
CHECK_EQ(street.counterName(), "Coil_1");
WHEN("An agent is added") {
street.addAgent(std::make_unique<Agent>(0, 1));
Expand All @@ -197,6 +211,21 @@
}
}
}

SUBCASE("Exit Counter") {
GIVEN("A street with an exit counter") {
Street street{1, std::make_pair(0, 1), 3.5};
street.enableCounter("ExitCoil", dsf::mobility::CounterPosition::EXIT);
CHECK_EQ(street.counterName(), "ExitCoil");
WHEN("An agent is added and enqueued") {
street.addAgent(std::make_unique<Agent>(0, 1));
street.enqueue(0);
THEN("The input flow is zero") { CHECK_EQ(street.counts(), 0); }
street.dequeue(0);
THEN("The input flow is one after dequeue") { CHECK_EQ(street.counts(), 1); }
}
}
}
}

TEST_CASE("Road") {
Expand Down
Loading