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 // duration
12 
13 // template <class ToDuration, class Rep, class Period>
14 //   constexpr
15 //   ToDuration
16 //   duration_cast(const duration<Rep, Period>& d);
17 
18 #include <chrono>
19 #include <type_traits>
20 #include <cassert>
21 
22 #include "test_macros.h"
23 
24 template <class ToDuration, class FromDuration>
25 void
test(const FromDuration & f,const ToDuration & d)26 test(const FromDuration& f, const ToDuration& d)
27 {
28     {
29     typedef decltype(std::chrono::duration_cast<ToDuration>(f)) R;
30     static_assert((std::is_same<R, ToDuration>::value), "");
31     assert(std::chrono::duration_cast<ToDuration>(f) == d);
32     }
33 }
34 
main(int,char **)35 int main(int, char**)
36 {
37     test(std::chrono::milliseconds(7265000), std::chrono::hours(2));
38     test(std::chrono::milliseconds(7265000), std::chrono::minutes(121));
39     test(std::chrono::milliseconds(7265000), std::chrono::seconds(7265));
40     test(std::chrono::milliseconds(7265000), std::chrono::milliseconds(7265000));
41     test(std::chrono::milliseconds(7265000), std::chrono::microseconds(7265000000LL));
42     test(std::chrono::milliseconds(7265000), std::chrono::nanoseconds(7265000000000LL));
43     test(std::chrono::milliseconds(7265000),
44          std::chrono::duration<double, std::ratio<3600> >(7265./3600));
45     test(std::chrono::duration<int, std::ratio<2, 3> >(9),
46          std::chrono::duration<int, std::ratio<3, 5> >(10));
47 #if TEST_STD_VER >= 11
48     {
49     constexpr std::chrono::hours h = std::chrono::duration_cast<std::chrono::hours>(std::chrono::milliseconds(7265000));
50     static_assert(h.count() == 2, "");
51     }
52 #endif
53 
54   return 0;
55 }
56