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
21 changes: 16 additions & 5 deletions include/fschuetz04/simcpp20/simulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,19 @@ template <typename Time = double> class simulation {
sev.ev_.process();
}

/// Run the simulation until no more events are scheduled.
void run() {
while (!empty()) {
/**
* Run the simulation until no more events are scheduled.
*
* @return Number of events processed.
*/
std::size_t run() {
std::size_t count = 0;

for (; !empty(); count++) {
step();
}

return count;
}

/**
Expand All @@ -140,15 +148,18 @@ template <typename Time = double> class simulation {
* scheduled at or after the target time.
*
* @param target Target time.
* @return Number of events processed.
*/
void run_until(Time target) {
std::size_t run_until(Time target) {
assert(target >= now());
std::size_t count = 0;

while (!empty() && scheduled_evs_.top().time_ < target) {
for (; !empty() && scheduled_evs_.top().time_ < target; count++) {
step();
}

now_ = target;
return count;
}

/// @return Whether no events are scheduled.
Expand Down
28 changes: 28 additions & 0 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,31 @@ TEST_CASE("all_of") {
REQUIRE(finished);
}
}

TEST_CASE("run returns number of events processed") {
simcpp20::simulation<> sim;

auto ev1 = sim.timeout(1);
auto ev2 = sim.timeout(2);
auto ev3 = sim.timeout(3);
auto ev4 = sim.timeout(3);
auto ev5 = sim.timeout(2);
auto ev6 = sim.timeout(1);

auto count = sim.run();
REQUIRE(count == 6);
}

TEST_CASE("run_until returns number of events processed") {
simcpp20::simulation<> sim;

auto ev1 = sim.timeout(1); // Should be run
auto ev2 = sim.timeout(2); // Should be run
auto ev3 = sim.timeout(3); // Should not be run
auto ev4 = sim.timeout(3); // Should not be run
auto ev5 = sim.timeout(2); // Should be run
auto ev6 = sim.timeout(1); // Should be run

auto count = sim.run_until(2.5);
REQUIRE(count == 4);
}
Loading