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++98, c++03, c++11, c++14, c++17
9 // <chrono>
10
11 // template <class Duration>
12 // class hh_mm_ss
13 // {
14 // public:
15 // static unsigned constexpr fractional_width = see below;
16 // using precision = see below;
17 //
18 // precision is duration<common_type_t<Duration::rep, seconds::rep>,
19 // ratio<1, 10^^fractional_width>>
20
21 #include <chrono>
22 #include <cassert>
23
24 #include "test_macros.h"
25
26 constexpr unsigned long long powers[] = {
27 1ULL,
28 10ULL,
29 100ULL,
30 1000ULL,
31 10000ULL,
32 100000ULL,
33 1000000ULL,
34 10000000ULL,
35 100000000ULL,
36 1000000000ULL,
37 10000000000ULL,
38 100000000000ULL,
39 1000000000000ULL,
40 10000000000000ULL,
41 100000000000000ULL,
42 1000000000000000ULL,
43 10000000000000000ULL,
44 100000000000000000ULL,
45 1000000000000000000ULL,
46 10000000000000000000ULL
47 };
48
49 template <typename Duration, unsigned width>
check_precision()50 constexpr bool check_precision()
51 {
52 using HMS = std::chrono::hh_mm_ss<Duration>;
53 using CT = std::common_type_t<typename Duration::rep, std::chrono::seconds::rep>;
54 using Pre = std::chrono::duration<CT, std::ratio<1, powers[width]>>;
55 return std::is_same_v<typename HMS::precision, Pre>;
56 }
57
main(int,char **)58 int main(int, char**)
59 {
60 using microfortnights = std::chrono::duration<int, std::ratio<756, 625>>;
61
62 static_assert( check_precision<std::chrono::hours, 0>(), "");
63 static_assert( check_precision<std::chrono::minutes, 0>(), "");
64 static_assert( check_precision<std::chrono::seconds, 0>(), "");
65 static_assert( check_precision<std::chrono::milliseconds, 3>(), "");
66 static_assert( check_precision<std::chrono::microseconds, 6>(), "");
67 static_assert( check_precision<std::chrono::nanoseconds, 9>(), "");
68 static_assert( check_precision<std::chrono::duration<int, std::ratio< 1, 2>>, 1>(), "");
69 static_assert( check_precision<std::chrono::duration<int, std::ratio< 1, 3>>, 6>(), "");
70 static_assert( check_precision<std::chrono::duration<int, std::ratio< 1, 4>>, 2>(), "");
71 static_assert( check_precision<std::chrono::duration<int, std::ratio< 1, 5>>, 1>(), "");
72 static_assert( check_precision<std::chrono::duration<int, std::ratio< 1, 6>>, 6>(), "");
73 static_assert( check_precision<std::chrono::duration<int, std::ratio< 1, 7>>, 6>(), "");
74 static_assert( check_precision<std::chrono::duration<int, std::ratio< 1, 8>>, 3>(), "");
75 static_assert( check_precision<std::chrono::duration<int, std::ratio< 1, 9>>, 6>(), "");
76 static_assert( check_precision<std::chrono::duration<int, std::ratio< 1, 10>>, 1>(), "");
77 static_assert( check_precision<microfortnights, 4>(), "");
78
79 return 0;
80 }
81