duration_cast_pass.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // Adaptation to Boost of the libcxx
  10. // Copyright 2010 Vicente J. Botet Escriba
  11. // Distributed under the Boost Software License, Version 1.0.
  12. // See http://www.boost.org/LICENSE_1_0.txt
  13. #include <boost/chrono/duration.hpp>
  14. #include <boost/type_traits.hpp>
  15. #include <boost/detail/lightweight_test.hpp>
  16. #if !defined(BOOST_NO_CXX11_STATIC_ASSERT)
  17. #define NOTHING ""
  18. #endif
  19. #ifdef BOOST_NO_CXX11_CONSTEXPR
  20. #define BOOST_CONSTEXPR_ASSERT(C) BOOST_TEST(C)
  21. #else
  22. #include <boost/static_assert.hpp>
  23. #define BOOST_CONSTEXPR_ASSERT(C) BOOST_STATIC_ASSERT(C)
  24. #endif
  25. template <class ToDuration, class FromDuration>
  26. void
  27. test(const FromDuration& f, const ToDuration& d)
  28. {
  29. //~ #if defined(BOOST_NO_CXX11_DECLTYPE)
  30. //~ typedef BOOST_TYPEOF_TPL(boost::chrono::duration_cast<ToDuration>(f)) R;
  31. //~ #else
  32. //~ typedef decltype(boost::chrono::duration_cast<ToDuration>(f)) R;
  33. //~ #endif
  34. //~ BOOST_CHRONO_STATIC_ASSERT((boost::is_same<R, ToDuration>::value), NOTHING, (R, ToDuration));
  35. BOOST_TEST(boost::chrono::duration_cast<ToDuration>(f) == d);
  36. }
  37. int main()
  38. {
  39. test(boost::chrono::milliseconds(7265000), boost::chrono::hours(2));
  40. test(boost::chrono::milliseconds(7265000), boost::chrono::minutes(121));
  41. test(boost::chrono::milliseconds(7265000), boost::chrono::seconds(7265));
  42. test(boost::chrono::milliseconds(7265000), boost::chrono::milliseconds(7265000));
  43. test(boost::chrono::milliseconds(7265000), boost::chrono::microseconds(7265000000LL));
  44. test(boost::chrono::milliseconds(7265000), boost::chrono::nanoseconds(7265000000000LL));
  45. test(boost::chrono::milliseconds(7265000),
  46. boost::chrono::duration<double, boost::ratio<3600> >(7265./3600));
  47. test(boost::chrono::duration<int, boost::ratio<2, 3> >(9),
  48. boost::chrono::duration<int, boost::ratio<3, 5> >(10));
  49. {
  50. BOOST_CONSTEXPR boost::chrono::hours h = boost::chrono::duration_cast<boost::chrono::hours>(boost::chrono::milliseconds(7265000));
  51. BOOST_CONSTEXPR_ASSERT(h.count() == 2);
  52. }
  53. return boost::report_errors();
  54. }