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