#include "test.h" #include namespace { // Shown time never decreases over [from, to] uptime, sampled every ms. bool monotonic(const ClockSlew& c, int64_t from, int64_t to) { int64_t last = c.nowMs(from); for (int64_t up = from + 1; up <= to; ++up) { int64_t now = c.nowMs(up); if (now < last) return false; last = now; } return true; } } TEST(clockSlewForwardSteps, "clock-slew-forward-steps", "A set at or after the current second steps the clock") { ClockSlew c; CHECK(!c.set(2000000, 5000)); CHECK(c.nowMs(5000) == 2000000000LL); CHECK(!c.set(2000100, 6000)); CHECK(c.nowMs(6000) == 2000100000LL); CHECK(c.remainingMs(6000) == 0); } TEST(clockSlewSameSecond, "clock-slew-same-second", "A set to the current second steps, without a slew") { ClockSlew c; c.step(2000000700LL, 0); // shows 2000000.700 s CHECK(!c.set(2000000, 0)); CHECK(c.nowMs(0) == 2000000000LL); CHECK(c.lead_ms == 0); } TEST(clockSlewBackward, "clock-slew-backward", "An earlier time is slewed at 1/12, never stepped back") { ClockSlew c; c.step(2000000000LL, 0); const int64_t up = 100000; // shows 2000100.000 s CHECK(c.set(2000040, up)); // 60 s lead CHECK(c.nowMs(up) == 2000100000LL); // nothing moved back CHECK(c.remainingMs(up) == 60000); CHECK(c.nowMs(up + 12000) - c.nowMs(up) == 11000); // runs at 11/12 CHECK(monotonic(c, up, up + 720000 + 5000)); CHECK(c.remainingMs(up + 720000) == 0); // 60 s lead gone after 12 min CHECK(c.nowMs(up + 720000) == 2000040000LL + 720000); CHECK(c.nowMs(up + 730000) - c.nowMs(up + 720000) == 10000); // full speed again } TEST(clockSlewSettle, "clock-slew-settle", "A finished slew folds into the offset once, without a jump") { ClockSlew c; c.step(2000000000LL, 0); CHECK(c.set(1999990, 0)); // 10 s lead, done after 120 s CHECK(!c.settle(119999)); int64_t before = c.nowMs(120000); CHECK(c.settle(120000)); CHECK(c.nowMs(120000) == before); CHECK(!c.settle(130000)); CHECK(c.nowMs(130000) == before + 10000); } TEST(clockSlewRetarget, "clock-slew-retarget", "A new set during a slew starts from the time shown, never back") { ClockSlew c; c.step(2000000000LL, 0); CHECK(c.set(1999700, 0)); // 300 s lead const int64_t up = 60000; // 5 s removed so far int64_t shown = c.nowMs(up); CHECK(shown == 2000055000LL); CHECK(c.set(2000050, up)); // retarget: 5 s lead from the time shown CHECK(c.nowMs(up) == shown); CHECK(c.remainingMs(up) == 5000); CHECK(monotonic(c, up, up + 70000)); CHECK(c.remainingMs(up + 60000) == 0); } TEST(clockSlewForwardCancels, "clock-slew-forward-cancels", "A forward set during a slew ends it with a step") { ClockSlew c; c.step(2000000000LL, 0); CHECK(c.set(1999000, 0)); CHECK(!c.set(2000010, 1000)); CHECK(c.remainingMs(1000) == 0); CHECK(c.nowMs(1000) == 2000010000LL); CHECK(c.nowMs(2000) == 2000011000LL); } TEST(clockSlewSecondsNeverRepeatBackward, "clock-slew-seconds-monotonic", "Whole seconds shown never decrease during a slew") { ClockSlew c; c.step(2000000999LL, 0); CHECK(c.set(1999999, 0)); int64_t last = c.nowMs(0) / 1000; for (int64_t up = 1; up <= 30000; ++up) { int64_t s = c.nowMs(up) / 1000; CHECK(s >= last); last = s; } }