1 // Copyright 2010-2011 Vicente J. Botet Escriba
2 // Distributed under the Boost Software License, Version 1.0.
3 // See http://www.boost.org/LICENSE_1_0.txt
5 #define BOOST_CHRONO_VERSION 2
8 #include <boost/type_traits/is_same.hpp>
9 #include <boost/chrono/stopwatches/stopwatch.hpp>
10 #include "../cycle_count.hpp"
11 #include <boost/chrono/stopwatches/reporters/stopwatch_reporter.hpp>
12 #include <boost/chrono/stopwatches/reporters/system_default_formatter.hpp>
14 #include <boost/chrono/chrono_io.hpp>
15 #include <boost/system/system_error.hpp>
16 #include <boost/detail/lightweight_test.hpp>
18 #if !defined(BOOST_NO_CXX11_STATIC_ASSERT)
22 using namespace boost::chrono
;
25 template <typename Stopwatch
>
26 void check_invariants()
28 BOOST_CHRONO_STATIC_ASSERT((boost::is_same
<typename
Stopwatch::rep
, typename
Stopwatch::clock::duration::rep
>::value
), NOTHING
, ());
29 BOOST_CHRONO_STATIC_ASSERT((boost::is_same
<typename
Stopwatch::period
, typename
Stopwatch::clock::duration::period
>::value
), NOTHING
, ());
30 BOOST_CHRONO_STATIC_ASSERT((boost::is_same
<typename
Stopwatch::duration
, typename
Stopwatch::clock::time_point::duration
>::value
), NOTHING
, ());
31 BOOST_CHRONO_STATIC_ASSERT(Stopwatch::is_steady
== Stopwatch::clock::is_steady
, NOTHING
, ());
34 template <typename Stopwatch
>
35 void check_default_constructor()
38 BOOST_TEST(sw
.is_running());
41 template <typename Stopwatch
>
42 void check_dont_start_constructor()
44 Stopwatch
sw(boost::chrono::dont_start
);
45 BOOST_TEST(!sw
.is_running());
46 ex::sleep_for
<typename
Stopwatch::clock
>(boost::chrono::milliseconds(100));
47 typename
Stopwatch::duration d
=sw
.elapsed();
48 BOOST_TEST(d
== Stopwatch::duration::zero());
51 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
52 template <typename Stopwatch
>
53 void check_constructor_ec()
55 boost::system::error_code ec
;
57 BOOST_TEST(sw
.is_running());
58 BOOST_TEST(ec
.value()==0);
61 template <typename Stopwatch
>
62 void check_constructor_throws()
64 Stopwatch
sw(boost::throws());
65 BOOST_TEST(sw
.is_running());
69 template <typename Stopwatch
>
73 BOOST_TEST(sw
.is_running());
74 ex::sleep_for
<typename
Stopwatch::clock
>(boost::chrono::milliseconds(100));
75 typename
Stopwatch::duration d
=sw
.elapsed();
76 BOOST_TEST(sw
.is_running());
77 BOOST_TEST(d
>= boost::chrono::milliseconds(100));
80 template <typename Stopwatch
>
81 void check_start_start()
84 BOOST_TEST(sw
.is_running());
85 ex::sleep_for
<typename
Stopwatch::clock
>(boost::chrono::milliseconds(100));
87 BOOST_TEST(sw
.is_running());
88 ex::sleep_for
<typename
Stopwatch::clock
>(boost::chrono::milliseconds(100));
89 typename
Stopwatch::duration d
=sw
.elapsed();
90 BOOST_TEST(sw
.is_running());
91 BOOST_TEST(d
>= boost::chrono::milliseconds(100));
92 BOOST_TEST(d
< boost::chrono::milliseconds(200));
95 template <typename Stopwatch
>
96 void check_dont_start_start()
98 Stopwatch
sw(boost::chrono::dont_start
);
99 BOOST_TEST(!sw
.is_running());
100 ex::sleep_for
<typename
Stopwatch::clock
>(boost::chrono::milliseconds(100));
102 BOOST_TEST(sw
.is_running());
103 ex::sleep_for
<typename
Stopwatch::clock
>(boost::chrono::milliseconds(100));
104 typename
Stopwatch::duration d
=sw
.elapsed();
105 BOOST_TEST(sw
.is_running());
106 BOOST_TEST(d
>= boost::chrono::milliseconds(100));
107 BOOST_TEST(d
< boost::chrono::milliseconds(200));
110 template <typename Stopwatch
>
111 void check_dont_start_start_stop()
113 Stopwatch
sw(boost::chrono::dont_start
);
114 BOOST_TEST(!sw
.is_running());
116 BOOST_TEST(sw
.is_running());
117 ex::sleep_for
<typename
Stopwatch::clock
>(boost::chrono::milliseconds(100));
119 BOOST_TEST(!sw
.is_running());
120 typename
Stopwatch::duration d
=sw
.elapsed();
121 BOOST_TEST(!sw
.is_running());
122 BOOST_TEST(d
>= boost::chrono::milliseconds(0));
125 template <typename Stopwatch
>
126 void check_dont_start_scoped_run()
128 Stopwatch
sw(boost::chrono::dont_start
);
129 BOOST_TEST(!sw
.is_running());
131 typename
Stopwatch::scoped_run
_(sw
);
132 BOOST_TEST(sw
.is_running());
133 ex::sleep_for
<typename
Stopwatch::clock
>(boost::chrono::milliseconds(100));
135 BOOST_TEST(!sw
.is_running());
136 typename
Stopwatch::duration d
=sw
.elapsed();
137 BOOST_TEST(!sw
.is_running());
138 BOOST_TEST(d
>= boost::chrono::milliseconds(0));
141 template <typename Stopwatch
>
145 BOOST_TEST(sw
.is_running());
146 ex::sleep_for
<typename
Stopwatch::clock
>(boost::chrono::milliseconds(100));
148 BOOST_TEST(!sw
.is_running());
149 typename
Stopwatch::duration d
=sw
.elapsed();
150 BOOST_TEST(!sw
.is_running());
151 BOOST_TEST(d
== boost::chrono::milliseconds(0));
154 template <typename Stopwatch
>
155 void check_stop_stop()
158 BOOST_TEST(sw
.is_running());
159 ex::sleep_for
<typename
Stopwatch::clock
>(boost::chrono::milliseconds(100));
161 BOOST_TEST(!sw
.is_running());
162 typename
Stopwatch::duration d
=sw
.elapsed();
163 BOOST_TEST(!sw
.is_running());
164 BOOST_TEST(d
== boost::chrono::milliseconds(0));
166 BOOST_TEST(!sw
.is_running());
168 BOOST_TEST(!sw
.is_running());
169 BOOST_TEST(d
== boost::chrono::milliseconds(0));
175 file_line(const char* file
, std::size_t line
)
176 : fmt("%1%[%2%] Elapsed time:")
188 template <typename Reporter
>
189 void check_file_line2()
192 file_line
fl(__FILE__
, __LINE__
);
193 ex::sleep_for
<typename
Reporter::clock
>(milliseconds(100));
196 template <typename Reporter
>
197 void check_file_line()
199 Reporter
rp("%1%[%2%] Elapsed time: %3%\n");
200 rp
.format() % __FILE__
% __LINE__
;
202 ex::sleep_for
<typename
Reporter::clock
>(milliseconds(100));
206 template <typename Reporter
>
210 ex::sleep_for
<typename
Reporter::clock
>(milliseconds(100));
217 template <typename Clock
>
220 typedef stopwatch_reporter
<stopwatch
<Clock
> > Reporter
;
221 typedef stopwatch_reporter
<stopwatch
<Clock
>, elapsed_formatter
> ReporterE
;
223 check_invariants
<Reporter
>();
224 check_default_constructor
<Reporter
>();
225 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
226 check_constructor_ec
<Reporter
>();
227 check_constructor_throws
<Reporter
>();
229 check_elapsed
<Reporter
>();
231 check_report
<Reporter
>();
232 check_file_line
<ReporterE
>();
234 check_start_start
<Reporter
>();
235 check_dont_start_constructor
<Reporter
>();
236 check_dont_start_start
<Reporter
>();
237 check_dont_start_start_stop
<Reporter
>();
238 check_dont_start_scoped_run
<Reporter
>();
239 check_stop
<Reporter
>();
240 check_stop_stop
<Reporter
>();
247 typedef stopwatch
<high_resolution_clock
> Stopwatch
;
248 typedef basic_stopwatch_reporter_default_formatter
<char, Stopwatch
>::type Formatter
;
249 typedef stopwatch_reporter
<Stopwatch
> Reporter
;
250 static Formatter fmtr
;
254 check_all
<ex::cycle_count
<1500> >();
256 return boost::report_errors();