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
8#define BOOST_THREAD_VERSION 2
9#define BOOST_TEST_MODULE Boost.Threads: xtime test suite
10
11#include <boost/thread/detail/config.hpp>
12
13#include <boost/thread/xtime.hpp>
14
15#include <boost/test/unit_test.hpp>
16#include <boost/thread/mutex.hpp>
17#include <boost/thread/condition.hpp>
18
19BOOST_AUTO_TEST_CASE(test_xtime_cmp)
20{
21 boost::xtime xt1, xt2, cur;
22 BOOST_CHECK_EQUAL(
23 boost::xtime_get(&cur, boost::TIME_UTC_),
24 static_cast<int>(boost::TIME_UTC_));
25
26 xt1 = xt2 = cur;
27 xt1.nsec -= 1;
28 xt2.nsec += 1;
29
30 BOOST_CHECK(boost::xtime_cmp(xt1, cur) < 0);
31 BOOST_CHECK(boost::xtime_cmp(xt2, cur) > 0);
32 BOOST_CHECK(boost::xtime_cmp(cur, cur) == 0);
33
34 xt1 = xt2 = cur;
35 xt1.sec -= 1;
36 xt2.sec += 1;
37
38 BOOST_CHECK(boost::xtime_cmp(xt1, cur) < 0);
39 BOOST_CHECK(boost::xtime_cmp(xt2, cur) > 0);
40 BOOST_CHECK(boost::xtime_cmp(cur, cur) == 0);
41}
42
43BOOST_AUTO_TEST_CASE(test_xtime_get)
44{
45 boost::xtime orig, cur, old;
46 BOOST_CHECK_EQUAL(
47 boost::xtime_get(&orig,
48 boost::TIME_UTC_), static_cast<int>(boost::TIME_UTC_));
49 old = orig;
50
51 for (int x=0; x < 100; ++x)
52 {
53 BOOST_CHECK_EQUAL(
54 boost::xtime_get(&cur, boost::TIME_UTC_),
55 static_cast<int>(boost::TIME_UTC_));
56 BOOST_CHECK(boost::xtime_cmp(cur, orig) >= 0);
57 BOOST_CHECK(boost::xtime_cmp(cur, old) >= 0);
58 old = cur;
59 }
60}
61
62BOOST_AUTO_TEST_CASE(test_xtime_mutex_backwards_compatibility)
63{
64 boost::timed_mutex m;
65 BOOST_CHECK(m.timed_lock(boost::get_xtime(boost::get_system_time()+boost::posix_time::milliseconds(10))));
66 m.unlock();
67 boost::timed_mutex::scoped_timed_lock lk(m,boost::get_xtime(abs_time: boost::get_system_time()+boost::posix_time::milliseconds(10)));
68 BOOST_CHECK(lk.owns_lock());
69 if(lk.owns_lock())
70 {
71 lk.unlock();
72 }
73 BOOST_CHECK(lk.timed_lock(boost::get_xtime(boost::get_system_time()+boost::posix_time::milliseconds(10))));
74 if(lk.owns_lock())
75 {
76 lk.unlock();
77 }
78}
79
80bool predicate()
81{
82 return false;
83}
84
85
86BOOST_AUTO_TEST_CASE(test_xtime_condvar_backwards_compatibility)
87{
88 boost::condition_variable cond;
89 boost::condition_variable_any cond_any;
90 boost::mutex m;
91
92 boost::unique_lock<boost::mutex> lk(m);
93 cond.timed_wait(m&: lk,abs_time: boost::get_xtime(abs_time: boost::get_system_time()+boost::posix_time::milliseconds(10)));
94 cond.timed_wait(m&: lk,abs_time: boost::get_xtime(abs_time: boost::get_system_time()+boost::posix_time::milliseconds(10)),pred: predicate);
95 cond_any.timed_wait(m&: lk,abs_time: boost::get_xtime(abs_time: boost::get_system_time()+boost::posix_time::milliseconds(10)));
96 cond_any.timed_wait(m&: lk,abs_time: boost::get_xtime(abs_time: boost::get_system_time()+boost::posix_time::milliseconds(10)),pred: predicate);
97}
98

source code of boost/libs/thread/test/test_xtime.cpp