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