|
| 1 | +#include <catch2/catch.hpp> |
| 2 | + |
| 3 | +#include "libslic3r/Debounce.hpp" |
| 4 | + |
| 5 | +#include <thread> |
| 6 | + |
| 7 | +using namespace Slic3r; |
| 8 | +using namespace std::chrono; |
| 9 | + |
| 10 | +// --------------------------------------------------------------------------- |
| 11 | +// debounce_elapsed() — unit tests |
| 12 | +// --------------------------------------------------------------------------- |
| 13 | + |
| 14 | +SCENARIO("debounce_elapsed: first call always fires", "[Debounce]") { |
| 15 | + GIVEN("A default-initialised time_point (epoch)") { |
| 16 | + steady_clock::time_point last{}; |
| 17 | + WHEN("debounce_elapsed is called with a 5-second interval") { |
| 18 | + const bool fired = debounce_elapsed(last, seconds(5)); |
| 19 | + THEN("It returns true") { |
| 20 | + REQUIRE(fired); |
| 21 | + } |
| 22 | + THEN("last_tp is updated to a non-epoch value") { |
| 23 | + REQUIRE(last != steady_clock::time_point{}); |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +SCENARIO("debounce_elapsed: second immediate call is suppressed", "[Debounce]") { |
| 30 | + GIVEN("A time_point primed by a first accepted call") { |
| 31 | + steady_clock::time_point last{}; |
| 32 | + debounce_elapsed(last, seconds(5)); // prime |
| 33 | + WHEN("debounce_elapsed is called again immediately") { |
| 34 | + const bool fired = debounce_elapsed(last, seconds(5)); |
| 35 | + THEN("It returns false") { |
| 36 | + REQUIRE_FALSE(fired); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +SCENARIO("debounce_elapsed: call fires again after interval expires", "[Debounce]") { |
| 43 | + GIVEN("A time_point set to 10 seconds in the past") { |
| 44 | + // Simulate 'last' being 10 seconds old without sleeping. |
| 45 | + steady_clock::time_point last = steady_clock::now() - seconds(10); |
| 46 | + WHEN("debounce_elapsed is called with a 5-second interval") { |
| 47 | + const bool fired = debounce_elapsed(last, seconds(5)); |
| 48 | + THEN("It returns true") { |
| 49 | + REQUIRE(fired); |
| 50 | + } |
| 51 | + THEN("last_tp is updated") { |
| 52 | + REQUIRE(last >= steady_clock::now() - milliseconds(100)); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +SCENARIO("debounce_elapsed: zero interval fires every time", "[Debounce]") { |
| 59 | + GIVEN("A time_point primed by a first call") { |
| 60 | + steady_clock::time_point last{}; |
| 61 | + debounce_elapsed(last, seconds(0)); |
| 62 | + WHEN("debounce_elapsed is called immediately again with zero interval") { |
| 63 | + const bool fired = debounce_elapsed(last, seconds(0)); |
| 64 | + THEN("It returns true") { |
| 65 | + REQUIRE(fired); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +SCENARIO("debounce_elapsed: last_tp is not modified on suppressed calls", "[Debounce]") { |
| 72 | + GIVEN("A time_point primed by a first accepted call") { |
| 73 | + steady_clock::time_point last{}; |
| 74 | + debounce_elapsed(last, seconds(5)); |
| 75 | + const auto snapshot = last; |
| 76 | + WHEN("A suppressed call is made") { |
| 77 | + debounce_elapsed(last, seconds(5)); |
| 78 | + THEN("last_tp is unchanged") { |
| 79 | + REQUIRE(last == snapshot); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments