]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/chrono/stopwatches/test/stopwatch/basic_stopwatch_reporter_pass.cpp
Add patch for failing prerm scripts
[ceph.git] / ceph / src / boost / libs / chrono / stopwatches / test / stopwatch / basic_stopwatch_reporter_pass.cpp
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
14 #include <boost/chrono/chrono_io.hpp>
15 #include <boost/system/system_error.hpp>
16 #include <boost/detail/lightweight_test.hpp>
17
18 #if !defined(BOOST_NO_CXX11_STATIC_ASSERT)
19 #define NOTHING ""
20 #endif
21
22 using namespace boost::chrono;
23
24
25 template <typename Stopwatch>
26 void check_invariants()
27 {
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, ());
32 }
33
34 template <typename Stopwatch>
35 void check_default_constructor()
36 {
37 Stopwatch sw;
38 BOOST_TEST(sw.is_running());
39 }
40
41 template <typename Stopwatch>
42 void check_dont_start_constructor()
43 {
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());
49 }
50
51 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
52 template <typename Stopwatch>
53 void check_constructor_ec()
54 {
55 boost::system::error_code ec;
56 Stopwatch sw(ec);
57 BOOST_TEST(sw.is_running());
58 BOOST_TEST(ec.value()==0);
59 }
60
61 template <typename Stopwatch>
62 void check_constructor_throws()
63 {
64 Stopwatch sw(boost::throws());
65 BOOST_TEST(sw.is_running());
66 }
67 #endif
68
69 template <typename Stopwatch>
70 void check_elapsed()
71 {
72 Stopwatch sw;
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));
78 }
79
80 template <typename Stopwatch>
81 void check_start_start()
82 {
83 Stopwatch sw;
84 BOOST_TEST(sw.is_running());
85 ex::sleep_for<typename Stopwatch::clock>(boost::chrono::milliseconds(100));
86 sw.start();
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));
93 }
94
95 template <typename Stopwatch>
96 void check_dont_start_start()
97 {
98 Stopwatch sw(boost::chrono::dont_start);
99 BOOST_TEST(!sw.is_running());
100 ex::sleep_for<typename Stopwatch::clock>(boost::chrono::milliseconds(100));
101 sw.start();
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));
108 }
109
110 template <typename Stopwatch>
111 void check_dont_start_start_stop()
112 {
113 Stopwatch sw(boost::chrono::dont_start);
114 BOOST_TEST(!sw.is_running());
115 sw.start();
116 BOOST_TEST(sw.is_running());
117 ex::sleep_for<typename Stopwatch::clock>(boost::chrono::milliseconds(100));
118 sw.stop();
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));
123 }
124
125 template <typename Stopwatch>
126 void check_dont_start_scoped_run()
127 {
128 Stopwatch sw(boost::chrono::dont_start);
129 BOOST_TEST(!sw.is_running());
130 {
131 typename Stopwatch::scoped_run _(sw);
132 BOOST_TEST(sw.is_running());
133 ex::sleep_for<typename Stopwatch::clock>(boost::chrono::milliseconds(100));
134 }
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));
139 }
140
141 template <typename Stopwatch>
142 void check_stop()
143 {
144 Stopwatch sw;
145 BOOST_TEST(sw.is_running());
146 ex::sleep_for<typename Stopwatch::clock>(boost::chrono::milliseconds(100));
147 sw.stop();
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));
152 }
153
154 template <typename Stopwatch>
155 void check_stop_stop()
156 {
157 Stopwatch sw;
158 BOOST_TEST(sw.is_running());
159 ex::sleep_for<typename Stopwatch::clock>(boost::chrono::milliseconds(100));
160 sw.stop();
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));
165 sw.stop();
166 BOOST_TEST(!sw.is_running());
167 d=sw.elapsed();
168 BOOST_TEST(!sw.is_running());
169 BOOST_TEST(d == boost::chrono::milliseconds(0));
170 }
171
172
173
174 struct file_line {
175 file_line(const char* file, std::size_t line)
176 : fmt("%1%[%2%] Elapsed time:")
177 {
178 fmt % file % line;
179 }
180 ~file_line()
181 {
182 std::cout << fmt;
183 }
184 boost::format fmt;
185
186 };
187
188 template <typename Reporter>
189 void check_file_line2()
190 {
191 Reporter _("%1%\n");
192 file_line fl(__FILE__, __LINE__);
193 ex::sleep_for<typename Reporter::clock>(milliseconds(100));
194
195 }
196 template <typename Reporter>
197 void check_file_line()
198 {
199 Reporter rp("%1%[%2%] Elapsed time: %3%\n");
200 rp.format() % __FILE__ % __LINE__;
201
202 ex::sleep_for<typename Reporter::clock>(milliseconds(100));
203
204 }
205
206 template <typename Reporter>
207 void check_report()
208 {
209 Reporter sw;
210 ex::sleep_for<typename Reporter::clock>(milliseconds(100));
211 sw.report();
212 }
213
214
215
216
217 template <typename Clock>
218 void check_all()
219 {
220 typedef stopwatch_reporter<stopwatch<Clock> > Reporter;
221 typedef stopwatch_reporter<stopwatch<Clock>, elapsed_formatter > ReporterE;
222
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>();
228 #endif
229 check_elapsed<Reporter>();
230
231 check_report<Reporter>();
232 check_file_line<ReporterE>();
233
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>();
241
242
243 }
244
245 int main()
246 {
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;
251
252 Reporter _(fmtr);
253
254 check_all<ex::cycle_count<1500> >();
255
256 return boost::report_errors();
257 }