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