]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/22_locale/conversions/string/3.cc
1 // { dg-do run { target c++11 } }
3 // Copyright (C) 2015-2023 Free Software Foundation, Inc.
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)
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.
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/>.
20 // 22.3.3.2.2 String conversions
24 #include <testsuite_hooks.h>
26 template<typename Elem
>
27 struct cvt
: std::codecvt
<Elem
, char, std::mbstate_t> { };
29 template<typename Elem
>
30 using str_conv
= std::wstring_convert
<cvt
<Elem
>, Elem
>;
36 // test construction with state, for partial conversions
40 typedef str_conv
<char> wsc
;
43 string input
= (const char*)u8
"\u00a3 shillings pence";
44 string woutput
= c
.from_bytes(input
.substr(0, 1));
45 auto partial_state
= c
.state();
46 auto partial_count
= c
.converted();
48 auto woutput2
= c
.from_bytes("state reset on next conversion");
49 VERIFY( woutput2
== "state reset on next conversion" );
51 wsc
c2(new cvt
<char>, partial_state
);
52 woutput
+= c2
.from_bytes(input
.substr(partial_count
));
53 VERIFY( (const char*)u8
"\u00a3 shillings pence" == woutput
);
55 string roundtrip
= c2
.to_bytes(woutput
);
56 VERIFY( input
== roundtrip
);
61 typedef str_conv
<char16_t
> wsc
;
64 string input
= (const char*)u8
"\u00a3 shillings pence";
65 u16string woutput
= c
.from_bytes(input
.substr(0, 1));
66 auto partial_state
= c
.state();
67 auto partial_count
= c
.converted();
69 auto woutput2
= c
.from_bytes("state reset on next conversion");
70 VERIFY( woutput2
== u
"state reset on next conversion" );
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
);
76 string roundtrip
= c2
.to_bytes(woutput
);
77 VERIFY( input
== roundtrip
);
82 typedef str_conv
<char32_t
> wsc
;
85 string input
= (const char*)u8
"\u00a3 shillings pence";
86 u32string woutput
= c
.from_bytes(input
.substr(0, 1));
87 auto partial_state
= c
.state();
88 auto partial_count
= c
.converted();
90 auto woutput2
= c
.from_bytes("state reset on next conversion");
91 VERIFY( woutput2
== U
"state reset on next conversion" );
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
);
97 string roundtrip
= c2
.to_bytes(woutput
);
98 VERIFY( input
== roundtrip
);