e13a4ba78d1ca6a3c3e3ab5a5d432889e3a0b805
[platform/upstream/boost.git] / libs / timer / test / cpu_timer_test.cpp
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
18 using std::string;
19 using std::cout;
20 using std::endl;
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;
27
28 namespace
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     BOOST_TEST(&t1.ostream() == &cout);
40     BOOST_TEST_EQ(t1.places(), default_places);
41     BOOST_TEST_EQ(t1.format_string(), default_format);
42     t1.stop();
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);
52  
53     auto_cpu_timer t1b;
54     BOOST_TEST(!t1b.is_stopped());
55     t1b = t1;
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);
63
64     auto_cpu_timer t2(1);
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);
69
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"));
75
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"));
81
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);
87
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);
93
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"));
99
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"));
105
106     t1.stop();
107     t1a.stop();
108     t1b.stop();
109     t2.stop();
110     t3.stop();
111     t4.stop();
112     t5.stop();
113     t6.stop();
114     t7.stop();
115     t8.stop();
116
117     cout << "  unit test complete\n";
118   }
119
120   void format_test()
121   {
122
123     cpu_times times;
124     times.wall = 5123456789LL;
125     times.user = 2123456789LL;
126     times.system = 1234567890LL;
127
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);
133
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"));
160
161     BOOST_TEST_EQ(format(times, 5, " %w, %u, %s, %t, %%p%"),
162       string(" 5.12346, 2.12346, 1.23457, 3.35802, %65.5%"));
163
164     BOOST_TEST_EQ(format(times, 5, "boo"), string("boo"));
165
166     cout << "  format test complete" << endl; 
167   }
168
169   void std_c_consistency_test()
170   {
171     cout << "C library consistency test..." << endl;
172
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.
175
176     cpu_timer t;    // calls start(), so ensures any cpu_timer dll loaded
177     std::time(0);   // ensure any system dll's loaded
178
179     std::time_t stop_time, start_time = std::time(0);
180
181     // wait until the time() clock ticks
182     while (std::time(0) == start_time) {}
183     
184     // start both timers
185     start_time = std::time(0);
186     t.start();
187
188     // wait until the time() clock ticks again
189     while (std::time(0) == start_time) {}
190
191     // stop both timers
192     stop_time = std::time(0);
193     t.stop();
194
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";
201
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);
205
206     cout << "  C library consistency test complete" << endl; 
207   }
208
209
210 }  // unnamed namespace
211
212 //--------------------------------------------------------------------------------------//
213
214 int cpp_main(int, char *[])
215 {
216   cout << "----------  timer_test  ----------\n";
217
218   unit_test();
219   format_test();
220   std_c_consistency_test();
221
222   return ::boost::report_errors();
223 }
224