duration_input.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Distributed under the Boost Software License, Version 1.0.
  2. // Copyright 2011 Vicente J. Botet Escriba
  3. // See http://www.boost.org/LICENSE_1_0.txt
  4. #include <boost/chrono/chrono_io.hpp>
  5. //#include <boost/chrono/io/duration_units.hpp>
  6. #include <sstream>
  7. #include <boost/detail/lightweight_test.hpp>
  8. template<typename D>
  9. void test_good(const char* str, D res)
  10. {
  11. std::istringstream in(str);
  12. D d(0);
  13. in >> d;
  14. BOOST_TEST(in.eof());
  15. BOOST_TEST(!in.fail());
  16. BOOST_TEST(d == res);
  17. std::cout << str << " " << res << " " << d << std::endl;
  18. }
  19. template<typename DFail>
  20. void test_fail(const char* str, DFail res)
  21. {
  22. {
  23. std::istringstream in(str);
  24. DFail d = DFail::zero();
  25. in >> d;
  26. BOOST_TEST(in.fail());
  27. BOOST_TEST(d == DFail::zero());
  28. std::cout << str << " " << res << " " << d << std::endl;
  29. }
  30. }
  31. template<typename D>
  32. void test_not_eof(const char* str, D res)
  33. {
  34. {
  35. std::istringstream in(str);
  36. D d = D::zero();
  37. in >> d;
  38. BOOST_TEST(!in.eof());
  39. BOOST_TEST(d == res);
  40. std::cout << str << " " << res << " " << d << std::endl;
  41. }
  42. }
  43. int main()
  44. {
  45. using namespace boost::chrono;
  46. using namespace boost;
  47. test_good("5000", 5000);
  48. std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
  49. test_good("5000 hours", hours(5000));
  50. std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
  51. test_good("5000 minutes", minutes(5000));
  52. test_good("5000 seconds", seconds(5000));
  53. test_fail("1.0 second", seconds(1));
  54. test_good("1.0 second", duration<float,ratio<1> >(1));
  55. /* BUG with DURATION_GET
  56. ../../../boost/math/common_factor_rt.hpp: In function 'RingType boost::math::detail::gcd_euclidean(RingType, RingType) [with RingType = long double]':
  57. ../../../boost/math/common_factor_rt.hpp:122: instantiated from 'IntegerType boost::math::detail::gcd_integer(const IntegerType&, const IntegerType&) [with IntegerType = long double]'
  58. ../../../boost/math/common_factor_rt.hpp:240: instantiated from 'T boost::math::detail::gcd_optimal_evaluator_helper_t<T, true, true>::operator()(const T&, const T&) [with T = long double]'
  59. ../../../boost/math/common_factor_rt.hpp:290: instantiated from 'T boost::math::detail::gcd_optimal_evaluator<T>::operator()(const T&, const T&) [with T = long double]'
  60. ../../../boost/math/common_factor_rt.hpp:442: instantiated from 'T boost::math::detail::gcd_optimal(const T&, const T&) [with T = long double]'
  61. ../../../boost/math/common_factor_rt.hpp:473: instantiated from 'typename boost::math::gcd_evaluator<IntegerType>::result_type boost::math::gcd_evaluator<IntegerType>::operator()(const IntegerType&, const IntegerType&) const [with IntegerType = long double]'
  62. ../../../boost/math/common_factor_rt.hpp:505: instantiated from 'IntegerType boost::math::gcd(const IntegerType&, const IntegerType&) [with IntegerType = long double]'
  63. ../../../boost/chrono/io/duration_get.hpp:239: instantiated from 'InputIterator boost::chrono::duration_get<CharT, InputIterator>::get(InputIterator, InputIterator, std::ios_base&, std::_Ios_Iostate&, boost::chrono::duration<Rep2, Period2>&, const CharT*, const CharT*) const [with Rep = double, Period = boost::ratio<1l, 1l>, CharT = char, InputIterator = std::istreambuf_iterator<char, std::char_traits<char> >]'
  64. ../../../boost/chrono/io/duration_get.hpp:294: instantiated from 'InputIterator boost::chrono::duration_get<CharT, InputIterator>::get(InputIterator, InputIterator, std::ios_base&, std::_Ios_Iostate&, boost::chrono::duration<Rep2, Period2>&) const [with Rep = double, Period = boost::ratio<1l, 1l>, CharT = char, InputIterator = std::istreambuf_iterator<char, std::char_traits<char> >]'
  65. ../../../boost/chrono/io/duration_io.hpp:593: instantiated from 'std::basic_istream<_CharT, _Traits>& boost::chrono::operator>>(std::basic_istream<_CharT, _Traits>&, boost::chrono::duration<Rep2, Period2>&) [with CharT = char, Traits = std::char_traits<char>, Rep = double, Period = boost::ratio<1l, 1l>]'
  66. io/duration_input.cpp:15: instantiated from 'void test_good(const char*, D) [with D = boost::chrono::duration<double, boost::ratio<1l, 1l> >]'
  67. io/duration_input.cpp:52: instantiated from here
  68. ../../../boost/math/common_factor_rt.hpp:102: error: invalid operands of types 'long double' and 'long double' to binary 'operator%'
  69. ../../../boost/math/common_factor_rt.hpp:102: error: in evaluation of 'operator%=(long double, long double)'
  70. ../../../boost/math/common_factor_rt.hpp:106: error: invalid operands of types 'long double' and 'long double' to binary 'operator%'
  71. ../../../boost/math/common_factor_rt.hpp:106: error: in evaluation of 'operator%=(long double, long double)'
  72. *
  73. */
  74. test_good("1 second", seconds(1));
  75. test_not_eof("1 second ", seconds(1));
  76. test_not_eof("1 seconde", seconds(1));
  77. test_good("1 seconds", seconds(1));
  78. test_good("0 seconds", seconds(0));
  79. test_good("-1 seconds", seconds(-1));
  80. test_good("5000 milliseconds", milliseconds(5000));
  81. test_good("5000 microseconds", microseconds(5000));
  82. test_good("5000 nanoseconds", nanoseconds(5000));
  83. test_good("5000 deciseconds", duration<boost::int_least64_t, deci> (5000));
  84. test_good("5000 [1/30]seconds", duration<boost::int_least64_t, ratio<1, 30> > (5000));
  85. test_good("5000 [1/30]second", duration<boost::int_least64_t, ratio<1, 30> > (5000));
  86. test_good("5000 h", hours(5000));
  87. #if BOOST_CHRONO_VERSION==2
  88. test_good("5000 min", minutes(5000));
  89. #else
  90. test_good("5000 m", minutes(5000));
  91. #endif
  92. test_good("5000 s", seconds(5000));
  93. test_good("5000 ms", milliseconds(5000));
  94. test_good("5000 ns", nanoseconds(5000));
  95. test_good("5000 ds", duration<boost::int_least64_t, deci> (5000));
  96. test_good("5000 [1/30]s", duration<boost::int_least64_t, ratio<1, 30> > (5000));
  97. test_not_eof("5000 [1/30]ss", duration<boost::int_least64_t, ratio<1, 30> > (5000));
  98. std::cout << __LINE__<< "*****" << std::endl;
  99. test_good("5000 milliseconds", seconds(5));
  100. test_good("5000 millisecond", seconds(5));
  101. test_good("5 milliseconds", nanoseconds(5000000));
  102. std::cout << __LINE__<< "*****" << std::endl;
  103. test_good("4000 ms", seconds(4));
  104. std::cout << __LINE__<< "*****" << std::endl;
  105. test_fail("3001 ms", seconds(3));
  106. std::cout << __LINE__<< "*****" << std::endl;
  107. test_fail("3001 ", milliseconds(3001));
  108. std::cout << __LINE__<< "*****" << std::endl;
  109. test_fail("one ms", milliseconds(1));
  110. test_fail("5000 millisecon", seconds(5));
  111. test_not_eof("3001 ms ", milliseconds(3001));
  112. return boost::report_errors();
  113. }