1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: no-localization
11 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
13 // TODO FMT This test should not require std::to_chars(floating-point)
14 // XFAIL: availability-fp_to_chars-missing
16 // TODO FMT Investigate Windows issues.
19 // REQUIRES: locale.fr_FR.UTF-8
20 // REQUIRES: locale.ja_JP.UTF-8
24 // template<class charT> struct formatter<chrono::day, charT>;
33 #include <type_traits>
35 #include "formatter_tests.h"
36 #include "make_string.h"
37 #include "platform_support.h" // locale name macros
38 #include "string_literal.h"
39 #include "test_macros.h"
41 template <class CharT
>
42 static void test_no_chrono_specs() {
43 using namespace std::literals::chrono_literals
;
46 check(SV("01"), SV("{}"), 1d
);
47 check(SV("*01*"), SV("{:*^4}"), 1d
);
48 check(SV("*01"), SV("{:*>3}"), 1d
);
51 check(SV("00 is not a valid day"), SV("{}"), 0d
);
52 check(SV("*00 is not a valid day*"), SV("{:*^23}"), 0d
);
55 template <class CharT
>
56 static void test_valid_values() {
57 using namespace std::literals::chrono_literals
;
59 constexpr std::basic_string_view
<CharT
> fmt
= SV("{:%%d='%d'%t%%Od='%Od'%t%%e='%e'%t%%Oe='%Oe'%n}");
60 constexpr std::basic_string_view
<CharT
> lfmt
= SV("{:L%%d='%d'%t%%Od='%Od'%t%%e='%e'%t%%Oe='%Oe'%n}");
62 const std::locale
loc(LOCALE_ja_JP_UTF_8
);
63 std::locale::global(std::locale(LOCALE_fr_FR_UTF_8
));
65 // Non localized output using C-locale
67 check(SV("%d=''\t%Od=''\t%e=''\t%Oe=''\n"), fmt
, 0d
);
69 check(SV("%d='00'\t%Od='00'\t%e=' 0'\t%Oe=' 0'\n"), fmt
, 0d
);
71 check(SV("%d='01'\t%Od='01'\t%e=' 1'\t%Oe=' 1'\n"), fmt
, 1d
);
72 check(SV("%d='31'\t%Od='31'\t%e='31'\t%Oe='31'\n"), fmt
, 31d
);
74 check(SV("%d=''\t%Od=''\t%e=''\t%Oe=''\n"), fmt
, 0d
);
76 check(SV("%d='55'\t%Od='55'\t%e='55'\t%Oe='55'\n"), fmt
, 255d
);
78 check(SV("%d='255'\t%Od='255'\t%e='255'\t%Oe='255'\n"), fmt
, 255d
);
81 // Use the global locale (fr_FR)
83 check(SV("%d=''\t%Od=''\t%e=''\t%Oe=''\n"), lfmt
, 0d
);
85 check(SV("%d='00'\t%Od='00'\t%e=' 0'\t%Oe=' 0'\n"), lfmt
, 0d
);
87 check(SV("%d='01'\t%Od='01'\t%e=' 1'\t%Oe=' 1'\n"), lfmt
, 1d
);
88 check(SV("%d='31'\t%Od='31'\t%e='31'\t%Oe='31'\n"), lfmt
, 31d
);
90 check(SV("%d=''\t%Od=''\t%e=''\t%Oe=''\n"), lfmt
, 255d
);
92 check(SV("%d='55'\t%Od='55'\t%e='55'\t%Oe='55'\n"), lfmt
, 255d
);
94 check(SV("%d='255'\t%Od='255'\t%e='255'\t%Oe='255'\n"), lfmt
, 255d
);
97 // Use supplied locale (ja_JP). This locale has a different alternate on some platforms.
98 #if defined(__APPLE__) || defined(_AIX) || defined(_WIN32) || defined(__FreeBSD__)
100 check(loc
, SV("%d=''\t%Od=''\t%e=''\t%Oe=''\n"), lfmt
, 0d
);
102 check(loc
, SV("%d='00'\t%Od='00'\t%e=' 0'\t%Oe=' 0'\n"), lfmt
, 0d
);
104 check(loc
, SV("%d='01'\t%Od='01'\t%e=' 1'\t%Oe=' 1'\n"), lfmt
, 1d
);
105 check(loc
, SV("%d='31'\t%Od='31'\t%e='31'\t%Oe='31'\n"), lfmt
, 31d
);
107 check(SV("%d=''\t%Od=''\t%e=''\t%Oe=''\n"), fmt
, 255d
);
109 check(SV("%d='55'\t%Od='55'\t%e='55'\t%Oe='55'\n"), fmt
, 255d
);
111 check(SV("%d='255'\t%Od='255'\t%e='255'\t%Oe='255'\n"), fmt
, 255d
);
113 #else // defined(__APPLE__) || defined(_AIX) || defined(_WIN32) || defined(__FreeBSD__)
114 check(loc
, SV("%d='00'\t%Od='〇'\t%e=' 0'\t%Oe='〇'\n"), lfmt
, 0d
);
115 check(loc
, SV("%d='01'\t%Od='一'\t%e=' 1'\t%Oe='一'\n"), lfmt
, 1d
);
116 check(loc
, SV("%d='31'\t%Od='三十一'\t%e='31'\t%Oe='三十一'\n"), lfmt
, 31d
);
117 check(loc
, SV("%d='255'\t%Od='255'\t%e='255'\t%Oe='255'\n"), lfmt
, 255d
);
118 #endif // defined(__APPLE__) || defined(_AIX) || defined(_WIN32) || defined(__FreeBSD__)
120 std::locale::global(std::locale::classic());
123 template <class CharT
>
125 using namespace std::literals::chrono_literals
;
127 test_no_chrono_specs
<CharT
>();
128 test_valid_values
<CharT
>();
129 check_invalid_types
<CharT
>({SV("d"), SV("e"), SV("Od"), SV("Oe")}, 0d
);
131 check_exception("The format specifier expects a '%' or a '}'", SV("{:A"), 0d
);
132 check_exception("The chrono specifiers contain a '{'", SV("{:%%{"), 0d
);
133 check_exception("End of input while parsing a conversion specifier", SV("{:%"), 0d
);
134 check_exception("End of input while parsing the modifier E", SV("{:%E"), 0d
);
135 check_exception("End of input while parsing the modifier O", SV("{:%O"), 0d
);
137 // Precision not allowed
138 check_exception("The format specifier expects a '%' or a '}'", SV("{:.3}"), 0d
);
141 int main(int, char**) {
144 #ifndef TEST_HAS_NO_WIDE_CHARACTERS