| 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 |
// TODO FMT Investigate Windows issues. |
| 16 |
// XFAIL: msvc |
| 17 |
|
| 18 |
// REQUIRES: locale.fr_FR.UTF-8 |
| 19 |
// REQUIRES: locale.ja_JP.UTF-8 |
| 20 |
|
| 21 |
// <chrono> |
| 22 |
|
| 23 |
// template<class charT> struct formatter<chrono::day, charT>; |
| 24 |
|
| 25 |
#include <chrono> |
| 26 |
#include <format> |
| 27 |
|
| 28 |
#include <cassert> |
| 29 |
#include <concepts> |
| 30 |
#include <locale> |
| 31 |
#include <iostream> |
| 32 |
#include <type_traits> |
| 33 |
|
| 34 |
#include "formatter_tests.h" |
| 35 |
#include "make_string.h" |
| 36 |
#include "platform_support.h" // locale name macros |
| 37 |
#include "string_literal.h" |
| 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("01"), SV("{}"), 1d); |
| 46 |
check(SV("*01*"), SV("{:*^4}"), 1d); |
| 47 |
check(SV("*01"), SV("{:*>3}"), 1d); |
| 48 |
|
| 49 |
// Invalid day |
| 50 |
check(SV("00 is not a valid day"), SV("{}"), 0d); |
| 51 |
check(SV("*00 is not a valid day*"), SV("{:*^23}"), 0d); |
| 52 |
} |
| 53 |
|
| 54 |
template <class CharT> |
| 55 |
static void test_valid_values() { |
| 56 |
using namespace std::literals::chrono_literals; |
| 57 |
|
| 58 |
constexpr std::basic_string_view<CharT> fmt = SV("{:%%d='%d'%t%%Od='%Od'%t%%e='%e'%t%%Oe='%Oe'%n}"); |
| 59 |
constexpr std::basic_string_view<CharT> lfmt = SV("{:L%%d='%d'%t%%Od='%Od'%t%%e='%e'%t%%Oe='%Oe'%n}"); |
| 60 |
|
| 61 |
const std::locale loc(LOCALE_ja_JP_UTF_8); |
| 62 |
std::locale::global(std::locale(LOCALE_fr_FR_UTF_8)); |
| 63 |
|
| 64 |
// Non localized output using C-locale |
| 65 |
#if defined(_WIN32) |
| 66 |
check(SV("%d=''\t%Od=''\t%e=''\t%Oe=''\n"), fmt, 0d); |
| 67 |
#else |
| 68 |
check(SV("%d='00'\t%Od='00'\t%e=' 0'\t%Oe=' 0'\n"), fmt, 0d); |
| 69 |
#endif |
| 70 |
check(SV("%d='01'\t%Od='01'\t%e=' 1'\t%Oe=' 1'\n"), fmt, 1d); |
| 71 |
check(SV("%d='31'\t%Od='31'\t%e='31'\t%Oe='31'\n"), fmt, 31d); |
| 72 |
#if defined(_WIN32) |
| 73 |
check(SV("%d=''\t%Od=''\t%e=''\t%Oe=''\n"), fmt, 0d); |
| 74 |
#elif defined(_AIX) |
| 75 |
check(SV("%d='55'\t%Od='55'\t%e='55'\t%Oe='55'\n"), fmt, 255d); |
| 76 |
#else |
| 77 |
check(SV("%d='255'\t%Od='255'\t%e='255'\t%Oe='255'\n"), fmt, 255d); |
| 78 |
#endif |
| 79 |
|
| 80 |
// Use the global locale (fr_FR) |
| 81 |
#if defined(_WIN32) |
| 82 |
check(SV("%d=''\t%Od=''\t%e=''\t%Oe=''\n"), lfmt, 0d); |
| 83 |
#else |
| 84 |
check(SV("%d='00'\t%Od='00'\t%e=' 0'\t%Oe=' 0'\n"), lfmt, 0d); |
| 85 |
#endif |
| 86 |
check(SV("%d='01'\t%Od='01'\t%e=' 1'\t%Oe=' 1'\n"), lfmt, 1d); |
| 87 |
check(SV("%d='31'\t%Od='31'\t%e='31'\t%Oe='31'\n"), lfmt, 31d); |
| 88 |
#if defined(_WIN32) |
| 89 |
check(SV("%d=''\t%Od=''\t%e=''\t%Oe=''\n"), lfmt, 255d); |
| 90 |
#elif defined(_AIX) |
| 91 |
check(SV("%d='55'\t%Od='55'\t%e='55'\t%Oe='55'\n"), lfmt, 255d); |
| 92 |
#else |
| 93 |
check(SV("%d='255'\t%Od='255'\t%e='255'\t%Oe='255'\n"), lfmt, 255d); |
| 94 |
#endif |
| 95 |
|
| 96 |
// Use supplied locale (ja_JP). This locale has a different alternate on some platforms. |
| 97 |
#if defined(__APPLE__) || defined(_AIX) || defined(_WIN32) || defined(__FreeBSD__) |
| 98 |
# if defined(_WIN32) |
| 99 |
check(loc, SV("%d=''\t%Od=''\t%e=''\t%Oe=''\n"), lfmt, 0d); |
| 100 |
# else |
| 101 |
check(loc, SV("%d='00'\t%Od='00'\t%e=' 0'\t%Oe=' 0'\n"), lfmt, 0d); |
| 102 |
# endif |
| 103 |
check(loc, SV("%d='01'\t%Od='01'\t%e=' 1'\t%Oe=' 1'\n"), lfmt, 1d); |
| 104 |
check(loc, SV("%d='31'\t%Od='31'\t%e='31'\t%Oe='31'\n"), lfmt, 31d); |
| 105 |
# if defined(_WIN32) |
| 106 |
check(SV("%d=''\t%Od=''\t%e=''\t%Oe=''\n"), fmt, 255d); |
| 107 |
# elif defined(_AIX) |
| 108 |
check(SV("%d='55'\t%Od='55'\t%e='55'\t%Oe='55'\n"), fmt, 255d); |
| 109 |
# else |
| 110 |
check(SV("%d='255'\t%Od='255'\t%e='255'\t%Oe='255'\n"), fmt, 255d); |
| 111 |
# endif |
| 112 |
#else // defined(__APPLE__) || defined(_AIX) || defined(_WIN32) || defined(__FreeBSD__) |
| 113 |
check(loc, SV("%d='00'\t%Od='〇'\t%e=' 0'\t%Oe='〇'\n"), lfmt, 0d); |
| 114 |
check(loc, SV("%d='01'\t%Od='一'\t%e=' 1'\t%Oe='一'\n"), lfmt, 1d); |
| 115 |
check(loc, SV("%d='31'\t%Od='三十一'\t%e='31'\t%Oe='三十一'\n"), lfmt, 31d); |
| 116 |
check(loc, SV("%d='255'\t%Od='255'\t%e='255'\t%Oe='255'\n"), lfmt, 255d); |
| 117 |
#endif // defined(__APPLE__) || defined(_AIX) || defined(_WIN32) || defined(__FreeBSD__) |
| 118 |
|
| 119 |
std::locale::global(std::locale::classic()); |
| 120 |
} |
| 121 |
|
| 122 |
template <class CharT> |
| 123 |
static void test() { |
| 124 |
using namespace std::literals::chrono_literals; |
| 125 |
|
| 126 |
test_no_chrono_specs<CharT>(); |
| 127 |
test_valid_values<CharT>(); |
| 128 |
check_invalid_types<CharT>({SV("d"), SV("e"), SV("Od"), SV("Oe")}, 0d); |
| 129 |
|
| 130 |
check_exception("The format specifier expects a '%' or a '}'", SV("{:A"), 0d); |
| 131 |
check_exception("The chrono specifiers contain a '{'", SV("{:%%{"), 0d); |
| 132 |
check_exception("End of input while parsing a conversion specifier", SV("{:%"), 0d); |
| 133 |
check_exception("End of input while parsing the modifier E", SV("{:%E"), 0d); |
| 134 |
check_exception("End of input while parsing the modifier O", SV("{:%O"), 0d); |
| 135 |
|
| 136 |
// Precision not allowed |
| 137 |
check_exception("The format specifier expects a '%' or a '}'", SV("{:.3}"), 0d); |
| 138 |
} |
| 139 |
|
| 140 |
int main(int, char**) { |
| 141 |
test<char>(); |
| 142 |
|
| 143 |
#ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 144 |
test<wchar_t>(); |
| 145 |
#endif |
| 146 |
|
| 147 |
return 0; |
| 148 |
} |