| 1 | // Copyright (C) 2017-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-additional-options "-Wno-deprecated-declarations" { target c++17 } } |
| 20 | |
| 21 | #include <codecvt> |
| 22 | #include <locale> |
| 23 | #include <string> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | using std::wstring_convert; |
| 27 | using std::codecvt_utf8; |
| 28 | |
| 29 | void |
| 30 | test01() |
| 31 | { |
| 32 | std::string src = (const char*)u8"1234\U00001111\U0001ffff"; |
| 33 | wstring_convert<codecvt_utf8<char16_t>, char16_t> c("bad", u"BAD"); |
| 34 | |
| 35 | // utf-8 to ucs2 conversion should fail on character outside BMP |
| 36 | auto ucs2 = c.from_bytes(src); |
| 37 | VERIFY( ucs2 == u"BAD" ); |
| 38 | VERIFY( c.converted() == 7 ); |
| 39 | |
| 40 | // ucs2 to utf-8 conversion should fail on invalid ucs2 input: |
| 41 | std::u16string utf16 = u"1234\U00001111\U0001ffff"; |
| 42 | auto out = c.to_bytes(utf16); |
| 43 | VERIFY( out == "bad" ); |
| 44 | VERIFY( c.converted() == 5 ); |
| 45 | |
| 46 | // And should also fail on incomplete surrogate pair (not return partial): |
| 47 | out = c.to_bytes(utf16.substr(0, utf16.size()-1)); |
| 48 | VERIFY( out == "bad" ); |
| 49 | VERIFY( c.converted() == 5 ); |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | test02() |
| 54 | { |
| 55 | std::string src = (const char*)u8"1234\U00001111\U0001ffff"; |
| 56 | wstring_convert<codecvt_utf8<char16_t, 0x1000>, char16_t> c("bad", u"BAD"); |
| 57 | |
| 58 | // utf-8 to ucs2 conversion should fail on character above Maxcode=0x1000 |
| 59 | auto ucs2 = c.from_bytes(src); |
| 60 | VERIFY( ucs2 == u"BAD" ); |
| 61 | VERIFY( c.converted() == 4 ); |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | test03() |
| 66 | { |
| 67 | std::string src = (const char*)u8"1234\U00001111\U0001ffff"; |
| 68 | wstring_convert<codecvt_utf8<char32_t, 0x10000>, char32_t> c("bad", U"BAD"); |
| 69 | |
| 70 | // utf-8 to ucs4 conversion should fail on character above Maxcode=0x10000 |
| 71 | auto ucs4 = c.from_bytes(src); |
| 72 | VERIFY( ucs4 == U"BAD" ); |
| 73 | VERIFY( c.converted() == 7 ); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | test04() |
| 78 | { |
| 79 | std::string src = (const char*)u8"1234\U00001111\U0001ffff"; |
| 80 | wstring_convert<codecvt_utf8<char32_t, 0x1000>, char32_t> c("bad", U"BAD"); |
| 81 | |
| 82 | // utf-8 to ucs4 conversion should fail on character above Maxcode=0x1000 |
| 83 | auto ucs4 = c.from_bytes(src); |
| 84 | VERIFY( ucs4 == U"BAD" ); |
| 85 | VERIFY( c.converted() == 4 ); |
| 86 | } |
| 87 | |
| 88 | int |
| 89 | main() |
| 90 | { |
| 91 | test01(); |
| 92 | test02(); |
| 93 | test03(); |
| 94 | test04(); |
| 95 | } |