1 // boost timer_test.cpp --------------------------------------------------------------//
3 // Copyright Beman Dawes 2006, 2011
5 // Distributed under the Boost Software License, Version 1.0.
6 // See http://www.boost.org/LICENSE_1_0.txt
8 // See http://www.boost.org/libs/timer for documentation.
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()
21 using boost::timer::default_places;
22 using boost::timer::nanosecond_type;
23 using boost::timer::cpu_times;
24 using boost::timer::format;
25 using boost::timer::cpu_timer;
26 using boost::timer::auto_cpu_timer;
32 cout << "unit test..." << endl;
34 string default_format(" %ws wall, %us user + %ss system = %ts CPU (%p%)\n");
38 BOOST_TEST(!t1.is_stopped());
39 BOOST_TEST(&t1.ostream() == &cout);
40 BOOST_TEST_EQ(t1.places(), default_places);
41 BOOST_TEST_EQ(t1.format_string(), default_format);
43 BOOST_TEST(t1.is_stopped());
44 auto_cpu_timer t1a(t1);
45 BOOST_TEST(t1a.is_stopped());
46 BOOST_TEST_EQ(t1a.elapsed().wall, t1.elapsed().wall);
47 BOOST_TEST_EQ(t1a.elapsed().user, t1.elapsed().user);
48 BOOST_TEST_EQ(t1a.elapsed().system, t1.elapsed().system);
49 BOOST_TEST(&t1a.ostream() == &cout);
50 BOOST_TEST_EQ(t1a.places(), default_places);
51 BOOST_TEST_EQ(t1a.format_string(), default_format);
54 BOOST_TEST(!t1b.is_stopped());
56 BOOST_TEST(t1b.is_stopped());
57 BOOST_TEST_EQ(t1b.elapsed().wall, t1.elapsed().wall);
58 BOOST_TEST_EQ(t1b.elapsed().user, t1.elapsed().user);
59 BOOST_TEST_EQ(t1b.elapsed().system, t1.elapsed().system);
60 BOOST_TEST(&t1b.ostream() == &cout);
61 BOOST_TEST_EQ(t1b.places(), default_places);
62 BOOST_TEST_EQ(t1b.format_string(), default_format);
65 BOOST_TEST(!t2.is_stopped());
66 BOOST_TEST(&t2.ostream() == &cout);
67 BOOST_TEST_EQ(t2.places(), 1);
68 BOOST_TEST_EQ(t2.format_string(), default_format);
70 auto_cpu_timer t3("foo");
71 BOOST_TEST(!t3.is_stopped());
72 BOOST_TEST(&t3.ostream() == &cout);
73 BOOST_TEST_EQ(t3.places(), default_places);
74 BOOST_TEST_EQ(t3.format_string(), string("foo"));
76 auto_cpu_timer t4(1, "foo");
77 BOOST_TEST(!t4.is_stopped());
78 BOOST_TEST(&t4.ostream() == &cout);
79 BOOST_TEST_EQ(t4.places(), 1);
80 BOOST_TEST_EQ(t4.format_string(), string("foo"));
82 auto_cpu_timer t5(std::cerr);
83 BOOST_TEST(!t5.is_stopped());
84 BOOST_TEST(&t5.ostream() == &std::cerr);
85 BOOST_TEST_EQ(t5.places(), default_places);
86 BOOST_TEST_EQ(t5.format_string(), default_format);
88 auto_cpu_timer t6(std::cerr, 1);
89 BOOST_TEST(!t6.is_stopped());
90 BOOST_TEST(&t6.ostream() == &std::cerr);
91 BOOST_TEST_EQ(t6.places(), 1);
92 BOOST_TEST_EQ(t6.format_string(), default_format);
94 auto_cpu_timer t7(std::cerr, "foo");
95 BOOST_TEST(!t7.is_stopped());
96 BOOST_TEST(&t7.ostream() == &std::cerr);
97 BOOST_TEST_EQ(t7.places(), default_places);
98 BOOST_TEST_EQ(t7.format_string(), string("foo"));
100 auto_cpu_timer t8(std::cerr, 1, "foo");
101 BOOST_TEST(!t8.is_stopped());
102 BOOST_TEST(&t8.ostream() == &std::cerr);
103 BOOST_TEST_EQ(t8.places(), 1);
104 BOOST_TEST_EQ(t8.format_string(), string("foo"));
117 cout << " unit test complete\n";
124 times.wall = 5123456789LL;
125 times.user = 2123456789LL;
126 times.system = 1234567890LL;
128 cout << " times.wall is " << times.wall << '\n';
129 cout << " times.user is " << times.user << '\n';
130 cout << " times.system is " << times.system << '\n';
131 cout << " user+system is " << times.user + times.system << '\n';
132 cout << " format(times, 9) output: " << format(times, 9);
134 BOOST_TEST_EQ(format(times, 9),
135 string(" 5.123456789s wall, 2.123456789s user + 1.234567890s system = 3.358024679s CPU (65.5%)\n"));
136 BOOST_TEST_EQ(format(times, 8),
137 string(" 5.12345679s wall, 2.12345679s user + 1.23456789s system = 3.35802468s CPU (65.5%)\n"));
138 BOOST_TEST_EQ(format(times, 7),
139 string(" 5.1234568s wall, 2.1234568s user + 1.2345679s system = 3.3580247s CPU (65.5%)\n"));
140 BOOST_TEST_EQ(format(times, 6),
141 string(" 5.123457s wall, 2.123457s user + 1.234568s system = 3.358025s CPU (65.5%)\n"));
142 BOOST_TEST_EQ(format(times, 5),
143 string(" 5.12346s wall, 2.12346s user + 1.23457s system = 3.35802s CPU (65.5%)\n"));
144 BOOST_TEST_EQ(format(times, 4),
145 string(" 5.1235s wall, 2.1235s user + 1.2346s system = 3.3580s CPU (65.5%)\n"));
146 BOOST_TEST_EQ(format(times, 3),
147 string(" 5.123s wall, 2.123s user + 1.235s system = 3.358s CPU (65.5%)\n"));
148 BOOST_TEST_EQ(format(times, 2),
149 string(" 5.12s wall, 2.12s user + 1.23s system = 3.36s CPU (65.5%)\n"));
150 BOOST_TEST_EQ(format(times, 1),
151 string(" 5.1s wall, 2.1s user + 1.2s system = 3.4s CPU (65.5%)\n"));
152 BOOST_TEST_EQ(format(times, 0),
153 string(" 5s wall, 2s user + 1s system = 3s CPU (65.5%)\n"));
154 BOOST_TEST_EQ(format(times, 10),
155 string(" 5.123456789s wall, 2.123456789s user + 1.234567890s system = 3.358024679s CPU (65.5%)\n"));
156 BOOST_TEST_EQ(format(times, -1),
157 string(" 5.123457s wall, 2.123457s user + 1.234568s system = 3.358025s CPU (65.5%)\n"));
158 BOOST_TEST_EQ(format(times),
159 string(" 5.123457s wall, 2.123457s user + 1.234568s system = 3.358025s CPU (65.5%)\n"));
161 BOOST_TEST_EQ(format(times, 5, " %w, %u, %s, %t, %%p%"),
162 string(" 5.12346, 2.12346, 1.23457, 3.35802, %65.5%"));
164 BOOST_TEST_EQ(format(times, 5, "boo"), string("boo"));
166 cout << " format test complete" << endl;
169 void std_c_consistency_test()
171 cout << "C library consistency test..." << endl;
173 // This test is designed to account for C timer resolution and for the possibility
174 // that another active process may take up a lot of time.
176 cpu_timer t; // calls start(), so ensures any cpu_timer dll loaded
177 std::time(0); // ensure any system dll's loaded
179 std::time_t stop_time, start_time = std::time(0);
181 // wait until the time() clock ticks
182 while (std::time(0) == start_time) {}
185 start_time = std::time(0);
188 // wait until the time() clock ticks again
189 while (std::time(0) == start_time) {}
192 stop_time = std::time(0);
195 cout << " std::time() elapsed is " << (stop_time - start_time) * 1.0L << " seconds\n";
196 cout << " cpu_timer wall elapsed is " << t.elapsed().wall / 1000000000.0L << " seconds\n";
197 cout << " The two clocks whose elapsed time is compared by this test are started\n"
198 " and stopped one right after the other. If the operating system suspends\n"
199 " the process in the interim, the test may fail. Thus no single failure\n"
200 " of this test is meaningful.\n";
202 // These tests allow lots of fuzz to reduce false positives
203 BOOST_TEST(t.elapsed().wall / 1000000000.0L > (stop_time - start_time) * 0.75L);
204 BOOST_TEST(t.elapsed().wall / 1000000000.0L < (stop_time - start_time) * 1.25L);
206 cout << " C library consistency test complete" << endl;
210 } // unnamed namespace
212 //--------------------------------------------------------------------------------------//
214 int cpp_main(int, char *[])
216 cout << "---------- timer_test ----------\n";
220 std_c_consistency_test();
222 return ::boost::report_errors();