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 // REQUIRES: locale.fr_FR.UTF-8 17 // REQUIRES: locale.ja_JP.UTF-8 18 19 // <chrono> 20 21 // template<class charT> struct formatter<chrono::month, charT>; 22 23 #include <chrono> 24 #include <format> 25 26 #include <cassert> 27 #include <concepts> 28 #include <locale> 29 #include <iostream> 30 #include <type_traits> 31 32 #include "formatter_tests.h" 33 #include "make_string.h" 34 #include "platform_support.h" // locale name macros 35 #include "string_literal.h" 36 #include "test_macros.h" 37 38 template <class CharT> 39 static void test_no_chrono_specs() { 40 // Valid month 41 check(SV("Jan"), SV("{}"), std::chrono::month{1}); 42 check(SV("*Jan*"), SV("{:*^5}"), std::chrono::month{1}); 43 check(SV("*Jan"), SV("{:*>4}"), std::chrono::month{1}); 44 45 // Invalid month 46 check(SV("0 is not a valid month"), SV("{}"), std::chrono::month{0}); 47 check(SV("*0 is not a valid month*"), SV("{:*^24}"), std::chrono::month{0}); 48 } 49 50 template <class CharT> 51 static void test_valid_values() { 52 // Test that %b, %h, and %B throw an exception. 53 check_exception("Formatting a month name from an invalid month number", SV("{:%b}"), std::chrono::month{200}); 54 check_exception("Formatting a month name from an invalid month number", SV("{:%b}"), std::chrono::month{13}); 55 check_exception("Formatting a month name from an invalid month number", SV("{:%b}"), std::chrono::month{255}); 56 57 check_exception("Formatting a month name from an invalid month number", SV("{:%h}"), std::chrono::month{0}); 58 check_exception("Formatting a month name from an invalid month number", SV("{:%h}"), std::chrono::month{13}); 59 check_exception("Formatting a month name from an invalid month number", SV("{:%h}"), std::chrono::month{255}); 60 61 check_exception("Formatting a month name from an invalid month number", SV("{:%B}"), std::chrono::month{0}); 62 check_exception("Formatting a month name from an invalid month number", SV("{:%B}"), std::chrono::month{13}); 63 check_exception("Formatting a month name from an invalid month number", SV("{:%B}"), std::chrono::month{255}); 64 65 constexpr std::basic_string_view<CharT> fmt = SV("{:%%b='%b'%t%%B='%B'%t%%h='%h'%t%%m='%m'%t%%Om='%Om'%n}"); 66 constexpr std::basic_string_view<CharT> lfmt = SV("{:L%%b='%b'%t%%B='%B'%t%%h='%h'%t%%m='%m'%t%%Om='%Om'%n}"); 67 68 const std::locale loc(LOCALE_ja_JP_UTF_8); 69 std::locale::global(std::locale(LOCALE_fr_FR_UTF_8)); 70 71 // Non localized output using C-locale 72 check(SV("%b='Jan'\t%B='January'\t%h='Jan'\t%m='01'\t%Om='01'\n"), fmt, std::chrono::January); 73 check(SV("%b='Feb'\t%B='February'\t%h='Feb'\t%m='02'\t%Om='02'\n"), fmt, std::chrono::February); 74 check(SV("%b='Mar'\t%B='March'\t%h='Mar'\t%m='03'\t%Om='03'\n"), fmt, std::chrono::March); 75 check(SV("%b='Apr'\t%B='April'\t%h='Apr'\t%m='04'\t%Om='04'\n"), fmt, std::chrono::April); 76 check(SV("%b='May'\t%B='May'\t%h='May'\t%m='05'\t%Om='05'\n"), fmt, std::chrono::May); 77 check(SV("%b='Jun'\t%B='June'\t%h='Jun'\t%m='06'\t%Om='06'\n"), fmt, std::chrono::June); 78 check(SV("%b='Jul'\t%B='July'\t%h='Jul'\t%m='07'\t%Om='07'\n"), fmt, std::chrono::July); 79 check(SV("%b='Aug'\t%B='August'\t%h='Aug'\t%m='08'\t%Om='08'\n"), fmt, std::chrono::August); 80 check(SV("%b='Sep'\t%B='September'\t%h='Sep'\t%m='09'\t%Om='09'\n"), fmt, std::chrono::September); 81 check(SV("%b='Oct'\t%B='October'\t%h='Oct'\t%m='10'\t%Om='10'\n"), fmt, std::chrono::October); 82 check(SV("%b='Nov'\t%B='November'\t%h='Nov'\t%m='11'\t%Om='11'\n"), fmt, std::chrono::November); 83 check(SV("%b='Dec'\t%B='December'\t%h='Dec'\t%m='12'\t%Om='12'\n"), fmt, std::chrono::December); 84 85 // Use the global locale (fr_FR) 86 #if defined(__APPLE__) 87 check(SV("%b='jan'\t%B='janvier'\t%h='jan'\t%m='01'\t%Om='01'\n"), lfmt, std::chrono::January); 88 check(SV("%b='fév'\t%B='février'\t%h='fév'\t%m='02'\t%Om='02'\n"), lfmt, std::chrono::February); 89 check(SV("%b='mar'\t%B='mars'\t%h='mar'\t%m='03'\t%Om='03'\n"), lfmt, std::chrono::March); 90 check(SV("%b='avr'\t%B='avril'\t%h='avr'\t%m='04'\t%Om='04'\n"), lfmt, std::chrono::April); 91 check(SV("%b='mai'\t%B='mai'\t%h='mai'\t%m='05'\t%Om='05'\n"), lfmt, std::chrono::May); 92 check(SV("%b='jui'\t%B='juin'\t%h='jui'\t%m='06'\t%Om='06'\n"), lfmt, std::chrono::June); 93 check(SV("%b='jul'\t%B='juillet'\t%h='jul'\t%m='07'\t%Om='07'\n"), lfmt, std::chrono::July); 94 check(SV("%b='aoû'\t%B='août'\t%h='aoû'\t%m='08'\t%Om='08'\n"), lfmt, std::chrono::August); 95 check(SV("%b='sep'\t%B='septembre'\t%h='sep'\t%m='09'\t%Om='09'\n"), lfmt, std::chrono::September); 96 check(SV("%b='oct'\t%B='octobre'\t%h='oct'\t%m='10'\t%Om='10'\n"), lfmt, std::chrono::October); 97 check(SV("%b='nov'\t%B='novembre'\t%h='nov'\t%m='11'\t%Om='11'\n"), lfmt, std::chrono::November); 98 check(SV("%b='déc'\t%B='décembre'\t%h='déc'\t%m='12'\t%Om='12'\n"), lfmt, std::chrono::December); 99 #else // defined(__APPLE__) 100 check(SV("%b='janv.'\t%B='janvier'\t%h='janv.'\t%m='01'\t%Om='01'\n"), lfmt, std::chrono::January); 101 check(SV("%b='févr.'\t%B='février'\t%h='févr.'\t%m='02'\t%Om='02'\n"), lfmt, std::chrono::February); 102 check(SV("%b='mars'\t%B='mars'\t%h='mars'\t%m='03'\t%Om='03'\n"), lfmt, std::chrono::March); 103 # if defined(_WIN32) || defined(_AIX) || defined(__FreeBSD__) 104 check(SV("%b='avr.'\t%B='avril'\t%h='avr.'\t%m='04'\t%Om='04'\n"), lfmt, std::chrono::April); 105 # else 106 check(SV("%b='avril'\t%B='avril'\t%h='avril'\t%m='04'\t%Om='04'\n"), lfmt, std::chrono::April); 107 # endif 108 check(SV("%b='mai'\t%B='mai'\t%h='mai'\t%m='05'\t%Om='05'\n"), lfmt, std::chrono::May); 109 check(SV("%b='juin'\t%B='juin'\t%h='juin'\t%m='06'\t%Om='06'\n"), lfmt, std::chrono::June); 110 check(SV("%b='juil.'\t%B='juillet'\t%h='juil.'\t%m='07'\t%Om='07'\n"), lfmt, std::chrono::July); 111 check(SV("%b='août'\t%B='août'\t%h='août'\t%m='08'\t%Om='08'\n"), lfmt, std::chrono::August); 112 check(SV("%b='sept.'\t%B='septembre'\t%h='sept.'\t%m='09'\t%Om='09'\n"), lfmt, std::chrono::September); 113 check(SV("%b='oct.'\t%B='octobre'\t%h='oct.'\t%m='10'\t%Om='10'\n"), lfmt, std::chrono::October); 114 check(SV("%b='nov.'\t%B='novembre'\t%h='nov.'\t%m='11'\t%Om='11'\n"), lfmt, std::chrono::November); 115 check(SV("%b='déc.'\t%B='décembre'\t%h='déc.'\t%m='12'\t%Om='12'\n"), lfmt, std::chrono::December); 116 #endif // defined(__APPLE__) 117 118 // Use supplied locale (ja_JP) 119 #if defined(_WIN32) 120 check(loc, SV("%b='1'\t%B='1月'\t%h='1'\t%m='01'\t%Om='01'\n"), lfmt, std::chrono::January); 121 check(loc, SV("%b='2'\t%B='2月'\t%h='2'\t%m='02'\t%Om='02'\n"), lfmt, std::chrono::February); 122 check(loc, SV("%b='3'\t%B='3月'\t%h='3'\t%m='03'\t%Om='03'\n"), lfmt, std::chrono::March); 123 check(loc, SV("%b='4'\t%B='4月'\t%h='4'\t%m='04'\t%Om='04'\n"), lfmt, std::chrono::April); 124 check(loc, SV("%b='5'\t%B='5月'\t%h='5'\t%m='05'\t%Om='05'\n"), lfmt, std::chrono::May); 125 check(loc, SV("%b='6'\t%B='6月'\t%h='6'\t%m='06'\t%Om='06'\n"), lfmt, std::chrono::June); 126 check(loc, SV("%b='7'\t%B='7月'\t%h='7'\t%m='07'\t%Om='07'\n"), lfmt, std::chrono::July); 127 check(loc, SV("%b='8'\t%B='8月'\t%h='8'\t%m='08'\t%Om='08'\n"), lfmt, std::chrono::August); 128 check(loc, SV("%b='9'\t%B='9月'\t%h='9'\t%m='09'\t%Om='09'\n"), lfmt, std::chrono::September); 129 check(loc, SV("%b='10'\t%B='10月'\t%h='10'\t%m='10'\t%Om='10'\n"), lfmt, std::chrono::October); 130 check(loc, SV("%b='11'\t%B='11月'\t%h='11'\t%m='11'\t%Om='11'\n"), lfmt, std::chrono::November); 131 check(loc, SV("%b='12'\t%B='12月'\t%h='12'\t%m='12'\t%Om='12'\n"), lfmt, std::chrono::December); 132 #elif defined(_AIX) // defined(_WIN32) 133 check(loc, SV("%b='1月'\t%B='1月'\t%h='1月'\t%m='01'\t%Om='01'\n"), lfmt, std::chrono::January); 134 check(loc, SV("%b='2月'\t%B='2月'\t%h='2月'\t%m='02'\t%Om='02'\n"), lfmt, std::chrono::February); 135 check(loc, SV("%b='3月'\t%B='3月'\t%h='3月'\t%m='03'\t%Om='03'\n"), lfmt, std::chrono::March); 136 check(loc, SV("%b='4月'\t%B='4月'\t%h='4月'\t%m='04'\t%Om='04'\n"), lfmt, std::chrono::April); 137 check(loc, SV("%b='5月'\t%B='5月'\t%h='5月'\t%m='05'\t%Om='05'\n"), lfmt, std::chrono::May); 138 check(loc, SV("%b='6月'\t%B='6月'\t%h='6月'\t%m='06'\t%Om='06'\n"), lfmt, std::chrono::June); 139 check(loc, SV("%b='7月'\t%B='7月'\t%h='7月'\t%m='07'\t%Om='07'\n"), lfmt, std::chrono::July); 140 check(loc, SV("%b='8月'\t%B='8月'\t%h='8月'\t%m='08'\t%Om='08'\n"), lfmt, std::chrono::August); 141 check(loc, SV("%b='9月'\t%B='9月'\t%h='9月'\t%m='09'\t%Om='09'\n"), lfmt, std::chrono::September); 142 check(loc, SV("%b='10月'\t%B='10月'\t%h='10月'\t%m='10'\t%Om='10'\n"), lfmt, std::chrono::October); 143 check(loc, SV("%b='11月'\t%B='11月'\t%h='11月'\t%m='11'\t%Om='11'\n"), lfmt, std::chrono::November); 144 check(loc, SV("%b='12月'\t%B='12月'\t%h='12月'\t%m='12'\t%Om='12'\n"), lfmt, std::chrono::December); 145 #elif defined(__FreeBSD__) // defined(_WIN32) 146 check(loc, SV("%b=' 1月'\t%B='1月'\t%h=' 1月'\t%m='01'\t%Om='01'\n"), lfmt, std::chrono::January); 147 check(loc, SV("%b=' 2月'\t%B='2月'\t%h=' 2月'\t%m='02'\t%Om='02'\n"), lfmt, std::chrono::February); 148 check(loc, SV("%b=' 3月'\t%B='3月'\t%h=' 3月'\t%m='03'\t%Om='03'\n"), lfmt, std::chrono::March); 149 check(loc, SV("%b=' 4月'\t%B='4月'\t%h=' 4月'\t%m='04'\t%Om='04'\n"), lfmt, std::chrono::April); 150 check(loc, SV("%b=' 5月'\t%B='5月'\t%h=' 5月'\t%m='05'\t%Om='05'\n"), lfmt, std::chrono::May); 151 check(loc, SV("%b=' 6月'\t%B='6月'\t%h=' 6月'\t%m='06'\t%Om='06'\n"), lfmt, std::chrono::June); 152 check(loc, SV("%b=' 7月'\t%B='7月'\t%h=' 7月'\t%m='07'\t%Om='07'\n"), lfmt, std::chrono::July); 153 check(loc, SV("%b=' 8月'\t%B='8月'\t%h=' 8月'\t%m='08'\t%Om='08'\n"), lfmt, std::chrono::August); 154 check(loc, SV("%b=' 9月'\t%B='9月'\t%h=' 9月'\t%m='09'\t%Om='09'\n"), lfmt, std::chrono::September); 155 check(loc, SV("%b='10月'\t%B='10月'\t%h='10月'\t%m='10'\t%Om='10'\n"), lfmt, std::chrono::October); 156 check(loc, SV("%b='11月'\t%B='11月'\t%h='11月'\t%m='11'\t%Om='11'\n"), lfmt, std::chrono::November); 157 check(loc, SV("%b='12月'\t%B='12月'\t%h='12月'\t%m='12'\t%Om='12'\n"), lfmt, std::chrono::December); 158 #elif defined(__APPLE__) // defined(_WIN32) 159 check(loc, SV("%b=' 1'\t%B='1月'\t%h=' 1'\t%m='01'\t%Om='01'\n"), lfmt, std::chrono::January); 160 check(loc, SV("%b=' 2'\t%B='2月'\t%h=' 2'\t%m='02'\t%Om='02'\n"), lfmt, std::chrono::February); 161 check(loc, SV("%b=' 3'\t%B='3月'\t%h=' 3'\t%m='03'\t%Om='03'\n"), lfmt, std::chrono::March); 162 check(loc, SV("%b=' 4'\t%B='4月'\t%h=' 4'\t%m='04'\t%Om='04'\n"), lfmt, std::chrono::April); 163 check(loc, SV("%b=' 5'\t%B='5月'\t%h=' 5'\t%m='05'\t%Om='05'\n"), lfmt, std::chrono::May); 164 check(loc, SV("%b=' 6'\t%B='6月'\t%h=' 6'\t%m='06'\t%Om='06'\n"), lfmt, std::chrono::June); 165 check(loc, SV("%b=' 7'\t%B='7月'\t%h=' 7'\t%m='07'\t%Om='07'\n"), lfmt, std::chrono::July); 166 check(loc, SV("%b=' 8'\t%B='8月'\t%h=' 8'\t%m='08'\t%Om='08'\n"), lfmt, std::chrono::August); 167 check(loc, SV("%b=' 9'\t%B='9月'\t%h=' 9'\t%m='09'\t%Om='09'\n"), lfmt, std::chrono::September); 168 check(loc, SV("%b='10'\t%B='10月'\t%h='10'\t%m='10'\t%Om='10'\n"), lfmt, std::chrono::October); 169 check(loc, SV("%b='11'\t%B='11月'\t%h='11'\t%m='11'\t%Om='11'\n"), lfmt, std::chrono::November); 170 check(loc, SV("%b='12'\t%B='12月'\t%h='12'\t%m='12'\t%Om='12'\n"), lfmt, std::chrono::December); 171 #else // defined(_WIN32) 172 check(loc, SV("%b=' 1月'\t%B='1月'\t%h=' 1月'\t%m='01'\t%Om='一'\n"), lfmt, std::chrono::January); 173 check(loc, SV("%b=' 2月'\t%B='2月'\t%h=' 2月'\t%m='02'\t%Om='二'\n"), lfmt, std::chrono::February); 174 check(loc, SV("%b=' 3月'\t%B='3月'\t%h=' 3月'\t%m='03'\t%Om='三'\n"), lfmt, std::chrono::March); 175 check(loc, SV("%b=' 4月'\t%B='4月'\t%h=' 4月'\t%m='04'\t%Om='四'\n"), lfmt, std::chrono::April); 176 check(loc, SV("%b=' 5月'\t%B='5月'\t%h=' 5月'\t%m='05'\t%Om='五'\n"), lfmt, std::chrono::May); 177 check(loc, SV("%b=' 6月'\t%B='6月'\t%h=' 6月'\t%m='06'\t%Om='六'\n"), lfmt, std::chrono::June); 178 check(loc, SV("%b=' 7月'\t%B='7月'\t%h=' 7月'\t%m='07'\t%Om='七'\n"), lfmt, std::chrono::July); 179 check(loc, SV("%b=' 8月'\t%B='8月'\t%h=' 8月'\t%m='08'\t%Om='八'\n"), lfmt, std::chrono::August); 180 check(loc, SV("%b=' 9月'\t%B='9月'\t%h=' 9月'\t%m='09'\t%Om='九'\n"), lfmt, std::chrono::September); 181 check(loc, SV("%b='10月'\t%B='10月'\t%h='10月'\t%m='10'\t%Om='十'\n"), lfmt, std::chrono::October); 182 check(loc, SV("%b='11月'\t%B='11月'\t%h='11月'\t%m='11'\t%Om='十一'\n"), lfmt, std::chrono::November); 183 check(loc, SV("%b='12月'\t%B='12月'\t%h='12月'\t%m='12'\t%Om='十二'\n"), lfmt, std::chrono::December); 184 #endif // defined(_WIN32) 185 186 std::locale::global(std::locale::classic()); 187 } 188 189 template <class CharT> 190 static void test() { 191 test_no_chrono_specs<CharT>(); 192 test_valid_values<CharT>(); 193 check_invalid_types<CharT>({SV("b"), SV("B"), SV("h"), SV("m"), SV("Om")}, std::chrono::January); 194 195 check_exception("The format specifier expects a '%' or a '}'", SV("{:A"), std::chrono::January); 196 check_exception("The chrono specifiers contain a '{'", SV("{:%%{"), std::chrono::January); 197 check_exception("End of input while parsing a conversion specifier", SV("{:%"), std::chrono::January); 198 check_exception("End of input while parsing the modifier E", SV("{:%E"), std::chrono::January); 199 check_exception("End of input while parsing the modifier O", SV("{:%O"), std::chrono::January); 200 201 // Precision not allowed 202 check_exception("The format specifier expects a '%' or a '}'", SV("{:.3}"), std::chrono::January); 203 } 204 205 int main(int, char**) { 206 test<char>(); 207 208 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 209 test<wchar_t>(); 210 #endif 211 212 return 0; 213 } 214