| 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 | // { dg-require-cstdint "" } |
| 20 | // { dg-options "-fchar8_t" } |
| 21 | |
| 22 | // [locale.codecvt], C++11 22.4.1.4. specialization. |
| 23 | |
| 24 | #include <locale> |
| 25 | #include <cstring> |
| 26 | #include <testsuite_hooks.h> |
| 27 | |
| 28 | void |
| 29 | test01() |
| 30 | { |
| 31 | using namespace std; |
| 32 | typedef codecvt<char16_t, char8_t, mbstate_t> codecvt_c16; |
| 33 | locale loc_c = locale::classic(); |
| 34 | VERIFY(has_facet<codecvt_c16>(loc_c)); |
| 35 | const codecvt_c16* const cvt = &use_facet<codecvt_c16>(loc_c); |
| 36 | |
| 37 | VERIFY(!cvt->always_noconv()); |
| 38 | VERIFY(cvt->max_length() == 4); |
| 39 | VERIFY(cvt->encoding() == 0); |
| 40 | |
| 41 | const char8_t u8dat[] = u8"H\U000000E4ll\U000000F6 \U0001F63F \U000056FD " |
| 42 | u8"\U0000222B f(\U000003BA) exp(-2\U000003C0\U000003C9) d\U000003BA " |
| 43 | u8"\U0001F6BF \U0001F6BF \U0001F648 \U00000413\U00000435\U0000043E" |
| 44 | u8"\U00000433\U00000440\U00000430\U00000444\U00000438\U0000044F \U0000FB05"; |
| 45 | const char8_t* const u8dat_end = std::end(u8dat); |
| 46 | |
| 47 | const char16_t u16dat[] = u"H\U000000E4ll\U000000F6 \U0001F63F \U000056FD " |
| 48 | u"\U0000222B f(\U000003BA) exp(-2\U000003C0\U000003C9) d\U000003BA " |
| 49 | u"\U0001F6BF \U0001F6BF \U0001F648 \U00000413\U00000435\U0000043E" |
| 50 | u"\U00000433\U00000440\U00000430\U00000444\U00000438\U0000044F \U0000FB05"; |
| 51 | const char16_t* const u16dat_end = std::end(u16dat); |
| 52 | |
| 53 | { |
| 54 | const size_t len = u16dat_end - u16dat + 1; |
| 55 | char16_t* const buffer = new char16_t[len]; |
| 56 | char16_t* const buffer_end = buffer + len; |
| 57 | |
| 58 | const char8_t* from_next; |
| 59 | char16_t* to_next; |
| 60 | |
| 61 | codecvt_c16::state_type state01; |
| 62 | state01 = {}; |
| 63 | codecvt_base::result res = cvt->in(state01, u8dat, u8dat_end, from_next, |
| 64 | buffer, buffer_end, to_next); |
| 65 | |
| 66 | VERIFY(res == codecvt_base::ok); |
| 67 | VERIFY(from_next == u8dat_end); |
| 68 | VERIFY(std::memcmp((void*)buffer, (void*)u16dat, sizeof(u16dat)) == 0); |
| 69 | |
| 70 | delete[] buffer; |
| 71 | } |
| 72 | |
| 73 | { |
| 74 | const size_t len = u8dat_end - u8dat + 1; |
| 75 | char8_t* const buffer = new char8_t[len]; |
| 76 | char8_t* const buffer_end = buffer + len; |
| 77 | |
| 78 | const char16_t* from_next; |
| 79 | char8_t* to_next; |
| 80 | |
| 81 | codecvt_c16::state_type state01; |
| 82 | state01 = {}; |
| 83 | codecvt_base::result res = cvt->out(state01, u16dat, u16dat_end, from_next, |
| 84 | buffer, buffer_end, to_next); |
| 85 | |
| 86 | VERIFY(res == codecvt_base::ok); |
| 87 | VERIFY(from_next == u16dat_end); |
| 88 | VERIFY(std::memcmp((void*)buffer, (void*)u8dat, sizeof(u8dat)) == 0); |
| 89 | |
| 90 | delete[] buffer; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | int |
| 95 | main() |
| 96 | { |
| 97 | test01(); |
| 98 | } |