1// { dg-options "-std=gnu++11" } 2 3// Copyright (C) 2012 Free Software Foundation 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; 33using std::u16string; 34using std::u32string; 35 36// test construction with state, for partial conversions 37 38void test01() 39{ 40 typedef str_conv<char> wsc; 41 42 wsc c; 43 string input = 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(); 47 48 auto woutput2 = c.from_bytes(u8"state reset on next conversion"); 49 VERIFY( woutput2 == u8"state reset on next conversion" ); 50 51 wsc c2(new cvt<char>, partial_state); 52 woutput += c2.from_bytes(input.substr(partial_count)); 53 VERIFY( u8"\u00a3 shillings pence" == woutput ); 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; 64 string input = 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(); 68 69 auto woutput2 = c.from_bytes(u8"state reset on next conversion"); 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() 81{ 82 typedef str_conv<char32_t> wsc; 83 84 wsc c; 85 string input = 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(); 89 90 auto woutput2 = c.from_bytes(u8"state reset on next conversion"); 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 101 102int main() 103{ 104 test01(); 105 test02(); 106 test03(); 107} 108