xref: /llvm-project/libcxx/test/std/time/time.hms/time.hms.members/to_duration.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 // UNSUPPORTED: c++03, c++11, c++14, c++17
9 // <chrono>
10 
11 // template <class Duration>
12 // class hh_mm_ss
13 //
14 // constexpr precision to_duration() const noexcept;
15 //
16 // See the table in hours.pass.cpp for correspondence between the magic values used below
17 
18 #include <chrono>
19 #include <cassert>
20 #include <ratio>
21 #include <utility>
22 
23 #include "test_macros.h"
24 
25 template <typename Duration>
check_duration(Duration d)26 constexpr long long check_duration(Duration d)
27 {
28     using HMS = std::chrono::hh_mm_ss<Duration>;
29     ASSERT_SAME_TYPE(typename HMS::precision, decltype(std::declval<HMS>().to_duration()));
30     ASSERT_NOEXCEPT(                                   std::declval<HMS>().to_duration());
31 
32     return HMS(d).to_duration().count();
33 }
34 
main(int,char **)35 int main(int, char**)
36 {
37     using microfortnights = std::chrono::duration<int, std::ratio<756, 625>>;
38 
39     static_assert( check_duration(std::chrono::minutes( 1)) ==  60, "");
40     static_assert( check_duration(std::chrono::minutes(-1)) == -60, "");
41 
42     assert( check_duration(std::chrono::seconds( 5000)) ==    5000LL);
43     assert( check_duration(std::chrono::seconds(-5000)) ==   -5000LL);
44     assert( check_duration(std::chrono::minutes( 5000)) ==  300000LL);
45     assert( check_duration(std::chrono::minutes(-5000)) == -300000LL);
46     assert( check_duration(std::chrono::hours( 11))     ==   39600LL);
47     assert( check_duration(std::chrono::hours(-11))     ==  -39600LL);
48 
49     assert( check_duration(std::chrono::milliseconds( 123456789LL)) ==  123456789LL);
50     assert( check_duration(std::chrono::milliseconds(-123456789LL)) == -123456789LL);
51     assert( check_duration(std::chrono::microseconds( 123456789LL)) ==  123456789LL);
52     assert( check_duration(std::chrono::microseconds(-123456789LL)) == -123456789LL);
53     assert( check_duration(std::chrono::nanoseconds( 123456789LL))  ==  123456789LL);
54     assert( check_duration(std::chrono::nanoseconds(-123456789LL))  == -123456789LL);
55 
56     assert( check_duration(microfortnights(  1000)) ==   12096000);
57     assert( check_duration(microfortnights( -1000)) ==  -12096000);
58     assert( check_duration(microfortnights( 10000)) ==  120960000);
59     assert( check_duration(microfortnights(-10000)) == -120960000);
60 
61     return 0;
62 }
63