]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/22_locale/conversions/string/3.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 22_locale / conversions / string / 3.cc
CommitLineData
52066eae 1// { dg-do run { target c++11 } }
b6584a72 2
8d9254fc 3// Copyright (C) 2015-2020 Free Software Foundation, Inc.
b6584a72
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
26template<typename Elem>
27struct cvt : std::codecvt<Elem, char, std::mbstate_t> { };
28
29template<typename Elem>
30using str_conv = std::wstring_convert<cvt<Elem>, Elem>;
31
32using std::string;
9933260f 33using std::u16string;
b6584a72
JW
34using std::u32string;
35
36// test construction with state, for partial conversions
37
38void test01()
9933260f
JW
39{
40 typedef str_conv<char> wsc;
41
42 wsc c;
102a4fe1 43 string input = (const char*)u8"\u00a3 shillings pence";
9933260f
JW
44 string woutput = c.from_bytes(input.substr(0, 1));
45 auto partial_state = c.state();
46 auto partial_count = c.converted();
47
102a4fe1
JW
48 auto woutput2 = c.from_bytes("state reset on next conversion");
49 VERIFY( woutput2 == "state reset on next conversion" );
9933260f
JW
50
51 wsc c2(new cvt<char>, partial_state);
52 woutput += c2.from_bytes(input.substr(partial_count));
102a4fe1 53 VERIFY( (const char*)u8"\u00a3 shillings pence" == woutput );
9933260f
JW
54
55 string roundtrip = c2.to_bytes(woutput);
56 VERIFY( input == roundtrip );
57}
58
59void test02()
60{
61 typedef str_conv<char16_t> wsc;
62
63 wsc c;
102a4fe1 64 string input = (const char*)u8"\u00a3 shillings pence";
9933260f
JW
65 u16string woutput = c.from_bytes(input.substr(0, 1));
66 auto partial_state = c.state();
67 auto partial_count = c.converted();
68
102a4fe1 69 auto woutput2 = c.from_bytes("state reset on next conversion");
9933260f
JW
70 VERIFY( woutput2 == u"state reset on next conversion" );
71
72 wsc c2(new cvt<char16_t>, partial_state);
73 woutput += c2.from_bytes(input.substr(partial_count));
74 VERIFY( u"\u00a3 shillings pence" == woutput );
75
76 string roundtrip = c2.to_bytes(woutput);
77 VERIFY( input == roundtrip );
78}
79
80void test03()
b6584a72
JW
81{
82 typedef str_conv<char32_t> wsc;
83
84 wsc c;
102a4fe1 85 string input = (const char*)u8"\u00a3 shillings pence";
b6584a72
JW
86 u32string woutput = c.from_bytes(input.substr(0, 1));
87 auto partial_state = c.state();
88 auto partial_count = c.converted();
89
102a4fe1 90 auto woutput2 = c.from_bytes("state reset on next conversion");
b6584a72
JW
91 VERIFY( woutput2 == U"state reset on next conversion" );
92
93 wsc c2(new cvt<char32_t>, partial_state);
94 woutput += c2.from_bytes(input.substr(partial_count));
95 VERIFY( U"\u00a3 shillings pence" == woutput );
96
97 string roundtrip = c2.to_bytes(woutput);
98 VERIFY( input == roundtrip );
99}
100
9933260f 101
b6584a72
JW
102int main()
103{
104 test01();
9933260f
JW
105 test02();
106 test03();
b6584a72 107}