1// boost timer_test.cpp --------------------------------------------------------------//
2
3// Copyright Beman Dawes 2006, 2011
4
5// Distributed under the Boost Software License, Version 1.0.
6// See http://www.boost.org/LICENSE_1_0.txt
7
8// See http://www.boost.org/libs/timer for documentation.
9
10#include <boost/timer/timer.hpp>
11#include <boost/detail/lightweight_main.hpp>
12#include <boost/detail/lightweight_test.hpp>
13#include <cstdlib> // for atol()
14#include <iostream>
15#include <string>
16#include <ctime>
17
18using std::string;
19using std::cout;
20using std::endl;
21using boost::timer::default_places;
22using boost::timer::nanosecond_type;
23using boost::timer::cpu_times;
24using boost::timer::format;
25using boost::timer::cpu_timer;
26using boost::timer::auto_cpu_timer;
27
28namespace
29{
30 void unit_test()
31 {
32 cout << "unit test..." << endl;
33
34 string default_format(" %ws wall, %us user + %ss system = %ts CPU (%p%)\n");
35
36 // each constructor
37 auto_cpu_timer t1;
38 BOOST_TEST(!t1.is_stopped());
39 // the following, and similar below, are giving false failures on MinGW/gcc
40 // so comment them out for now
41 //BOOST_TEST(&t1.ostream() == &cout);
42 BOOST_TEST_EQ(t1.places(), default_places);
43 BOOST_TEST_EQ(t1.format_string(), default_format);
44 t1.stop();
45 BOOST_TEST(t1.is_stopped());
46 auto_cpu_timer t1a(t1);
47 BOOST_TEST(t1a.is_stopped());
48 BOOST_TEST_EQ(t1a.elapsed().wall, t1.elapsed().wall);
49 BOOST_TEST_EQ(t1a.elapsed().user, t1.elapsed().user);
50 BOOST_TEST_EQ(t1a.elapsed().system, t1.elapsed().system);
51 //BOOST_TEST(&t1a.ostream() == &cout);
52 BOOST_TEST_EQ(t1a.places(), default_places);
53 BOOST_TEST_EQ(t1a.format_string(), default_format);
54
55 auto_cpu_timer t1b;
56 BOOST_TEST(!t1b.is_stopped());
57 t1b = t1;
58 BOOST_TEST(t1b.is_stopped());
59 BOOST_TEST_EQ(t1b.elapsed().wall, t1.elapsed().wall);
60 BOOST_TEST_EQ(t1b.elapsed().user, t1.elapsed().user);
61 BOOST_TEST_EQ(t1b.elapsed().system, t1.elapsed().system);
62 //BOOST_TEST(&t1b.ostream() == &cout);
63 BOOST_TEST_EQ(t1b.places(), default_places);
64 BOOST_TEST_EQ(t1b.format_string(), default_format);
65
66 auto_cpu_timer t2(1);
67 BOOST_TEST(!t2.is_stopped());
68 //BOOST_TEST(&t2.ostream() == &cout);
69 BOOST_TEST_EQ(t2.places(), 1);
70 BOOST_TEST_EQ(t2.format_string(), default_format);
71
72 auto_cpu_timer t3("foo");
73 BOOST_TEST(!t3.is_stopped());
74 //BOOST_TEST(&t3.ostream() == &cout);
75 BOOST_TEST_EQ(t3.places(), default_places);
76 BOOST_TEST_EQ(t3.format_string(), string("foo"));
77
78 auto_cpu_timer t4(1, "foo");
79 BOOST_TEST(!t4.is_stopped());
80 //BOOST_TEST(&t4.ostream() == &cout);
81 BOOST_TEST_EQ(t4.places(), 1);
82 BOOST_TEST_EQ(t4.format_string(), string("foo"));
83
84 auto_cpu_timer t5(std::cerr);
85 BOOST_TEST(!t5.is_stopped());
86 BOOST_TEST(&t5.ostream() == &std::cerr);
87 BOOST_TEST_EQ(t5.places(), default_places);
88 BOOST_TEST_EQ(t5.format_string(), default_format);
89
90 auto_cpu_timer t6(std::cerr, 1);
91 BOOST_TEST(!t6.is_stopped());
92 BOOST_TEST(&t6.ostream() == &std::cerr);
93 BOOST_TEST_EQ(t6.places(), 1);
94 BOOST_TEST_EQ(t6.format_string(), default_format);
95
96 auto_cpu_timer t7(std::cerr, "foo");
97 BOOST_TEST(!t7.is_stopped());
98 BOOST_TEST(&t7.ostream() == &std::cerr);
99 BOOST_TEST_EQ(t7.places(), default_places);
100 BOOST_TEST_EQ(t7.format_string(), string("foo"));
101
102 auto_cpu_timer t8(std::cerr, 1, "foo");
103 BOOST_TEST(!t8.is_stopped());
104 BOOST_TEST(&t8.ostream() == &std::cerr);
105 BOOST_TEST_EQ(t8.places(), 1);
106 BOOST_TEST_EQ(t8.format_string(), string("foo"));
107
108 t1.stop();
109 t1a.stop();
110 t1b.stop();
111 t2.stop();
112 t3.stop();
113 t4.stop();
114 t5.stop();
115 t6.stop();
116 t7.stop();
117 t8.stop();
118
119 cout << " unit test complete" << endl;
120 }
121
122 void format_test()
123 {
124 cout << "format test..." << endl;
125
126 cpu_times times;
127 times.wall = 5123456789LL;
128 times.user = 2123456789LL;
129 times.system = 1234567890LL;
130
131 cout << " times.wall is " << times.wall << '\n';
132 cout << " times.user is " << times.user << '\n';
133 cout << " times.system is " << times.system << '\n';
134 cout << " user+system is " << times.user + times.system << '\n';
135 cout << " format(times, 9) output: " << format(times, places: 9);
136
137 BOOST_TEST_EQ(format(times, 9),
138 string(" 5.123456789s wall, 2.123456789s user + 1.234567890s system = 3.358024679s CPU (65.5%)\n"));
139 BOOST_TEST_EQ(format(times, 8),
140 string(" 5.12345679s wall, 2.12345679s user + 1.23456789s system = 3.35802468s CPU (65.5%)\n"));
141 BOOST_TEST_EQ(format(times, 7),
142 string(" 5.1234568s wall, 2.1234568s user + 1.2345679s system = 3.3580247s CPU (65.5%)\n"));
143 BOOST_TEST_EQ(format(times, 6),
144 string(" 5.123457s wall, 2.123457s user + 1.234568s system = 3.358025s CPU (65.5%)\n"));
145 BOOST_TEST_EQ(format(times, 5),
146 string(" 5.12346s wall, 2.12346s user + 1.23457s system = 3.35802s CPU (65.5%)\n"));
147 BOOST_TEST_EQ(format(times, 4),
148 string(" 5.1235s wall, 2.1235s user + 1.2346s system = 3.3580s CPU (65.5%)\n"));
149 BOOST_TEST_EQ(format(times, 3),
150 string(" 5.123s wall, 2.123s user + 1.235s system = 3.358s CPU (65.5%)\n"));
151 BOOST_TEST_EQ(format(times, 2),
152 string(" 5.12s wall, 2.12s user + 1.23s system = 3.36s CPU (65.5%)\n"));
153 BOOST_TEST_EQ(format(times, 1),
154 string(" 5.1s wall, 2.1s user + 1.2s system = 3.4s CPU (65.5%)\n"));
155 BOOST_TEST_EQ(format(times, 0),
156 string(" 5s wall, 2s user + 1s system = 3s CPU (65.5%)\n"));
157 BOOST_TEST_EQ(format(times, 10),
158 string(" 5.123456789s wall, 2.123456789s user + 1.234567890s system = 3.358024679s CPU (65.5%)\n"));
159 BOOST_TEST_EQ(format(times, -1),
160 string(" 5.123457s wall, 2.123457s user + 1.234568s system = 3.358025s CPU (65.5%)\n"));
161 BOOST_TEST_EQ(format(times),
162 string(" 5.123457s wall, 2.123457s user + 1.234568s system = 3.358025s CPU (65.5%)\n"));
163
164 BOOST_TEST_EQ(format(times, 5, " %w, %u, %s, %t, %%p%"),
165 string(" 5.12346, 2.12346, 1.23457, 3.35802, %65.5%"));
166
167 BOOST_TEST_EQ(format(times, 5, "boo"), string("boo"));
168
169 cout << " format test complete" << endl;
170 }
171
172 void std_c_consistency_test()
173 {
174 cout << "C library consistency test..." << endl;
175
176 // This test is designed to account for C timer resolution and for the possibility
177 // that another active process may take up a lot of time.
178
179 cpu_timer t; // calls start(), so ensures any cpu_timer dll loaded
180 std::time(timer: 0); // ensure any system dll's loaded
181
182 std::time_t stop_time, start_time = std::time(timer: 0);
183
184 // wait until the time() clock ticks
185 while (std::time(timer: 0) == start_time) {}
186
187 // start both timers
188 start_time = std::time(timer: 0);
189 t.start();
190
191 // wait until the time() clock ticks again
192 while (std::time(timer: 0) == start_time) {}
193
194 // stop both timers
195 stop_time = std::time(timer: 0);
196 t.stop();
197
198 cout << " std::time() elapsed is " << (stop_time - start_time) * 1.0L << " seconds\n";
199 cout << " cpu_timer wall elapsed is " << t.elapsed().wall / 1000000000.0L << " seconds\n";
200 cout << " The two clocks whose elapsed time is compared by this test are started\n"
201 " and stopped one right after the other. If the operating system suspends\n"
202 " the process in the interim, the test may fail. Thus no single failure\n"
203 " of this test is meaningful.\n";
204
205 // These tests allow lots of fuzz to reduce false positives
206 BOOST_TEST(t.elapsed().wall / 1000000000.0L > (stop_time - start_time) * 0.75L);
207 BOOST_TEST(t.elapsed().wall / 1000000000.0L < (stop_time - start_time) * 1.25L);
208
209 cout << " C library consistency test complete" << endl;
210 }
211
212
213} // unnamed namespace
214
215//--------------------------------------------------------------------------------------//
216
217int cpp_main(int, char *[])
218{
219 cout << "---------- timer_test ----------\n";
220
221 unit_test();
222 format_test();
223 std_c_consistency_test();
224
225 return ::boost::report_errors();
226}
227
228

source code of boost/libs/timer/test/cpu_timer_test.cpp