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