150 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++1710// UNSUPPORTED: no-localization11// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME12 13// TODO FMT This test should not require std::to_chars(floating-point)14// XFAIL: availability-fp_to_chars-missing15 16// TODO FMT Investigate Windows issues.17// XFAIL: msvc18 19// REQUIRES: locale.fr_FR.UTF-820// REQUIRES: locale.ja_JP.UTF-821 22// <chrono>23 24// template<class charT> struct formatter<chrono::day, 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 macros38#include "string_literal.h"39#include "test_macros.h"40 41template <class CharT>42static void test_no_chrono_specs() {43 using namespace std::literals::chrono_literals;44 45 // Valid day46 check(SV("01"), SV("{}"), 1d);47 check(SV("*01*"), SV("{:*^4}"), 1d);48 check(SV("*01"), SV("{:*>3}"), 1d);49 50 // Invalid day51 check(SV("00 is not a valid day"), SV("{}"), 0d);52 check(SV("*00 is not a valid day*"), SV("{:*^23}"), 0d);53}54 55template <class CharT>56static void test_valid_values() {57 using namespace std::literals::chrono_literals;58 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}");61 62 const std::locale loc(LOCALE_ja_JP_UTF_8);63 std::locale::global(std::locale(LOCALE_fr_FR_UTF_8));64 65 // Non localized output using C-locale66#if defined(_WIN32)67 check(SV("%d=''\t%Od=''\t%e=''\t%Oe=''\n"), fmt, 0d);68#else69 check(SV("%d='00'\t%Od='00'\t%e=' 0'\t%Oe=' 0'\n"), fmt, 0d);70#endif71 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);73#if defined(_WIN32)74 check(SV("%d=''\t%Od=''\t%e=''\t%Oe=''\n"), fmt, 0d);75#elif defined(_AIX)76 check(SV("%d='55'\t%Od='55'\t%e='55'\t%Oe='55'\n"), fmt, 255d);77#else78 check(SV("%d='255'\t%Od='255'\t%e='255'\t%Oe='255'\n"), fmt, 255d);79#endif80 81 // Use the global locale (fr_FR)82#if defined(_WIN32)83 check(SV("%d=''\t%Od=''\t%e=''\t%Oe=''\n"), lfmt, 0d);84#else85 check(SV("%d='00'\t%Od='00'\t%e=' 0'\t%Oe=' 0'\n"), lfmt, 0d);86#endif87 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);89#if defined(_WIN32)90 check(SV("%d=''\t%Od=''\t%e=''\t%Oe=''\n"), lfmt, 255d);91#elif defined(_AIX)92 check(SV("%d='55'\t%Od='55'\t%e='55'\t%Oe='55'\n"), lfmt, 255d);93#else94 check(SV("%d='255'\t%Od='255'\t%e='255'\t%Oe='255'\n"), lfmt, 255d);95#endif96 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__)99# if defined(_WIN32)100 check(loc, SV("%d=''\t%Od=''\t%e=''\t%Oe=''\n"), lfmt, 0d);101# else102 check(loc, SV("%d='00'\t%Od='00'\t%e=' 0'\t%Oe=' 0'\n"), lfmt, 0d);103# endif104 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);106# if defined(_WIN32)107 check(SV("%d=''\t%Od=''\t%e=''\t%Oe=''\n"), fmt, 255d);108# elif defined(_AIX)109 check(SV("%d='55'\t%Od='55'\t%e='55'\t%Oe='55'\n"), fmt, 255d);110# else111 check(SV("%d='255'\t%Od='255'\t%e='255'\t%Oe='255'\n"), fmt, 255d);112# endif113#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__)119 120 std::locale::global(std::locale::classic());121}122 123template <class CharT>124static void test() {125 using namespace std::literals::chrono_literals;126 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);130 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);136 137 // Precision not allowed138 check_exception("The format specifier expects a '%' or a '}'", SV("{:.3}"), 0d);139}140 141int main(int, char**) {142 test<char>();143 144#ifndef TEST_HAS_NO_WIDE_CHARACTERS145 test<wchar_t>();146#endif147 148 return 0;149}150