time_point_input.cpp 9.3 KB

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