constructor_pass.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/detail/lightweight_test.hpp>
  15. #include "../rep.h"
  16. #include <iostream>
  17. #ifdef BOOST_NO_CXX11_CONSTEXPR
  18. #define BOOST_CONSTEXPR_ASSERT(C) BOOST_TEST(C)
  19. #else
  20. #include <boost/static_assert.hpp>
  21. #define BOOST_CONSTEXPR_ASSERT(C) BOOST_STATIC_ASSERT(C)
  22. #endif
  23. template <class D>
  24. void
  25. check_default()
  26. {
  27. {
  28. D d;
  29. BOOST_TEST(d.count() == typename D::rep());
  30. }
  31. }
  32. template <class D>
  33. void
  34. check_constexpr()
  35. {
  36. BOOST_CONSTEXPR D d(0);
  37. BOOST_CONSTEXPR_ASSERT(d.count() == typename D::rep());
  38. }
  39. template <class D, class R>
  40. void
  41. check_from_rep(R r)
  42. {
  43. {
  44. D d(r);
  45. BOOST_TEST(d.count() == r);
  46. }
  47. }
  48. int main()
  49. {
  50. // exact conversions allowed for integral reps
  51. {
  52. boost::chrono::milliseconds ms(1);
  53. boost::chrono::microseconds us = ms;
  54. BOOST_TEST(us.count() == 1000);
  55. {
  56. BOOST_CONSTEXPR boost::chrono::milliseconds ms(1);
  57. BOOST_CONSTEXPR boost::chrono::microseconds us = ms;
  58. BOOST_CONSTEXPR_ASSERT(us.count() == 1000);
  59. }
  60. }
  61. // inexact conversions allowed for floating point reps
  62. {
  63. boost::chrono::duration<double, boost::micro> us(1);
  64. boost::chrono::duration<double, boost::milli> ms = us;
  65. BOOST_TEST(ms.count() == 1./1000);
  66. {
  67. BOOST_CONSTEXPR boost::chrono::duration<double, boost::micro> us(1);
  68. BOOST_CONSTEXPR boost::chrono::duration<double, boost::milli> ms = us;
  69. BOOST_CONSTEXPR_ASSERT(ms.count() == 1./1000);
  70. }
  71. }
  72. // Convert int to float
  73. {
  74. boost::chrono::duration<int> i(3);
  75. boost::chrono::duration<double> d = i;
  76. BOOST_TEST(d.count() == 3);
  77. {
  78. BOOST_CONSTEXPR boost::chrono::duration<int> i(3);
  79. BOOST_CONSTEXPR boost::chrono::duration<double> d = i;
  80. BOOST_CONSTEXPR_ASSERT(d.count() == 3);
  81. }
  82. }
  83. // default constructor
  84. {
  85. check_default<boost::chrono::duration<Rep> >();
  86. }
  87. {
  88. check_constexpr<boost::chrono::duration<int> >();
  89. }
  90. // constructor from rep
  91. {
  92. check_from_rep<boost::chrono::duration<int> >(5);
  93. {
  94. BOOST_CONSTEXPR boost::chrono::duration<int> d(5);
  95. BOOST_CONSTEXPR_ASSERT(d.count() == 5);
  96. }
  97. check_from_rep<boost::chrono::duration<int, boost::ratio<3, 2> > >(5);
  98. {
  99. BOOST_CONSTEXPR boost::chrono::duration<int, boost::ratio<3, 2> > d(5);
  100. BOOST_CONSTEXPR_ASSERT(d.count() == 5);
  101. }
  102. check_from_rep<boost::chrono::duration<Rep, boost::ratio<3, 2> > >(Rep(3));
  103. {
  104. BOOST_CONSTEXPR boost::chrono::duration<Rep, boost::ratio<3, 2> > d(Rep(3));
  105. BOOST_CONSTEXPR_ASSERT(d.count() == Rep(3));
  106. }
  107. check_from_rep<boost::chrono::duration<double, boost::ratio<2, 3> > >(5.5);
  108. {
  109. BOOST_CONSTEXPR boost::chrono::duration<double, boost::ratio<3, 2> > d(5.5);
  110. BOOST_CONSTEXPR_ASSERT(d.count() == 5.5);
  111. }
  112. }
  113. // constructor from other rep
  114. {
  115. boost::chrono::duration<double> d(5);
  116. BOOST_TEST(d.count() == 5);
  117. {
  118. BOOST_CONSTEXPR boost::chrono::duration<double> d(5);
  119. BOOST_CONSTEXPR_ASSERT(d.count() == 5);
  120. }
  121. }
  122. return boost::report_errors();
  123. }