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
5 #include <boost/chrono/chrono_io.hpp>
6 //#include <boost/chrono/io/duration_units.hpp>
7 #include <sstream>
8 #include <boost/detail/lightweight_test.hpp>
9
10
11 template<typename D>
test_good(const char * str,D res)12 void test_good(const char* str, D res)
13 {
14 std::istringstream in(str);
15 D d(0);
16 in >> d;
17 BOOST_TEST(in.eof());
18 BOOST_TEST(!in.fail());
19 BOOST_TEST(d == res);
20 std::cout << str << " " << res << " " << d << std::endl;
21 }
22
23 template<typename DFail>
test_fail(const char * str,DFail res)24 void test_fail(const char* str, DFail res)
25 {
26 {
27 std::istringstream in(str);
28 DFail d = DFail::zero();
29 in >> d;
30 BOOST_TEST(in.fail());
31 BOOST_TEST(d == DFail::zero());
32 std::cout << str << " " << res << " " << d << std::endl;
33 }
34 }
35
36 template<typename D>
test_not_eof(const char * str,D res)37 void test_not_eof(const char* str, D res)
38 {
39 {
40 std::istringstream in(str);
41 D d = D::zero();
42 in >> d;
43 BOOST_TEST(!in.eof());
44 BOOST_TEST(d == res);
45 std::cout << str << " " << res << " " << d << std::endl;
46 }
47 }
main()48 int main()
49 {
50 using namespace boost::chrono;
51 using namespace boost;
52
53 test_good("5000", 5000);
54
55 std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
56 test_good("5000 hours", hours(5000));
57 std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
58 test_good("5000 minutes", minutes(5000));
59 test_good("5000 seconds", seconds(5000));
60 test_fail("1.0 second", seconds(1));
61
62 test_good("1.0 second", duration<float,ratio<1> >(1));
63 /* BUG with DURATION_GET
64 ../../../boost/math/common_factor_rt.hpp: In function 'RingType boost::math::detail::gcd_euclidean(RingType, RingType) [with RingType = long double]':
65 ../../../boost/math/common_factor_rt.hpp:122: instantiated from 'IntegerType boost::math::detail::gcd_integer(const IntegerType&, const IntegerType&) [with IntegerType = long double]'
66 ../../../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]'
67 ../../../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]'
68 ../../../boost/math/common_factor_rt.hpp:442: instantiated from 'T boost::math::detail::gcd_optimal(const T&, const T&) [with T = long double]'
69 ../../../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]'
70 ../../../boost/math/common_factor_rt.hpp:505: instantiated from 'IntegerType boost::math::gcd(const IntegerType&, const IntegerType&) [with IntegerType = long double]'
71 ../../../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> >]'
72 ../../../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> >]'
73 ../../../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>]'
74 io/duration_input.cpp:15: instantiated from 'void test_good(const char*, D) [with D = boost::chrono::duration<double, boost::ratio<1l, 1l> >]'
75 io/duration_input.cpp:52: instantiated from here
76 ../../../boost/math/common_factor_rt.hpp:102: error: invalid operands of types 'long double' and 'long double' to binary 'operator%'
77 ../../../boost/math/common_factor_rt.hpp:102: error: in evaluation of 'operator%=(long double, long double)'
78 ../../../boost/math/common_factor_rt.hpp:106: error: invalid operands of types 'long double' and 'long double' to binary 'operator%'
79 ../../../boost/math/common_factor_rt.hpp:106: error: in evaluation of 'operator%=(long double, long double)'
80 *
81 */
82 test_good("1 second", seconds(1));
83 test_not_eof("1 second ", seconds(1));
84 test_not_eof("1 seconde", seconds(1));
85 test_good("1 seconds", seconds(1));
86 test_good("0 seconds", seconds(0));
87 test_good("-1 seconds", seconds(-1));
88 test_good("5000 milliseconds", milliseconds(5000));
89 test_good("5000 microseconds", microseconds(5000));
90 test_good("5000 nanoseconds", nanoseconds(5000));
91 test_good("5000 deciseconds", duration<boost::int_least64_t, deci> (5000));
92 test_good("5000 [1/30]seconds", duration<boost::int_least64_t, ratio<1, 30> > (5000));
93 test_good("5000 [1/30]second", duration<boost::int_least64_t, ratio<1, 30> > (5000));
94 test_good("5000 h", hours(5000));
95 #if BOOST_CHRONO_VERSION==2
96 test_good("5000 min", minutes(5000));
97 #else
98 test_good("5000 m", minutes(5000));
99 #endif
100 test_good("5000 s", seconds(5000));
101 test_good("5000 ms", milliseconds(5000));
102 test_good("5000 ns", nanoseconds(5000));
103 test_good("5000 ds", duration<boost::int_least64_t, deci> (5000));
104 test_good("5000 [1/30]s", duration<boost::int_least64_t, ratio<1, 30> > (5000));
105 test_not_eof("5000 [1/30]ss", duration<boost::int_least64_t, ratio<1, 30> > (5000));
106 std::cout << __LINE__<< "*****" << std::endl;
107 test_good("5000 milliseconds", seconds(5));
108 test_good("5000 millisecond", seconds(5));
109 test_good("5 milliseconds", nanoseconds(5000000));
110 std::cout << __LINE__<< "*****" << std::endl;
111 test_good("4000 ms", seconds(4));
112 std::cout << __LINE__<< "*****" << std::endl;
113 test_fail("3001 ms", seconds(3));
114 std::cout << __LINE__<< "*****" << std::endl;
115 test_fail("3001 ", milliseconds(3001));
116 std::cout << __LINE__<< "*****" << std::endl;
117 test_fail("one ms", milliseconds(1));
118 test_fail("5000 millisecon", seconds(5));
119 test_not_eof("3001 ms ", milliseconds(3001));
120
121 return boost::report_errors();
122
123 }
124
125