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