| 1 | // Copyright 2011 Vicente J. Botet Escriba |
| 2 | // Distributed under the Boost Software License, Version 1.0. |
| 3 | // See http://www.boost.org/LICENSE_1_0.txt |
| 4 | |
| 5 | #include <boost/chrono/chrono_io.hpp> |
| 6 | #include <sstream> |
| 7 | #include <boost/detail/lightweight_test.hpp> |
| 8 | |
| 9 | template<typename D> |
| 10 | void test_good_prefix(const char* str, D d) |
| 11 | { |
| 12 | std::ostringstream out; |
| 13 | out << d; |
| 14 | BOOST_TEST(out.good()); |
| 15 | |
| 16 | BOOST_TEST(out.str() == str); |
| 17 | } |
| 18 | |
| 19 | template<typename D> |
| 20 | void test_good_symbol(const char* str, D d) |
| 21 | { |
| 22 | std::ostringstream out; |
| 23 | #if BOOST_CHRONO_VERSION==2 |
| 24 | out << boost::chrono::duration_fmt(boost::chrono::duration_style::symbol) << d; |
| 25 | #else |
| 26 | out << boost::chrono::duration_short << d; |
| 27 | #endif |
| 28 | BOOST_TEST(out.good()); |
| 29 | BOOST_TEST_EQ(out.str(), str); |
| 30 | } |
| 31 | #if BOOST_CHRONO_VERSION==2 |
| 32 | |
| 33 | template<typename D> |
| 34 | void test_good(const char* str, D d, boost::chrono::duration_style style) |
| 35 | { |
| 36 | std::ostringstream out; |
| 37 | |
| 38 | out << boost::chrono::duration_fmt(style) << d; |
| 39 | BOOST_TEST(out.good()); |
| 40 | BOOST_TEST(out.str() == str); |
| 41 | } |
| 42 | |
| 43 | template<typename D> |
| 44 | void test_state_saver(const char* str, const char* str2, D d, boost::chrono::duration_style style) |
| 45 | { |
| 46 | std::ostringstream out; |
| 47 | { |
| 48 | boost::chrono::duration_style_io_saver ios(out); |
| 49 | out << boost::chrono::duration_fmt(style) << d; |
| 50 | BOOST_TEST(out.good()); |
| 51 | BOOST_TEST(out.str() == str); |
| 52 | } |
| 53 | out << " " << d; |
| 54 | BOOST_TEST(out.good()); |
| 55 | BOOST_TEST(out.str() == str2); |
| 56 | } |
| 57 | |
| 58 | template<typename D> |
| 59 | void test_state_saver2(const char* str, const char* str2, D d, boost::chrono::duration_style style) |
| 60 | { |
| 61 | std::ostringstream out; |
| 62 | { |
| 63 | boost::chrono::duration_style_io_saver ios(out, style); |
| 64 | out << d; |
| 65 | BOOST_TEST(out.good()); |
| 66 | BOOST_TEST(out.str() == str); |
| 67 | } |
| 68 | out << " " << d; |
| 69 | BOOST_TEST(out.good()); |
| 70 | BOOST_TEST(out.str() == str2); |
| 71 | } |
| 72 | |
| 73 | #endif |
| 74 | |
| 75 | int main() |
| 76 | { |
| 77 | using namespace boost::chrono; |
| 78 | using namespace boost; |
| 79 | |
| 80 | test_good_prefix(str: "5000 hours" , d: hours(5000)); |
| 81 | test_good_prefix(str: "5000 minutes" , d: minutes(5000)); |
| 82 | test_good_prefix(str: "5000 seconds" , d: seconds(5000)); |
| 83 | test_good_prefix(str: "0 seconds" , d: seconds(0)); |
| 84 | test_good_prefix(str: "1 second" , d: seconds(1)); |
| 85 | test_good_prefix(str: "-1 second" , d: seconds(-1)); |
| 86 | test_good_prefix(str: "5000 milliseconds" , d: milliseconds(5000)); |
| 87 | test_good_prefix(str: "5000 microseconds" , d: microseconds(5000)); |
| 88 | test_good_prefix(str: "5000 nanoseconds" , d: nanoseconds(5000)); |
| 89 | test_good_prefix(str: "5000 deciseconds" , d: duration<boost::int_least64_t, deci> (5000)); |
| 90 | test_good_prefix(str: "5000 [1/30]seconds" , d: duration<boost::int_least64_t, ratio<1, 30> > (5000)); |
| 91 | |
| 92 | test_good_symbol(str: "5000 h" , d: hours(5000)); |
| 93 | #if BOOST_CHRONO_VERSION==2 |
| 94 | test_good_symbol("5000 min" , minutes(5000)); |
| 95 | #else |
| 96 | test_good_symbol(str: "5000 m" , d: minutes(5000)); |
| 97 | #endif |
| 98 | test_good_symbol(str: "5000 s" , d: seconds(5000)); |
| 99 | test_good_symbol(str: "5000 ms" , d: milliseconds(5000)); |
| 100 | test_good_symbol(str: "5000 ns" , d: nanoseconds(5000)); |
| 101 | test_good_symbol(str: "5000 ds" , d: duration<boost::int_least64_t, deci> (5000)); |
| 102 | test_good_symbol(str: "5000 [1/30]s" , d: duration<boost::int_least64_t, ratio<1, 30> > (5000)); |
| 103 | |
| 104 | #if BOOST_CHRONO_VERSION==2 |
| 105 | test_good("5000 hours" , hours(5000), duration_style::prefix); |
| 106 | test_good("5000 h" , hours(5000), duration_style::symbol); |
| 107 | test_state_saver("5000 h" , "5000 h 5000 hours" , hours(5000), duration_style::symbol); |
| 108 | test_state_saver2("5000 h" , "5000 h 5000 hours" , hours(5000), duration_style::symbol); |
| 109 | #endif |
| 110 | |
| 111 | return boost::report_errors(); |
| 112 | |
| 113 | } |
| 114 | |
| 115 | |