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>
13 #include <boost/chrono/stopwatches/reporters/laps_accumulator_set_stopwatch_default_formatter.hpp>
14 #include <boost/chrono/stopwatches/collectors/laps_accumulator_set.hpp>
16 #include <boost/chrono/chrono_io.hpp>
17 #include <boost/system/system_error.hpp>
18 #include <boost/detail/lightweight_test.hpp>
20 #if !defined(BOOST_NO_CXX11_STATIC_ASSERT)
24 using namespace boost::chrono
;
27 template <typename Stopwatch
>
28 void check_invariants()
30 BOOST_CHRONO_STATIC_ASSERT((boost::is_same
<typename
Stopwatch::rep
, typename
Stopwatch::clock::duration::rep
>::value
), NOTHING
, ());
31 BOOST_CHRONO_STATIC_ASSERT((boost::is_same
<typename
Stopwatch::period
, typename
Stopwatch::clock::duration::period
>::value
), NOTHING
, ());
32 BOOST_CHRONO_STATIC_ASSERT((boost::is_same
<typename
Stopwatch::duration
, typename
Stopwatch::clock::time_point::duration
>::value
), NOTHING
, ());
33 BOOST_CHRONO_STATIC_ASSERT(Stopwatch::is_steady
== Stopwatch::clock::is_steady
, NOTHING
, ());
36 template <typename Stopwatch
>
37 void check_default_constructor()
40 BOOST_TEST(sw
.is_running());
43 template <typename Stopwatch
>
44 void check_dont_start_constructor()
46 Stopwatch
sw(boost::chrono::dont_start
);
47 BOOST_TEST(!sw
.is_running());
48 ex::sleep_for
<typename
Stopwatch::clock
>(boost::chrono::milliseconds(100));
49 typename
Stopwatch::duration d
=sw
.elapsed();
50 BOOST_TEST(d
== Stopwatch::duration::zero());
53 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
54 template <typename Stopwatch
>
55 void check_constructor_ec()
57 boost::system::error_code ec
;
59 BOOST_TEST(sw
.is_running());
60 BOOST_TEST(ec
.value()==0);
63 template <typename Stopwatch
>
64 void check_constructor_throws()
66 Stopwatch
sw(boost::throws());
67 BOOST_TEST(sw
.is_running());
71 template <typename Stopwatch
>
75 BOOST_TEST(sw
.is_running());
76 ex::sleep_for
<typename
Stopwatch::clock
>(boost::chrono::milliseconds(100));
77 typename
Stopwatch::duration d
=sw
.elapsed();
78 BOOST_TEST(sw
.is_running());
79 BOOST_TEST(d
>= boost::chrono::milliseconds(100));
82 template <typename Stopwatch
>
83 void check_start_start()
86 BOOST_TEST(sw
.is_running());
87 ex::sleep_for
<typename
Stopwatch::clock
>(boost::chrono::milliseconds(100));
89 BOOST_TEST(sw
.is_running());
90 ex::sleep_for
<typename
Stopwatch::clock
>(boost::chrono::milliseconds(100));
91 typename
Stopwatch::duration d
=sw
.elapsed();
92 BOOST_TEST(sw
.is_running());
93 BOOST_TEST(d
>= boost::chrono::milliseconds(100));
94 BOOST_TEST(d
< boost::chrono::milliseconds(200));
97 template <typename Stopwatch
>
98 void check_dont_start_start()
100 Stopwatch
sw(boost::chrono::dont_start
);
101 BOOST_TEST(!sw
.is_running());
102 ex::sleep_for
<typename
Stopwatch::clock
>(boost::chrono::milliseconds(100));
104 BOOST_TEST(sw
.is_running());
105 ex::sleep_for
<typename
Stopwatch::clock
>(boost::chrono::milliseconds(100));
106 typename
Stopwatch::duration d
=sw
.elapsed();
107 BOOST_TEST(sw
.is_running());
108 BOOST_TEST(d
>= boost::chrono::milliseconds(100));
109 BOOST_TEST(d
< boost::chrono::milliseconds(200));
112 template <typename Stopwatch
>
113 void check_dont_start_start_stop()
115 Stopwatch
sw(boost::chrono::dont_start
);
116 BOOST_TEST(!sw
.is_running());
118 BOOST_TEST(sw
.is_running());
119 ex::sleep_for
<typename
Stopwatch::clock
>(boost::chrono::milliseconds(100));
121 BOOST_TEST(!sw
.is_running());
122 typename
Stopwatch::duration d
=sw
.elapsed();
123 BOOST_TEST(!sw
.is_running());
124 BOOST_TEST(d
>= boost::chrono::milliseconds(0));
127 template <typename Stopwatch
>
128 void check_dont_start_scoped_run()
130 Stopwatch
sw(boost::chrono::dont_start
);
131 BOOST_TEST(!sw
.is_running());
133 typename
Stopwatch::scoped_run
_(sw
);
134 BOOST_TEST(sw
.is_running());
135 ex::sleep_for
<typename
Stopwatch::clock
>(boost::chrono::milliseconds(100));
137 BOOST_TEST(!sw
.is_running());
138 typename
Stopwatch::duration d
=sw
.elapsed();
139 BOOST_TEST(!sw
.is_running());
140 BOOST_TEST(d
>= boost::chrono::milliseconds(0));
143 template <typename Stopwatch
>
147 BOOST_TEST(sw
.is_running());
148 ex::sleep_for
<typename
Stopwatch::clock
>(boost::chrono::milliseconds(100));
150 BOOST_TEST(!sw
.is_running());
151 typename
Stopwatch::duration d
=sw
.elapsed();
152 BOOST_TEST(!sw
.is_running());
153 BOOST_TEST(d
== boost::chrono::milliseconds(100));
156 template <typename Stopwatch
>
157 void check_stop_stop()
160 BOOST_TEST(sw
.is_running());
161 ex::sleep_for
<typename
Stopwatch::clock
>(boost::chrono::milliseconds(100));
163 BOOST_TEST(!sw
.is_running());
164 typename
Stopwatch::duration d
=sw
.elapsed();
165 BOOST_TEST(!sw
.is_running());
166 BOOST_TEST(d
== boost::chrono::milliseconds(100));
168 BOOST_TEST(!sw
.is_running());
170 BOOST_TEST(!sw
.is_running());
171 BOOST_TEST(d
== boost::chrono::milliseconds(100));
177 file_line(const char* file
, std::size_t line
)
178 : fmt("%1%[%2%] Elapsed time:")
190 template <typename Reporter
>
191 void check_file_line2()
194 file_line
fl(__FILE__
, __LINE__
);
195 ex::sleep_for
<typename
Reporter::clock
>(milliseconds(100));
198 template <typename Reporter
>
199 void check_file_line()
201 Reporter
rp("%1%[%2%] Elapsed time: %3%\n");
202 rp
.format() % __FILE__
% __LINE__
;
204 ex::sleep_for
<typename
Reporter::clock
>(milliseconds(100));
208 template <typename Reporter
>
212 ex::sleep_for
<typename
Reporter::clock
>(milliseconds(100));
219 template <typename Clock
>
222 typedef stopwatch_reporter
<stopwatch
<Clock
, boost::chrono::laps_accumulator_set
<typename
Clock::duration
> > > Reporter
;
223 typedef stopwatch_reporter
<stopwatch
<Clock
>, elapsed_formatter
> ReporterE
;
225 check_invariants
<Reporter
>();
226 check_default_constructor
<Reporter
>();
227 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
228 check_constructor_ec
<Reporter
>();
229 check_constructor_throws
<Reporter
>();
231 check_elapsed
<Reporter
>();
233 check_report
<Reporter
>();
234 check_file_line
<ReporterE
>();
236 check_start_start
<Reporter
>();
237 check_dont_start_constructor
<Reporter
>();
238 check_dont_start_start
<Reporter
>();
239 check_dont_start_start_stop
<Reporter
>();
240 check_dont_start_scoped_run
<Reporter
>();
241 check_stop
<Reporter
>();
242 check_stop_stop
<Reporter
>();
250 typedef stopwatch
<high_resolution_clock
> Stopwatch
;
251 typedef basic_stopwatch_reporter_default_formatter
<char, Stopwatch
>::type Formatter
;
252 typedef stopwatch_reporter
<Stopwatch
> Reporter
;
253 static Formatter fmtr
;
257 check_all
<ex::cycle_count
<1500> >();
259 return boost::report_errors();