Debian Codegraph @

libstdc++-v3/testsuite/22_locale/codecvt/char16_t.cc

raw
1 // Copyright (C) 2015-2025 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-do run { target c++11 } }
19
20 // [locale.codecvt], C++11 22.4.1.4. specialization.
21
22 #include <locale>
23 #include <cstring>
24 #include <testsuite_hooks.h>
25
26 void
27 test01()
28 {
29 using namespace std;
30 typedef codecvt<char16_t, char, mbstate_t> codecvt_c16;
31 locale loc_c = locale::classic();
32 VERIFY(has_facet<codecvt_c16>(loc_c));
33 const codecvt_c16* const cvt = &use_facet<codecvt_c16>(loc_c);
34
35 VERIFY(!cvt->always_noconv());
36 VERIFY(cvt->max_length() == 4);
37 VERIFY(cvt->encoding() == 0);
38
39 #ifndef _GLIBCXX_USE_CHAR8_T
40 using char8_t = char;
41 #endif
42 const char8_t u8dat_[] = u8"H\U000000E4ll\U000000F6 \U0001F63F \U000056FD "
43 u8"\U0000222B f(\U000003BA) exp(-2\U000003C0\U000003C9) d\U000003BA "
44 u8"\U0001F6BF \U0001F6BF \U0001F648 \U00000413\U00000435\U0000043E"
45 u8"\U00000433\U00000440\U00000430\U00000444\U00000438\U0000044F \U0000FB05";
46 const char* const u8dat = (const char*)u8dat_;
47 const char* const u8dat_end = (const char*)std::end(u8dat_);
48
49 const char16_t u16dat[] = u"H\U000000E4ll\U000000F6 \U0001F63F \U000056FD "
50 u"\U0000222B f(\U000003BA) exp(-2\U000003C0\U000003C9) d\U000003BA "
51 u"\U0001F6BF \U0001F6BF \U0001F648 \U00000413\U00000435\U0000043E"
52 u"\U00000433\U00000440\U00000430\U00000444\U00000438\U0000044F \U0000FB05";
53 const char16_t* const u16dat_end = std::end(u16dat);
54
55 {
56 const size_t len = u16dat_end - u16dat + 1;
57 char16_t* const buffer = new char16_t[len];
58 char16_t* const buffer_end = buffer + len;
59
60 const char* from_next;
61 char16_t* to_next;
62
63 codecvt_c16::state_type state01;
64 state01 = {};
65 codecvt_base::result res = cvt->in(state01, u8dat, u8dat_end, from_next,
66 buffer, buffer_end, to_next);
67
68 VERIFY(res == codecvt_base::ok);
69 VERIFY(from_next == u8dat_end);
70 VERIFY(std::memcmp((void*)buffer, (void*)u16dat, sizeof(u16dat)) == 0);
71
72 delete[] buffer;
73 }
74
75 {
76 const size_t len = u8dat_end - u8dat + 1;
77 char* const buffer = new char[len];
78 char* const buffer_end = buffer + len;
79
80 const char16_t* from_next;
81 char* to_next;
82
83 codecvt_c16::state_type state01;
84 state01 = {};
85 codecvt_base::result res = cvt->out(state01, u16dat, u16dat_end, from_next,
86 buffer, buffer_end, to_next);
87
88 VERIFY(res == codecvt_base::ok);
89 VERIFY(from_next == u16dat_end);
90 VERIFY(std::memcmp((void*)buffer, (void*)u8dat_, sizeof(u8dat_)) == 0);
91
92 delete[] buffer;
93 }
94 }
95
96 int
97 main()
98 {
99 test01();
100 }