time_point_cast.pass.cpp revision 832b304076afcd832a5ffe95281b46a7abdaf869
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
10// <chrono>
11
12// time_point
13
14// template <class ToDuration, class Clock, class Duration>
15//   time_point<Clock, ToDuration>
16//   time_point_cast(const time_point<Clock, Duration>& t);
17
18#include <chrono>
19#include <type_traits>
20#include <cassert>
21
22template <class FromDuration, class ToDuration>
23void
24test(const FromDuration& df, const ToDuration& d)
25{
26    typedef std::chrono::system_clock Clock;
27    typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;
28    typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;
29    {
30    FromTimePoint f(df);
31    ToTimePoint t(d);
32    typedef decltype(std::chrono::time_point_cast<ToDuration>(f)) R;
33    static_assert((std::is_same<R, ToTimePoint>::value), "");
34    assert(std::chrono::time_point_cast<ToDuration>(f) == t);
35    }
36}
37
38template<class FromDuration, long long From, class ToDuration, long long To>
39void test_constexpr ()
40{
41    typedef std::chrono::system_clock Clock;
42    typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;
43    typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;
44    {
45    constexpr FromTimePoint f{FromDuration{From}};
46    constexpr ToTimePoint   t{ToDuration{To}};
47    static_assert(std::chrono::time_point_cast<ToDuration>(f) == t, "");
48    }
49
50}
51
52int main()
53{
54    test(std::chrono::milliseconds(7265000), std::chrono::hours(2));
55    test(std::chrono::milliseconds(7265000), std::chrono::minutes(121));
56    test(std::chrono::milliseconds(7265000), std::chrono::seconds(7265));
57    test(std::chrono::milliseconds(7265000), std::chrono::milliseconds(7265000));
58    test(std::chrono::milliseconds(7265000), std::chrono::microseconds(7265000000LL));
59    test(std::chrono::milliseconds(7265000), std::chrono::nanoseconds(7265000000000LL));
60    test(std::chrono::milliseconds(7265000),
61         std::chrono::duration<double, std::ratio<3600> >(7265./3600));
62    test(std::chrono::duration<int, std::ratio<2, 3> >(9),
63         std::chrono::duration<int, std::ratio<3, 5> >(10));
64#if _LIBCPP_STD_VER > 11
65    {
66    test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::hours,    2> ();
67    test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::minutes,121> ();
68    test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::seconds,7265> ();
69    test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::milliseconds,7265000> ();
70    test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::microseconds,7265000000LL> ();
71    test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::nanoseconds,7265000000000LL> ();
72    typedef std::chrono::duration<int, std::ratio<3, 5>> T1;
73    test_constexpr<std::chrono::duration<int, std::ratio<2, 3>>, 9, T1, 10> ();
74    }
75#endif
76}
77