b744e159c56ac1c7b59966472dba815f9a44f2af
[platform/upstream/boost.git] / libs / chrono / test / duration / constructor_pass.cpp
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     BOOST_CONSTEXPR D d;
39     BOOST_CONSTEXPR_ASSERT(d.count() == typename D::rep());
40   }
41 }
42
43 template <class D, class R>
44 void
45 check_from_rep(R r)
46 {
47   {
48     D d(r);
49     BOOST_TEST(d.count() == r);
50   }
51 }
52
53 int main()
54 {
55     // exact conversions allowed for integral reps
56     {
57         boost::chrono::milliseconds ms(1);
58         boost::chrono::microseconds us = ms;
59         BOOST_TEST(us.count() == 1000);
60         {
61           BOOST_CONSTEXPR boost::chrono::milliseconds ms(1);
62           BOOST_CONSTEXPR boost::chrono::microseconds us = ms;
63           BOOST_CONSTEXPR_ASSERT(us.count() == 1000);
64         }
65     }
66     // inexact conversions allowed for floating point reps
67     {
68         boost::chrono::duration<double, boost::micro> us(1);
69         boost::chrono::duration<double, boost::milli> ms = us;
70         BOOST_TEST(ms.count() == 1./1000);
71         {
72           BOOST_CONSTEXPR boost::chrono::duration<double, boost::micro> us(1);
73           BOOST_CONSTEXPR boost::chrono::duration<double, boost::milli> ms = us;
74           BOOST_CONSTEXPR_ASSERT(ms.count() == 1./1000);
75         }
76     }
77     // Convert int to float
78     {
79         boost::chrono::duration<int> i(3);
80         boost::chrono::duration<double> d = i;
81         BOOST_TEST(d.count() == 3);
82         {
83           BOOST_CONSTEXPR boost::chrono::duration<int> i(3);
84           BOOST_CONSTEXPR boost::chrono::duration<double> d = i;
85           BOOST_CONSTEXPR_ASSERT(d.count() == 3);
86         }
87     }
88     // default constructor
89     {
90       check_default<boost::chrono::duration<Rep> >();
91     }
92     // constructor from rep
93     {
94         check_from_rep<boost::chrono::duration<int> >(5);
95         {
96           BOOST_CONSTEXPR boost::chrono::duration<int> d(5);
97           BOOST_CONSTEXPR_ASSERT(d.count() == 5);
98         }
99         check_from_rep<boost::chrono::duration<int, boost::ratio<3, 2> > >(5);
100         {
101           BOOST_CONSTEXPR boost::chrono::duration<int, boost::ratio<3, 2> > d(5);
102           BOOST_CONSTEXPR_ASSERT(d.count() == 5);
103         }
104         check_from_rep<boost::chrono::duration<Rep, boost::ratio<3, 2> > >(Rep(3));
105         {
106           BOOST_CONSTEXPR boost::chrono::duration<Rep, boost::ratio<3, 2> > d(Rep(3));
107           BOOST_CONSTEXPR_ASSERT(d.count() == Rep(3));
108         }
109         check_from_rep<boost::chrono::duration<double, boost::ratio<2, 3> > >(5.5);
110         {
111           BOOST_CONSTEXPR boost::chrono::duration<double, boost::ratio<3, 2> > d(5.5);
112           BOOST_CONSTEXPR_ASSERT(d.count() == 5.5);
113         }
114
115
116     }
117     // constructor from other rep
118     {
119         boost::chrono::duration<double> d(5);
120         BOOST_TEST(d.count() == 5);
121         {
122           BOOST_CONSTEXPR boost::chrono::duration<double> d(5);
123           BOOST_CONSTEXPR_ASSERT(d.count() == 5);
124         }
125     }
126
127     return boost::report_errors();
128 }