]>
Commit | Line | Data |
---|---|---|
52066eae | 1 | // { dg-do run { target c++11 } } |
96d8c147 | 2 | |
7adcbafe | 3 | // Copyright (C) 2015-2022 Free Software Foundation, Inc. |
96d8c147 JW |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
8 | // Free Software Foundation; either version 3, or (at your option) | |
9 | // any later version. | |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
16 | // You should have received a copy of the GNU General Public License along | |
17 | // with this library; see the file COPYING3. If not see | |
18 | // <http://www.gnu.org/licenses/>. | |
19 | ||
20 | // 22.3.3.2.2 String conversions | |
21 | ||
22 | #include <locale> | |
23 | #include <string> | |
24 | #include <testsuite_hooks.h> | |
25 | ||
26 | template<typename Elem> | |
27 | struct cvt : std::codecvt<Elem, char, std::mbstate_t> { }; | |
28 | ||
29 | template<typename Elem> | |
30 | using str_conv = std::wstring_convert<cvt<Elem>, Elem>; | |
31 | ||
32 | using std::string; | |
b6584a72 JW |
33 | using std::u16string; |
34 | using std::u32string; | |
96d8c147 JW |
35 | |
36 | // test conversion errors, with and without error strings | |
37 | ||
38 | void test01() | |
9933260f JW |
39 | { |
40 | typedef str_conv<char> sc; | |
41 | ||
42 | const sc::byte_string berr = "invalid wide string"; | |
102a4fe1 | 43 | const sc::wide_string werr = "invalid byte string"; |
9933260f JW |
44 | |
45 | sc c(berr, werr); | |
46 | string input = "Stop"; | |
47 | input += char(0xFF); | |
48 | string woutput = c.from_bytes(input); | |
49 | VERIFY( input == woutput ); // noconv case doesn't detect invalid input | |
102a4fe1 | 50 | string winput = "Stop"; |
9933260f JW |
51 | winput += char(0xFF); |
52 | string output = c.to_bytes(winput); | |
53 | VERIFY( winput == output ); // noconv case doesn't detect invalid input | |
54 | } | |
55 | ||
56 | void test02() | |
96d8c147 | 57 | { |
b6584a72 | 58 | typedef str_conv<char16_t> sc; |
96d8c147 JW |
59 | |
60 | const sc::byte_string berr = "invalid wide string"; | |
b6584a72 | 61 | const sc::wide_string werr = u"invalid byte string"; |
96d8c147 JW |
62 | |
63 | sc c(berr, werr); | |
64 | string input = "Stop"; | |
b6584a72 JW |
65 | input += char(0xFF); |
66 | u16string woutput = c.from_bytes(input); | |
67 | VERIFY( werr == woutput ); | |
68 | u16string winput = u"Stop"; | |
69 | winput += char16_t(0xDC00); | |
70 | string output = c.to_bytes(winput); | |
71 | VERIFY( berr == output ); | |
72 | } | |
73 | ||
9933260f | 74 | void test03() |
b6584a72 JW |
75 | { |
76 | typedef str_conv<char32_t> sc; | |
77 | ||
78 | const sc::byte_string berr = "invalid wide string"; | |
79 | const sc::wide_string werr = U"invalid byte string"; | |
80 | ||
81 | sc c(berr, werr); | |
82 | string input = "Halt"; | |
96d8c147 | 83 | input += char(0xff); |
b6584a72 | 84 | u32string woutput = c.from_bytes(input); |
96d8c147 | 85 | VERIFY( werr == woutput ); |
b6584a72 JW |
86 | u32string winput = U"Halt"; |
87 | winput += char32_t(-1); | |
96d8c147 JW |
88 | string output = c.to_bytes(winput); |
89 | VERIFY( berr == output ); | |
90 | } | |
91 | ||
92 | int main() | |
93 | { | |
94 | test01(); | |
b6584a72 | 95 | test02(); |
9933260f | 96 | test03(); |
96d8c147 | 97 | } |