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

source code of libcxx/test/std/time/time.duration/time.duration.cast/duration_cast.pass.cpp