xref: /llvm-project/libcxx/test/std/time/time.hms/time.hms.members/hours.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 chrono::hours hours() const noexcept;
15 
16 // Test values
17 // duration     hours   minutes seconds fractional
18 // 5000s            1       23      20      0
19 // 5000 minutes     83      20      0       0
20 // 123456789ms      34      17      36      789ms
21 // 123456789us      0       2       3       456789us
22 // 123456789ns      0       0       0       123456789ns
23 // 1000mfn          0       20      9       0.6 (6000/10000)
24 // 10000mfn         3       21      36      0
25 
26 
27 #include <chrono>
28 #include <cassert>
29 #include <ratio>
30 #include <utility>
31 
32 #include "test_macros.h"
33 
34 template <typename Duration>
check_hours(Duration d)35 constexpr long check_hours(Duration d)
36 {
37     using HMS = std::chrono::hh_mm_ss<Duration>;
38     ASSERT_SAME_TYPE(std::chrono::hours, decltype(std::declval<HMS>().hours()));
39     ASSERT_NOEXCEPT(                              std::declval<HMS>().hours());
40     return HMS(d).hours().count();
41 }
42 
main(int,char **)43 int main(int, char**)
44 {
45     using microfortnights = std::chrono::duration<int, std::ratio<756, 625>>;
46 
47     static_assert( check_hours(std::chrono::minutes( 1)) == 0, "");
48     static_assert( check_hours(std::chrono::minutes(-1)) == 0, "");
49 
50     assert( check_hours(std::chrono::seconds( 5000)) == 1);
51     assert( check_hours(std::chrono::seconds(-5000)) == 1);
52     assert( check_hours(std::chrono::minutes( 5000)) == 83);
53     assert( check_hours(std::chrono::minutes(-5000)) == 83);
54     assert( check_hours(std::chrono::hours( 11))     == 11);
55     assert( check_hours(std::chrono::hours(-11))     == 11);
56 
57     assert( check_hours(std::chrono::milliseconds( 123456789LL)) == 34);
58     assert( check_hours(std::chrono::milliseconds(-123456789LL)) == 34);
59     assert( check_hours(std::chrono::microseconds( 123456789LL)) ==  0);
60     assert( check_hours(std::chrono::microseconds(-123456789LL)) ==  0);
61     assert( check_hours(std::chrono::nanoseconds( 123456789LL))  ==  0);
62     assert( check_hours(std::chrono::nanoseconds(-123456789LL))  ==  0);
63 
64     assert( check_hours(microfortnights(  1000)) == 0);
65     assert( check_hours(microfortnights( -1000)) == 0);
66     assert( check_hours(microfortnights( 10000)) == 3);
67     assert( check_hours(microfortnights(-10000)) == 3);
68 
69     return 0;
70 }
71