| 1 |
// { dg-do run { target c++23 } } |
| 2 |
// { dg-options "-fexec-charset=UTF-8" } |
| 3 |
// { dg-timeout-factor 2 } |
| 4 |
|
| 5 |
#include <algorithm> |
| 6 |
#include <chrono> |
| 7 |
#include <testsuite_hooks.h> |
| 8 |
|
| 9 |
#define WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S) |
| 10 |
#define WIDEN(S) WIDEN_(_CharT, S) |
| 11 |
|
| 12 |
template<typename _CharT> |
| 13 |
void |
| 14 |
test_from_format_string() |
| 15 |
{ |
| 16 |
std::basic_string<_CharT> res; |
| 17 |
using namespace std::chrono_literals; |
| 18 |
auto date = 2025y/std::chrono::May/05d; |
| 19 |
|
| 20 |
res = std::format(WIDEN("{:+<13%F\U0001f921}"), date); |
| 21 |
VERIFY( res == WIDEN("2025-05-05\U0001f921+") ); |
| 22 |
|
| 23 |
res = std::format(WIDEN("{:->15%F\U0001f921}"), date); |
| 24 |
VERIFY( res == WIDEN("---2025-05-05\U0001f921") ); |
| 25 |
|
| 26 |
res = std::format(WIDEN("{:=^20%F\U0001f921}"), date); |
| 27 |
VERIFY( res == WIDEN("====2025-05-05\U0001f921====") ); |
| 28 |
} |
| 29 |
|
| 30 |
template<typename _CharT> |
| 31 |
void |
| 32 |
test_formatted_value() |
| 33 |
{ |
| 34 |
// Custom time_put facet which returns Ideographic Telegraph Symbol |
| 35 |
// for given month for Om. |
| 36 |
struct TimePut : std::time_put<_CharT> |
| 37 |
{ |
| 38 |
using iter_type = std::time_put<_CharT>::iter_type; |
| 39 |
using char_type = std::time_put<_CharT>::char_type; |
| 40 |
|
| 41 |
iter_type |
| 42 |
do_put(iter_type out, std::ios_base& io, char_type fill, const tm* t, |
| 43 |
char format, char modifier) const override |
| 44 |
{ |
| 45 |
if (format != 'm' && modifier != 'm') |
| 46 |
return std::time_put<_CharT>::do_put(out, io, fill, t, format, modifier); |
| 47 |
std::basic_string_view<_CharT> str; |
| 48 |
switch (t->tm_mon) |
| 49 |
{ |
| 50 |
case 0: |
| 51 |
str = WIDEN("\u32C0"); |
| 52 |
break; |
| 53 |
case 1: |
| 54 |
str = WIDEN("\u32C1"); |
| 55 |
break; |
| 56 |
case 2: |
| 57 |
str = WIDEN("\u32C2"); |
| 58 |
break; |
| 59 |
case 3: |
| 60 |
str = WIDEN("\u32C3"); |
| 61 |
break; |
| 62 |
case 4: |
| 63 |
str = WIDEN("\u32C4"); |
| 64 |
break; |
| 65 |
case 5: |
| 66 |
str = WIDEN("\u32C5"); |
| 67 |
break; |
| 68 |
case 6: |
| 69 |
str = WIDEN("\u32C6"); |
| 70 |
break; |
| 71 |
case 7: |
| 72 |
str = WIDEN("\u32C7"); |
| 73 |
break; |
| 74 |
case 8: |
| 75 |
str = WIDEN("\u32C8"); |
| 76 |
break; |
| 77 |
case 9: |
| 78 |
str = WIDEN("\u32C9"); |
| 79 |
break; |
| 80 |
case 10: |
| 81 |
str = WIDEN("\u32CA"); |
| 82 |
break; |
| 83 |
case 11: |
| 84 |
str = WIDEN("\u32CB"); |
| 85 |
break; |
| 86 |
}; |
| 87 |
return std::copy(str.begin(), str.end(), out); |
| 88 |
} |
| 89 |
}; |
| 90 |
const std::locale loc(std::locale::classic(), new TimePut); |
| 91 |
|
| 92 |
std::basic_string<_CharT> res; |
| 93 |
|
| 94 |
res = std::format(loc, WIDEN("{:<1L%Om}"), std::chrono::January); |
| 95 |
VERIFY( res == WIDEN("\u32C0") ); |
| 96 |
|
| 97 |
res = std::format(loc, WIDEN("{:>2L%Om}"), std::chrono::February); |
| 98 |
VERIFY( res == WIDEN("\u32C1") ); |
| 99 |
|
| 100 |
res = std::format(loc, WIDEN("{:<3L%Om}"), std::chrono::March); |
| 101 |
VERIFY( res == WIDEN("\u32C2 ") ); |
| 102 |
|
| 103 |
res = std::format(loc, WIDEN("{:^4L%Om}"), std::chrono::April); |
| 104 |
VERIFY( res == WIDEN(" \u32C3 ") ); |
| 105 |
|
| 106 |
res = std::format(loc, WIDEN("{:>5L%Om}"), std::chrono::May); |
| 107 |
VERIFY( res == WIDEN(" \u32C4") ); |
| 108 |
|
| 109 |
res = std::format(loc, WIDEN("{:+<6L%Om}"), std::chrono::June); |
| 110 |
VERIFY( res == WIDEN("\u32C5++++") ); |
| 111 |
|
| 112 |
res = std::format(loc, WIDEN("{:=^7L%Om}"), std::chrono::July); |
| 113 |
VERIFY( res == WIDEN("==\u32C6===") ); |
| 114 |
|
| 115 |
res = std::format(loc, WIDEN("{:->8L%Om}"), std::chrono::August); |
| 116 |
VERIFY( res == WIDEN("------\u32C7") ); |
| 117 |
} |
| 118 |
|
| 119 |
int main() |
| 120 |
{ |
| 121 |
test_from_format_string<char>(); |
| 122 |
test_from_format_string<wchar_t>(); |
| 123 |
test_formatted_value<char>(); |
| 124 |
test_formatted_value<wchar_t>(); |
| 125 |
} |