xref: /llvm-project/libcxx/test/std/time/time.point/time.point.cast/time_point_cast.pass.cpp (revision 7738db2c06b177fe916ac1bcf3633449b3c1fca9)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <chrono>
10 
11 // time_point
12 
13 // template <class ToDuration, class Clock, class Duration>
14 //   time_point<Clock, ToDuration>
15 //   time_point_cast(const time_point<Clock, Duration>& t);
16 
17 #include <chrono>
18 #include <cassert>
19 #include <ratio>
20 #include <type_traits>
21 
22 #include "test_macros.h"
23 
24 template <class FromDuration, class ToDuration>
25 void
test(const FromDuration & df,const ToDuration & d)26 test(const FromDuration& df, const ToDuration& d)
27 {
28     typedef std::chrono::system_clock Clock;
29     typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;
30     typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;
31     {
32     FromTimePoint f(df);
33     ToTimePoint t(d);
34     typedef decltype(std::chrono::time_point_cast<ToDuration>(f)) R;
35     static_assert((std::is_same<R, ToTimePoint>::value), "");
36     assert(std::chrono::time_point_cast<ToDuration>(f) == t);
37     }
38 }
39 
40 #if TEST_STD_VER > 11
41 
42 template<class FromDuration, long long From, class ToDuration, long long To>
test_constexpr()43 void test_constexpr ()
44 {
45     typedef std::chrono::system_clock Clock;
46     typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;
47     typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;
48     {
49     constexpr FromTimePoint f{FromDuration{From}};
50     constexpr ToTimePoint   t{ToDuration{To}};
51     static_assert(std::chrono::time_point_cast<ToDuration>(f) == t, "");
52     }
53 
54 }
55 
56 #endif
57 
main(int,char **)58 int main(int, char**)
59 {
60     test(std::chrono::milliseconds(7265000), std::chrono::hours(2));
61     test(std::chrono::milliseconds(7265000), std::chrono::minutes(121));
62     test(std::chrono::milliseconds(7265000), std::chrono::seconds(7265));
63     test(std::chrono::milliseconds(7265000), std::chrono::milliseconds(7265000));
64     test(std::chrono::milliseconds(7265000), std::chrono::microseconds(7265000000LL));
65     test(std::chrono::milliseconds(7265000), std::chrono::nanoseconds(7265000000000LL));
66     test(std::chrono::milliseconds(7265000),
67          std::chrono::duration<double, std::ratio<3600> >(7265./3600));
68     test(std::chrono::duration<int, std::ratio<2, 3> >(9),
69          std::chrono::duration<int, std::ratio<3, 5> >(10));
70 #if TEST_STD_VER > 11
71     {
72     test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::hours,    2> ();
73     test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::minutes,121> ();
74     test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::seconds,7265> ();
75     test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::milliseconds,7265000> ();
76     test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::microseconds,7265000000LL> ();
77     test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::nanoseconds,7265000000000LL> ();
78     typedef std::chrono::duration<int, std::ratio<3, 5>> T1;
79     test_constexpr<std::chrono::duration<int, std::ratio<2, 3>>, 9, T1, 10> ();
80     }
81 #endif
82 
83   return 0;
84 }
85