1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // Adaptation to Boost of the libcxx
10 // Copyright 2010 Vicente J. Botet Escriba
11 // Copyright (c) Microsoft Corporation 2014
12 // Distributed under the Boost Software License, Version 1.0.
13 // See http://www.boost.org/LICENSE_1_0.txt
14
15 #include <boost/type_traits/is_same.hpp>
16 #include <boost/chrono/chrono.hpp>
17 #include <boost/chrono/process_cpu_clocks.hpp>
18 #include <boost/chrono/thread_clock.hpp>
19 #include <boost/system/system_error.hpp>
20 #include <boost/detail/lightweight_test.hpp>
21
22 #if !defined(BOOST_NO_CXX11_STATIC_ASSERT)
23 #define NOTHING ""
24 #endif
25
26 template <typename Clock>
check_clock_invariants()27 void check_clock_invariants()
28 {
29 BOOST_CHRONO_STATIC_ASSERT((boost::is_same<typename Clock::rep, typename Clock::duration::rep>::value), NOTHING, ());
30 BOOST_CHRONO_STATIC_ASSERT((boost::is_same<typename Clock::period, typename Clock::duration::period>::value), NOTHING, ());
31 BOOST_CHRONO_STATIC_ASSERT((boost::is_same<typename Clock::duration, typename Clock::time_point::duration>::value), NOTHING, ());
32 BOOST_CHRONO_STATIC_ASSERT(Clock::is_steady || !Clock::is_steady, NOTHING, ());
33 // to be replaced by has static member bool is_steady
34 }
35
36 template <typename Clock>
check_clock_now()37 void check_clock_now()
38 {
39 typename Clock::time_point t1 = Clock::now();
40 (void)t1;
41 }
42
43 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
44
45 template <typename Clock>
check_clock_now_ec()46 void check_clock_now_ec()
47 {
48 boost::system::error_code ec;
49 typename Clock::time_point t1 = Clock::now(ec);
50 (void)t1;
51 BOOST_TEST(ec.value()==0);
52 }
53
54 template <typename Clock>
check_clock_now_throws()55 void check_clock_now_throws()
56 {
57 typename Clock::time_point t1 = Clock::now(boost::throws());
58 (void)t1;
59 }
60
61 #ifndef BOOST_NO_EXCEPTIONS
62
63 template <typename Clock>
check_clock_now_err(int err)64 void check_clock_now_err(int err)
65 {
66 Clock::set_errno(err);
67 try {
68 typename Clock::time_point t1 = Clock::now();
69 } catch (boost::system::system_error& ex) {
70 BOOST_TEST(ex.code().value()==err);
71 // BOOST_TEST(ex.code().category() == ::boost::system::system_category());
72 // BOOST_TEST(std::string(ex.what()) == std::string("errored_clock"));
73 }
74 Clock::set_errno(0);
75 }
76 #endif
77
78 template <typename Clock>
check_clock_now_ec_err(int err)79 void check_clock_now_ec_err(int err)
80 {
81 Clock::set_errno(err);
82 boost::system::error_code ec;
83 typename Clock::time_point t1 = Clock::now(ec);
84 BOOST_TEST(ec.value()==err);
85 // BOOST_TEST(ec.category() == ::boost::system::system_category());
86 Clock::set_errno(0);
87 }
88
89 #ifndef BOOST_NO_EXCEPTIONS
90 template <typename Clock>
check_clock_now_throws_err(int err)91 void check_clock_now_throws_err(int err)
92 {
93 Clock::set_errno(err);
94 try {
95 typename Clock::time_point t1 = Clock::now(boost::throws());
96 BOOST_TEST(0&&"exception not thown");
97 } catch (boost::system::system_error& ex) {
98 BOOST_TEST(ex.code().value()==err);
99 // BOOST_TEST(ex.code().category() == ::boost::system::system_category());
100 // BOOST_TEST(std::string(ex.what()) == std::string("errored_clock"));
101 }
102 Clock::set_errno(0);
103 }
104 #endif
105 #endif
106
main()107 int main()
108 {
109 check_clock_invariants<boost::chrono::high_resolution_clock>();
110 check_clock_now<boost::chrono::high_resolution_clock>();
111
112 #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
113 check_clock_invariants<boost::chrono::steady_clock>();
114 BOOST_CHRONO_STATIC_ASSERT(boost::chrono::steady_clock::is_steady, NOTHING, ());
115 check_clock_now<boost::chrono::steady_clock>();
116 #endif
117
118 check_clock_invariants<boost::chrono::system_clock>();
119 BOOST_CHRONO_STATIC_ASSERT(!boost::chrono::system_clock::is_steady, NOTHING, ());
120 check_clock_now<boost::chrono::system_clock>();
121 {
122 typedef boost::chrono::system_clock C;
123 C::time_point t1 = C::from_time_t(C::to_time_t(C::now()));
124 (void)t1;
125 }
126 {
127 typedef boost::chrono::system_clock C;
128 std::time_t t1 = C::to_time_t(C::now());
129 (void)t1;
130
131 }
132 {
133 BOOST_TEST((boost::chrono::system_clock::duration::min)() <
134 boost::chrono::system_clock::duration::zero());
135
136 }
137
138 #if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
139 check_clock_invariants<boost::chrono::thread_clock>();
140 BOOST_CHRONO_STATIC_ASSERT(boost::chrono::thread_clock::is_steady, NOTHING, ());
141 check_clock_now<boost::chrono::thread_clock>();
142 #endif
143
144 #if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS)
145 check_clock_invariants<boost::chrono::process_real_cpu_clock>();
146 BOOST_CHRONO_STATIC_ASSERT(boost::chrono::process_real_cpu_clock::is_steady, NOTHING, ());
147 check_clock_now<boost::chrono::process_real_cpu_clock>();
148
149 #if ! BOOST_OS_WINDOWS || BOOST_PLAT_WINDOWS_DESKTOP
150 check_clock_invariants<boost::chrono::process_user_cpu_clock>();
151 BOOST_CHRONO_STATIC_ASSERT(boost::chrono::process_user_cpu_clock::is_steady, NOTHING, ());
152 check_clock_now<boost::chrono::process_user_cpu_clock>();
153
154 check_clock_invariants<boost::chrono::process_system_cpu_clock>();
155 BOOST_CHRONO_STATIC_ASSERT(boost::chrono::process_system_cpu_clock::is_steady, NOTHING, ());
156 check_clock_now<boost::chrono::process_system_cpu_clock>();
157
158 check_clock_invariants<boost::chrono::process_cpu_clock>();
159 BOOST_CHRONO_STATIC_ASSERT(boost::chrono::process_cpu_clock::is_steady, NOTHING, ());
160 check_clock_now<boost::chrono::process_cpu_clock>();
161 #endif
162 #endif
163
164
165 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
166 check_clock_now_ec<boost::chrono::high_resolution_clock>();
167 check_clock_now_throws<boost::chrono::high_resolution_clock>();
168
169 #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
170 check_clock_now_ec<boost::chrono::steady_clock>();
171 check_clock_now_throws<boost::chrono::steady_clock>();
172 #endif
173
174 check_clock_now_ec<boost::chrono::system_clock>();
175 check_clock_now_throws<boost::chrono::system_clock>();
176
177 #if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
178 check_clock_now_ec<boost::chrono::thread_clock>();
179 check_clock_now_throws<boost::chrono::thread_clock>();
180 #endif
181
182 #if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS)
183 check_clock_now_ec<boost::chrono::process_real_cpu_clock>();
184 check_clock_now_throws<boost::chrono::process_real_cpu_clock>();
185
186 #if ! BOOST_OS_WINDOWS || BOOST_PLAT_WINDOWS_DESKTOP
187 check_clock_now_ec<boost::chrono::process_user_cpu_clock>();
188 check_clock_now_throws<boost::chrono::process_user_cpu_clock>();
189
190 check_clock_now_ec<boost::chrono::process_system_cpu_clock>();
191 check_clock_now_throws<boost::chrono::process_system_cpu_clock>();
192
193 check_clock_now_ec<boost::chrono::process_cpu_clock>();
194 check_clock_now_throws<boost::chrono::process_cpu_clock>();
195 #endif
196 #endif
197
198 #endif
199
200 return boost::report_errors();
201 }
202