test_xtime.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright (C) 2001-2003
  2. // William E. Kempf
  3. // Copyright (C) 2008 Anthony Williams
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #define BOOST_THREAD_VERSION 2
  8. #define BOOST_TEST_MODULE Boost.Threads: xtime test suite
  9. #include <boost/thread/detail/config.hpp>
  10. #include <boost/thread/xtime.hpp>
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/thread/mutex.hpp>
  13. #include <boost/thread/condition.hpp>
  14. BOOST_AUTO_TEST_CASE(test_xtime_cmp)
  15. {
  16. boost::xtime xt1, xt2, cur;
  17. BOOST_CHECK_EQUAL(
  18. boost::xtime_get(&cur, boost::TIME_UTC_),
  19. static_cast<int>(boost::TIME_UTC_));
  20. xt1 = xt2 = cur;
  21. xt1.nsec -= 1;
  22. xt2.nsec += 1;
  23. BOOST_CHECK(boost::xtime_cmp(xt1, cur) < 0);
  24. BOOST_CHECK(boost::xtime_cmp(xt2, cur) > 0);
  25. BOOST_CHECK(boost::xtime_cmp(cur, cur) == 0);
  26. xt1 = xt2 = cur;
  27. xt1.sec -= 1;
  28. xt2.sec += 1;
  29. BOOST_CHECK(boost::xtime_cmp(xt1, cur) < 0);
  30. BOOST_CHECK(boost::xtime_cmp(xt2, cur) > 0);
  31. BOOST_CHECK(boost::xtime_cmp(cur, cur) == 0);
  32. }
  33. BOOST_AUTO_TEST_CASE(test_xtime_get)
  34. {
  35. boost::xtime orig, cur, old;
  36. BOOST_CHECK_EQUAL(
  37. boost::xtime_get(&orig,
  38. boost::TIME_UTC_), static_cast<int>(boost::TIME_UTC_));
  39. old = orig;
  40. for (int x=0; x < 100; ++x)
  41. {
  42. BOOST_CHECK_EQUAL(
  43. boost::xtime_get(&cur, boost::TIME_UTC_),
  44. static_cast<int>(boost::TIME_UTC_));
  45. BOOST_CHECK(boost::xtime_cmp(cur, orig) >= 0);
  46. BOOST_CHECK(boost::xtime_cmp(cur, old) >= 0);
  47. old = cur;
  48. }
  49. }
  50. BOOST_AUTO_TEST_CASE(test_xtime_mutex_backwards_compatibility)
  51. {
  52. boost::timed_mutex m;
  53. BOOST_CHECK(m.timed_lock(boost::get_xtime(boost::get_system_time()+boost::posix_time::milliseconds(10))));
  54. m.unlock();
  55. boost::timed_mutex::scoped_timed_lock lk(m,boost::get_xtime(boost::get_system_time()+boost::posix_time::milliseconds(10)));
  56. BOOST_CHECK(lk.owns_lock());
  57. if(lk.owns_lock())
  58. {
  59. lk.unlock();
  60. }
  61. BOOST_CHECK(lk.timed_lock(boost::get_xtime(boost::get_system_time()+boost::posix_time::milliseconds(10))));
  62. if(lk.owns_lock())
  63. {
  64. lk.unlock();
  65. }
  66. }
  67. bool predicate()
  68. {
  69. return false;
  70. }
  71. BOOST_AUTO_TEST_CASE(test_xtime_condvar_backwards_compatibility)
  72. {
  73. boost::condition_variable cond;
  74. boost::condition_variable_any cond_any;
  75. boost::mutex m;
  76. boost::unique_lock<boost::mutex> lk(m);
  77. cond.timed_wait(lk,boost::get_xtime(boost::get_system_time()+boost::posix_time::milliseconds(10)));
  78. cond.timed_wait(lk,boost::get_xtime(boost::get_system_time()+boost::posix_time::milliseconds(10)),predicate);
  79. cond_any.timed_wait(lk,boost::get_xtime(boost::get_system_time()+boost::posix_time::milliseconds(10)));
  80. cond_any.timed_wait(lk,boost::get_xtime(boost::get_system_time()+boost::posix_time::milliseconds(10)),predicate);
  81. }