]>
Commit | Line | Data |
---|---|---|
4f803217 | 1 | // { dg-do compile } |
2 | // { dg-options "-std=gnu++11" } | |
3 | ||
f1717362 | 4 | // Copyright (C) 2015-2016 Free Software Foundation, Inc. |
4f803217 | 5 | // |
6 | // This file is part of the GNU ISO C++ Library. This library is free | |
7 | // software; you can redistribute it and/or modify it under the | |
8 | // terms of the GNU General Public License as published by the | |
9 | // Free Software Foundation; either version 3, or (at your option) | |
10 | // any later version. | |
11 | ||
12 | // This library is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | ||
17 | // You should have received a copy of the GNU General Public License along | |
18 | // with this library; see the file COPYING3. If not see | |
19 | // <http://www.gnu.org/licenses/>. | |
20 | ||
21 | // 22.3.3.2.2 String conversions | |
22 | ||
23 | #include <locale> | |
24 | #include <string> | |
25 | #include <testsuite_hooks.h> | |
26 | ||
27 | template<typename Elem> | |
28 | struct cvt : std::codecvt<Elem, char, std::mbstate_t> { }; | |
29 | ||
30 | template<typename Elem> | |
31 | using str_conv = std::wstring_convert<cvt<Elem>, Elem>; | |
32 | ||
33 | using std::string; | |
34 | using std::wstring; | |
35 | ||
36 | void test01() | |
37 | { | |
38 | typedef str_conv<char> sc; // noconv | |
39 | sc c; | |
40 | string input = "King for a day..."; | |
41 | string output = c.from_bytes(input); | |
42 | VERIFY( input == output ); | |
43 | VERIFY( c.converted() == output.length() ); | |
44 | string roundtrip = c.to_bytes(output); | |
45 | VERIFY( input == roundtrip ); | |
46 | VERIFY( c.converted() == roundtrip.length() ); | |
47 | } | |
48 | ||
49 | void test02() | |
50 | { | |
51 | typedef str_conv<wchar_t> wsc; | |
52 | wsc c; | |
53 | string input = "Fool for a lifetime"; | |
54 | wstring output = c.from_bytes(input); | |
55 | VERIFY( c.converted() == output.length() ); | |
56 | VERIFY( L"Fool for a lifetime" == output ); | |
57 | string roundtrip = c.to_bytes(output); | |
58 | VERIFY( input == roundtrip ); | |
59 | VERIFY( c.converted() == roundtrip.length() ); | |
60 | ||
61 | VERIFY( c.from_bytes(input[0]) == output.substr(0, 1) ); | |
62 | VERIFY( c.from_bytes(input.c_str()) == output ); | |
63 | VERIFY( c.from_bytes(input.data(), input.data()+input.size()) == output ); | |
64 | ||
65 | VERIFY( c.to_bytes(output[0]) == input.substr(0, 1) ); | |
66 | VERIFY( c.to_bytes(output.c_str()) == input ); | |
67 | VERIFY( c.to_bytes(output.data(), output.data()+output.size()) == input ); | |
68 | } | |
69 | ||
70 | int main() | |
71 | { | |
72 | test01(); | |
73 | test02(); | |
74 | } |