| 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: GCC-ALWAYS_INLINE-FIXME |
| 12 | |
| 13 | // TODO FMT This test should not require std::to_chars(floating-point) |
| 14 | // XFAIL: availability-fp_to_chars-missing |
| 15 | |
| 16 | // TODO FMT Investigate Windows issues. |
| 17 | // XFAIL: msvc |
| 18 | |
| 19 | // REQUIRES: locale.fr_FR.UTF-8 |
| 20 | // REQUIRES: locale.ja_JP.UTF-8 |
| 21 | |
| 22 | // <chrono> |
| 23 | |
| 24 | // template<class charT> struct formatter<chrono::weekday, charT>; |
| 25 | |
| 26 | #include <chrono> |
| 27 | #include <format> |
| 28 | |
| 29 | #include <cassert> |
| 30 | #include <concepts> |
| 31 | #include <locale> |
| 32 | #include <iostream> |
| 33 | #include <type_traits> |
| 34 | |
| 35 | #include "formatter_tests.h" |
| 36 | #include "make_string.h" |
| 37 | #include "platform_support.h" // locale name macros |
| 38 | #include "test_macros.h" |
| 39 | |
| 40 | template <class CharT> |
| 41 | static void test_no_chrono_specs() { |
| 42 | using namespace std::literals::chrono_literals; |
| 43 | |
| 44 | // Valid day |
| 45 | check(SV("Sun" ), SV("{}" ), std::chrono::weekday(0)); |
| 46 | |
| 47 | // Invalid day |
| 48 | check(SV("8 is not a valid weekday" ), SV("{}" ), std::chrono::weekday(8)); |
| 49 | } |
| 50 | |
| 51 | template <class CharT> |
| 52 | static void test_valid_values() { |
| 53 | constexpr std::basic_string_view<CharT> fmt = |
| 54 | SV("{:%%u='%u'%t%%Ou='%Ou'%t%%w='%w'%t%%Ow='%Ow'%t%%a='%a'%t%%A='%A'%n}" ); |
| 55 | constexpr std::basic_string_view<CharT> lfmt = |
| 56 | SV("{:L%%u='%u'%t%%Ou='%Ou'%t%%w='%w'%t%%Ow='%Ow'%t%%a='%a'%t%%A='%A'%n}" ); |
| 57 | |
| 58 | const std::locale loc(LOCALE_ja_JP_UTF_8); |
| 59 | std::locale::global(std::locale(LOCALE_fr_FR_UTF_8)); |
| 60 | |
| 61 | // Non localized output using C-locale |
| 62 | check(SV("%u='7'\t%Ou='7'\t%w='0'\t%Ow='0'\t%a='Sun'\t%A='Sunday'\n" ), fmt, std::chrono::weekday(0)); |
| 63 | check(SV("%u='1'\t%Ou='1'\t%w='1'\t%Ow='1'\t%a='Mon'\t%A='Monday'\n" ), fmt, std::chrono::weekday(1)); |
| 64 | check(SV("%u='2'\t%Ou='2'\t%w='2'\t%Ow='2'\t%a='Tue'\t%A='Tuesday'\n" ), fmt, std::chrono::weekday(2)); |
| 65 | check(SV("%u='3'\t%Ou='3'\t%w='3'\t%Ow='3'\t%a='Wed'\t%A='Wednesday'\n" ), fmt, std::chrono::weekday(3)); |
| 66 | check(SV("%u='4'\t%Ou='4'\t%w='4'\t%Ow='4'\t%a='Thu'\t%A='Thursday'\n" ), fmt, std::chrono::weekday(4)); |
| 67 | check(SV("%u='5'\t%Ou='5'\t%w='5'\t%Ow='5'\t%a='Fri'\t%A='Friday'\n" ), fmt, std::chrono::weekday(5)); |
| 68 | check(SV("%u='6'\t%Ou='6'\t%w='6'\t%Ow='6'\t%a='Sat'\t%A='Saturday'\n" ), fmt, std::chrono::weekday(6)); |
| 69 | check(SV("%u='7'\t%Ou='7'\t%w='0'\t%Ow='0'\t%a='Sun'\t%A='Sunday'\n" ), fmt, std::chrono::weekday(7)); |
| 70 | |
| 71 | #if defined(__APPLE__) |
| 72 | // Use the global locale (fr_FR) |
| 73 | check(SV("%u='7'\t%Ou='7'\t%w='0'\t%Ow='0'\t%a='Dim'\t%A='Dimanche'\n" ), lfmt, std::chrono::weekday(0)); |
| 74 | check(SV("%u='1'\t%Ou='1'\t%w='1'\t%Ow='1'\t%a='Lun'\t%A='Lundi'\n" ), lfmt, std::chrono::weekday(1)); |
| 75 | check(SV("%u='2'\t%Ou='2'\t%w='2'\t%Ow='2'\t%a='Mar'\t%A='Mardi'\n" ), lfmt, std::chrono::weekday(2)); |
| 76 | check(SV("%u='3'\t%Ou='3'\t%w='3'\t%Ow='3'\t%a='Mer'\t%A='Mercredi'\n" ), lfmt, std::chrono::weekday(3)); |
| 77 | check(SV("%u='4'\t%Ou='4'\t%w='4'\t%Ow='4'\t%a='Jeu'\t%A='Jeudi'\n" ), lfmt, std::chrono::weekday(4)); |
| 78 | check(SV("%u='5'\t%Ou='5'\t%w='5'\t%Ow='5'\t%a='Ven'\t%A='Vendredi'\n" ), lfmt, std::chrono::weekday(5)); |
| 79 | check(SV("%u='6'\t%Ou='6'\t%w='6'\t%Ow='6'\t%a='Sam'\t%A='Samedi'\n" ), lfmt, std::chrono::weekday(6)); |
| 80 | check(SV("%u='7'\t%Ou='7'\t%w='0'\t%Ow='0'\t%a='Dim'\t%A='Dimanche'\n" ), lfmt, std::chrono::weekday(7)); |
| 81 | #else |
| 82 | // Use the global locale (fr_FR) |
| 83 | check(SV("%u='7'\t%Ou='7'\t%w='0'\t%Ow='0'\t%a='dim.'\t%A='dimanche'\n" ), lfmt, std::chrono::weekday(0)); |
| 84 | check(SV("%u='1'\t%Ou='1'\t%w='1'\t%Ow='1'\t%a='lun.'\t%A='lundi'\n" ), lfmt, std::chrono::weekday(1)); |
| 85 | check(SV("%u='2'\t%Ou='2'\t%w='2'\t%Ow='2'\t%a='mar.'\t%A='mardi'\n" ), lfmt, std::chrono::weekday(2)); |
| 86 | check(SV("%u='3'\t%Ou='3'\t%w='3'\t%Ow='3'\t%a='mer.'\t%A='mercredi'\n" ), lfmt, std::chrono::weekday(3)); |
| 87 | check(SV("%u='4'\t%Ou='4'\t%w='4'\t%Ow='4'\t%a='jeu.'\t%A='jeudi'\n" ), lfmt, std::chrono::weekday(4)); |
| 88 | check(SV("%u='5'\t%Ou='5'\t%w='5'\t%Ow='5'\t%a='ven.'\t%A='vendredi'\n" ), lfmt, std::chrono::weekday(5)); |
| 89 | check(SV("%u='6'\t%Ou='6'\t%w='6'\t%Ow='6'\t%a='sam.'\t%A='samedi'\n" ), lfmt, std::chrono::weekday(6)); |
| 90 | check(SV("%u='7'\t%Ou='7'\t%w='0'\t%Ow='0'\t%a='dim.'\t%A='dimanche'\n" ), lfmt, std::chrono::weekday(7)); |
| 91 | #endif |
| 92 | |
| 93 | // Use supplied locale (ja_JP). |
| 94 | // This locale has a different alternate, but not on all platforms |
| 95 | #if defined(_WIN32) || defined(__APPLE__) || defined(_AIX) || defined(__FreeBSD__) |
| 96 | check(loc, SV("%u='7'\t%Ou='7'\t%w='0'\t%Ow='0'\t%a='日'\t%A='日曜日'\n" ), lfmt, std::chrono::weekday(0)); |
| 97 | check(loc, SV("%u='1'\t%Ou='1'\t%w='1'\t%Ow='1'\t%a='月'\t%A='月曜日'\n" ), lfmt, std::chrono::weekday(1)); |
| 98 | check(loc, SV("%u='2'\t%Ou='2'\t%w='2'\t%Ow='2'\t%a='火'\t%A='火曜日'\n" ), lfmt, std::chrono::weekday(2)); |
| 99 | check(loc, SV("%u='3'\t%Ou='3'\t%w='3'\t%Ow='3'\t%a='水'\t%A='水曜日'\n" ), lfmt, std::chrono::weekday(3)); |
| 100 | check(loc, SV("%u='4'\t%Ou='4'\t%w='4'\t%Ow='4'\t%a='木'\t%A='木曜日'\n" ), lfmt, std::chrono::weekday(4)); |
| 101 | check(loc, SV("%u='5'\t%Ou='5'\t%w='5'\t%Ow='5'\t%a='金'\t%A='金曜日'\n" ), lfmt, std::chrono::weekday(5)); |
| 102 | check(loc, SV("%u='6'\t%Ou='6'\t%w='6'\t%Ow='6'\t%a='土'\t%A='土曜日'\n" ), lfmt, std::chrono::weekday(6)); |
| 103 | check(loc, SV("%u='7'\t%Ou='7'\t%w='0'\t%Ow='0'\t%a='日'\t%A='日曜日'\n" ), lfmt, std::chrono::weekday(7)); |
| 104 | #else |
| 105 | check(loc, SV("%u='7'\t%Ou='七'\t%w='0'\t%Ow='〇'\t%a='日'\t%A='日曜日'\n" ), lfmt, std::chrono::weekday(0)); |
| 106 | check(loc, SV("%u='1'\t%Ou='一'\t%w='1'\t%Ow='一'\t%a='月'\t%A='月曜日'\n" ), lfmt, std::chrono::weekday(1)); |
| 107 | check(loc, SV("%u='2'\t%Ou='二'\t%w='2'\t%Ow='二'\t%a='火'\t%A='火曜日'\n" ), lfmt, std::chrono::weekday(2)); |
| 108 | check(loc, SV("%u='3'\t%Ou='三'\t%w='3'\t%Ow='三'\t%a='水'\t%A='水曜日'\n" ), lfmt, std::chrono::weekday(3)); |
| 109 | check(loc, SV("%u='4'\t%Ou='四'\t%w='4'\t%Ow='四'\t%a='木'\t%A='木曜日'\n" ), lfmt, std::chrono::weekday(4)); |
| 110 | check(loc, SV("%u='5'\t%Ou='五'\t%w='5'\t%Ow='五'\t%a='金'\t%A='金曜日'\n" ), lfmt, std::chrono::weekday(5)); |
| 111 | check(loc, SV("%u='6'\t%Ou='六'\t%w='6'\t%Ow='六'\t%a='土'\t%A='土曜日'\n" ), lfmt, std::chrono::weekday(6)); |
| 112 | check(loc, SV("%u='7'\t%Ou='七'\t%w='0'\t%Ow='〇'\t%a='日'\t%A='日曜日'\n" ), lfmt, std::chrono::weekday(7)); |
| 113 | #endif |
| 114 | |
| 115 | std::locale::global(loc: std::locale::classic()); |
| 116 | } |
| 117 | |
| 118 | template <class CharT> |
| 119 | static void test_invalid_values() { |
| 120 | // Test that %a and %A throw an exception. |
| 121 | check_exception("Formatting a weekday name needs a valid weekday" , SV("{:%a}" ), std::chrono::weekday(8)); |
| 122 | check_exception("Formatting a weekday name needs a valid weekday" , SV("{:%a}" ), std::chrono::weekday(255)); |
| 123 | check_exception("Formatting a weekday name needs a valid weekday" , SV("{:%A}" ), std::chrono::weekday(8)); |
| 124 | check_exception("Formatting a weekday name needs a valid weekday" , SV("{:%A}" ), std::chrono::weekday(255)); |
| 125 | |
| 126 | constexpr std::basic_string_view<CharT> fmt = SV("{:%%u='%u'%t%%Ou='%Ou'%t%%w='%w'%t%%Ow='%Ow'%n}" ); |
| 127 | constexpr std::basic_string_view<CharT> lfmt = SV("{:L%%u='%u'%t%%Ou='%Ou'%t%%w='%w'%t%%Ow='%Ow'%n}" ); |
| 128 | |
| 129 | const std::locale loc(LOCALE_ja_JP_UTF_8); |
| 130 | std::locale::global(std::locale(LOCALE_fr_FR_UTF_8)); |
| 131 | |
| 132 | #if defined(__APPLE__) || defined(__FreeBSD__) |
| 133 | // Non localized output using C-locale |
| 134 | check(SV("%u='8'\t%Ou='8'\t%w='8'\t%Ow='8'\n" ), fmt, std::chrono::weekday(8)); |
| 135 | check(SV("%u='255'\t%Ou='255'\t%w='255'\t%Ow='255'\n" ), fmt, std::chrono::weekday(255)); |
| 136 | |
| 137 | // Use the global locale (fr_FR) |
| 138 | check(SV("%u='8'\t%Ou='8'\t%w='8'\t%Ow='8'\n" ), lfmt, std::chrono::weekday(8)); |
| 139 | check(SV("%u='255'\t%Ou='255'\t%w='255'\t%Ow='255'\n" ), lfmt, std::chrono::weekday(255)); |
| 140 | |
| 141 | // Use supplied locale (ja_JP). This locale has a different alternate. |
| 142 | check(loc, SV("%u='8'\t%Ou='8'\t%w='8'\t%Ow='8'\n" ), lfmt, std::chrono::weekday(8)); |
| 143 | check(loc, SV("%u='255'\t%Ou='255'\t%w='255'\t%Ow='255'\n" ), lfmt, std::chrono::weekday(255)); |
| 144 | #elif defined(_AIX) // defined(__APPLE__) || defined(__FreeBSD__) |
| 145 | // Non localized output using C-locale |
| 146 | check(SV("%u='8'\t%Ou='8'\t%w='8'\t%Ow='8'\n" ), fmt, std::chrono::weekday(8)); |
| 147 | check(SV("%u='5'\t%Ou='5'\t%w='5'\t%Ow='5'\n" ), fmt, std::chrono::weekday(255)); |
| 148 | |
| 149 | // Use the global locale (fr_FR) |
| 150 | check(SV("%u='8'\t%Ou='8'\t%w='8'\t%Ow='8'\n" ), lfmt, std::chrono::weekday(8)); |
| 151 | check(SV("%u='5'\t%Ou='5'\t%w='5'\t%Ow='5'\n" ), lfmt, std::chrono::weekday(255)); |
| 152 | |
| 153 | // Use supplied locale (ja_JP). This locale has a different alternate. |
| 154 | check(loc, SV("%u='8'\t%Ou='8'\t%w='8'\t%Ow='8'\n" ), lfmt, std::chrono::weekday(8)); |
| 155 | check(loc, SV("%u='5'\t%Ou='5'\t%w='5'\t%Ow='5'\n" ), lfmt, std::chrono::weekday(255)); |
| 156 | #elif defined(_WIN32) // defined(__APPLE__) || defined(__FreeBSD__) |
| 157 | // Non localized output using C-locale |
| 158 | check(SV("%u=''\t%Ou=''\t%w=''\t%Ow=''\n" ), fmt, std::chrono::weekday(8)); |
| 159 | check(SV("%u=''\t%Ou=''\t%w=''\t%Ow=''\n" ), fmt, std::chrono::weekday(255)); |
| 160 | |
| 161 | // Use the global locale (fr_FR) |
| 162 | check(SV("%u=''\t%Ou=''\t%w=''\t%Ow=''\n" ), lfmt, std::chrono::weekday(8)); |
| 163 | check(SV("%u=''\t%Ou=''\t%w=''\t%Ow=''\n" ), lfmt, std::chrono::weekday(255)); |
| 164 | |
| 165 | // Use supplied locale (ja_JP). This locale has a different alternate. |
| 166 | check(loc, SV("%u=''\t%Ou=''\t%w=''\t%Ow=''\n" ), lfmt, std::chrono::weekday(8)); |
| 167 | check(loc, SV("%u=''\t%Ou=''\t%w=''\t%Ow=''\n" ), lfmt, std::chrono::weekday(255)); |
| 168 | #else // defined(__APPLE__) || defined(__FreeBSD__) |
| 169 | // Non localized output using C-locale |
| 170 | check(SV("%u='1'\t%Ou='1'\t%w='8'\t%Ow='8'\n" ), fmt, std::chrono::weekday(8)); |
| 171 | check(SV("%u='3'\t%Ou='3'\t%w='255'\t%Ow='255'\n" ), fmt, std::chrono::weekday(255)); |
| 172 | |
| 173 | // Use the global locale (fr_FR) |
| 174 | check(SV("%u='1'\t%Ou='1'\t%w='8'\t%Ow='8'\n" ), lfmt, std::chrono::weekday(8)); |
| 175 | check(SV("%u='3'\t%Ou='3'\t%w='255'\t%Ow='255'\n" ), lfmt, std::chrono::weekday(255)); |
| 176 | |
| 177 | // Use supplied locale (ja_JP). This locale has a different alternate. |
| 178 | check(loc, SV("%u='1'\t%Ou='一'\t%w='8'\t%Ow='八'\n" ), lfmt, std::chrono::weekday(8)); |
| 179 | check(loc, SV("%u='3'\t%Ou='三'\t%w='255'\t%Ow='255'\n" ), lfmt, std::chrono::weekday(255)); |
| 180 | #endif // defined(__APPLE__) || defined(__FreeBSD__) |
| 181 | |
| 182 | std::locale::global(loc: std::locale::classic()); |
| 183 | } |
| 184 | |
| 185 | template <class CharT> |
| 186 | static void test() { |
| 187 | test_no_chrono_specs<CharT>(); |
| 188 | test_valid_values<CharT>(); |
| 189 | test_invalid_values<CharT>(); |
| 190 | check_invalid_types<CharT>( |
| 191 | {SV("a" ), SV("A" ), SV("t" ), SV("u" ), SV("w" ), SV("Ou" ), SV("Ow" )}, std::chrono::weekday(0)); |
| 192 | |
| 193 | check_exception("The format specifier expects a '%' or a '}'" , SV("{:A" ), std::chrono::weekday(0)); |
| 194 | check_exception("The chrono specifiers contain a '{'" , SV("{:%%{" ), std::chrono::weekday(0)); |
| 195 | check_exception("End of input while parsing a conversion specifier" , SV("{:%" ), std::chrono::weekday(0)); |
| 196 | check_exception("End of input while parsing the modifier E" , SV("{:%E" ), std::chrono::weekday(0)); |
| 197 | check_exception("End of input while parsing the modifier O" , SV("{:%O" ), std::chrono::weekday(0)); |
| 198 | |
| 199 | // Precision not allowed |
| 200 | check_exception("The format specifier expects a '%' or a '}'" , SV("{:.3}" ), std::chrono::weekday(0)); |
| 201 | } |
| 202 | |
| 203 | int main(int, char**) { |
| 204 | test<char>(); |
| 205 | |
| 206 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 207 | test<wchar_t>(); |
| 208 | #endif |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |