Debian Codegraph @

libstdc++-v3/testsuite/22_locale/conversions/string/3.cc

raw
1 // { dg-do run { target c++11 } }
2
3 // Copyright (C) 2015-2025 Free Software Foundation, Inc.
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 // { dg-warning "deprecated" "" { target c++17 } 30 }
32
33 using std::string;
34 using std::u16string;
35 using std::u32string;
36
37 // test construction with state, for partial conversions
38
39 void test01()
40 {
41 typedef str_conv<char> wsc;
42
43 wsc c;
44 string input = (const char*)u8"\u00a3 shillings pence";
45 string woutput = c.from_bytes(input.substr(0, 1));
46 auto partial_state = c.state();
47 auto partial_count = c.converted();
48
49 auto woutput2 = c.from_bytes("state reset on next conversion");
50 VERIFY( woutput2 == "state reset on next conversion" );
51
52 wsc c2(new cvt<char>, partial_state);
53 woutput += c2.from_bytes(input.substr(partial_count));
54 VERIFY( (const char*)u8"\u00a3 shillings pence" == woutput );
55
56 string roundtrip = c2.to_bytes(woutput);
57 VERIFY( input == roundtrip );
58 }
59
60 void test02()
61 {
62 typedef str_conv<char16_t> wsc;
63
64 wsc c;
65 string input = (const char*)u8"\u00a3 shillings pence";
66 u16string woutput = c.from_bytes(input.substr(0, 1));
67 auto partial_state = c.state();
68 auto partial_count = c.converted();
69
70 auto woutput2 = c.from_bytes("state reset on next conversion");
71 VERIFY( woutput2 == u"state reset on next conversion" );
72
73 wsc c2(new cvt<char16_t>, partial_state);
74 woutput += c2.from_bytes(input.substr(partial_count));
75 VERIFY( u"\u00a3 shillings pence" == woutput );
76
77 string roundtrip = c2.to_bytes(woutput);
78 VERIFY( input == roundtrip );
79 }
80
81 void test03()
82 {
83 typedef str_conv<char32_t> wsc;
84
85 wsc c;
86 string input = (const char*)u8"\u00a3 shillings pence";
87 u32string woutput = c.from_bytes(input.substr(0, 1));
88 auto partial_state = c.state();
89 auto partial_count = c.converted();
90
91 auto woutput2 = c.from_bytes("state reset on next conversion");
92 VERIFY( woutput2 == U"state reset on next conversion" );
93
94 wsc c2(new cvt<char32_t>, partial_state);
95 woutput += c2.from_bytes(input.substr(partial_count));
96 VERIFY( U"\u00a3 shillings pence" == woutput );
97
98 string roundtrip = c2.to_bytes(woutput);
99 VERIFY( input == roundtrip );
100 }
101
102
103 int main()
104 {
105 test01();
106 test02();
107 test03();
108 }