Kaido Kert | 56d7c4e | 2024-04-13 12:59:27 -0700 | [diff] [blame] | 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 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 10 | // UNSUPPORTED: no-localization |
| 11 | // UNSUPPORTED: libcpp-has-no-incomplete-format |
| 12 | |
| 13 | // TODO FMT Evaluate gcc-12 status |
| 14 | // UNSUPPORTED: gcc-12 |
| 15 | |
| 16 | // TODO FMT Investigate Windows issues. |
| 17 | // UNSUPPORTED: msvc, target={{.+}}-windows-gnu |
| 18 | |
| 19 | // XFAIL: availability-fp_to_chars-missing |
| 20 | |
| 21 | // REQUIRES: locale.fr_FR.UTF-8 |
| 22 | // REQUIRES: locale.ja_JP.UTF-8 |
| 23 | |
| 24 | // <chrono> |
| 25 | |
| 26 | // class hh_mm_ss; |
| 27 | |
| 28 | // template<class charT, class traits, class Duration> |
| 29 | // basic_ostream<charT, traits>& |
| 30 | // operator<<(basic_ostream<charT, traits>& os, const hh_mm_ss<Duration>& hms); |
| 31 | |
| 32 | #include <cassert> |
| 33 | #include <chrono> |
| 34 | #include <ratio> |
| 35 | #include <sstream> |
| 36 | |
| 37 | #include "make_string.h" |
| 38 | #include "platform_support.h" // locale name macros |
| 39 | #include "test_macros.h" |
| 40 | |
| 41 | #define SV(S) MAKE_STRING_VIEW(CharT, S) |
| 42 | |
| 43 | template <class CharT, class Duration> |
| 44 | static std::basic_string<CharT> stream_c_locale(std::chrono::hh_mm_ss<Duration> hms) { |
| 45 | std::basic_stringstream<CharT> sstr; |
| 46 | sstr << hms; |
| 47 | return sstr.str(); |
| 48 | } |
| 49 | |
| 50 | template <class CharT, class Duration> |
| 51 | static std::basic_string<CharT> stream_fr_FR_locale(std::chrono::hh_mm_ss<Duration> hms) { |
| 52 | std::basic_stringstream<CharT> sstr; |
| 53 | const std::locale locale(LOCALE_fr_FR_UTF_8); |
| 54 | sstr.imbue(locale); |
| 55 | sstr << hms; |
| 56 | return sstr.str(); |
| 57 | } |
| 58 | |
| 59 | template <class CharT, class Duration> |
| 60 | static std::basic_string<CharT> stream_ja_JP_locale(std::chrono::hh_mm_ss<Duration> hms) { |
| 61 | std::basic_stringstream<CharT> sstr; |
| 62 | const std::locale locale(LOCALE_ja_JP_UTF_8); |
| 63 | sstr.imbue(locale); |
| 64 | sstr << hms; |
| 65 | return sstr.str(); |
| 66 | } |
| 67 | |
| 68 | template <class CharT> |
| 69 | static void test() { |
| 70 | // Note std::atto can't be tested since the ratio conversion from std::atto |
| 71 | // std::chrono::seconds to std::chrono::hours overflows when intmax_t is a |
| 72 | // 64-bit type. This is a limitiation in the constructor of |
| 73 | // std::chrono::hh_mm_ss. |
| 74 | |
| 75 | // C locale - integral power of 10 ratios |
| 76 | assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::femto>{1'234'567'890}}) == |
| 77 | SV("00:00:00.000001234567890")); |
| 78 | assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::pico>{1'234'567'890}}) == |
| 79 | SV("00:00:00.001234567890")); |
| 80 | assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::nano>{1'234'567'890}}) == |
| 81 | SV("00:00:01.234567890")); |
| 82 | assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::micro>{1'234'567}}) == |
| 83 | SV("00:00:01.234567")); |
| 84 | assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::milli>{123'456}}) == |
| 85 | SV("00:02:03.456")); |
| 86 | assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::centi>{12'345}}) == |
| 87 | SV("00:02:03.45")); |
| 88 | assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::deci>{1'234}}) == |
| 89 | SV("00:02:03.4")); |
| 90 | |
| 91 | assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t>{123}}) == SV("00:02:03")); |
| 92 | |
| 93 | assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::deca>{-366}}) == |
| 94 | SV("-01:01:00")); |
| 95 | assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::hecto>{-72}}) == |
| 96 | SV("-02:00:00")); |
| 97 | assert(stream_c_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::kilo>{-86}}) == |
| 98 | SV("-23:53:20")); |
| 99 | |
| 100 | // Starting at mega it will pass one day |
| 101 | |
| 102 | // fr_FR locale - integral power of not 10 ratios |
| 103 | assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{ |
| 104 | std::chrono::duration<std::intmax_t, std::ratio<1, 5'000'000>>{5'000}}) == SV("00:00:00,0010000")); |
| 105 | assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 8'000>>{3}}) == |
| 106 | SV("00:00:00,000375")); |
| 107 | assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 4'000>>{1}}) == |
| 108 | SV("00:00:00,00025")); |
| 109 | assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 5'000>>{5}}) == |
| 110 | SV("00:00:00,0010")); |
| 111 | assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 8>>{-4}}) == |
| 112 | SV("-00:00:00,500")); |
| 113 | assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 4>>{-8}}) == |
| 114 | SV("-00:00:02,00")); |
| 115 | assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 5>>{-5}}) == |
| 116 | SV("-00:00:01,0")); |
| 117 | |
| 118 | // TODO FMT Note there's no wording on the rounding |
| 119 | assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 9>>{5}}) == |
| 120 | SV("00:00:00,555555")); |
| 121 | assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 7>>{7}}) == |
| 122 | SV("00:00:01,000000")); |
| 123 | assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 6>>{1}}) == |
| 124 | SV("00:00:00,166666")); |
| 125 | assert(stream_fr_FR_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<std::intmax_t, std::ratio<1, 3>>{2}}) == |
| 126 | SV("00:00:00,666666")); |
| 127 | |
| 128 | // ja_JP locale - floating points |
| 129 | assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{ |
| 130 | std::chrono::duration<long double, std::femto>{1'234'567'890.123}}) == SV("00:00:00.000001234567890")); |
| 131 | assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{ |
| 132 | std::chrono::duration<long double, std::pico>{1'234'567'890.123}}) == SV("00:00:00.001234567890")); |
| 133 | assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{ |
| 134 | std::chrono::duration<long double, std::nano>{1'234'567'890.123}}) == SV("00:00:01.234567890")); |
| 135 | assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<double, std::micro>{1'234'567.123}}) == |
| 136 | SV("00:00:01.234567")); |
| 137 | assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<double, std::milli>{123'456.123}}) == |
| 138 | SV("00:02:03.456")); |
| 139 | assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<double, std::centi>{12'345.123}}) == |
| 140 | SV("00:02:03.45")); |
| 141 | assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<float, std::deci>{1'234.123}}) == |
| 142 | SV("00:02:03.4")); |
| 143 | |
| 144 | assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<float>{123.123}}) == SV("00:02:03")); |
| 145 | |
| 146 | assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<double, std::deca>{-366.5}}) == |
| 147 | SV("-01:01:05")); |
| 148 | assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<double, std::hecto>{-72.64}}) == |
| 149 | SV("-02:01:04")); |
| 150 | assert(stream_ja_JP_locale<CharT>(std::chrono::hh_mm_ss{std::chrono::duration<double, std::kilo>{-86}}) == |
| 151 | SV("-23:53:20")); |
| 152 | } |
| 153 | |
| 154 | int main(int, char**) { |
| 155 | test<char>(); |
| 156 | |
| 157 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 158 | test<wchar_t>(); |
| 159 | #endif |
| 160 | |
| 161 | return 0; |
| 162 | } |