[1/2] Add user-defined clock to libstdc++ condition_variable tests
diff mbox series

Message ID 61b76668abe901d2849469e209956ed269cf3e25.1563209229.git-series.mac@mcrowe.com
State New
Headers show
Series
  • PR libstdc++/41861 Add full steady_clock support to condition_variable
Related show

Commit Message

Mike Crowe July 15, 2019, 4:47 p.m. UTC
libstdc++-v3/

	* testsuite/30_threads/condition_variable/members/2.cc (test01):
	  Parameterise so that test can be run against an arbitrary clock.
	  (main): Test using std::chrono::steady_clock and a user-defined
	  clock in addition to the previous std::chrono::system_clock. *
	  testsuite/30_threads/condition_variable_any/members/2.cc:
	  Likewise.
---
 libstdc++-v3/ChangeLog                                                |  9 +++++++++
 libstdc++-v3/testsuite/30_threads/condition_variable/members/2.cc     | 29 ++++++++++++++++++++++++++---
 libstdc++-v3/testsuite/30_threads/condition_variable_any/members/2.cc | 29 ++++++++++++++++++++++++++---
 3 files changed, 61 insertions(+), 6 deletions(-)

Patch
diff mbox series

diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index 8e96a89..a52a704 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,3 +1,12 @@ 
+2019-07-15  Mike Crowe  <mac@mcrowe.com>
+
+	* testsuite/30_threads/condition_variable/members/2.cc (test01):
+	Parameterise so that test can be run against an arbitrary clock.
+	(main): Test using std::chrono::steady_clock and a user-defined
+	clock in addition to the previous std::chrono::system_clock.
+	* testsuite/30_threads/condition_variable_any/members/2.cc:
+	Likewise.
+
 2019-07-12  Jonathan Wakely  <jwakely@redhat.com>
 
 	* testsuite/29_atomics/atomic_float/1.cc: Fix comment.
diff --git a/libstdc++-v3/testsuite/30_threads/condition_variable/members/2.cc b/libstdc++-v3/testsuite/30_threads/condition_variable/members/2.cc
index 0ecb09d..02a0d84 100644
--- a/libstdc++-v3/testsuite/30_threads/condition_variable/members/2.cc
+++ b/libstdc++-v3/testsuite/30_threads/condition_variable/members/2.cc
@@ -26,6 +26,7 @@ 
 #include <system_error>
 #include <testsuite_hooks.h>
 
+template <typename ClockType>
 void test01()
 {
   try 
@@ -35,10 +36,10 @@  void test01()
       std::mutex m;
       std::unique_lock<std::mutex> l(m);
 
-      auto then = std::chrono::steady_clock::now();
+      auto then = ClockType::now();
       std::cv_status result = c1.wait_until(l, then + ms);
       VERIFY( result == std::cv_status::timeout );
-      VERIFY( (std::chrono::steady_clock::now() - then) >= ms );
+      VERIFY( (ClockType::now() - then) >= ms );
       VERIFY( l.owns_lock() );
     }
   catch (const std::system_error& e)
@@ -102,9 +103,31 @@  void test01_alternate_clock()
     }
 }
 
+/* User defined clock that ticks in two-thousandths of a second
+   forty-two minutes ahead of steady_clock. */
+struct user_defined_clock
+{
+  typedef std::chrono::steady_clock::rep rep;
+  typedef std::ratio<1, 2000> period;
+  typedef std::chrono::duration<rep, period> duration;
+  typedef std::chrono::time_point<user_defined_clock> time_point;
+
+  static constexpr bool is_steady = true;
+
+  static time_point now() noexcept
+  {
+    using namespace std::chrono;
+    const auto steady_since_epoch = steady_clock::now().time_since_epoch();
+    const auto user_since_epoch = duration_cast<duration>(steady_since_epoch);
+    return time_point(user_since_epoch + minutes(42));
+  }
+};
+
 int main()
 {
-  test01();
+  test01<std::chrono::steady_clock>();
+  test01<std::chrono::system_clock>();
+  test01<user_defined_clock>();
   test01_alternate_clock();
   return 0;
 }
diff --git a/libstdc++-v3/testsuite/30_threads/condition_variable_any/members/2.cc b/libstdc++-v3/testsuite/30_threads/condition_variable_any/members/2.cc
index e6fbc44..840c283 100644
--- a/libstdc++-v3/testsuite/30_threads/condition_variable_any/members/2.cc
+++ b/libstdc++-v3/testsuite/30_threads/condition_variable_any/members/2.cc
@@ -51,6 +51,7 @@  struct Mutex
 };
 
 
+template <typename ClockType>
 void test01()
 {
   try 
@@ -60,10 +61,10 @@  void test01()
       Mutex m;
       m.lock();
 
-      auto then = std::chrono::steady_clock::now();
+      auto then = ClockType::now();
       std::cv_status result = c1.wait_until(m, then + ms);
       VERIFY( result == std::cv_status::timeout );
-      VERIFY( (std::chrono::steady_clock::now() - then) >= ms );
+      VERIFY( (ClockType::now() - then) >= ms );
       VERIFY( m.locked );
     }
   catch (const std::system_error& e)
@@ -76,8 +77,30 @@  void test01()
     }
 }
 
+/* User defined clock that ticks in two-thousandths of a second
+   forty-two minutes ahead of steady_clock. */
+struct user_defined_clock
+{
+  typedef std::chrono::steady_clock::rep rep;
+  typedef std::ratio<1, 2000> period;
+  typedef std::chrono::duration<rep, period> duration;
+  typedef std::chrono::time_point<user_defined_clock> time_point;
+
+  static constexpr bool is_steady = true;
+
+  static time_point now() noexcept
+  {
+    using namespace std::chrono;
+    const auto steady_since_epoch = steady_clock::now().time_since_epoch();
+    const auto user_since_epoch = duration_cast<duration>(steady_since_epoch);
+    return time_point(user_since_epoch + minutes(42));
+  }
+};
+
 int main()
 {
-  test01();
+  test01<std::chrono::steady_clock>();
+  test01<std::chrono::system_clock>();
+  test01<user_defined_clock>();
   return 0;
 }