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
11template<typename D>
12void 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
23template<typename DFail>
24void 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
36template<typename D>
37void 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}
48int main()
49{
50 using namespace boost::chrono;
51 using namespace boost;
52
53 test_good(str: "5000", res: 5000);
54
55 std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
56 test_good(str: "5000 hours", res: hours(5000));
57 std::cerr << __FILE__ << "[" << __LINE__ << "]"<< std::endl;
58 test_good(str: "5000 minutes", res: minutes(5000));
59 test_good(str: "5000 seconds", res: seconds(5000));
60 test_fail(str: "1.0 second", res: seconds(1));
61
62 test_good(str: "1.0 second", res: 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>]'
74io/duration_input.cpp:15: instantiated from 'void test_good(const char*, D) [with D = boost::chrono::duration<double, boost::ratio<1l, 1l> >]'
75io/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(str: "1 second", res: seconds(1));
83 test_not_eof(str: "1 second ", res: seconds(1));
84 test_not_eof(str: "1 seconde", res: seconds(1));
85 test_good(str: "1 seconds", res: seconds(1));
86 test_good(str: "0 seconds", res: seconds(0));
87 test_good(str: "-1 seconds", res: seconds(-1));
88 test_good(str: "5000 milliseconds", res: milliseconds(5000));
89 test_good(str: "5000 microseconds", res: microseconds(5000));
90 test_good(str: "5000 nanoseconds", res: nanoseconds(5000));
91 test_good(str: "5000 deciseconds", res: duration<boost::int_least64_t, deci> (5000));
92 test_good(str: "5000 [1/30]seconds", res: duration<boost::int_least64_t, ratio<1, 30> > (5000));
93 test_good(str: "5000 [1/30]second", res: duration<boost::int_least64_t, ratio<1, 30> > (5000));
94 test_good(str: "5000 h", res: hours(5000));
95#if BOOST_CHRONO_VERSION==2
96 test_good("5000 min", minutes(5000));
97#else
98 test_good(str: "5000 m", res: minutes(5000));
99#endif
100 test_good(str: "5000 s", res: seconds(5000));
101 test_good(str: "5000 ms", res: milliseconds(5000));
102 test_good(str: "5000 ns", res: nanoseconds(5000));
103 test_good(str: "5000 ds", res: duration<boost::int_least64_t, deci> (5000));
104 test_good(str: "5000 [1/30]s", res: duration<boost::int_least64_t, ratio<1, 30> > (5000));
105 test_not_eof(str: "5000 [1/30]ss", res: duration<boost::int_least64_t, ratio<1, 30> > (5000));
106 std::cout << __LINE__<< "*****" << std::endl;
107 test_good(str: "5000 milliseconds", res: seconds(5));
108 test_good(str: "5000 millisecond", res: seconds(5));
109 test_good(str: "5 milliseconds", res: nanoseconds(5000000));
110 std::cout << __LINE__<< "*****" << std::endl;
111 test_good(str: "4000 ms", res: seconds(4));
112 std::cout << __LINE__<< "*****" << std::endl;
113 test_fail(str: "3001 ms", res: seconds(3));
114 std::cout << __LINE__<< "*****" << std::endl;
115 test_fail(str: "3001 ", res: milliseconds(3001));
116 std::cout << __LINE__<< "*****" << std::endl;
117 test_fail(str: "one ms", res: milliseconds(1));
118 test_fail(str: "5000 millisecon", res: seconds(5));
119 test_not_eof(str: "3001 ms ", res: milliseconds(3001));
120
121 return boost::report_errors();
122
123}
124
125

source code of boost/libs/chrono/test/io/duration_input.cpp