| 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 | // class year_month_day; |
| 22 | |
| 23 | // template<class charT, class traits> |
| 24 | // basic_ostream<charT, traits>& |
| 25 | // operator<<(basic_ostream<charT, traits>& os, const year_month_day& ymd); |
| 26 | |
| 27 | #include <chrono> |
| 28 | #include <cassert> |
| 29 | #include <sstream> |
| 30 | |
| 31 | #include "make_string.h" |
| 32 | #include "platform_support.h" // locale name macros |
| 33 | #include "test_macros.h" |
| 34 | #include "assert_macros.h" |
| 35 | #include "concat_macros.h" |
| 36 | |
| 37 | #define SV(S) MAKE_STRING_VIEW(CharT, S) |
| 38 | |
| 39 | #define TEST_EQUAL(OUT, EXPECTED) \ |
| 40 | TEST_REQUIRE(OUT == EXPECTED, \ |
| 41 | TEST_WRITE_CONCATENATED( \ |
| 42 | "\nExpression ", #OUT, "\nExpected output ", EXPECTED, "\nActual output ", OUT, '\n')); |
| 43 | |
| 44 | template <class CharT> |
| 45 | static std::basic_string<CharT> stream_c_locale(std::chrono::year_month_day ymd) { |
| 46 | std::basic_stringstream<CharT> sstr; |
| 47 | sstr << ymd; |
| 48 | return sstr.str(); |
| 49 | } |
| 50 | |
| 51 | template <class CharT> |
| 52 | static std::basic_string<CharT> stream_fr_FR_locale(std::chrono::year_month_day ymd) { |
| 53 | std::basic_stringstream<CharT> sstr; |
| 54 | const std::locale locale(LOCALE_fr_FR_UTF_8); |
| 55 | sstr.imbue(locale); |
| 56 | sstr << ymd; |
| 57 | return sstr.str(); |
| 58 | } |
| 59 | |
| 60 | template <class CharT> |
| 61 | static std::basic_string<CharT> stream_ja_JP_locale(std::chrono::year_month_day ymd) { |
| 62 | std::basic_stringstream<CharT> sstr; |
| 63 | const std::locale locale(LOCALE_ja_JP_UTF_8); |
| 64 | sstr.imbue(locale); |
| 65 | sstr << ymd; |
| 66 | return sstr.str(); |
| 67 | } |
| 68 | |
| 69 | template <class CharT> |
| 70 | static void test() { |
| 71 | TEST_EQUAL(stream_c_locale<CharT>( |
| 72 | std::chrono::year_month_day{std::chrono::year{-32'768}, std::chrono::month{1}, std::chrono::day{1}}), |
| 73 | SV("-32768-01-01 is not a valid date" )); |
| 74 | TEST_EQUAL(stream_c_locale<CharT>( |
| 75 | std::chrono::year_month_day{std::chrono::year{-32'767}, std::chrono::month{0}, std::chrono::day{1}}), |
| 76 | SV("-32767-00-01 is not a valid date" )); |
| 77 | TEST_EQUAL(stream_c_locale<CharT>( |
| 78 | std::chrono::year_month_day{std::chrono::year{-32'767}, std::chrono::month{1}, std::chrono::day{0}}), |
| 79 | SV("-32767-01-00 is not a valid date" )); |
| 80 | TEST_EQUAL(stream_c_locale<CharT>( |
| 81 | std::chrono::year_month_day{std::chrono::year{1970}, std::chrono::month{1}, std::chrono::day{1}}), |
| 82 | SV("1970-01-01" )); |
| 83 | TEST_EQUAL(stream_c_locale<CharT>( |
| 84 | std::chrono::year_month_day{std::chrono::year{1999}, std::chrono::month{2}, std::chrono::day{29}}), |
| 85 | SV("1999-02-29 is not a valid date" )); |
| 86 | TEST_EQUAL(stream_c_locale<CharT>( |
| 87 | std::chrono::year_month_day{std::chrono::year{2000}, std::chrono::month{2}, std::chrono::day{29}}), |
| 88 | SV("2000-02-29" )); |
| 89 | TEST_EQUAL(stream_c_locale<CharT>( |
| 90 | std::chrono::year_month_day{std::chrono::year{32'767}, std::chrono::month{12}, std::chrono::day{31}}), |
| 91 | SV("32767-12-31" )); |
| 92 | |
| 93 | TEST_EQUAL(stream_fr_FR_locale<CharT>( |
| 94 | std::chrono::year_month_day{std::chrono::year{-32'768}, std::chrono::month{1}, std::chrono::day{1}}), |
| 95 | SV("-32768-01-01 is not a valid date" )); |
| 96 | TEST_EQUAL(stream_fr_FR_locale<CharT>( |
| 97 | std::chrono::year_month_day{std::chrono::year{-32'767}, std::chrono::month{0}, std::chrono::day{1}}), |
| 98 | SV("-32767-00-01 is not a valid date" )); |
| 99 | TEST_EQUAL(stream_fr_FR_locale<CharT>( |
| 100 | std::chrono::year_month_day{std::chrono::year{-32'767}, std::chrono::month{1}, std::chrono::day{0}}), |
| 101 | SV("-32767-01-00 is not a valid date" )); |
| 102 | TEST_EQUAL(stream_fr_FR_locale<CharT>( |
| 103 | std::chrono::year_month_day{std::chrono::year{1970}, std::chrono::month{1}, std::chrono::day{1}}), |
| 104 | SV("1970-01-01" )); |
| 105 | TEST_EQUAL(stream_fr_FR_locale<CharT>( |
| 106 | std::chrono::year_month_day{std::chrono::year{1999}, std::chrono::month{2}, std::chrono::day{29}}), |
| 107 | SV("1999-02-29 is not a valid date" )); |
| 108 | TEST_EQUAL(stream_fr_FR_locale<CharT>( |
| 109 | std::chrono::year_month_day{std::chrono::year{2000}, std::chrono::month{2}, std::chrono::day{29}}), |
| 110 | SV("2000-02-29" )); |
| 111 | TEST_EQUAL(stream_fr_FR_locale<CharT>( |
| 112 | std::chrono::year_month_day{std::chrono::year{32'767}, std::chrono::month{12}, std::chrono::day{31}}), |
| 113 | SV("32767-12-31" )); |
| 114 | |
| 115 | TEST_EQUAL(stream_ja_JP_locale<CharT>( |
| 116 | std::chrono::year_month_day{std::chrono::year{-32'768}, std::chrono::month{1}, std::chrono::day{1}}), |
| 117 | SV("-32768-01-01 is not a valid date" )); |
| 118 | TEST_EQUAL(stream_ja_JP_locale<CharT>( |
| 119 | std::chrono::year_month_day{std::chrono::year{-32'767}, std::chrono::month{0}, std::chrono::day{1}}), |
| 120 | SV("-32767-00-01 is not a valid date" )); |
| 121 | TEST_EQUAL(stream_ja_JP_locale<CharT>( |
| 122 | std::chrono::year_month_day{std::chrono::year{-32'767}, std::chrono::month{1}, std::chrono::day{0}}), |
| 123 | SV("-32767-01-00 is not a valid date" )); |
| 124 | TEST_EQUAL(stream_ja_JP_locale<CharT>( |
| 125 | std::chrono::year_month_day{std::chrono::year{1970}, std::chrono::month{1}, std::chrono::day{1}}), |
| 126 | SV("1970-01-01" )); |
| 127 | TEST_EQUAL(stream_ja_JP_locale<CharT>( |
| 128 | std::chrono::year_month_day{std::chrono::year{1999}, std::chrono::month{2}, std::chrono::day{29}}), |
| 129 | SV("1999-02-29 is not a valid date" )); |
| 130 | TEST_EQUAL(stream_ja_JP_locale<CharT>( |
| 131 | std::chrono::year_month_day{std::chrono::year{2000}, std::chrono::month{2}, std::chrono::day{29}}), |
| 132 | SV("2000-02-29" )); |
| 133 | TEST_EQUAL(stream_ja_JP_locale<CharT>( |
| 134 | std::chrono::year_month_day{std::chrono::year{32'767}, std::chrono::month{12}, std::chrono::day{31}}), |
| 135 | SV("32767-12-31" )); |
| 136 | } |
| 137 | |
| 138 | int main(int, char**) { |
| 139 | test<char>(); |
| 140 | |
| 141 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 142 | test<wchar_t>(); |
| 143 | #endif |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |