From 4ea42f6d015cbb4079954e90ad4e63cc8e5b21c6 Mon Sep 17 00:00:00 2001 From: Marco Satti Date: Thu, 19 Mar 2026 11:07:30 +0000 Subject: [PATCH 1/3] Return number of events processed when run --- include/fschuetz04/simcpp20/simulation.hpp | 14 +++++++++++--- tests/tests.cpp | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/include/fschuetz04/simcpp20/simulation.hpp b/include/fschuetz04/simcpp20/simulation.hpp index ee98947..04385cb 100644 --- a/include/fschuetz04/simcpp20/simulation.hpp +++ b/include/fschuetz04/simcpp20/simulation.hpp @@ -127,11 +127,19 @@ template 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; } /** diff --git a/tests/tests.cpp b/tests/tests.cpp index 68e33c7..4e27db0 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -167,3 +167,17 @@ 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); +} \ No newline at end of file From ad4244f13266224875f33461c2dcecfc16b771c9 Mon Sep 17 00:00:00 2001 From: Marco Satti Date: Thu, 19 Mar 2026 11:33:30 +0000 Subject: [PATCH 2/3] Return number of events processed for run_until --- include/fschuetz04/simcpp20/simulation.hpp | 7 +++++-- tests/tests.cpp | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/include/fschuetz04/simcpp20/simulation.hpp b/include/fschuetz04/simcpp20/simulation.hpp index 04385cb..359f625 100644 --- a/include/fschuetz04/simcpp20/simulation.hpp +++ b/include/fschuetz04/simcpp20/simulation.hpp @@ -148,15 +148,18 @@ template 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. diff --git a/tests/tests.cpp b/tests/tests.cpp index 4e27db0..4997c6f 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -180,4 +180,19 @@ TEST_CASE("run returns number of events processed") { auto count = sim.run(); REQUIRE(count == 6); -} \ No newline at end of file +} + +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); + REQUIRE(count == 4); +} From 0d8b45d1f09821245c4cf188fc8d606719011f87 Mon Sep 17 00:00:00 2001 From: Marco Satti Date: Thu, 19 Mar 2026 11:34:54 +0000 Subject: [PATCH 3/3] Remove typo --- tests/tests.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/tests.cpp b/tests/tests.cpp index 4997c6f..cccbf1a 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -194,5 +194,4 @@ TEST_CASE("run_until returns number of events processed") { auto count = sim.run_until(2.5); REQUIRE(count == 4); - REQUIRE(count == 4); }