• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  Copyright 2011 Vicente J. Botet Escriba
2 //  Copyright (c) Microsoft Corporation 2014
3 //  Distributed under the Boost Software License, Version 1.0.
4 //  See http://www.boost.org/LICENSE_1_0.txt
5 
6 #include <boost/chrono/chrono_io.hpp>
7 #include <sstream>
8 #include <boost/detail/lightweight_test.hpp>
9 #include <boost/chrono/system_clocks.hpp>
10 #include <boost/chrono/thread_clock.hpp>
11 #include <boost/chrono/process_cpu_clocks.hpp>
12 #if 0
13 template <typename Clock, typename D>
14 void test_good(std::string str, D res)
15 {
16   typedef typename Clock::time_point clock_time_point;
17   typedef typename Clock::duration clock_duration;
18   std::istringstream in(str + boost::chrono::clock_string<Clock, char>::since());
19   clock_time_point tp;
20   in >> tp;
21   BOOST_TEST(in.eof());
22   BOOST_TEST(!in.fail());
23   std::cout << "Input=    " << str << std::endl;
24   std::cout << "Expected= " << clock_time_point(boost::chrono::duration_cast<clock_duration>(res)) << std::endl;
25   std::cout << "Obtained= " << tp << std::endl;
26   BOOST_TEST( (tp == clock_time_point(boost::chrono::duration_cast<clock_duration>(res)) ));
27 }
28 
29 #else
30 template <typename Clock, typename D>
test_good(std::string str,D res)31 void test_good(std::string str, D res)
32 {
33   typedef boost::chrono::time_point<Clock, D> clock_time_point;
34   //typedef typename Clock::duration clock_duration;
35   std::istringstream in(str + boost::chrono::clock_string<Clock, char>::since());
36   clock_time_point tp;
37   in >> tp;
38   BOOST_TEST(in.eof());
39   BOOST_TEST(!in.fail());
40   std::cout << "Input=    " << str << std::endl;
41   std::cout << "Expected= " << clock_time_point(res) << std::endl;
42   std::cout << "Obtained= " << tp << std::endl;
43   BOOST_TEST( tp == clock_time_point(res) );
44 }
45 
46 #endif
47 #if BOOST_CHRONO_VERSION >= 2
48 template <typename D>
test_good_system_clock(std::string str,D res)49 void test_good_system_clock(std::string str, D res)
50 {
51   typedef boost::chrono::system_clock Clock;
52 
53   std::istringstream in(str);
54   boost::chrono::time_point<Clock, D> tp;
55   in >> tp;
56   BOOST_TEST(in.eof());
57   BOOST_TEST(!in.fail());
58   std::cout << "Input=    " << str << std::endl;
59   std::cout << "Expected= " << boost::chrono::time_point<Clock, D>(res) << std::endl;
60   std::cout << "Obtained= " << tp << std::endl;
61   std::cout << "Expected= " << boost::chrono::duration_cast<boost::chrono::nanoseconds>(boost::chrono::time_point<Clock, D>(res).time_since_epoch()).count() << std::endl;
62   std::cout << "Obtained= " << boost::chrono::duration_cast<boost::chrono::nanoseconds>(tp.time_since_epoch()).count() << std::endl;
63   BOOST_TEST( (tp == boost::chrono::time_point<Clock, D>(res)));
64 }
65 
66 template <typename D>
test_good_utc_fmt_system_clock(std::string str,std::string fmt,D res)67 void test_good_utc_fmt_system_clock(std::string str, std::string fmt, D res)
68 {
69   typedef boost::chrono::system_clock Clock;
70 
71   std::istringstream in(str);
72   boost::chrono::time_point<Clock, D> tp;
73   in >> time_fmt(boost::chrono::timezone::utc, fmt);
74   in >> tp;
75   BOOST_TEST(in.eof());
76   BOOST_TEST(!in.fail());
77   std::cout << "Input=    " << str << std::endl;
78   std::cout << "Expected= " << boost::chrono::time_point<Clock, D>(res) << std::endl;
79   std::cout << "Obtained= " << tp << std::endl;
80   std::cout << "Expected= " << boost::chrono::duration_cast<boost::chrono::nanoseconds>(boost::chrono::time_point<Clock, D>(res).time_since_epoch()).count() << std::endl;
81   std::cout << "Obtained= " << boost::chrono::duration_cast<boost::chrono::nanoseconds>(tp.time_since_epoch()).count() << std::endl;
82   BOOST_TEST_EQ( tp , (boost::chrono::time_point<Clock, D>(res)));
83 }
84 #endif
85 template <typename Clock, typename D>
test_fail(const char * str,D)86 void test_fail(const char* str, D)
87 {
88   std::istringstream in(str + boost::chrono::clock_string<Clock, char>::since());
89   boost::chrono::time_point<Clock, D> tp;
90   in >> tp;
91   BOOST_TEST(in.fail());
92   BOOST_TEST( (tp == boost::chrono::time_point<Clock, D>()));
93 }
94 
95 template <typename Clock, typename D>
test_fail_no_epoch(const char * str,D)96 void test_fail_no_epoch(const char* str, D )
97 {
98   std::istringstream in(str);
99   boost::chrono::time_point<Clock, D> tp;
100   in >> tp;
101   BOOST_TEST(in.fail());
102   BOOST_TEST( (tp == boost::chrono::time_point<Clock, D>()));
103 }
104 
105 template <typename Clock, typename D>
test_fail_epoch(const char * str,D)106 void test_fail_epoch(const char* str, D)
107 {
108   std::istringstream in(str);
109   boost::chrono::time_point<Clock, D> tp;
110   in >> tp;
111   BOOST_TEST(in.fail());
112   BOOST_TEST( (tp == boost::chrono::time_point<Clock, D>()));
113 }
114 
115 template <typename Clock>
check_all()116 void check_all()
117 {
118   using namespace boost::chrono;
119   using namespace boost;
120 
121   test_good<Clock> ("5000 hours", hours(5000));
122   test_good<Clock> ("5000 minutes", minutes(5000));
123   test_good<Clock> ("5000 seconds", seconds(5000));
124   test_good<Clock> ("1 seconds", seconds(1));
125   test_good<Clock> ("1 second", seconds(1));
126   test_good<Clock> ("-1 seconds", seconds(-1));
127   test_good<Clock> ("0 second", seconds(0));
128   test_good<Clock> ("0 seconds", seconds(0));
129   test_good<Clock> ("5000 milliseconds", milliseconds(5000));
130   test_good<Clock> ("5000 microseconds", microseconds(5000));
131   test_good<Clock> ("5000 nanoseconds", nanoseconds(5000));
132   test_good<Clock> ("5000 deciseconds", duration<boost::int_least64_t, deci> (5000));
133   test_good<Clock> ("5000 [1/30]seconds", duration<boost::int_least64_t, ratio<1, 30> > (5000));
134 
135   test_good<Clock> ("5000 h", hours(5000));
136 #if BOOST_CHRONO_VERSION >= 2
137   test_good<Clock>("5000 min", minutes(5000));
138 #else
139   test_good<Clock> ("5000 m", minutes(5000));
140 #endif
141   test_good<Clock> ("5000 s", seconds(5000));
142   test_good<Clock> ("5000 ms", milliseconds(5000));
143   test_good<Clock> ("5000 ns", nanoseconds(5000));
144   test_good<Clock> ("5000 ds", duration<boost::int_least64_t, deci> (5000));
145   test_good<Clock> ("5000 [1/30]s", duration<boost::int_least64_t, ratio<1, 30> > (5000));
146 
147   test_good<Clock> ("5000 milliseconds", seconds(5));
148   test_good<Clock> ("5 milliseconds", nanoseconds(5000000));
149   test_good<Clock> ("4000 ms", seconds(4));
150   test_fail<Clock> ("3001 ms", seconds(3));
151   test_fail_epoch<Clock> ("3001 ms", seconds(3));
152   test_fail_epoch<Clock> ("3001 ms since", seconds(3));
153 
154 }
155 
156 #if ! BOOST_OS_WINDOWS || BOOST_PLAT_WINDOWS_DESKTOP
check_all_process_cpu_clock()157 void check_all_process_cpu_clock()
158 {
159   using namespace boost::chrono;
160   using namespace boost;
161   //typedef process_cpu_clock Clock;
162   //test_good<Clock> ("{5000;0;0} nanoseconds", process_cpu_clock::duration(process_cpu_clock::times(5000,0,0)));
163 }
164 #endif
165 
166 #if BOOST_CHRONO_VERSION >= 2
check_all_system_clock()167 void check_all_system_clock()
168 {
169   using namespace boost::chrono;
170   using namespace boost;
171 
172   test_good_system_clock ("1970-01-01 02:00:00.000000 +0000", hours(2));
173   test_good_system_clock ("1970-07-28 08:00:00.000000 +0000", hours(5000));
174   test_good_system_clock ("1970-01-04 11:20:00.000000 +0000", minutes(5000));
175   test_good_system_clock ("1970-01-01 01:23:20.000000 +0000", seconds(5000));
176   test_good_system_clock ("1970-01-01 00:00:01.000000 +0000", seconds(1));
177   test_good_system_clock ("1970-01-01 00:00:01.000000 +0000", seconds(1));
178   test_good_system_clock ("1969-12-31 23:59:59.000000 +0000", seconds(-1));
179   test_good_system_clock ("1970-01-01 00:00:00.000000 +0000", seconds(0));
180   test_good_system_clock ("1970-01-01 00:00:00.000000 +0000", seconds(0));
181   test_good_system_clock ("1970-01-01 00:00:05.000000 +0000", milliseconds(5000));
182   test_good_system_clock ("1970-01-01 00:00:00.005000 +0000", microseconds(5000));
183   test_good_system_clock ("1970-01-01 00:00:00.000005 +0000", nanoseconds(5000));
184   test_good_system_clock ("1970-01-01 00:08:20.000000 +0000", duration<boost::int_least64_t, deci> (5000));
185   test_good_system_clock ("1970-01-01 00:02:46.666667 +0000", duration<boost::int_least64_t, ratio<1, 30> > (5000));
186 
187   test_good_utc_fmt_system_clock ("1970-01-01 02:00:00", "%Y-%m-%d %H:%M:%S", hours(2));
188   test_good_utc_fmt_system_clock ("1970-01-01 02:00:00", "%F %H:%M:%S", hours(2));
189   test_good_utc_fmt_system_clock ("1970-01-01 02", "%Y-%m-%d %H", hours(2));
190   test_good_utc_fmt_system_clock ("1970-01-01 02", "%F %H", hours(2));
191   test_good_utc_fmt_system_clock ("1970-01-01 02:00:00", "%Y-%m-%d %T", hours(2));
192   test_good_utc_fmt_system_clock ("1970-01-01 02:00", "%Y-%m-%d %R", hours(2));
193   test_good_utc_fmt_system_clock ("% 1970-01-01 02:00", "%% %Y-%m-%d %R", hours(2));
194   //test_good_utc_fmt_system_clock ("1970-01-01 02:00 Thursday January", "%Y-%m-%d %R %A %B", hours(2));
195 
196 
197 //  test_fail<Clock> ("3001 ms", seconds(3));
198 //  test_fail_epoch<Clock> ("3001 ms", seconds(3));
199 //  test_fail_epoch<Clock> ("3001 ms since", seconds(3));
200 
201 }
202 #endif
main()203 int main()
204 {
205   std::cout << "high_resolution_clock=" << std::endl;
206   check_all<boost::chrono::high_resolution_clock> ();
207 #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
208   std::cout << "steady_clock=" << std::endl;
209   check_all<boost::chrono::steady_clock> ();
210 #endif
211   std::cout << "system_clock=" << std::endl;
212 #if BOOST_CHRONO_VERSION >= 2  && defined BOOST_CHRONO_PROVIDES_DATE_IO_FOR_SYSTEM_CLOCK_TIME_POINT
213   check_all_system_clock();
214 #else
215   check_all<boost::chrono::system_clock> ();
216 #endif
217 #if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
218   std::cout << "thread_clock="<< std::endl;
219   check_all<boost::chrono::thread_clock>();
220 #endif
221 
222 #if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS)
223   std::cout << "process_real_cpu_clock=" << std::endl;
224   check_all<boost::chrono::process_real_cpu_clock> ();
225 #if ! BOOST_OS_WINDOWS || BOOST_PLAT_WINDOWS_DESKTOP
226   std::cout << "process_user_cpu_clock=" << std::endl;
227   check_all<boost::chrono::process_user_cpu_clock> ();
228   std::cout << "process_system_cpu_clock=" << std::endl;
229   check_all<boost::chrono::process_system_cpu_clock> ();
230   std::cout << "process_cpu_clock=" << std::endl;
231   //check_all_process_cpu_clock();
232 #endif
233 #endif
234 
235   return boost::report_errors();
236 
237 }
238 
239