From 6e9ab669b584496dd628700d0d69bd20c5c12ca7 Mon Sep 17 00:00:00 2001 From: Mike Crowe Date: Sun, 21 Sep 2025 17:15:52 +0100 Subject: [PATCH] libstdc++: Add negative this_thread::sleep tests [PR116586] Add tests to ensure that std::this_thread::sleep_for() and std::this_thread::sleep_until() cope with being passed negative times correctly. These tests prove that the functions don't suffer from libstdc++/PR116586, and will stay that way. libstdc++-v3/ChangeLog: PR libstdc++/116586 * testsuite/30_threads/this_thread/sleep_for.cc: Add test_negative() test. * testsuite/30_threads/this_thread/sleep_until.cc: Make existing test use both system_clock and steady_clock. Add test_negative() test. Signed-off-by: Mike Crowe --- .../30_threads/this_thread/sleep_for.cc | 13 ++++++++++ .../30_threads/this_thread/sleep_until.cc | 26 ++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/libstdc++-v3/testsuite/30_threads/this_thread/sleep_for.cc b/libstdc++-v3/testsuite/30_threads/this_thread/sleep_for.cc index 3f55ccc12aa5..5b0518d6bb9f 100644 --- a/libstdc++-v3/testsuite/30_threads/this_thread/sleep_for.cc +++ b/libstdc++-v3/testsuite/30_threads/this_thread/sleep_for.cc @@ -37,7 +37,20 @@ test01() VERIFY( (chr::system_clock::now() - begin) >= ms ); } +void +test_negative() +{ + chr::system_clock::time_point begin = chr::system_clock::now(); + + std::this_thread::sleep_for(-chr::hours(8)); + + // That should have completed immediately, but be generous because we don't + // want spurious failures on busy machines. + VERIFY( (chr::system_clock::now() - begin) < chr::seconds(10) ); +} + int main() { test01(); + test_negative(); } diff --git a/libstdc++-v3/testsuite/30_threads/this_thread/sleep_until.cc b/libstdc++-v3/testsuite/30_threads/this_thread/sleep_until.cc index 1fb82b671772..8c70c2ee78fd 100644 --- a/libstdc++-v3/testsuite/30_threads/this_thread/sleep_until.cc +++ b/libstdc++-v3/testsuite/30_threads/this_thread/sleep_until.cc @@ -26,18 +26,36 @@ namespace chr = std::chrono; +template void test01() { - chr::system_clock::time_point begin = chr::system_clock::now(); + typename Clock::time_point begin = Clock::now(); chr::microseconds ms(500); - std::this_thread::sleep_until(chr::system_clock::now() + ms); + std::this_thread::sleep_until(Clock::now() + ms); - VERIFY( (chr::system_clock::now() - begin) >= ms ); + VERIFY( (Clock::now() - begin) >= ms ); +} + +template +void +test_negative() +{ + typename Clock::time_point begin = Clock::now(); + + typename Clock::time_point tp(-chr::hours(8)); + std::this_thread::sleep_until(tp); + + // That should have completed immediately, but be generous because we don't + // want spurious failures on busy machines. + VERIFY( (Clock::now() - begin) < chr::seconds(10) ); } int main() { - test01(); + test01(); + test01(); + test_negative(); + test_negative(); } -- 2.47.3