duration_output.cpp 3.3 KB

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