]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | |
4 | ||
5 | #define BOOST_CHRONO_VERSION 2 | |
6 | ||
7 | #include <iostream> | |
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> | |
15 | ||
16 | #include <boost/chrono/chrono_io.hpp> | |
17 | #include <boost/system/system_error.hpp> | |
18 | #include <boost/detail/lightweight_test.hpp> | |
19 | ||
20 | #if !defined(BOOST_NO_CXX11_STATIC_ASSERT) | |
21 | #define NOTHING "" | |
22 | #endif | |
23 | ||
24 | using namespace boost::chrono; | |
25 | ||
26 | ||
27 | template <typename Stopwatch> | |
28 | void check_invariants() | |
29 | { | |
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 | } | |
35 | ||
36 | template <typename Stopwatch> | |
37 | void check_default_constructor() | |
38 | { | |
39 | Stopwatch sw; | |
40 | BOOST_TEST(sw.is_running()); | |
41 | } | |
42 | ||
43 | template <typename Stopwatch> | |
44 | void check_dont_start_constructor() | |
45 | { | |
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()); | |
51 | } | |
52 | ||
53 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING | |
54 | template <typename Stopwatch> | |
55 | void check_constructor_ec() | |
56 | { | |
57 | boost::system::error_code ec; | |
58 | Stopwatch sw(ec); | |
59 | BOOST_TEST(sw.is_running()); | |
60 | BOOST_TEST(ec.value()==0); | |
61 | } | |
62 | ||
63 | template <typename Stopwatch> | |
64 | void check_constructor_throws() | |
65 | { | |
66 | Stopwatch sw(boost::throws()); | |
67 | BOOST_TEST(sw.is_running()); | |
68 | } | |
69 | #endif | |
70 | ||
71 | template <typename Stopwatch> | |
72 | void check_elapsed() | |
73 | { | |
74 | Stopwatch sw; | |
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)); | |
80 | } | |
81 | ||
82 | template <typename Stopwatch> | |
83 | void check_start_start() | |
84 | { | |
85 | Stopwatch sw; | |
86 | BOOST_TEST(sw.is_running()); | |
87 | ex::sleep_for<typename Stopwatch::clock>(boost::chrono::milliseconds(100)); | |
88 | sw.start(); | |
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)); | |
95 | } | |
96 | ||
97 | template <typename Stopwatch> | |
98 | void check_dont_start_start() | |
99 | { | |
100 | Stopwatch sw(boost::chrono::dont_start); | |
101 | BOOST_TEST(!sw.is_running()); | |
102 | ex::sleep_for<typename Stopwatch::clock>(boost::chrono::milliseconds(100)); | |
103 | sw.start(); | |
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)); | |
110 | } | |
111 | ||
112 | template <typename Stopwatch> | |
113 | void check_dont_start_start_stop() | |
114 | { | |
115 | Stopwatch sw(boost::chrono::dont_start); | |
116 | BOOST_TEST(!sw.is_running()); | |
117 | sw.start(); | |
118 | BOOST_TEST(sw.is_running()); | |
119 | ex::sleep_for<typename Stopwatch::clock>(boost::chrono::milliseconds(100)); | |
120 | sw.stop(); | |
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)); | |
125 | } | |
126 | ||
127 | template <typename Stopwatch> | |
128 | void check_dont_start_scoped_run() | |
129 | { | |
130 | Stopwatch sw(boost::chrono::dont_start); | |
131 | BOOST_TEST(!sw.is_running()); | |
132 | { | |
133 | typename Stopwatch::scoped_run _(sw); | |
134 | BOOST_TEST(sw.is_running()); | |
135 | ex::sleep_for<typename Stopwatch::clock>(boost::chrono::milliseconds(100)); | |
136 | } | |
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)); | |
141 | } | |
142 | ||
143 | template <typename Stopwatch> | |
144 | void check_stop() | |
145 | { | |
146 | Stopwatch sw; | |
147 | BOOST_TEST(sw.is_running()); | |
148 | ex::sleep_for<typename Stopwatch::clock>(boost::chrono::milliseconds(100)); | |
149 | sw.stop(); | |
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)); | |
154 | } | |
155 | ||
156 | template <typename Stopwatch> | |
157 | void check_stop_stop() | |
158 | { | |
159 | Stopwatch sw; | |
160 | BOOST_TEST(sw.is_running()); | |
161 | ex::sleep_for<typename Stopwatch::clock>(boost::chrono::milliseconds(100)); | |
162 | sw.stop(); | |
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)); | |
167 | sw.stop(); | |
168 | BOOST_TEST(!sw.is_running()); | |
169 | d=sw.elapsed(); | |
170 | BOOST_TEST(!sw.is_running()); | |
171 | BOOST_TEST(d == boost::chrono::milliseconds(100)); | |
172 | } | |
173 | ||
174 | ||
175 | ||
176 | struct file_line { | |
177 | file_line(const char* file, std::size_t line) | |
178 | : fmt("%1%[%2%] Elapsed time:") | |
179 | { | |
180 | fmt % file % line; | |
181 | } | |
182 | ~file_line() | |
183 | { | |
184 | std::cout << fmt; | |
185 | } | |
186 | boost::format fmt; | |
187 | ||
188 | }; | |
189 | ||
190 | template <typename Reporter> | |
191 | void check_file_line2() | |
192 | { | |
193 | Reporter _("%1%\n"); | |
194 | file_line fl(__FILE__, __LINE__); | |
195 | ex::sleep_for<typename Reporter::clock>(milliseconds(100)); | |
196 | ||
197 | } | |
198 | template <typename Reporter> | |
199 | void check_file_line() | |
200 | { | |
201 | Reporter rp("%1%[%2%] Elapsed time: %3%\n"); | |
202 | rp.format() % __FILE__ % __LINE__; | |
203 | ||
204 | ex::sleep_for<typename Reporter::clock>(milliseconds(100)); | |
205 | ||
206 | } | |
207 | ||
208 | template <typename Reporter> | |
209 | void check_report() | |
210 | { | |
211 | Reporter sw; | |
212 | ex::sleep_for<typename Reporter::clock>(milliseconds(100)); | |
213 | sw.report(); | |
214 | } | |
215 | ||
216 | ||
217 | ||
218 | ||
219 | template <typename Clock> | |
220 | void check_all() | |
221 | { | |
222 | typedef stopwatch_reporter<stopwatch<Clock, boost::chrono::laps_accumulator_set<typename Clock::duration> > > Reporter; | |
223 | typedef stopwatch_reporter<stopwatch<Clock>, elapsed_formatter > ReporterE; | |
224 | ||
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>(); | |
230 | #endif | |
231 | check_elapsed<Reporter>(); | |
232 | ||
233 | check_report<Reporter>(); | |
234 | check_file_line<ReporterE>(); | |
235 | ||
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>(); | |
243 | ||
244 | } | |
245 | ||
246 | ||
247 | ||
248 | int main() | |
249 | { | |
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; | |
254 | ||
255 | Reporter _(fmtr); | |
256 | ||
257 | check_all<ex::cycle_count<1500> >(); | |
258 | ||
259 | return boost::report_errors(); | |
260 | } |