1 // Copyright 2010-2011 Vicente J. Botet Escriba
2 // Copyright (c) Microsoft Corporation 2014
3 // Distributed under the Boost Software License, Version 1.0.
4 // See http://www.boost.org/LICENSE_1_0.txt
6 #define BOOST_CHRONO_VERSION 2
9 #include <boost/type_traits/is_same.hpp>
10 #include <boost/chrono/stopwatches/strict_stopwatch.hpp>
11 #include "../cycle_count.hpp"
12 #include <boost/chrono/stopwatches/reporters/stopwatch_reporter.hpp>
13 #include <boost/chrono/stopwatches/reporters/system_default_formatter.hpp>
15 #include <boost/chrono/chrono_io.hpp>
16 #include <boost/system/system_error.hpp>
17 #include <boost/detail/lightweight_test.hpp>
19 #if !defined(BOOST_NO_CXX11_STATIC_ASSERT)
23 using namespace boost::chrono
;
26 template <typename Reporter
>
27 void check_invariants()
29 typedef Reporter Stopwatch
;
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
, ());
34 BOOST_CHRONO_STATIC_ASSERT((boost::is_same
<typename
Stopwatch::stopwatch_type
, strict_stopwatch
<typename
Stopwatch::clock
> >::value
), NOTHING
, ());
37 template <typename Reporter
>
38 void check_default_constructor()
44 file_line(const char* file
, std::size_t line
)
45 : fmt("%1%[%2%] Elapsed time:")
57 template <typename Reporter
>
58 void check_file_line2()
61 file_line
fl(__FILE__
, __LINE__
);
62 ex::sleep_for
<typename
Reporter::clock
>(milliseconds(100));
65 template <typename Reporter
>
66 void check_file_line()
68 Reporter
rp("%1%[%2%] Elapsed time: %3%\n");
69 rp
.format() % __FILE__
% __LINE__
;
71 ex::sleep_for
<typename
Reporter::clock
>(milliseconds(100));
76 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
77 template <typename Reporter
>
78 void check_constructor_ec()
80 boost::system::error_code ec
;
82 BOOST_TEST(ec
.value()==0);
85 template <typename Reporter
>
86 void check_constructor_throws()
88 Reporter
_(boost::throws());
92 template <typename Reporter
>
93 void check_elapsed(bool check
=true)
96 ex::sleep_for
<typename
Reporter::clock
>(milliseconds(100));
97 typename
Reporter::duration d
=sw
.elapsed();
98 std::cout
<< d
<< std::endl
;
100 BOOST_TEST(d
>= milliseconds(100));
103 template <typename Reporter
>
107 ex::sleep_for
<typename
Reporter::clock
>(milliseconds(100));
111 template <typename Clock
>
112 void check_all(bool check
=true)
114 typedef stopwatch_reporter
<strict_stopwatch
<Clock
> > Reporter
;
115 typedef stopwatch_reporter
<strict_stopwatch
<Clock
>, elapsed_formatter
> ReporterE
;
117 check_invariants
<Reporter
>();
118 check_default_constructor
<Reporter
>();
119 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
120 check_constructor_ec
<Reporter
>();
121 check_constructor_throws
<Reporter
>();
123 check_elapsed
<Reporter
>(check
);
124 check_report
<Reporter
>();
125 check_file_line
<ReporterE
>();
130 typedef strict_stopwatch
<high_resolution_clock
> Stopwatch
;
131 typedef basic_stopwatch_reporter_default_formatter
<char, Stopwatch
>::type Formatter
;
132 typedef stopwatch_reporter
<Stopwatch
> Reporter
;
133 static Formatter fmtr
;
137 check_all
<ex::cycle_count
<1500> >(true);
140 std::cout
<< "high_resolution_clock=\n";
142 check_all
<high_resolution_clock
>();
143 #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
144 std::cout
<< "steady_clock=\n";
145 check_all
<steady_clock
>(false);
147 std::cout
<< "system_clock=\n";
148 check_all
<system_clock
>(false);
150 #if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
151 std::cout
<< "thread_clock=\n";
152 check_all
<thread_clock
>(false);
155 #if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS)
156 std::cout
<< "process_real_cpu_clock=\n";
157 check_all
<process_real_cpu_clock
>(false);
158 #if ! BOOST_OS_WINDOWS || BOOST_PLAT_WINDOWS_DESKTOP
159 std::cout
<< "process_user_cpu_clock=\n";
160 check_all
<process_user_cpu_clock
>(false);
161 std::cout
<< "process_system_cpu_clock=\n";
162 check_all
<process_system_cpu_clock
>(false);
163 std::cout
<< "process_cpu_clock=\n";
164 check_all
<process_cpu_clock
>(false);
169 return boost::report_errors();